-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate sql usage of location service to db gateway (#29)
* db gateway now create location service * add etl for location_service * remove sqlalchemy and use raw sql querries * bug fixes, remote fkey rule for etl purposes * Remove print stmt
- Loading branch information
Showing
10 changed files
with
163 additions
and
398 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from pathlib import Path | ||
|
||
import geopandas as gpd | ||
import numpy as np | ||
import pandas as pd | ||
from geoalchemy2 import Geometry | ||
from sqlalchemy import create_engine | ||
|
||
|
||
def seed_from_csv(csv_filepath, db_user, db_password, db_host, db_name): | ||
file = Path(csv_filepath) | ||
if not file.is_file(): | ||
raise FileNotFoundError("No file exists at the location specified") | ||
gdf = gpd.GeoDataFrame(pd.read_csv(file)) | ||
gdf.fillna(np.nan).replace([np.nan], [None]) | ||
gdf = gdf[["lat", "lon", "geo"]] | ||
gdf.index.name = "id" | ||
gdf.index += 1 | ||
# gdf = gdf.head(10) # For testing purposes | ||
|
||
engine = create_engine( | ||
f"postgresql+psycopg2://{db_user}:{db_password}@{db_host}/{db_name}" | ||
) | ||
|
||
response = gdf.to_sql( | ||
name="location_service", | ||
con=engine, | ||
schema="public", | ||
if_exists="replace", | ||
index=True, | ||
method="multi", | ||
dtype={"geo": Geometry("POINT", srid=4326)}, | ||
) | ||
if gdf.shape[0] != response: | ||
raise SystemError("dataframe insertion failed") | ||
else: | ||
print("location_service dataframe insertion success") | ||
|
||
|
||
def main(csv_filepath, db_user, db_password, db_host, db_name): | ||
print("1) Parsing and seeding routemodel into location_service...") | ||
seed_from_csv(csv_filepath, db_user, db_password, db_host, db_name) | ||
|
||
|
||
if __name__ == "__main__": | ||
csv_filepath = "" | ||
db_user = "" | ||
db_password = "" | ||
db_host = "" | ||
db_name = "" | ||
main(csv_filepath, db_user, db_password, db_host, db_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
DATABASE_URI="postgresql+psycopg2://user:password@host:port/database" | ||
DB_HOST="" | ||
DB_NAME="" | ||
DB_USER="" | ||
DB_PASSWORD="" | ||
DB_PORT="" | ||
REQUIRE_AUTH="" | ||
AUTH_KEY="" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,10 @@ | ||
blinker==1.6.2 | ||
click==8.1.5 | ||
blinker==1.7.0 | ||
click==8.1.7 | ||
colorama==0.4.6 | ||
Flask==2.3.2 | ||
Flask-SQLAlchemy==3.0.5 | ||
GeoAlchemy2==0.14.0 | ||
greenlet==2.0.2 | ||
Flask==3.0.0 | ||
itsdangerous==2.1.2 | ||
Jinja2==3.1.2 | ||
MarkupSafe==2.1.3 | ||
numpy==1.25.1 | ||
packaging==23.1 | ||
pandas==2.0.3 | ||
psycopg2==2.9.6 | ||
python-dateutil==2.8.2 | ||
psycopg2==2.9.9 | ||
python-dotenv==1.0.0 | ||
pytz==2023.3 | ||
six==1.16.0 | ||
SQLAlchemy==2.0.18 | ||
typing_extensions==4.7.1 | ||
tzdata==2023.3 | ||
Werkzeug==3.0.1 |
Oops, something went wrong.