-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfill_db_extract.py
82 lines (67 loc) · 3.19 KB
/
fill_db_extract.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import os
import json
import csv
import tables_queries as tq
#Create the tables in the database
def create_tables(database):
cursor = database.cursor()
cursor.execute(tq.CREATE_STATIONS_TABLE)
cursor.execute(tq.CREATE_OBSERVATIONS_TABLE)
database.commit()
#Insert data from the JSON files into the stations table
def insert_stations_data(database, raw_data_path):
for filename in os.listdir(raw_data_path):
if "stations" in filename.lower() and filename.endswith(".json"):
with open(os.path.join(raw_data_path, filename), "r") as file:
try:
data = json.load(file)
except json.decoder.JSONDecodeError as e:
print(f"Error loading data from {file}: {e}")
continue
cursor = database.cursor()
for station in data:
cursor.execute(tq.INSERT_STATIONS_DATA,(
station["properties"]["idEstacao"],
station["geometry"]["coordinates"][0],
station["geometry"]["coordinates"][1],
station["properties"]["localEstacao"]
))
database.commit()
#Insert data from the JSON files into the observations table
def insert_observations_data(database, raw_data_path):
for filename in os.listdir(raw_data_path):
if "observations" in filename.lower() and filename.endswith(".json"):
with open(os.path.join(raw_data_path, filename), "r") as file:
try:
data = json.load(file)
except json.decoder.JSONDecodeError as e:
print(f"Error loading data from {file}: {e}")
cursor = database.cursor()
for date, station in data.items():
for id, observations in station.items():
if observations is not None:
cursor.execute(tq.INSERT_OBSERVATIONS_TABLE, (
date,
id,
observations["intensidadeVentoKM"],
observations["temperatura"],
observations["radiacao"],
observations["idDireccVento"],
observations["precAcumulada"],
observations["intensidadeVento"],
observations["humidade"],
observations["pressao"]
))
database.commit()
#Retrieve and fetch all the data from the 'stations' table
def get_coordinates(database):
cursor = database.cursor()
cursor.execute(tq.GET_STATIONS_DATA)
return cursor.fetchall()
#Save stations data into a CSV file to import it into QGIS
def save_to_csv(data, filename):
os.makedirs(os.path.dirname(filename), exist_ok=True)
with open(filename, 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['id_estacao', 'latitude', 'longitude', 'local_estacao'])
writer.writerows(data)