From 89f2387c84e901e8ea0b8189d5fdf64e57ced4c5 Mon Sep 17 00:00:00 2001 From: snowman2 Date: Tue, 16 Apr 2024 15:07:25 -0500 Subject: [PATCH] BUG: Recalc transform when vectorizing --- docs/history.rst | 2 +- geocube/vector.py | 2 +- test/integration/test_vector.py | 9 ++++++++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/docs/history.rst b/docs/history.rst index 2b32e4e..b0c19df 100644 --- a/docs/history.rst +++ b/docs/history.rst @@ -3,7 +3,7 @@ History Latest ------ - +- BUG: Recalc transform when vectorizing (issue #165) 0.5.1 ------ diff --git a/geocube/vector.py b/geocube/vector.py index e873c0c..26ff29d 100644 --- a/geocube/vector.py +++ b/geocube/vector.py @@ -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, ) ) diff --git a/test/integration/test_vector.py b/test/integration/test_vector.py index 111a812..0bf203a 100644 --- a/test/integration/test_vector.py +++ b/test/integration/test_vector.py @@ -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",