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

zonal stats "mini raster" is not the same shape as input data #546

Open
Joshdpaul opened this issue Feb 21, 2025 · 0 comments
Open

zonal stats "mini raster" is not the same shape as input data #546

Joshdpaul opened this issue Feb 21, 2025 · 0 comments

Comments

@Joshdpaul
Copy link
Contributor

This is basically the same issue @kyleredilla found here: #399

I found this while attempting to resample the input dataset in the beetles endpoint, with the goal of improving zonal stats weighted average output. (Resampling was tried using a method similar to the one described here, where we can just apply a scaling factor to the lat/lon axis and not worry about the actual grid size).

In this example (http://localhost:5000//beetles/area/1908030801), I use a scaling factor of 3. So what is originally a 5x5 input dataset becomes a 15x15 input dataset.

No scaling factor, shapes match (this is a working return but returns nodata):

Data array of all zeroes derived from bbox
shape: (5, 5)
[[0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]] 

Mask array from zonal_stats (all_touched=False)
shape: (5, 5)
[[-- -- -- -- --]
 [-- -- -- 0.0 --]
 [-- -- 0.0 0.0 --]
 [-- 0.0 0.0 -- --]
 [-- -- -- -- --]] 

Mask array from zonal_stats (all_touched=True)
shape: (5, 5)
[[-- -- -- 0.0 0.0]
 [-- -- 0.0 0.0 0.0]
 [0.0 0.0 0.0 0.0 0.0]
 [0.0 0.0 0.0 0.0 0.0]
 [-- 0.0 -- -- --]] 

Bounds of original data array:
272320.95112357556 1466730.4403734806 320085.01195382874 1514494.5012037337 

Bounds of the geojson (all_touched=False):
274748.77465159545 1472485.4052244318 318266.31339097966 1509566.0564900415 

Bounds of the geojson (all_touched=True):
274748.77465159545 1472485.4052244318 318266.31339097966 1509566.0564900415 

Scaling factor of 3 applied. Notice how the shape of the raster mini map is now smaller than the input data array, regardless of all_touched method used:

Data array of all zeroes derived from bbox
shape: (15, 15)
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]] 

Mask array from zonal_stats (all_touched=False)
shape: (13, 12)
[[-- -- -- -- -- -- -- -- -- -- -- -- --]
 [-- -- -- -- -- -- -- -- -- -- -- 0.0 --]
 [-- -- -- -- -- -- -- -- -- -- 0.0 0.0 --]
 [-- -- -- -- -- -- -- -- 0.0 0.0 0.0 0.0 --]
 [-- -- -- -- -- 0.0 0.0 0.0 0.0 0.0 0.0 -- --]
 [-- -- -- -- -- -- 0.0 0.0 0.0 0.0 0.0 0.0 0.0]
 [-- -- -- -- -- -- 0.0 0.0 0.0 0.0 0.0 0.0 0.0]
 [-- -- -- -- -- 0.0 0.0 0.0 0.0 0.0 0.0 0.0 --]
 [-- 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -- -- -- 0.0 --]
 [0.0 0.0 0.0 0.0 0.0 0.0 0.0 -- -- -- -- -- --]
 [0.0 0.0 0.0 0.0 0.0 0.0 0.0 -- -- -- -- -- --]
 [-- 0.0 -- -- -- -- -- -- -- -- -- -- --]] 

Mask array from zonal_stats (all_touched=True)
shape: (13, 12)
[[-- -- -- -- -- -- -- -- -- -- -- 0.0 --]
 [-- -- -- -- -- -- -- -- -- -- 0.0 0.0 0.0]
 [-- -- -- -- -- -- -- -- -- 0.0 0.0 0.0 0.0]
 [-- -- -- -- -- -- 0.0 0.0 0.0 0.0 0.0 0.0 0.0]
 [-- -- -- -- -- 0.0 0.0 0.0 0.0 0.0 0.0 0.0 --]
 [-- -- -- -- -- 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0]
 [-- -- -- -- -- 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0]
 [-- 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0]
 [0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 --]
 [0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -- -- -- 0.0 --]
 [0.0 0.0 0.0 0.0 0.0 0.0 0.0 -- -- -- -- -- --]
 [0.0 0.0 0.0 0.0 0.0 0.0 -- -- -- -- -- -- --]] 

Bounds of original data array:
272320.95112357556 1466730.4403734806 320085.01195382874 1514494.5012037337 

Bounds of the geojson (all_touched=False):
274748.77465159545 1472485.4052244318 318266.31339097966 1509566.0564900415 

Bounds of the geojson (all_touched=True):
274748.77465159545 1472485.4052244318 318266.31339097966 1509566.0564900415 

Shape mismatch between rasterstats mask and input data array:
mask shape: (13, 12), input data shape: (15, 15)

We might need to figure out a way to use rasterio's rasterize function directly instead of using zonal stats to produce the rasterized polygon (zonal stats uses this under the hood anyways, but it appears to have issues)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant