Topics

cannot correctly change a TIFF's interleave


guohoucai@...
 

I'm looking for some help about how to change a TIFF's interleave. My code could change a TIFF's nodata value but cannot succeed on the interleave, could anyone give me some advice? I'll appreciate your kindness.


Sean Gillies
 

Hello,

On Thu, Mar 4, 2021 at 7:24 PM <guohoucai@...> wrote:
I'm looking for some help about how to change a TIFF's interleave. My code could change a TIFF's nodata value but cannot succeed on the interleave, could anyone give me some advice? I'll appreciate your kindness.
_._,_._,_

The nodata value is nothing but an annotation of the data and is simple to change. To change the interleaving of a GeoTIFF, it is required to rewrite the file entirely. https://rasterio.readthedocs.io/en/latest/api/rasterio.shutil.html?highlight=copy#rasterio.shutil.copy is the function to use for this purpose. I'm sorry that the documentation of the shutil module is incomplete, we're working on it.

I recommend this usage:

with rasterio.open(test_img) as src:
    prof = src.profile
    prof.update(interleave="pixel", nodata=0)
    rasterio.shutil.copy(src, save_path, driver="GTiff", **prof)

--
Sean Gillies


guohoucai@...
 

Thanks for your help! It really helps me a lot! But I got another question: rasterio.shutil.copy() could only copy a raster to a new destination, what should I do if I wanna use a raster's profile for the creation of a new raster? Like I use a multi-bands TIFF file to calculate the NDVI and then save it, the multi-bands TIFF's profile is useful for the NDVI TIFF file cause they have the same "crs" and "transform". I'm a rasterio rookie and I appreciate your generosity a lot!


Sean Gillies
 

The "profile" attribute of a dataset is a dictionary that describes key characteristics of the dataset. You can use this dictionary in any way you like. Modifying it does not affect the dataset that it originated from. Common rasterio usage is to get the profile of one dataset and then use that as a baseline for creation of a new dataset. Here's an example: https://github.com/mapbox/rasterio/blob/master/rasterio/rio/convert.py#L62-L77.

Does this make sense? I hope it helps.


On Fri, Mar 5, 2021 at 6:46 PM <guohoucai@...> wrote:
Thanks for your help! It really helps me a lot! But I got another question: rasterio.shutil.copy() could only copy a raster to a new destination, what should I do if I wanna use a raster's profile for the creation of a new raster? Like I use a multi-bands TIFF file to calculate the NDVI and then save it, the multi-bands TIFF's profile is useful for the NDVI TIFF file cause they have the same "crs" and "transform". I'm a rasterio rookie and I appreciate your generosity a lot!


--
Sean Gillies


guohoucai@...
 

Thanks! It really helps me a lot. Hope you have a nice day :)