Topics

can resampling not populate cell if source has nodata?


Nick Forfinski-Sarkozi
 

Hi rasterio.  New user here.  Thanks for a fantastic module.  During resampling, is there a way to force the cells of the resampled raster to nodata if any of the corresponding original cells are nodata?   (If by chance anyone is familiar with it, I'm looking for functionality analogous to arcpy's Aggregate function with ignore_data=True https://pro.arcgis.com/en/pro-app/tool-reference/spatial-analyst/aggregate.htm )

Thanks,
Nick


Sean Gillies
 

Hi Nick,

Ignoring nodata values is the default behavior for rasterio, as it is for the GDAL library that rasterio uses. Here's an example using one of rasterio's test files.

$ rio insp tests/data/RGB.byte.tif
Rasterio 1.1.5dev Interactive Inspector (Python 3.6.6)
Type "src.meta", "src.read(1)", or "help(src)" for more information.
>>> show(src.read())

That command uses matplotlib to display the raw data and I've zoomed in to the bottom to show the hard edge along the image collar. Valid data on the inside, nodata on the outside.

Figure_1.png
Now, I'll dilate the image by a factor of 3 on reading and use bilinear resampling to fill in the extra pixels.

>>> from rasterio.enums import Resampling
>>> resampled = src.read(out_shape=(3, src.height * 3, src.width * 3), resampling=Resampling.bilinear)
>>> show(resampled)

Zooming in again to the collar, you can see that the valid data has been smoothed, but the hard nodata edge remains. Nodata values were ignored by the resampling system.
Figure_2.png

On Tue, May 26, 2020 at 11:30 AM <nick.forfinski@...> wrote:
Hi rasterio.  New user here.  Thanks for a fantastic module.  During resampling, is there a way to force the cells of the resampled raster to nodata if any of the corresponding original cells are nodata?   (If by chance anyone is familiar with it, I'm looking for functionality analogous to arcpy's Aggregate function with ignore_data=True https://pro.arcgis.com/en/pro-app/tool-reference/spatial-analyst/aggregate.htm )

Thanks,
Nick



--
Sean Gillies


Nick Forfinski-Sarkozi
 

Hi Sean.  Thanks for the message.  I understand your example, but I may have been confusing in my email.  I think I'm asking something different.  Is there a way to force the resampling process to not populate a resampled value where there are corresponding nodata values in the original raster?  For example, in the image below, the lighter 5-m purple raster is the resampled version of the darker 1-m blue raster.  Many of the resampled raster's pixels correspond to gaps in the original raster.  How could I resample so that all 25 source cells are required to produce a resampled value?  
 
Thanks again for your help and a great module,
Nick
 


Sean Gillies
 

I see what you mean. Rasterio doesn't have a built in option for this, unless there is a GDAL resampling trick that I am not aware of.

On Wed, May 27, 2020 at 9:55 AM <nick.forfinski@...> wrote:
Hi Sean.  Thanks for the message.  I understand your example, but I may have been confusing in my email.  I think I'm asking something different.  Is there a way to force the resampling process to not populate a resampled value where there are corresponding nodata values in the original raster?  For example, in the image below, the lighter 5-m purple raster is the resampled version of the darker 1-m blue raster.  Many of the resampled raster's pixels correspond to gaps in the original raster.  How could I resample so that all 25 source cells are required to produce a resampled value?  
 
Thanks again for your help and a great module,
Nick
 



--
Sean Gillies


Alan Snow
 

Here is an example using rioxarray: https://corteva.github.io/rioxarray/stable/examples/interpolate_na.html

It uses scipy griddata.


Nick Forfinski-Sarkozi
 

FYI...I was able to achieve the desired behavior by "lying" about the nodata value.  The actual nodata was nan, but when I specified something else (e.g., nodata=-9999) in the profile, the resampled raster didn't contain values for cells corresponding to source nans.


On Wed, May 27, 2020 at 4:20 PM Sean Gillies via groups.io <sean=mapbox.com@groups.io> wrote:
I see what you mean. Rasterio doesn't have a built in option for this, unless there is a GDAL resampling trick that I am not aware of.

On Wed, May 27, 2020 at 9:55 AM <nick.forfinski@...> wrote:
Hi Sean.  Thanks for the message.  I understand your example, but I may have been confusing in my email.  I think I'm asking something different.  Is there a way to force the resampling process to not populate a resampled value where there are corresponding nodata values in the original raster?  For example, in the image below, the lighter 5-m purple raster is the resampled version of the darker 1-m blue raster.  Many of the resampled raster's pixels correspond to gaps in the original raster.  How could I resample so that all 25 source cells are required to produce a resampled value?  
 
Thanks again for your help and a great module,
Nick
 



--
Sean Gillies