Skip to content
Snippets Groups Projects
Commit d7363fe0 authored by Clemens Berteld's avatar Clemens Berteld
Browse files

Added API endpoint for getting extrema

parent 94001e78
No related branches found
No related tags found
No related merge requests found
...@@ -24,7 +24,7 @@ def get_average_of_multiple_years(cursor, years): ...@@ -24,7 +24,7 @@ def get_average_of_multiple_years(cursor, years):
avg_strings = avg_strings[:-2] avg_strings = avg_strings[:-2]
query = """SELECT station_id, ROUND(({}) / {}, 1), transparent FROM stations WHERE file IS NULL GROUP BY station_id, transparent ORDER BY station_id ASC;""".format(avg_strings, n) query = """SELECT station_id, ROUND(({}) / {}, 1), transparent FROM stations WHERE file IS NULL GROUP BY station_id, transparent ORDER BY station_id ASC;""".format(avg_strings, n)
print(query) # print(query)
cursor.execute(query) cursor.execute(query)
return cursor.fetchall() return cursor.fetchall()
...@@ -43,6 +43,23 @@ def get_year_columns(cursor): ...@@ -43,6 +43,23 @@ def get_year_columns(cursor):
return columns return columns
def find_extremum(cursor, years, extremum):
extremum_strings = ''
and_strings = ''
for year in years:
extremum_string = ' {}("{}"), '.format(extremum, year)
and_string = """ AND "{}" != 'NaN' """.format(year)
extremum_strings += extremum_string
and_strings += and_string
extremum_strings = extremum_strings[:-2]
query = 'SELECT {} FROM stations WHERE lon IS NOT NULL {};'.format(extremum_strings, and_strings)
cursor.execute(query)
results = cursor.fetchall()
np_array = np.array(results)
result = np.max(np_array) if extremum == 'MAX' else np.min(np_array)
return result
# Find n (defined in config) neighbours and return them ordered by distance # Find n (defined in config) neighbours and return them ordered by distance
def get_neighbours(cursor, lat, lon, columns): def get_neighbours(cursor, lat, lon, columns):
values = '' # Used in second parameter of cursor.execute() (Avoids SQL injection) values = '' # Used in second parameter of cursor.execute() (Avoids SQL injection)
......
...@@ -3,7 +3,7 @@ from flask import Flask, jsonify, request, send_from_directory ...@@ -3,7 +3,7 @@ from flask import Flask, jsonify, request, send_from_directory
# from flask_cors import cross_origin # from flask_cors import cross_origin
from psycopg2 import sql from psycopg2 import sql
import configparser import configparser
from GetAverageData import get_interpolation_data_for_point, get_year_columns, get_average_of_multiple_years from GetAverageData import get_interpolation_data_for_point, get_year_columns, get_average_of_multiple_years, find_extremum
from dataacquisition import write_raster from dataacquisition import write_raster
# import SQLPandasTools as s2pTool # import SQLPandasTools as s2pTool
...@@ -20,7 +20,7 @@ app.config['TESTING'] = False ...@@ -20,7 +20,7 @@ app.config['TESTING'] = False
@app.route('/', methods=['GET']) @app.route('/', methods=['GET'])
@cross_origin() # @cross_origin()
def index(): def index():
columns = sql.SQL(' * ') # columns to be queried (e.g. years) columns = sql.SQL(' * ') # columns to be queried (e.g. years)
wheres = sql.SQL('') # where filters wheres = sql.SQL('') # where filters
...@@ -96,6 +96,17 @@ def getStandardQuery(): ...@@ -96,6 +96,17 @@ def getStandardQuery():
return query return query
@app.route('/minmax', methods=['GET'])
def get_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)
return {'min': str(min), 'max': str(max)}
@app.route('/raster', methods=['GET']) @app.route('/raster', methods=['GET'])
def get_raster(): def get_raster():
if 'years' in request.args: if 'years' in request.args:
...@@ -107,25 +118,25 @@ def get_raster(): ...@@ -107,25 +118,25 @@ def get_raster():
# all_years = get_year_columns(cursor) # all_years = get_year_columns(cursor)
average_data = get_average_of_multiple_years(cursor, years) average_data = get_average_of_multiple_years(cursor, years)
write_raster.write_raster(average_data) 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 send_from_directory('D:/Uni/Master/01_SS2021/Automatisierte_Geodatenprozessierung/temperaturverteilung/dataacquisition/output', filename='myraster.tif', as_attachment=True)
# return 'Läuft, Brudi' return 'Läuft, Brudi'
@app.route('/annualMean', methods=['GET']) # @app.route('/annualMean', methods=['GET'])
@cross_origin() # @cross_origin()
def annualMean(): # def annualMean():
#
query = getStandardQuery() # query = getStandardQuery()
#
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 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: # with connection.cursor() as cursor:
#
cursor.execute(query) # cursor.execute(query)
#
results = cursor.fetchall()[0][0] # results = cursor.fetchall()[0][0]
#
return jsonify(s2pTool.determineAnnualMean(results)) # return jsonify(s2pTool.determineAnnualMean(results))
#
return "{}" # return "{}"
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -15,13 +15,10 @@ def write_raster(data): ...@@ -15,13 +15,10 @@ def write_raster(data):
value = 0 if str(value) == 'NaN' else value value = 0 if str(value) == 'NaN' else value
alpha_value = data[i + j][2] alpha_value = data[i + j][2]
alpha_value = 0 if alpha_value else 255 alpha_value = 0 if alpha_value else 255
# print(value)
row_array.append(value) row_array.append(value)
row_array_alpha.append(alpha_value) row_array_alpha.append(alpha_value)
np_row_array = np.array(row_array) np_row_array = np.array(row_array)
np_row_array_alpha = np.array(row_array_alpha) np_row_array_alpha = np.array(row_array_alpha)
# np_row_array_alpha = np.where(np_row_array_alpha < 8, 0, 255)
# print(row_array)
pixel_array.append(np_row_array) pixel_array.append(np_row_array)
pixel_array_alpha.append(np_row_array_alpha) pixel_array_alpha.append(np_row_array_alpha)
np_pixel_array = np.array(pixel_array) np_pixel_array = np.array(pixel_array)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment