-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create and load db "ports" with alembic & new ports db: UpdatedPub150…
….csv
- Loading branch information
Showing
10 changed files
with
15,773 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import logging | ||
import os | ||
from pathlib import Path | ||
|
||
import pandas as pd | ||
from sqlalchemy import create_engine | ||
|
||
logging.basicConfig() | ||
logging.getLogger("sqlalchemy.engine").setLevel(logging.INFO) | ||
|
||
postgres_user = os.environ.get("POSTGRES_USER") | ||
postgres_password = os.environ.get("POSTGRES_PASSWORD") | ||
postgres_hostname = os.environ.get("POSTGRES_HOSTNAME") | ||
postgres_db = os.environ.get("POSTGRES_DB") | ||
postgres_port = os.environ.get("POSTGRES_PORT") | ||
|
||
# The db url is configured with the db connexion variables declared in the db.yaml file. | ||
db_url = ( | ||
"postgresql://" | ||
+ postgres_user | ||
+ ":" | ||
+ postgres_password | ||
+ "@" | ||
+ postgres_hostname | ||
+ ":"s | ||
+ postgres_port | ||
+ "/" | ||
+ postgres_db | ||
) | ||
|
||
engine = create_engine(db_url) | ||
|
||
df = pd.read_csv( | ||
Path(os.path.dirname(__file__)).joinpath("../../data/ports_rad3000_res10.csv"), | ||
sep=";", | ||
) | ||
|
||
df.to_sql("ports", engine, if_exists="append", index=False) |
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,49 @@ | ||
"""create ports table | ||
Revision ID: 7962eee40abe | ||
Revises: 961cee5426d6 | ||
Create Date: 2024-02-26 18:38:37.726130 | ||
""" | ||
import sqlalchemy as sa | ||
from geoalchemy2 import Geometry | ||
|
||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "7962eee40abe" | ||
down_revision = "961cee5426d6" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
""" | ||
ports.csv is a file that contains the data of the ports. | ||
url;country;port;locode;latitude;longitude;geometry_point;geometry_buffer | ||
https://www.vesselfinder.com/ports/ALSAR001;Albania;Sarande;ALSAR;39.8701;20.0062;POINT (20.0062 39.8701 | ||
""" | ||
op.create_table( | ||
"ports", | ||
sa.Column("id", sa.Integer, primary_key=True), | ||
sa.Column("country", sa.String(255), nullable=False), | ||
sa.Column("port", sa.String(255), nullable=False), | ||
sa.Column("url", sa.String(255), nullable=False), | ||
sa.Column("locode", sa.String(255), nullable=False), | ||
sa.Column("latitude", sa.String(255), nullable=False), | ||
sa.Column("longitude", sa.String(255), nullable=False), | ||
sa.Column( | ||
"geometry_point", | ||
Geometry(geometry_type="POINT", srid=4326), | ||
nullable=False, | ||
), | ||
sa.Column( | ||
"geometry_buffer", | ||
Geometry(geometry_type="POLYGON", srid=4326), | ||
nullable=False, | ||
), | ||
) | ||
|
||
|
||
def downgrade() -> None: | ||
op.drop_table("ports") |
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
Oops, something went wrong.