-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqgis_data_insert.py
115 lines (82 loc) · 4.3 KB
/
qgis_data_insert.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import csv
import tables_queries as tq
#Add dicofre and concelho columns in stations table
def add_columns(database):
cursor = database.cursor()
cursor.execute(tq.ADD_DICOFRE_COLUMN)
cursor.execute(tq.ADD_CONCELHO_COLUMN)
database.commit()
#--------------------------------------------------
# IN CASE OF NEEDING TO REMOVE THE NEW COLUMNS:
#--------------------------------------------------
def drop_columns(database):
cursor = database.cursor()
cursor.execute(tq.DROP_DICOFRE_COLUMN)
cursor.execute(tq.DROP_CONCELHO_COLUMN)
database.commit()
#--------------------------------------------------
#Add 'dicofre' and 'concelho' values per each 'Portugal continental' station into the stations table
def add_portugal_data(database, cleaned_file):
cursor=database.cursor()
with open(cleaned_file, 'r', encoding='utf-8') as csv_file:
readed_file = csv.reader(csv_file)
next(readed_file) # Skip the header row
for row in readed_file:
id_estacao, dicofre, concelho = row[0], row[1], row[2]
try:
cursor.execute("UPDATE stations SET dicofre = ?, concelho = ? WHERE id_estacao = ?", (dicofre, concelho, id_estacao))
except Exception as e:
print(f"There is an error for station {id_estacao}: {e}")
database.commit()
#Add 'dicofre' and 'concelho' values per each 'Madeira Island' station into the stations table
def add_madeira_data(database, cleaned_file):
cursor = database.cursor()
with open(cleaned_file, 'r', encoding='utf-8') as csv_file:
readed_file = csv.reader(csv_file)
next(readed_file) # Skip the header row
for row in readed_file:
id_estacao, dicofre, concelho = row[0], row[1], row[2]
try:
cursor.execute("UPDATE stations SET dicofre = ?, concelho = ? WHERE id_estacao = ?", (dicofre, concelho, id_estacao))
except Exception as e:
print(f"There is an error for station {id_estacao}: {e}")
database.commit()
#Add 'dicofre' and 'concelho' values per each 'Central Açores Island' station into the stations table
def add_açores_central_data(database, cleaned_file):
cursor = database.cursor()
with open(cleaned_file, 'r', encoding='utf-8') as csv_file:
readed_file = csv.reader(csv_file)
next(readed_file) # Skip the header row
for row in readed_file:
id_estacao, dicofre, concelho = row[0], row[1], row[2]
try:
cursor.execute("UPDATE stations SET dicofre = ?, concelho = ? WHERE id_estacao = ?", (dicofre, concelho, id_estacao))
except Exception as e:
print(f"There is an error for station {id_estacao}: {e}")
database.commit()
#Add 'dicofre' and 'concelho' values per each 'Occidental Açores Island' station into the stations table
def add_açores_occidental_data(database, cleaned_file):
cursor = database.cursor()
with open(cleaned_file, 'r', encoding='utf-8') as csv_file:
readed_file = csv.reader(csv_file)
next(readed_file) # Skip the header row
for row in readed_file:
id_estacao, dicofre, concelho = row[0], row[1], row[2]
try:
cursor.execute("UPDATE stations SET dicofre = ?, concelho = ? WHERE id_estacao = ?", (dicofre, concelho, id_estacao))
except Exception as e:
print(f"There is an error for station {id_estacao}: {e}")
database.commit()
#Add 'dicofre' and 'concelho' values per each 'Oriental Açores Island' station into the stations table
def add_açores_oriental_data(database, cleaned_file):
cursor = database.cursor()
with open(cleaned_file, 'r', encoding='utf-8') as csv_file:
readed_file = csv.reader(csv_file)
next(readed_file) # Skip the header row
for row in readed_file:
id_estacao, dicofre, concelho = row[0], row[1], row[2]
try:
cursor.execute("UPDATE stations SET dicofre = ?, concelho = ? WHERE id_estacao = ?", (dicofre, concelho, id_estacao))
except Exception as e:
print(f"There is an error for station {id_estacao}: {e}")
database.commit()