-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract.py
More file actions
54 lines (42 loc) · 1.73 KB
/
extract.py
File metadata and controls
54 lines (42 loc) · 1.73 KB
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
import json
import psycopg2 as pspg
import geopandas as gpd
import os
from datetime import datetime
import warnings
warnings.filterwarnings("ignore", category=UserWarning)
f = open("config.json", "r")
config = json.load(f)
db_name = config['DB_NAME']
db_user = config['DB_USER']
db_pass = config['DB_PASS']
db_host = config['DB_HOST']
try:
conn = pspg.connect(database=db_name, user=db_user, password=db_pass, host=db_host)
# print("Connessione effettuata")
except:
print("Connessione fallita")
tables = ["cetacei", "tartarughe"]
# Eliminazione file di log esistente
if os.path.exists("log.txt"):
os.remove("log.txt")
for table in tables:
# Estrazione dati da Postgres
sql = """SELECT codice, data_rilievo, regione, provincia, comune,
specie, cod_specie, sesso, lunghezza AS lunghezza_cm, tipo_lunghezza, condizioni,
targhetta AS tag, targhetta_rilascio AS tag_rilascio, interazione,
segnalatore AS segnalato_da, rilevatore, struttura_rilevatore, the_geom AS geometry
FROM """+table+"""; """
gdf = gpd.GeoDataFrame.from_postgis(sql, conn, geom_col="geometry").sort_values(by="data_rilievo", ascending=False)
gdf['data_rilievo'] = gdf['data_rilievo'].astype('str')
# Scrittura file GeoJSON
gdf.to_file(os.path.join(r'data/geo', table+".geojson"), driver='GeoJSON')
# Scrittura file CSV
gdf['lng'] = gdf['geometry'].x
gdf['lat'] = gdf['geometry'].y
df = gdf.drop('geometry', axis = 1)
df.to_csv(os.path.join(r'data/csv', table+".csv"), encoding='utf-8', index=False)
# Log
with open("log.txt", "a") as log:
log.write("Dataset {} estratto con successo il {}\n".format(table, datetime.now()))
log.close()