Deltares / xugrid

Xarray and unstructured grids
https://deltares.github.io/xugrid/
MIT License
64 stars 8 forks source link

snap_to_grid throws index error _find_largest_cells #122

Closed JoerivanEngelen closed 1 year ago

JoerivanEngelen commented 1 year ago

Issue:

In certain cases an IndexError is thrown when calling xu.snap_to_grid

To reproduce:

hfb_debug2.zip

# %% Imports
from pathlib import Path
import geopandas as gpd
import xugrid as xu

# %% Path

root = Path(r".")
hfb_path = root / r"hfb_debug2\hfb.shp"
target_path = root / r"hfb_debug2\target.nc"

# %% Open

lines = gpd.read_file(hfb_path)
uda_target = xu.open_dataarray(target_path)

# %% Snap

target = uda_target.grid
snapped = xu.snap_to_grid(lines, target, max_snap_distance=0.5)

This will throw: IndexError: index 720 is out of bounds for axis 0 with size 105

Debugging shows this happens in:

def _find_largest_edges(
    intersection_edges: FloatArray,
    segment_index: IntArray,
    edges: IntArray,
    line_index: IntArray,
):
    valid_edges = intersection_edges[segment_index]
    ...

Where segment_index contains indices ranging from 720 to 742, whereas intersection_edges only has a shape (105,).