Skip to content

Commit

Permalink
Support multiple polygons
Browse files Browse the repository at this point in the history
  • Loading branch information
wybert authored and koettert committed Jan 7, 2025
1 parent 8b06dd7 commit 79ec430
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions knime_extension/src/nodes/spatialtool.py
Original file line number Diff line number Diff line change
Expand Up @@ -1266,25 +1266,32 @@ 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

gdf_boundary = knut.load_geo_data_frame(input_table, self.geo_col, exec_context)

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)
Expand All @@ -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)

Expand Down

0 comments on commit 79ec430

Please sign in to comment.