From 256b488ab4f2954a7da41865c842f9d6ca6a5e94 Mon Sep 17 00:00:00 2001 From: Xiaokang Fu <1072534112@qq.com> Date: Thu, 19 Dec 2024 18:07:17 -0500 Subject: [PATCH] Support multiple polygons --- knime_extension/src/nodes/spatialtool.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/knime_extension/src/nodes/spatialtool.py b/knime_extension/src/nodes/spatialtool.py index 92a6c27..817dbbb 100644 --- a/knime_extension/src/nodes/spatialtool.py +++ b/knime_extension/src/nodes/spatialtool.py @@ -1266,7 +1266,8 @@ def configure(self, configure_context, input_schema): def execute(self, exec_context: knext.ExecutionContext, input_table): import h3 - from shapely.geometry import Polygon + from shapely.geometry import Polygon, mapping + from shapely.ops import unary_union import geopandas as gpd import pandas as pd @@ -1274,17 +1275,23 @@ def execute(self, exec_context: knext.ExecutionContext, input_table): knut.check_canceled(exec_context) exec_context.set_progress(0.1, "Projecting input polygon...") - gdf_boundary.to_crs(4326, inplace=True) + USE_CRS = 4326 + gdf_boundary.to_crs(USE_CRS, inplace=True) knut.check_canceled(exec_context) exec_context.set_progress(0.3, "Combining input polygon...") - gdf_boundary["geometry"] = gdf_boundary.unary_union + gdf_boundary = unary_union(gdf_boundary[self.geo_col]) + gdf_boundary = gpd.GeoDataFrame(geometry=[gdf_boundary], crs=USE_CRS) + + knut.check_canceled(exec_context) + exec_context.set_progress(0.4, "Exploding input polygon...") + gdf_boundary = gdf_boundary.explode(index_parts=True) + gdf_boundary.reset_index(drop=True, inplace=True) knut.check_canceled(exec_context) exec_context.set_progress(0.5, "Computing H3 hexagons...") - h3_hexes = h3.polyfill_geojson( - gdf_boundary.geometry.__geo_interface__["features"][0]["geometry"], - self.zoom, + h3_hexes = set().union( + *[h3.polyfill_geojson(mapping(p), self.zoom) for p in gdf_boundary.geometry] ) knut.check_canceled(exec_context) @@ -1295,7 +1302,7 @@ def execute(self, exec_context: knext.ExecutionContext, input_table): Polygon(h3.h3_to_geo_boundary(h3_hex, geo_json=True)) for h3_hex in h3_hexes ], - crs=gdf_boundary.crs, + crs=USE_CRS, ) grid.rename_geometry(self._COL_GEOMETRY, True)