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

BUG: Recalc transform when vectorizing #171

Merged
merged 1 commit into from
Apr 17, 2024
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
2 changes: 1 addition & 1 deletion docs/history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ History

Latest
------

- BUG: Recalc transform when vectorizing (issue #165)

0.5.1
------
Expand Down
2 changes: 1 addition & 1 deletion geocube/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def vectorize(data_array: xarray.DataArray) -> geopandas.GeoDataFrame:
(value, shapely.geometry.shape(polygon))
for polygon, value in rasterio.features.shapes(
data_array,
transform=data_array.rio.transform(),
transform=data_array.rio.transform(recalc=True),
mask=mask,
)
)
Expand Down
9 changes: 8 additions & 1 deletion test/integration/test_vector.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import geopandas
import pytest
import xarray
from numpy.testing import assert_almost_equal

from geocube.vector import vectorize
from test.conftest import TEST_COMPARE_DATA_DIR


@pytest.mark.parametrize("invert_y", [True, False])
@pytest.mark.parametrize("mask_and_scale", [True, False])
def test_vectorize(mask_and_scale):
def test_vectorize(mask_and_scale, invert_y):
xds = xarray.open_dataset(
TEST_COMPARE_DATA_DIR / "soil_grid_flat.nc",
decode_coords="all",
mask_and_scale=mask_and_scale,
)
if invert_y:
# https://github.com/corteva/geocube/issues/165
xds.rio.write_transform(xds.rio.transform(), inplace=True)
xds = xds.sortby("y")
gdf = vectorize(xds.om_r)
assert isinstance(gdf, geopandas.GeoDataFrame)
assert_almost_equal(gdf.total_bounds, [700330, 4595210, 701750, 4597070])
assert gdf.dtypes.astype(str).to_dict() == {
"geometry": "geometry",
"om_r": "float64",
Expand Down
Loading