Skip to content

Commit

Permalink
enforce limitation to polygon geometries cf. #5 #6
Browse files Browse the repository at this point in the history
  • Loading branch information
alpha-beta-soup committed Sep 12, 2023
1 parent 355913f commit ad746fe
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions vector2dggs/h3.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def polyfill_star(args) -> None:
def _parent_partitioning(
input_dir: Path,
output_dir: Path,
resolution,
resolution: int,
parent_res: Union[None, int],
**kwargs,
) -> None:
Expand All @@ -128,6 +128,16 @@ def _parent_partitioning(

return

def drop_condition(df : pd.DataFrame, drop_index : pd.Index, log_statement : str, warning_threshold : float =0.01):
LOGGER.info(log_statement)
_before = len(df)
df = df.drop(drop_index)
_after = len(df)
_diff = _before - _after
if _diff:
log_method = LOGGER.info if (_diff/float(_before)) < warning_threshold else LOGGER.warn
log_method(f"Dropped {_diff} rows ({_diff/float(_before)*100:.2f}%)")
return df

def _index(
input_file: Union[Path, str],
Expand Down Expand Up @@ -193,12 +203,19 @@ def _index(
.explode(index_parts=False) # Explode from GeometryCollection
.explode(index_parts=False) # Explode multipolygons to polygons
).reset_index()
LOGGER.info("Dropping empty or null geometries")
df = (
df.drop(df[(df.geometry.is_empty|df.geometry.isna())].index)
.reset_index()
)

drop_conditions = [
{
'index': lambda frame: frame[(frame.geometry.is_empty|frame.geometry.isna())],
'message': "Dropping empty or null geometries"
}, {
'index': lambda frame: frame[(frame.geometry.geom_type!="Polygon")], # NB currently points and lines are lost; in principle, these could be indexed
'message': "Dropping non-polygonal 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)
Expand Down

0 comments on commit ad746fe

Please sign in to comment.