all_touched=True unexpected behavior
#167
-
|
Hello all! I am using the In my application, I expect that if The testing suite does not have a good # Testing
gdf = gpd.read_file("./soil_data_flat.geojson")
gdf.reset_index(drop=False, inplace=True)
# Generate random array
# Get the bounds of the GeoDataFrame
bounds = gdf.total_bounds
minx, miny, maxx, maxy = bounds
# Generate a grid of longitude and latitude values
lon = np.linspace(minx, maxx, 100)
lat = np.linspace(miny, maxy, 100)
# Generate random data for the grid
data = np.random.rand(len(lat), len(lon))
# Create the DataArray
da = xr.DataArray(
data,
coords=[('lat', lat), ('lon', lon)],
name='random_data'
)
da.rio.set_spatial_dims('lon', 'lat', inplace=True)
da.rio.set_crs(gdf.crs, inplace=True)Now, with from geocube.api.core import make_geocube
from functools import partial
from geocube.rasterize import rasterize_image
from rasterio.enums import MergeAlg
template = make_geocube(
vector_data=gdf[["index", "geometry"]],
measurements=["index"],
like=da,
rasterize_function=partial(rasterize_image, all_touched=False),
)
np.unique(template.index.values).shape
> (8, )and with template = make_geocube(
vector_data=gdf[["index", "geometry"]],
measurements=["index"],
like=da,
rasterize_function=partial(rasterize_image, all_touched=True),
)
np.unique(template.index.values).shape
> (7, )Is this behavior expected? I am misusing the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
If you look at the values: {
"original": numpy.unique(original_raster.index).tolist(),
"all_touched": numpy.unique(all_touched_raster.index).tolist(),
}
I created a raster diff layer on top of the origin input polygons to illustrate the differences between the two and it appears to work as expected. Difference mask: different_mask = all_touched_raster.index != original_raster.indexRaster original_raster.index.where(different_mask)In this scenario, the differing cells use the values within the shapes they touch and don't extend past the boundaries. Raster all_touched_raster.index.where(different_mask)In this scenario, the values in the cells often are pulled from the shapes next to the cells as well as values on the boundaries of the raster. |
Beta Was this translation helpful? Give feedback.


If you look at the values:
{ "original": numpy.unique(original_raster.index).tolist(), "all_touched": numpy.unique(all_touched_raster.index).tolist(), }all_touched=Trueappears to fill in thenanvalues at the edges:I created a raster diff layer on top of the origin input polygons to illustrate the differences between the two and it appears to work as expected.
Difference mask:
Raster
all_touched=Falsewhere it is different fromall_touched=Trueoverlayed on the original shapefile: