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

Not working at all

parent 15923263
No related branches found
No related tags found
No related merge requests found
......@@ -4,7 +4,7 @@ import psycopg2
from psycopg2 import sql
cfg = configparser.ConfigParser()
cfg.read('config.ini')
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"]
......
......@@ -5,7 +5,7 @@ import configparser
from GetAverageData import get_interpolation_data_for_point
cfg = configparser.ConfigParser()
cfg.read('config.ini')
cfg.read('../config.ini')
assert "POSTGRES" in cfg, "missing POSTGRES in config.ini"
param_postgres = cfg["POSTGRES"]
......
......@@ -7,6 +7,7 @@ export the stationlist to database
@author: geopeter
"""
import os
import sqlite3
import psycopg2
import psycopg2.extras
......@@ -18,11 +19,14 @@ from pathlib import Path
from sqlite3 import Error
cfg = configparser.ConfigParser()
cfg.read('config.ini')
cfg.read('../config.ini')
assert "POSTGRES" in cfg, "missing POSTGRES in config.ini"
param_postgres = cfg["POSTGRES"]
param_interpol = cfg["INTERPOLATION"]
stationGPD = None
cwd = Path(os.path.dirname(os.path.abspath(__file__)))
spatialite_path = 'D:/Uni/Master/01_SS2021/Automatisierte_Geodatenprozessierung/temperaturverteilung/dataacquisition/mod_spatialite-5.0.1-win-amd64'
os.environ['PATH'] = spatialite_path + ';' + os.environ['PATH']
# Create DB "temperatures_berteld_morstein"
......@@ -52,26 +56,26 @@ def check_for_table_existence(cursor):
try:
cursor.execute("SELECT * FROM temperatures")
db_exists = cursor.fetchall()[0]
print('DB existing')
print('Table existing')
print('Updating data')
return True
except Error as error:
print('No DB found')
print('Table not found')
return False
# Connect to DB "temperatures_berteld_morstein" to create table "temperatures". Also installs PostGIS
def create_table(station_list, cursor):
# Connect to DB "temperatures_berteld_morstein" to create table "temperatures".
def create_table(cursor, station_list):
print('Creating table stations')
df_columns = list(station_list)
columns = ['id INTEGER', 'lon NUMERIC', 'lat NUMERIC', 'country TEXT', 'file TEXT']
columns = ['"id" INTEGER', '"lon" NUMERIC', '"lat" NUMERIC', '"country" TEXT', '"file" TEXT']
for column in df_columns:
if str(column).startswith('19') or str(column).startswith('20'):
columns.append('"{}" NUMERIC'.format(column))
columns_clean = str(columns).strip('[]').replace("'", "")
query = """CREATE TABLE stations ({});""".format(columns_clean)
#print(query)
print(query)
cursor.execute(query)
print('Done')
......@@ -131,8 +135,6 @@ def insert_data(cursor, station_list):
print('Inserting data into database')
if len(station_list) > 0:
# print(stationList)
cursor.execute("DELETE FROM stations;")
df_columns = list(station_list)
# create (col1,col2,...)
......@@ -141,14 +143,34 @@ def insert_data(cursor, station_list):
# for column in df_columns:
# columns.append('"' + column + '"')
columns = str(columns).replace('[', '').replace(']', '').replace("'", "").replace('\n', '').replace(' ', '')
station_list = station_list.round(decimals=3)
# create VALUES('%s', '%s",...) one '%s' per column
values = "VALUES({})".format(",".join(["%s" for _ in df_columns]))
# create INSERT INTO table (columns) VALUES('%s',...)
insert_stmt = """INSERT INTO {} ({}) {}""".format('stations', columns, values)
psycopg2.extras.execute_batch(cursor, insert_stmt, station_list.values)
values = "VALUES({})".format(",".join(['?' for _ in df_columns]))
print(list(station_list[df_columns].to_records(index=False)))
cursor.executemany("INSERT INTO stations (?) {}".format(values), df_columns, list(station_list[df_columns].to_records(index=False)))
# cursor.execute("DELETE FROM stations;")
#
# df_columns = list(station_list)
# # create (col1,col2,...)
#
# # As integers like 2018, 2017, etc. are not possible as column names, double quotes have to be added. This requires some tricks and cleanups
# columns = ['"' + column + '"' for column in df_columns]
# # for column in df_columns:
# # columns.append('"' + column + '"')
# columns = str(columns).replace('[', '').replace(']', '').replace("'", "").replace('\n', '').replace(' ', '')
# station_list = station_list.round(decimals=3)
# print('colimns:')
# print(columns)
#
# # create VALUES('%s', '%s",...) one '%s' per column
# values = "VALUES({})".format(",".join([value for value in df_columns]))
#
# # create INSERT INTO table (columns) VALUES('%s',...)
# insert_stmt = """INSERT INTO stations ({}) {};""".format(columns, values)
# print(insert_stmt)
# cursor.execute(insert_stmt)
print('Done')
......@@ -165,18 +187,36 @@ def create_connection(db_file):
def export(station_list):
connection = create_connection('temperatures.db') # ':memory:' for saving in RAM
try:
connection = create_connection('temperatures.db')
c = connection.cursor()
c.execute("CREATE TABLE test (id INTEGER);")
# c.execute("CREATE TABLE 123 (id INTEGER);")
new_db = False if check_for_table_existence(c) else True
if new_db: create_table(c, station_list)
# c.close()
# connection.commit()
# c = connection.cursor()
connection.commit()
connection = create_connection('temperatures.db')
c = connection.cursor()
c.execute('select * from stations')
print(c.fetchall())
insert_data(c, station_list)
c.execute('select * from stations')
print(c.fetchall())
amount_points = insert_empty_matrix_into_db(c)
c.execute('select * from stations')
print(c.fetchall())
create_matrix_data(c, amount_points)
# create_matrix_data(c, amount_points)
# c.close()
# connection.commit()
# connection.close()
except Error as e:
print(e)
......
[POSTGRES]
host = localhost
port = 5432
user = postgres
password = postgres
dbName = temperatures_berteld_morstein
[INTERPOLATION]
; neighbouring measurements used for interpolation
amount_neighbours = 5
; in km. 25km or 10km possible
matrix_density = 25
\ No newline at end of file
......@@ -26,7 +26,8 @@ connection = create_connection('temperatures.db') # ':memory:' for saving in
try:
c = connection.cursor()
# c.execute("CREATE TABLE test (id INTEGER);")
# c.execute("select * from test;")
# c.execute('INSERT INTO test (id) VALUES (1);')
c.execute("select * from stations;")
print(c.fetchall())
except Error as e:
print(e)
......
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