Skip to content
Open
15 changes: 15 additions & 0 deletions test/io/test_geopandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,18 @@ def test_load_xarray_with_from_file(gridpath):
nc_filename = gridpath("scrip", "outCSne8", "outCSne8.nc")
uxgrid = ux.Grid.from_file(nc_filename, backend="xarray")
uxgrid.validate()


def test_read_failure_raises(tmp_path):
"""A read failure must surface its cause, not an UnboundLocalError.
Regression test for issue #1693."""
import pytest
Comment thread
Sevans711 marked this conversation as resolved.
Outdated

from uxarray.errors import GridInvalidError
from uxarray.io._geopandas import _gpd_read

not_geospatial = tmp_path / "not_geospatial.shp"
not_geospatial.write_text("this is not a shapefile")

with pytest.raises(GridInvalidError, match="Could not read"):
_gpd_read(str(not_geospatial))
8 changes: 6 additions & 2 deletions uxarray/io/_geopandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from uxarray.constants import INT_DTYPE, INT_FILL_VALUE, WGS84_CRS
from uxarray.conventions import ugrid
from uxarray.errors import GridInvalidError


def _read_geodataframe(filepath, driver=None, **kwargs):
Expand Down Expand Up @@ -68,9 +69,12 @@ def _gpd_read(filepath, driver=None, **kwargs):

try:
gdf = gpd.read_file(filepath, driver=driver, **kwargs)
gdf = _set_crs(gdf)
except Exception as e:
print(f"An error occurred while reading the geospatial data: {e}")
raise GridInvalidError(
f"Could not read geospatial data from {filepath!r}: {e}"
) from e

gdf = _set_crs(gdf)

max_polygon_nodes = gdf["geometry"].apply(_get_num_nodes).max()

Expand Down