Skip to content

Commit

Permalink
revises logging, cf #31
Browse files Browse the repository at this point in the history
  • Loading branch information
alpha-beta-soup committed Aug 30, 2024
1 parent 8553309 commit 7816508
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions vector2dggs/h3.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

LOGGER = logging.getLogger(__name__)
click_log.basic_config(LOGGER)
click_log.ColorFormatter.colors['info'] = dict(fg="green")
MIN_H3, MAX_H3 = 0, 15

warnings.filterwarnings(
Expand Down Expand Up @@ -150,7 +151,7 @@ def drop_condition(
log_statement: str,
warning_threshold: float = 0.01,
):
LOGGER.info(log_statement)
LOGGER.debug(log_statement)
_before = len(df)
df = df.drop(drop_index)
_after = len(df)
Expand Down Expand Up @@ -201,7 +202,7 @@ def _index(

if cut_crs:
df = df.to_crs(cut_crs)
LOGGER.info("Cutting with CRS: %s", df.crs)
LOGGER.debug("Cutting with CRS: %s", df.crs)

if id_field:
df = df.set_index(id_field)
Expand All @@ -213,15 +214,15 @@ def _index(
# Remove all attributes except the geometry
df = df.loc[:, ["geometry"]]

LOGGER.info("Cutting large geometries")
with tqdm(total=df.shape[0]) as pbar:
LOGGER.debug("Cutting large geometries")
with tqdm(total=df.shape[0], desc='Splitting') as pbar:
for index, row in df.iterrows():
df.loc[index, "geometry"] = GeometryCollection(
katana.katana(row.geometry, cut_threshold)
)
pbar.update(1)

LOGGER.info("Exploding geometry collections and multipolygons")
LOGGER.debug("Exploding geometry collections and multipolygons")
df = (
df.to_crs(4326)
.explode(index_parts=False) # Explode from GeometryCollection
Expand All @@ -233,22 +234,22 @@ def _index(
"index": lambda frame: frame[
(frame.geometry.is_empty | frame.geometry.isna())
],
"message": "Dropping empty or null geometries",
"message": "Considering empty or null geometries",
},
{
"index": lambda frame: frame[
(frame.geometry.geom_type != "Polygon")
& (frame.geometry.geom_type != "LineString")
], # NB currently points and other types are lost; in principle, these could be indexed
"message": "Dropping unsupported geometries",
"message": "Considering unsupported geometries",
},
]
for condition in drop_conditions:
df = drop_condition(df, condition["index"](df).index, condition["message"])

ddf = dgpd.from_geopandas(df, chunksize=max(1, chunksize), sort=True)

LOGGER.info("Spatially sorting and partitioning (%s)", spatial_sorting)
LOGGER.debug("Spatially sorting and partitioning (%s)", spatial_sorting)
ddf = ddf.spatial_shuffle(by=spatial_sorting)
spatial_sort_col = (
spatial_sorting
Expand All @@ -257,13 +258,13 @@ def _index(
)

with tempfile.TemporaryDirectory(suffix=".parquet") as tmpdir:
with TqdmCallback():
with TqdmCallback(desc=f'Spatially partitioning'):
ddf.to_parquet(tmpdir, overwrite=True)

filepaths = list(map(lambda f: f.absolute(), Path(tmpdir).glob("*")))

# Multithreaded polyfilling
LOGGER.info(
LOGGER.debug(
"H3 Indexing on spatial partitions by polyfill with H3 resolution: %d",
resolution,
)
Expand All @@ -273,7 +274,7 @@ def _index(
(filepath, spatial_sort_col, resolution, parent_res, tmpdir2)
for filepath in filepaths
]
list(tqdm(pool.imap(polyfill_star, args), total=len(args)))
list(tqdm(pool.imap(polyfill_star, args), total=len(args), desc='DGGS indexing'))

_parent_partitioning(
tmpdir2, output_directory, resolution, parent_res, overwrite=overwrite
Expand Down Expand Up @@ -424,7 +425,7 @@ def h3(
con = sqlalchemy.create_engine(vector_input)
elif not Path(vector_input).exists():
if not scheme:
LOGGER.warning(
LOGGER.error(
f"Input vector {vector_input} does not exist, and is not recognised as a remote URI"
)
raise FileNotFoundError(
Expand All @@ -441,7 +442,7 @@ def h3(
f"{output_directory} already exists; if you want to overwrite this, use the -o/--overwrite flag"
)
elif outputexists and overwrite:
LOGGER.info(f"Overwriting the contents of {output_directory}")
LOGGER.warn(f"Overwriting the contents of {output_directory}")
shutil.rmtree(output_directory)
output_directory.mkdir(parents=True, exist_ok=True)

Expand Down

0 comments on commit 7816508

Please sign in to comment.