-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput.py
38 lines (31 loc) · 1.19 KB
/
output.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
import os
import psycopg2
from sqlalchemy import create_engine
from sqlalchemy import text
from dotenv import load_dotenv
import geopandas as gpd
load_dotenv()
host = os.getenv("HOST")
database = os.getenv("DB")
user = os.getenv("USER")
password = os.getenv("PASSWORD")
port = os.getenv("PORT")
def create_geojson(dbname, path):
"""
Creates shapefiles of output tables
"""
engine = create_engine(f"postgresql://{user}:{password}@{host}:{port}/{dbname}")
connection = engine.connect()
query = text(f"SELECT table_name FROM information_schema.tables WHERE table_schema = 'output' AND table_name LIKE '%gis%'")
tables = connection.execute(query)
table_names = [table[0] for table in tables]
for table_name in table_names:
if 'crashes' in table_name:
gdf = gpd.read_postgis(f"SELECT * FROM output.{table_name} where geom is not null", engine)
elif 'rhin' in table_name:
gdf = gpd.read_postgis(f"SELECT * FROM output.rhin_gis", engine)
else:
continue
output_file = os.path.join(path, f"{table_name}.geojson")
gdf.to_file(output_file)
print(f"Created {output_file} in output folder...\n")