I used rasterio
to scale a raster I have to a new resolution. It was at (5.0, 5.0) resolution, and I wanted to make it (15.0, 15.0).
However, the resulting resolution is not square anymore.
Here's my code (it's pretty standard, pretty much just from the rasterio documentation):
with rio.open(f_path) as ds:
orig_res = ds.res[0] # 5.0
des_res = 15.0
# Calculate scale factor for this specific input layer
upscale_factor = orig_res / des_res
print("upscale factor: ", upscale_factor)
# Scale while reading in array
scaled_arr = ds.read(
out_shape=(
ds.count,
int(ds.height * upscale_factor),
int(ds.width * upscale_factor)
),
resampling=Resampling.bilinear
)
# Also have to update the transform to be scaled
scaled_transform = ds.transform * ds.transform.scale(
(ds.height / scaled_arr.shape[1]),
(ds.width / scaled_arr.shape[2])
)
scaled_profile = ds.profile
scaled_profile.update(transform=scaled_transform, height=scaled_arr.shape[1], width=scaled_arr.shape[2])
with rio.open(f_out_path, 'w', **scaled_profile) as scaled_ds:
scaled_ds.write(scaled_arr)
print(f"Orig arr shape: ({ds.count}, {ds.shape[0]}, {ds.shape[1]})")
print(f"Scaled ds shape: ({scaled_ds.count}, {scaled_ds.shape[0]}, {scaled_ds.shape[1]})")
print("Orig ds res: ", ds.res)
print("Scaled ds res: ", scaled_ds.res)
Here is the output printed:
upscale factor: 0.3333333333333333
Orig arr shape: (1, 9240, 2671)
Scaled ds shape: (1, 3080, 890)
Orig ds res: (5.0, 5.0)
Scaled ds res: (15.0, 15.00561797752809)
So is it bad that my new raster does not have square pixels anymore? (15.0, 15.00561797752809)
Will this affect anything with how I use the raster, or will it be okay? I also have other rasters that I'm up/down sampling to all be (15.0, 15.0), and they are also not exactly 15.0, so will it cause issues if my rasters have very slightly different resolutions?
Or is there a way to remedy this?