How can I choose to not change the image size when reprojecting with Python rioxarray?

51 views Asked by At

I have an image in tif-format of size 18346x10218 in coordinate system EPSG:4326 that I want to convert to a new coordinate system, which I do with the following method with rioxarray in Python.

import rioxarray

rast_path = "old_projection.tif"

rds = rioxarray.open_rasterio(rast_path)
rds_3996 = rds.rio.reproject("EPSG:3996")
rds_3996.rio.to_raster("new_projection.tif")

This saves the tif file in the new projection, but I noticed that it increases the image size to 20198x20198. When I open the file, the new pixels are not visible, so I assume they do not contain any data, but the size of the file has more than doubled. Is there a way to re-project data in this manner without saving the empty pixels? Thanks for any help!

1

There are 1 answers

2
softghost On

Try like this:

import rioxarray

rast_path = "old_projection.tif"
rds = rioxarray.open_rasterio(rast_path)
# Specify the destination CRS (EPSG:3996) and use 'nearest' resampling
rds_3996 = rds.rio.reproject("EPSG:3996", resampling='nearest')
# Save the reprojected raster
rds_3996.rio.to_raster("new_projection.tif")