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
183def33
Commit
183def33
authored
3 years ago
by
Peter Morstein
Browse files
Options
Downloads
Plain Diff
Merge branch 'master' of gitlab.beuth-hochschule.de:s39174/temperaturverteilung
parents
0af1ceb6
9b34fd73
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
api/GetAverageData.py
+7
-0
7 additions, 0 deletions
api/GetAverageData.py
api/api.py
+22
-21
22 additions, 21 deletions
api/api.py
api/write_raster.py
+104
-23
104 additions, 23 deletions
api/write_raster.py
config.ini
+1
-0
1 addition, 0 deletions
config.ini
with
134 additions
and
44 deletions
api/GetAverageData.py
+
7
−
0
View file @
183def33
...
...
@@ -61,6 +61,13 @@ def find_extremum(cursor, years, extremum):
return
result
def
get_min_max
(
cursor
):
years
=
get_year_columns
(
cursor
)
min
=
find_extremum
(
cursor
,
years
,
'
MIN
'
)
max
=
find_extremum
(
cursor
,
years
,
'
MAX
'
)
return
min
,
max
# Find n (defined in config) neighbours and return them ordered by distance
def
get_neighbours
(
cursor
,
lat
,
lon
,
columns
):
values
=
''
# Used in second parameter of cursor.execute() (Avoids SQL injection)
...
...
This diff is collapsed.
Click to expand it.
api/api.py
+
22
−
21
View file @
183def33
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
from
GetAverageData
import
get_interpolation_data_for_point
,
get_year_columns
,
get_average_of_multiple_years
,
find_extremum
from
api
import
write_raster
from
GetAverageData
import
get_interpolation_data_for_point
,
get_year_columns
,
get_average_of_multiple_years
,
get_min_max
from
write_raster
import
write_raster
import
SQLPandasTools
as
s2pTool
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
"
...
...
@@ -97,29 +96,31 @@ def getStandardQuery():
@app.route
(
'
/minmax
'
,
methods
=
[
'
GET
'
])
def
g
et_min_max
():
def
r
et
urn
_min_max
():
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
:
years
=
get_year_columns
(
cursor
)
min
=
find_extremum
(
cursor
,
years
,
'
MIN
'
)
max
=
find_extremum
(
cursor
,
years
,
'
MAX
'
)
print
(
min
,
max
)
min
,
max
=
get_min_max
(
cursor
)
return
{
'
min
'
:
str
(
min
),
'
max
'
:
str
(
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
.
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
+
104
−
23
View file @
183def33
import
configparser
import
math
import
os
from
datetime
import
datetime
import
numpy
as
np
import
psycopg2
from
osgeo
import
gdal
,
osr
from
GetAverageData
import
get_min_max
cfg
=
configparser
.
ConfigParser
()
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]]
def
clean_temp
(
path
):
for
file
in
os
.
listdir
(
path
):
try
:
os
.
remove
(
path
+
file
)
except
:
pass
def
get_class_steps
(
colour_ramp
):
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
:
min_max
=
get_min_max
(
cursor
)
classes
=
len
(
colour_ramp
)
temp_range
=
min_max
[
1
]
-
min_max
[
0
]
steps
=
temp_range
/
classes
min
=
min_max
[
0
]
return
min
,
steps
,
classes
def
colour_picker
(
min
,
steps
,
classes
,
colour_ramp
,
value
):
rgba
=
None
for
i
in
range
(
0
,
classes
+
1
):
def
write_raster
(
data
):
pixel_array
=
[]
pixel_array_alpha
=
[]
for
j
in
range
(
0
,
36
):
row_array
=
[]
row_array_alpha
=
[]
minor
=
math
.
floor
(
min
+
(
i
*
steps
))
major
=
math
.
ceil
(
min
+
((
i
+
1
)
*
steps
))
if
minor
<=
value
<=
major
:
try
:
rgba
=
colour_ramp
[
i
]
except
IndexError
:
rgba
=
colour_ramp
[
-
1
]
if
not
rgba
:
rgba
=
[
0
,
0
,
0
,
0
]
return
rgba
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
,
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
alpha_value
=
data
[
i
+
j
][
2
]
alpha_value
=
0
if
alpha_value
else
255
row_array
.
append
(
value
)
row_array_alpha
.
append
(
alpha_value
)
np_row_array
=
np
.
array
(
row_array
)
np_row_array_alpha
=
np
.
array
(
row_array_alpha
)
pixel_array
.
append
(
np_row_array
)
pixel_array_alpha
.
append
(
np_row_array_alpha
)
np_pixel_array
=
np
.
array
(
pixel_array
)
np_pixel_array_alpha
=
np
.
array
(
pixel_array_alpha
)
if
not
value
==
0
:
rgba
=
colour_picker
(
min
,
steps
,
classes
,
ramp
,
value
)
r
,
g
,
b
,
a
=
rgba
[
0
],
rgba
[
1
],
rgba
[
2
],
rgba
[
3
]
else
:
r
,
g
,
b
,
a
=
0
,
0
,
0
,
0
transparent
=
data
[
i
+
j
][
2
]
a
=
0
if
transparent
else
a
a
=
255
if
a
==
1
else
a
row_array_r
.
append
(
r
)
row_array_g
.
append
(
g
)
row_array_b
.
append
(
b
)
row_array_a
.
append
(
a
)
np_row_array_r
=
np
.
array
(
row_array_r
)
np_row_array_g
=
np
.
array
(
row_array_g
)
np_row_array_b
=
np
.
array
(
row_array_b
)
np_row_array_a
=
np
.
array
(
row_array_a
)
pixel_array_r
.
append
(
np_row_array_r
)
pixel_array_g
.
append
(
np_row_array_g
)
pixel_array_b
.
append
(
np_row_array_b
)
pixel_array_a
.
append
(
np_row_array_a
)
np_pixel_array_r
=
np
.
array
(
pixel_array_r
)
np_pixel_array_g
=
np
.
array
(
pixel_array_g
)
np_pixel_array_b
=
np
.
array
(
pixel_array_b
)
np_pixel_array_a
=
np
.
array
(
pixel_array_a
)
r_band
=
np_pixel_array_r
g_band
=
np_pixel_array_g
b_band
=
np_pixel_array_b
a_band
=
np_pixel_array_a
xmin
,
ymin
,
xmax
,
ymax
=
[
5.01
,
47.15
,
14.81
,
55.33
]
nrows
,
ncols
=
np
.
shape
(
np_pixel_array
)
nrows
,
ncols
=
np
.
shape
(
r_band
)
xres
=
(
xmax
-
xmin
)
/
float
(
ncols
)
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
)
output_raster
.
SetProjection
(
srs
.
ExportToWkt
())
output_raster
.
GetRasterBand
(
1
).
WriteArray
(
np_pixel_array
)
#
output_raster.GetRasterBand(2).WriteArray(
np_pixel_array
)
#
output_raster.GetRasterBand(3).WriteArray(
np_pixel_array
)
output_raster
.
GetRasterBand
(
4
).
WriteArray
(
np_pixel_array_alpha
)
output_raster
.
GetRasterBand
(
1
).
WriteArray
(
r_band
)
output_raster
.
GetRasterBand
(
2
).
WriteArray
(
g_band
)
output_raster
.
GetRasterBand
(
3
).
WriteArray
(
b_band
)
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 @
183def33
...
...
@@ -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