Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Create Grid Node #385

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions knime_extension/src/nodes/spatialtool.py
Original file line number Diff line number Diff line change
Expand Up @@ -1145,12 +1145,16 @@ class CreateGrid:

geo_col = knut.geo_col_parameter()

grid_length = knext.IntParameter(
"Grid Length",
"The length in meters of the grid. ",
default_value=100,
grid_length = kproj.Distance.get_distance_parameter(
label="Grid Length",
description="The length in meters of the grid. ",
default_value=100.0,
)

unit = kproj.Distance.get_unit_parameter(since_version="1.4.0")

keep_input_crs = kproj.Distance.get_keep_input_crs_parameter(since_version="1.4.0")

_COL_ID = "Grid ID"
_COL_GEOMETRY = "geometry"

Expand All @@ -1165,13 +1169,20 @@ def configure(self, configure_context, input_schema):
def execute(self, exec_context: knext.ExecutionContext, input_table):
gdf = knut.load_geo_data_frame(input_table, self.geo_col, exec_context)

xmin, ymin, xmax, ymax = gdf.total_bounds
width = self.grid_length
height = self.grid_length
helper = kproj.Distance(self.unit, self.keep_input_crs)
projected_gdf = helper.pre_processing(exec_context, gdf, False)
new_grid_length = helper.convert_input_distance(self.grid_length)
knut.check_canceled(exec_context)
xmin, ymin, xmax, ymax = projected_gdf.total_bounds
width = new_grid_length
height = new_grid_length
import numpy as np

rows = int(np.ceil((ymax - ymin) / height))
cols = int(np.ceil((xmax - xmin) / width))

exec_context.set_progress(0.1, f"Creating grid ({rows}x{cols})...")

XleftOrigin = xmin
XrightOrigin = xmin + width
YtopOrigin = ymax
Expand All @@ -1180,9 +1191,15 @@ def execute(self, exec_context: knext.ExecutionContext, input_table):

from shapely.geometry import Polygon # For Grid

# init j to use it in progress computation
j = 0
for i in range(cols):
Ytop = YtopOrigin
Ybottom = YbottomOrigin
knut.check_canceled(exec_context)
exec_context.set_progress(
(i * j) / (rows * cols), f"Creating grid of size {rows}x{cols}..."
)
for j in range(rows):
polygons.append(
Polygon(
Expand All @@ -1199,8 +1216,10 @@ def execute(self, exec_context: knext.ExecutionContext, input_table):
XleftOrigin = XleftOrigin + width
XrightOrigin = XrightOrigin + width

grid = gp.GeoDataFrame({self._COL_GEOMETRY: polygons}, crs=gdf.crs)
grid = gp.GeoDataFrame({self._COL_GEOMETRY: polygons}, crs=projected_gdf.crs)
grid[self._COL_ID] = list(range(1, grid.shape[0] + 1))
grid = helper.post_processing(exec_context, grid)

return knut.to_table(grid, exec_context)


Expand Down
2 changes: 2 additions & 0 deletions knime_extension/src/util/projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def get_unit_parameter(
label: str = "Distance unit",
description: str = "Choose the distance unit to use.",
default_value: str = "INPUT",
since_version="1.1.0",
):
"Distance unit parameter. Usually named 'unit'"
return knext.EnumParameter(
Expand All @@ -124,6 +125,7 @@ def get_keep_input_crs_parameter(
description: str = "If checked the CRS of the input table is retained even if a re-projection was necessary "
+ "for the selected distance unit.",
default_value: bool = False,
since_version="1.1.0",
):
"Boolean parameter that indicates if the input CRS should be retained. Usually named 'keep_input_crs'."
return knext.BoolParameter(
Expand Down