Re: can resampling not populate cell if source has nodata?
nick.forfinski@...
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.
|
|
Re: can resampling not populate cell if source has nodata?
Alan Snow
Here is an example using rioxarray: https://corteva.github.io/rioxarray/stable/examples/interpolate_na.html
It uses scipy griddata.
|
|
Re: can resampling not populate cell if source has nodata?
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:
--
Sean Gillies
|
|
Re: can resampling not populate cell if source has nodata?
nick.forfinski@...
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
|
|
Re: can resampling not populate cell if source has nodata?
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. 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.
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 ) --
Sean Gillies
|
|
Re: Get bounds after rasterio merge
Luke
rasterio.transform.array_bounds should do the trick - https://rasterio.readthedocs.io/en/latest/api/rasterio.transform.html#rasterio.transform.array_bounds
On Wed, 27 May 2020 at 04:18, <ashnair0007@...> wrote: I merged two tiffs via the rasterio.merge function which returned the merged numpy array and the geo transform. Is there a way to get the bounds (in latitude and longitude) of this new merged image?
|
|
Re: Window from bounds is shifted by 1 Pixel
thomaswanderer@...
I am not 100 sure, but I now simply rounded all windows (origin + offsets) and this seems to have fixed it.
|
|
Re: A question about the rasterio precision & python floats (Pixel width & height)
thomaswanderer@...
Thank you for the info. I saw shorter values tan in QGIS and those came from Python's float limitations. When using the Decimal class I also get the same over-precise values.
|
|
Get bounds after rasterio merge
ashnair0007@...
I merged two tiffs via the rasterio.merge function which returned the merged numpy array and the geo transform. Is there a way to get the bounds (in latitude and longitude) of this new merged image?
|
|
can resampling not populate cell if source has nodata?
nick.forfinski@...
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
|
|
Re: A question about the rasterio precision & python floats (Pixel width & height)
Sean Gillies
Hi, On Mon, May 25, 2020 at 3:30 AM <thomaswanderer@...> wrote:
QGIS and rasterio both use GDAL to read raster data and all of these use double precision floats to store values of the georeferencing matrix. I believe you're seeing different string representations of the same numbers. Since the hex representations of the two numbers are the same, I suspect the values shown by QGIS are overly precise and that the last digits are meaningless. >>> 0.0008333333299974727193.hex() '0x1.b4e81b312a043p-11' >>> 0.0008333333299974727.hex() '0x1.b4e81b312a043p-11' Sean Gillies
|
|
Re: Create raster aligned with one created by gdal_translate
thomaswanderer@...
I also still fight with the shifted by one pixel problem for 1 extent out of 5 extents. 4 return perfectly aligned clipped subsets of the source raster, 1 does not.
|
|
Re: Create raster aligned with one created by gdal_translate
Denis Rykov
Thanks Thomas. I've tried your approach but it still gives me a shifted output compared to gdal_translate.
|
|
A question about the rasterio precision & python floats (Pixel width & height)
thomaswanderer@...
When opening my raster with QGIS, I can see the Pixel Size of my raster as:
When I read he same raster with rasterio, I get pixel size values with a lower precision and I guess this causes some problems when calculating new affines for another extent
I have tries to pass the decimal_precision=X when opening my raster with rasterio, but this has no effect. I guess this is a general float problem on my system. My suggestion therefore would be to use python Decimals or any other type representing real floats.
|
|
Re: Create raster aligned with one created by gdal_translate
I had a very similar approach to calculate the new affine & width/height manually as you. But I have now replaced it with just rasterio functions and when comparing the results of the new affine, they were identical :-)
I just wonder, why you add 0.5 when calculating the height & width? My code looked like this (windows is the extent I am matching my raster to, profile is the target raster profile, copied from my source raster and then modified)
But i have replaced all this now by:
Both methods actually created and affine with the same values, that is why I replaced my own code with the rasterio functions.
|
|
Re: Window from bounds is shifted by 1 Pixel
Good to know I am not the only one.
I guess it could be rounding problems, but the extent I use for creating the window aligns perfectly with the input rater (as seen above). But the output window get's shifted and only if my extent overlaps on the western side of the original source raster. All other windows (created from the intersection extents) are just fine for both input and output! Very strange. Ah by the way, I use rasterio 1.13.
|
|
Re: Window from bounds is shifted by 1 Pixel
Denis Rykov
Hi! I had faced the similar shifting issue yesterday: https://github.com/mapbox/rasterio/issues/1932
|
|
Window from bounds is shifted by 1 Pixel
thomaswanderer@...
Hi. this is my first post. I am new to raster.io and really like it so far. Thumbs up for all the work :-)
But I have already a first problem: I created a function which creates a new raster for a given geographical extent and copies the values of the source raster into it. The new raster has the same characteristics as the source raster. The geographical extents can now
For all of these cases, I create a new affine for the new raster and then fill it with the values of the source raster (or any other optionally specified value). I still have a problem when copying over the values from the source to the target raster. In one case the two rasters (source -> copy) are not aligned but shifted by 1 pixel. (See yellow extent in above images - All other extents create perfectly aligned new rasters) My strategy for copying data from the source to the target is the following:
My guess is that the write_window = target.window(*intersection.bounds)) is the part where the error happens. While the read_window still reads the correct data, the write window places the copied data shifted by 1 pixel. I use the same intersection extent for the creation of both windows, and the intersection is also pixel aligned as visible in the above image. So I wonder what I do wrong. Is this maybe a rounding or precision error?
|
|
Create raster aligned with one created by gdal_translate
Denis Rykov
Hi everyone! I tried to convert
Eventually I came up with the following solution that produces raster aligned with one produced by
It works fine but I still have couple of questions:
Any advice would be appreciated.
|
|
Re: Create sparse raster with Rasterio. Is this possible?
Brendan Ward
Ben - I'm not sure I follow your intent here.
Is the idea to make the smallest possible geotiff after rasterizing all shapes in test.shp to pixels, where SPARSE_OK omits non-rasterized pixels where possible from the geotiff? In rasterio, the best way to approach that would be to first do a windowed read of your data from raster that overlap the bounding box of all features in test.shp (see geometry_window() https://github.com/mapbox/rasterio/blob/master/rasterio/features.py#L387), then rasterize within that smaller window, then write it out to a new geotiff (note: it will have a different geotransform / width / height than the original). If your shapes in test.shape are scattered around the raster with large areas of nodata in between, there's not really a way to handle that case aside from windowing out and writing each rasterized shape to a separate geotiff, then maybe making a VRT of the individual geotiffs it in GDAL to mosaic them back together.
|
|