Resampling a raster using rasterio – simple modification of grid spacing
Question:
I am resampling raster data using Python’s rasterio. Looking at the rasterio.enums.Resampling
class, it appears the only way to do this is to interpolate between adjacent raster grids, essentially smoothing the data.
Is there some way to do a simple upsampling that effectively divides one raster grid into many and preserves the original value for all of the sub-grids?
My resampling script is as follows – currently using the bi-linear method:
with rasterio.open(str(rasterpath+filename), crs="EPSG:4326") as src:
data = src.read(
out_shape=(
src.count,
int(src.height * upscale_factor),
int(src.width * upscale_factor)
),
resampling=Resampling.bilinear)
# scale image transform
transform = src.transform * src.transform.scale(
(src.width / data.shape[-1]),
(src.height / data.shape[-2])
)
Any suggestions? I would think some sort of treatment for discrete data would be built in but have not found it yet…
Answers:
I found a solution.
Deleting resampling=Resampling.bilinear
avoids interpolation and performs a "simple" resampling.
What you’re trying to do is nearest neighbor interpolation, so you could specify resampling = Resampling.nearest
. This is the default resampling algorithm, so not specifying any resampling algorithm equates to this. You can find all available resampling algorithms here.
I am resampling raster data using Python’s rasterio. Looking at the rasterio.enums.Resampling
class, it appears the only way to do this is to interpolate between adjacent raster grids, essentially smoothing the data.
Is there some way to do a simple upsampling that effectively divides one raster grid into many and preserves the original value for all of the sub-grids?
My resampling script is as follows – currently using the bi-linear method:
with rasterio.open(str(rasterpath+filename), crs="EPSG:4326") as src:
data = src.read(
out_shape=(
src.count,
int(src.height * upscale_factor),
int(src.width * upscale_factor)
),
resampling=Resampling.bilinear)
# scale image transform
transform = src.transform * src.transform.scale(
(src.width / data.shape[-1]),
(src.height / data.shape[-2])
)
Any suggestions? I would think some sort of treatment for discrete data would be built in but have not found it yet…
I found a solution.
Deleting resampling=Resampling.bilinear
avoids interpolation and performs a "simple" resampling.
What you’re trying to do is nearest neighbor interpolation, so you could specify resampling = Resampling.nearest
. This is the default resampling algorithm, so not specifying any resampling algorithm equates to this. You can find all available resampling algorithms here.