Skip to content

Commit

Permalink
BUG: Fix vectorize when nodata is None
Browse files Browse the repository at this point in the history
  • Loading branch information
snowman2 committed May 22, 2023
1 parent e32d163 commit 2d35cf9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ History

Latest
-------
- BUG: Fix :func:`geocube.vector.vectorize` when nodata is None


0.4.0
-------
Expand Down
9 changes: 5 additions & 4 deletions geocube/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ def vectorize(data_array: xarray.DataArray) -> geopandas.GeoDataFrame:
"""
# nodata mask
mask = None
if numpy.isnan(data_array.rio.nodata):
mask = ~data_array.isnull()
elif data_array.rio.nodata is not None:
mask = data_array != data_array.rio.nodata
if data_array.rio.nodata is not None:
if numpy.isnan(data_array.rio.nodata):
mask = ~data_array.isnull()
else:
mask = data_array != data_array.rio.nodata

# vectorize generator
vectorized_data = (
Expand Down
18 changes: 18 additions & 0 deletions test/integration/test_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,21 @@ def test_vectorize(mask_and_scale):
else:
assert not (gdf.om_r == xds.om_r.rio.nodata).any()
assert len(gdf.index) == 7


def test_vectorize__missing_nodata():
xds = xarray.open_dataset(
TEST_COMPARE_DATA_DIR / "soil_grid_flat.nc",
decode_coords="all",
mask_and_scale=False,
)
xds.om_r.attrs.pop("_FillValue")
gdf = vectorize(xds.om_r)
assert isinstance(gdf, geopandas.GeoDataFrame)
assert gdf.dtypes.astype(str).to_dict() == {
"geometry": "geometry",
"om_r": "float64",
}
assert not gdf.geometry.isnull().any()
assert not gdf.om_r.isnull().any()
assert len(gdf.index) == 8

0 comments on commit 2d35cf9

Please sign in to comment.