Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
Temperaturverteilung
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Package Registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Contributor analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
s39174
Temperaturverteilung
Commits
9b34fd73
Commit
9b34fd73
authored
3 years ago
by
Clemens Berteld
Browse files
Options
Downloads
Patches
Plain Diff
Raster endpoint fully working now. Temp dir gets cleared on startup
parent
4e9d358f
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
api/api.py
+18
-14
18 additions, 14 deletions
api/api.py
api/write_raster.py
+20
-14
20 additions, 14 deletions
api/write_raster.py
config.ini
+1
-0
1 addition, 0 deletions
config.ini
with
39 additions
and
28 deletions
api/api.py
+
18
−
14
View file @
9b34fd73
import
json
import
psycopg2
from
flask
import
Flask
,
jsonify
,
request
from
flask
import
Flask
,
jsonify
,
request
,
send_from_directory
from
flask_cors
import
cross_origin
from
psycopg2
import
sql
import
configparser
...
...
@@ -11,7 +12,6 @@ cfg = configparser.ConfigParser()
cfg
.
read
(
'
../config.ini
'
)
assert
"
POSTGRES
"
in
cfg
,
"
missing POSTGRES in config.ini
"
param_postgres
=
cfg
[
"
POSTGRES
"
]
app
=
Flask
(
__name__
)
app
.
config
[
'
ENV
'
]
=
"
development
"
app
.
config
[
'
DEBUG
'
]
=
True
...
...
@@ -85,7 +85,6 @@ def index():
def
getStandardQuery
():
columns
=
sql
.
SQL
(
'
*
'
)
# columns to be queried (e.g. years)
wheres
=
sql
.
SQL
(
''
)
# where filters
values
=
''
# Used in second parameter of cursor.execute() (Avoids SQL injection)
query
=
sql
.
SQL
(
"
SELECT array_to_json(array_agg(row_to_json(t))) from (
"
"
SELECT station_id, {} FROM stations
"
...
...
@@ -106,17 +105,22 @@ def return_min_max():
@app.route
(
'
/raster
'
,
methods
=
[
'
GET
'
])
def
get_raster
():
if
'
years
'
in
request
.
args
:
years
=
request
.
args
[
'
years
'
].
split
(
'
-
'
)
if
int
(
years
[
1
])
<
int
(
years
[
0
]):
years
=
[
years
[
1
],
years
[
0
]]
with
psycopg2
.
connect
(
database
=
param_postgres
[
"
dbName
"
],
user
=
param_postgres
[
"
user
"
],
password
=
param_postgres
[
"
password
"
],
host
=
param_postgres
[
"
host
"
],
port
=
param_postgres
[
"
port
"
])
as
connection
:
with
connection
.
cursor
()
as
cursor
:
# all_years = get_year_columns(cursor)
average_data
=
get_average_of_multiple_years
(
cursor
,
years
)
write_raster
(
average_data
)
# return send_from_directory('D:/Uni/Master/01_SS2021/Automatisierte_Geodatenprozessierung/temperaturverteilung/dataacquisition/output', filename='myraster.tif', as_attachment=True)
return
'
Läuft, Brudi
'
"""
Request like: http://127.0.0.1:42000/raster?years=2010-2018&ramp=[[255,255,255,1],[255,244,191,1],[255,233,128,1],[255,221,64,1],[255,210,0,1],[243,105,0,1],[230,0,0,1],[153,0,0,1],[77,0,0,1],[0,0,0,1]]
"""
if
'
ramp
'
in
request
.
args
:
ramp
=
json
.
loads
(
request
.
args
[
'
ramp
'
])
print
(
ramp
)
if
'
years
'
in
request
.
args
:
years
=
request
.
args
[
'
years
'
].
split
(
'
-
'
)
if
int
(
years
[
1
])
<
int
(
years
[
0
]):
years
=
[
years
[
1
],
years
[
0
]]
with
psycopg2
.
connect
(
database
=
param_postgres
[
"
dbName
"
],
user
=
param_postgres
[
"
user
"
],
password
=
param_postgres
[
"
password
"
],
host
=
param_postgres
[
"
host
"
],
port
=
param_postgres
[
"
port
"
])
as
connection
:
with
connection
.
cursor
()
as
cursor
:
average_data
=
get_average_of_multiple_years
(
cursor
,
years
)
path
,
filename
=
write_raster
(
average_data
,
ramp
)
return
send_from_directory
(
path
,
filename
=
filename
,
as_attachment
=
True
)
# return 'Läuft, Brudi'
@app.route
(
'
/annualMean
'
,
methods
=
[
'
GET
'
])
@cross_origin
()
...
...
This diff is collapsed.
Click to expand it.
api/write_raster.py
+
20
−
14
View file @
9b34fd73
import
configparser
import
math
import
os
from
datetime
import
datetime
import
numpy
as
np
import
psycopg2
...
...
@@ -10,8 +12,17 @@ cfg.read('../config.ini')
assert
"
POSTGRES
"
in
cfg
,
"
missing POSTGRES in config.ini
"
assert
"
INTERPOLATION
"
in
cfg
,
"
missing INTERPOLATION in config.ini
"
param_postgres
=
cfg
[
"
POSTGRES
"
]
raster_columns
=
int
(
cfg
[
"
INTERPOLATION
"
][
'
raster_columns
'
])
ramp
=
[[
255
,
255
,
255
,
1
],[
255
,
244
,
191
,
1
],[
255
,
233
,
128
,
1
],[
255
,
221
,
64
,
1
],[
255
,
210
,
0
,
1
],[
243
,
105
,
0
,
1
],[
230
,
0
,
0
,
1
],[
153
,
0
,
0
,
1
],[
77
,
0
,
0
,
1
],[
0
,
0
,
0
,
1
]]
# ramp = [[255,255,255,1],[255,244,191,1],[255,233,128,1],[255,221,64,1],[255,210,0,1],[243,105,0,1],[230,0,0,1],[153,0,0,1],[77,0,0,1],[0,0,0,1]]
def
clean_temp
(
path
):
for
file
in
os
.
listdir
(
path
):
try
:
os
.
remove
(
path
+
file
)
except
:
pass
def
get_class_steps
(
colour_ramp
):
...
...
@@ -22,57 +33,48 @@ def get_class_steps(colour_ramp):
temp_range
=
min_max
[
1
]
-
min_max
[
0
]
steps
=
temp_range
/
classes
min
=
min_max
[
0
]
# print(min_max)
return
min
,
steps
,
classes
def
colour_picker
(
min
,
steps
,
classes
,
colour_ramp
,
value
):
# print(min, steps, classes, value)
rgba
=
None
for
i
in
range
(
0
,
classes
+
1
):
minor
=
math
.
floor
(
min
+
(
i
*
steps
))
major
=
math
.
ceil
(
min
+
((
i
+
1
)
*
steps
))
# print('k:', minor, 'wert:', value, 'g:', major)
if
minor
<=
value
<=
major
:
try
:
rgba
=
colour_ramp
[
i
]
except
IndexError
:
rgba
=
colour_ramp
[
-
1
]
# print(i)
# print('ramp:', rgba)
if
not
rgba
:
rgba
=
[
0
,
0
,
0
,
0
]
return
rgba
def
write_raster
(
data
):
def
write_raster
(
data
,
ramp
):
min
,
steps
,
classes
=
get_class_steps
(
ramp
)
pixel_array_r
=
[]
pixel_array_g
=
[]
pixel_array_b
=
[]
pixel_array_a
=
[]
for
j
in
range
(
0
,
36
):
for
j
in
range
(
0
,
raster_columns
):
row_array_r
=
[]
row_array_g
=
[]
row_array_b
=
[]
row_array_a
=
[]
for
i
,
station_id
in
enumerate
(
data
):
if
i
%
36
==
0
:
if
i
%
raster_columns
==
0
:
value
=
data
[
i
+
j
][
1
]
value
=
0
if
not
value
else
value
value
=
0
if
str
(
value
)
==
'
NaN
'
else
value
if
not
value
==
0
:
rgba
=
colour_picker
(
min
,
steps
,
classes
,
ramp
,
value
)
# print(rgba)
r
,
g
,
b
,
a
=
rgba
[
0
],
rgba
[
1
],
rgba
[
2
],
rgba
[
3
]
else
:
r
,
g
,
b
,
a
=
0
,
0
,
0
,
0
# print('r', r, 'g', g, 'b', b, 'a', a)
transparent
=
data
[
i
+
j
][
2
]
print
(
transparent
)
a
=
0
if
transparent
else
a
a
=
255
if
a
==
1
else
a
row_array_r
.
append
(
r
)
...
...
@@ -103,7 +105,9 @@ def write_raster(data):
yres
=
(
ymax
-
ymin
)
/
float
(
nrows
)
geotransform
=
(
xmin
,
xres
,
0
,
ymax
,
0
,
-
yres
)
output_raster
=
gdal
.
GetDriverByName
(
'
GTiff
'
).
Create
(
'
D:/Uni/Master/01_SS2021/Automatisierte_Geodatenprozessierung/temperaturverteilung/dataacquisition/output/myraster.tif
'
,
ncols
,
nrows
,
4
,
gdal
.
GDT_Float32
)
# Open the file
path
=
os
.
getcwd
()
+
'
/temp/
'
filename
=
'
raster_{}.tif
'
.
format
(
datetime
.
now
().
strftime
(
"
%Y%m%d%H%M%S
"
))
output_raster
=
gdal
.
GetDriverByName
(
'
GTiff
'
).
Create
(
path
+
filename
,
ncols
,
nrows
,
4
,
gdal
.
GDT_Float32
)
# Open the file
output_raster
.
SetGeoTransform
(
geotransform
)
srs
=
osr
.
SpatialReference
()
srs
.
ImportFromEPSG
(
4326
)
...
...
@@ -114,5 +118,7 @@ def write_raster(data):
output_raster
.
GetRasterBand
(
4
).
WriteArray
(
a_band
)
output_raster
.
FlushCache
()
clean_temp
(
path
)
return
path
,
filename
This diff is collapsed.
Click to expand it.
config.ini
+
1
−
0
View file @
9b34fd73
...
...
@@ -10,3 +10,4 @@ dbName = temperatures_berteld_morstein
amount_neighbours
=
5
; in km. 25km or 10km possible
matrix_density
=
25
raster_columns
=
36
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment