Skip to content

Commit

Permalink
Add default value in vectorize when dataarray has no name, and a warn…
Browse files Browse the repository at this point in the history
…ing (#166)
  • Loading branch information
Imanflow authored Mar 18, 2024
1 parent ec59b49 commit 5fa79a4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
11 changes: 10 additions & 1 deletion geocube/vector.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""
Module for vector methods
"""
import warnings

import geopandas
import numpy
import rasterio.features
Expand Down Expand Up @@ -53,8 +55,15 @@ def vectorize(data_array: xarray.DataArray) -> geopandas.GeoDataFrame:
mask=mask,
)
)

if data_array.name:
name = data_array.name
else:
warnings.warn("The array has no name. Column name defaults to _data")
name = "_data"

return geopandas.GeoDataFrame(
vectorized_data,
columns=[data_array.name, "geometry"],
columns=[name, "geometry"],
crs=data_array.rio.crs,
)
15 changes: 15 additions & 0 deletions test/integration/test_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,18 @@ def test_vectorize__missing_nodata():
assert not gdf.geometry.isnull().any()
assert not gdf.om_r.isnull().any()
assert len(gdf.index) == 8


def test_vectorize__no_name():
xds = xarray.open_dataset(
TEST_COMPARE_DATA_DIR / "soil_grid_flat.nc",
decode_coords="all",
mask_and_scale=False,
)

test_array = xarray.DataArray(xds.om_r.data)

with pytest.warns(UserWarning):
gdf = vectorize(test_array)

assert "_data" in gdf.columns

0 comments on commit 5fa79a4

Please sign in to comment.