Skip to content

Commit

Permalink
Fix flake8 errors
Browse files Browse the repository at this point in the history
  • Loading branch information
vikineema committed Feb 20, 2024
1 parent 8c2d7f2 commit 3fc1f5f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
33 changes: 17 additions & 16 deletions deafrica_conflux/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from deafrica_conflux.db_tables import Waterbody, WaterbodyBase, WaterbodyObservation
from deafrica_conflux.id_field import guess_id_field
from deafrica_conflux.io import PARQUET_EXTENSIONS, check_file_exists, read_table_from_parquet
from deafrica_conflux.text import task_id_string_from_parquet_file_name

_log = logging.getLogger(__name__)

Expand Down Expand Up @@ -245,8 +244,8 @@ def drop_all_waterbody_tables(engine: Engine):
def add_waterbody_polygons_to_db(
engine: Engine,
waterbodies_polygons_fp: str | Path,
drop_table: bool = True,
update_rows: bool = True,
drop_table: bool = False,
update_rows: bool = False,
):
"""
Add the waterbody polygon into the waterbodies table.
Expand All @@ -257,10 +256,10 @@ def add_waterbody_polygons_to_db(
drop_table : bool, optional
If True drop the waterbodies table first and create a new table., by default True
update_rows : bool, optional
If True if the polygon uid already exists in the waterbodies table, it will be updated,
If True if the polygon uid already exists in the waterbodies table, the row will be updatedit will be updated,
else it will be skipped.
waterbodies_polygons_fp : str | Path | None, optional
Path to the shapefile/geojson/geoparquet file containing the waterbodies polygons, by default None, by default None
Path to the shapefile/geojson/geoparquet file containing the waterbodies polygons, by default None, by default None
"""
# connect to the db
if not engine:
Expand Down Expand Up @@ -350,15 +349,14 @@ def add_waterbody_polygons_to_db(
with Session() as session:
row_to_update = session.query(table).filter_by(uid=row.UID).first()
# Modify the attributes of the queried object
if row_to_update:
row_to_update.area_m2 = row.area_m2
row_to_update.wb_id = row.WB_ID
row_to_update.length_m = row.length_m
row_to_update.perim_m = row.perim_m
row_to_update.timeseries = row.timeseries
row_to_update.geometry = f"SRID={srid};{row.geometry.wkt}"
# Commit the changes to the session
session.commit()
row_to_update.area_m2 = row.area_m2
row_to_update.wb_id = row.WB_ID
row_to_update.length_m = row.length_m
row_to_update.perim_m = row.perim_m
row_to_update.timeseries = row.timeseries
row_to_update.geometry = f"SRID={srid};{row.geometry.wkt}"
# Commit the changes to the session
session.commit()
# Close the session
session.close()
else:
Expand Down Expand Up @@ -694,8 +692,11 @@ def add_waterbody_observations_pq_files_to_db_v2(

# read the drill output table in...
df = pd.read_parquet(path)
# parse the date...
task_id_string = task_id_string_from_parquet_file_name(path)
# Parse the drill name and task id from the file path.
# This only works for files named using `make_parquet_file_name`
base_name, _ = os.path.splitext(os.path.basename(path))
_, x, y, period = base_name.split("_")
task_id_string = f"{period}/{x}/{y}"

# Note: Doing it this way because drill outputs can be millions of rows.
with Session() as session:
Expand Down
8 changes: 2 additions & 6 deletions deafrica_conflux/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,10 +492,10 @@ def add_missing_metadata(path: str):
"""
try:
df = read_table_from_parquet(path)
read_table_from_parquet(path)
except KeyError:
# Parse the drill name and task id from the file path.
# This only works for files named using th
# This only works for files named using `make_parquet_file_name`
base_name, _ = os.path.splitext(os.path.basename(path))
drill_name, x, y, period = base_name.split("_")
task_id_string = f"{period}/{x}/{y}"
Expand Down Expand Up @@ -530,10 +530,6 @@ def add_missing_metadata(path: str):

# Write the table back.
is_s3 = check_if_s3_uri(path)
if is_s3:
fs = fsspec.filesystem("s3")
else:
fs = fsspec.filesystem("file")

if is_s3:
s3 = boto3.client("s3")
Expand Down

0 comments on commit 3fc1f5f

Please sign in to comment.