Skip to content

Commit 0fef249

Browse files
Merge pull request #33 from manaakiwhenua/index-points
Supports point geometries
2 parents 22b98d8 + 404a546 commit 0fef249

File tree

5 files changed

+28
-19
lines changed

5 files changed

+28
-19
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ This is the vector equivalent of [raster2dggs](https://github.com/manaakiwhenua/
88

99
Currently this tool supports the following DGGSs:
1010

11-
- H3 (polygons, linestrings)
12-
- rHEALPix (polygons)
11+
- H3
12+
- rHEALPix (points, polygons)
1313

1414
Contributions (espeically for other DGGSs), suggestions, bug reports and strongly worded letters are all welcome.
1515

vector2dggs/common.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ def drop_condition(
104104
_diff = _before - _after
105105
if _diff:
106106
log_method = (
107-
LOGGER.info if (_diff / float(_before)) < warning_threshold else LOGGER.warn
107+
LOGGER.info
108+
if (_diff / float(_before)) < warning_threshold
109+
else LOGGER.warning
108110
)
109111
log_method(f"Dropped {_diff} rows ({_diff/float(_before)*100:.2f}%)")
110112
return df
@@ -179,25 +181,23 @@ def polyfill(
179181
) -> None:
180182
"""
181183
Reads a geoparquet, performs polyfilling (for Polygon),
182-
linetracing (for LineString), and writes out to parquet.
184+
linetracing (for LineString), or indexing (for Point),
185+
and writes out to parquet.
183186
"""
184187
df = gpd.read_parquet(pq_in).reset_index().drop(columns=[spatial_sort_col])
185188
if len(df.index) == 0:
186-
# Input is empty, nothing to polyfill
189+
# Input is empty, nothing to convert
187190
return None
188191

189-
# DGGS specific polyfill
192+
# DGGS specific conversion
190193
df = dggsfunc(df, resolution)
191194

192195
if len(df.index) == 0:
193-
# Polyfill resulted in empty output (e.g. large cell, small feature)
196+
# Conversion resulted in empty output (e.g. large cell, small feature)
194197
return None
195198

196199
df.index.rename(f"{dggs}_{resolution:02}", inplace=True)
197200
parent_res: int = get_parent_res(dggs, parent_res, resolution)
198-
# print(parent_res)
199-
# print(df.index)
200-
# print(df.columns)
201201

202202
# Secondary (parent) index, used later for partitioning
203203
df = secondary_index_func(df, parent_res)
@@ -233,7 +233,7 @@ def index(
233233
overwrite: bool = False,
234234
) -> Path:
235235
"""
236-
Performs multi-threaded polyfilling on (multi)polygons.
236+
Performs multi-threaded DGGS indexing on geometries (including multipart and collections).
237237
"""
238238

239239
if table and con:
@@ -291,7 +291,8 @@ def index(
291291
"index": lambda frame: frame[
292292
(frame.geometry.geom_type != "Polygon")
293293
& (frame.geometry.geom_type != "LineString")
294-
], # NB currently points and other types are lost; in principle, these could be indexed
294+
& (frame.geometry.geom_type != "Point")
295+
],
295296
"message": "Considering unsupported geometries",
296297
},
297298
]
@@ -314,9 +315,9 @@ def index(
314315

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

317-
# Multithreaded polyfilling
318+
# Multithreaded DGGS indexing
318319
LOGGER.debug(
319-
"Indexing on spatial partitions by polyfill with resolution: %d",
320+
"DGGS indexing by spatial partitions with resolution: %d",
320321
resolution,
321322
)
322323
with tempfile.TemporaryDirectory(suffix=".parquet") as tmpdir2:
@@ -344,7 +345,7 @@ def index(
344345

345346
parent_partitioning(
346347
dggs,
347-
tmpdir2,
348+
Path(tmpdir2),
348349
output_directory,
349350
resolution,
350351
parent_res,

vector2dggs/h3.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def h3_secondary_index(df: gpd.GeoDataFrame, parent_res: int) -> gpd.GeoDataFram
2424

2525
def h3polyfill(df: gpd.GeoDataFrame, resolution: int):
2626
df_polygon = df[df.geom_type == "Polygon"]
27-
if len(df_polygon.index) > 0:
27+
if not df_polygon.empty:
2828
df_polygon = df_polygon.h3.polyfill_resample(
2929
resolution, return_geometry=False
3030
).drop(columns=["index"])
@@ -38,10 +38,14 @@ def h3polyfill(df: gpd.GeoDataFrame, resolution: int):
3838
)
3939
df_linestring = df_linestring[~df_linestring.index.duplicated(keep="first")]
4040

41+
df_point = df[df.geom_type == "Point"]
42+
if len(df_point.index) > 0:
43+
df_point = df_point.h3.geo_to_h3(resolution, set_index=True)
44+
4145
return pd.concat(
4246
map(
4347
lambda _df: pd.DataFrame(_df.drop(columns=[_df.geometry.name])),
44-
[df_polygon, df_linestring],
48+
[df_polygon, df_linestring, df_point],
4549
)
4650
)
4751

vector2dggs/katana.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def katana(geometry, threshold, count=0) -> GeometryCollection:
3333
if not geometry.is_valid:
3434
# print(explain_validity(geometry))
3535
geometry = make_valid(geometry)
36-
if geometry.type == "GeometryCollection":
36+
if geometry.geom_type == "GeometryCollection":
3737
geometry.normalize()
3838
geometry = geometry.buffer(0)
3939
bounds = geometry.bounds

vector2dggs/rHP.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,14 @@ def rhppolyfill(df: gpd.GeoDataFrame, resolution: int):
4444
# )
4545
# df_linestring = df_linestring[~df_linestring.index.duplicated(keep="first")]
4646

47+
df_point = df[df.geom_type == "Point"]
48+
if len(df_point.index) > 0:
49+
df_point = df_point.rhp.geo_to_rhp(resolution, set_index=True)
50+
4751
return pd.concat(
4852
map(
4953
lambda _df: pd.DataFrame(_df.drop(columns=[_df.geometry.name])),
50-
[df_polygon, df_multipolygon], # df_linestring],
54+
[df_polygon, df_multipolygon, df_point], # df_linestring],
5155
)
5256
)
5357

0 commit comments

Comments
 (0)