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

Weighted interpolation fully working now (lat and long were mixed up)

parent f97e69ce
No related branches found
No related tags found
No related merge requests found
......@@ -91,7 +91,7 @@ def create_table(station_list, db_name):
def insert_empty_matrix_into_db():
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 open('clipped_matrix_10x10.csv', 'r') as matrix:
with open('clipped_matrix_25x25.csv', 'r') as matrix:
matrix_data = matrix.readlines()
for line in matrix_data[1:]:
values = '' # Used in second parameter of cursor.execute() (Avoids SQL injection)
......@@ -121,13 +121,11 @@ def create_matrix_data():
id = point[0]
lon = point[1]
lat = point[2]
print(lat, lon)
interpolation_data = get_interpolation_data_for_point(lat, lon, sql.SQL('*'))
for year, value in interpolation_data.items():
update_data.append({'year': year, 'value': value, 'id': id})
print(update_data)
query = sql.SQL("""UPDATE stations SET "%(year)s" = %(value)s WHERE id = %(id)s; """)
psycopg2.extras.execute_batch(cursor, query, update_data) # 2 times faster than using execute() in a for loop, ~20 mins instead of 40
......
......@@ -31,7 +31,7 @@ def get_neighbours(cursor, lat, lon, columns):
query = sql.SQL("""
SELECT array_to_json(array_agg(row_to_json(t))) from (
SELECT {columns}, ST_Distance(ST_MakePoint(lon, lat), ST_MakePoint({lon}, {lat})) AS distance
SELECT {columns}, ST_Distance(ST_MakePoint(lat, lon), ST_MakePoint({lon}, {lat})) AS distance
FROM stations
WHERE file IS NOT NULL
ORDER BY distance
......
# import psycopg2
# from psycopg2 import sql
import configparser
import psycopg2
from psycopg2 import sql
cfg = configparser.ConfigParser()
cfg.read('config.ini')
assert "POSTGRES" in cfg, "missing POSTGRES in config.ini"
param_postgres = cfg["POSTGRES"]
param_interpol = cfg["INTERPOLATION"]
#
# query = sql.SQL("select {0} from {1} where {2} LIKE {3} {4}").format(sql.SQL(', ').join([sql.Identifier('foo'), sql.Identifier('bar')]), sql.Identifier('table'), sql.Identifier('coulumn'), sql.Placeholder(),
# sql.SQL('AND 1 = 1'))
......@@ -20,8 +29,30 @@
# list_b = [x * 2 for x in list_a if x % 2 == 0]
# print(list_b)
# list_a = [1,2,3,4,5,6,7,8,9,10]
# new_list = list_a[0::2]
# print(new_list)
def get_neighbours(lat, lon, columns):
values = '' # Used in second parameter of cursor.execute() (Avoids SQL injection)
for n in [lat, lon]:
values = (*values, n) # adding n to existing tuple
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:
query = sql.SQL("""
SELECT array_to_json(array_agg(row_to_json(t))) from (
SELECT {columns}, ST_Distance(ST_MakePoint(lat, lon), ST_MakePoint({lon}, {lat})) AS distance
FROM stations
WHERE file IS NOT NULL
ORDER BY distance
LIMIT {amount_neighbours}
) t;
""").format(columns=columns, lon=sql.Placeholder(), lat=sql.Placeholder(), amount_neighbours=sql.SQL(param_interpol["amount_neighbours"]))
cursor.execute(query, values)
neighbours = cursor.fetchall()[0][0]
print(neighbours)
# return neighbours
list_a = [1,2,3,4,5,6,7,8,9,10]
new_list = list_a[0::2]
print(new_list)
\ No newline at end of file
get_neighbours(53.42, 9.24, sql.SQL('"2015", "2014", "2010"'))
\ No newline at end of file
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