Note
Go to the end to download the full example code.
Reproject a raster#
This example demonstrates the reprojection of a raster using reproject().
We open two example rasters.
import geoutils as gu
filename_rast1 = gu.examples.get_path("everest_landsat_b4")
filename_rast2 = gu.examples.get_path("everest_landsat_b4_cropped")
rast1 = gu.open_raster(filename_rast1)
rast2 = gu.open_raster(filename_rast2)
The first raster has larger extent and higher resolution than the second one.
Driver: GTiff
Filename: /home/docs/checkouts/geoutils/checkouts/latest/geoutils/example_data/Everest_Landsat/LE71400412000304SGS00_B4.tif
Loaded? False
Grid size: 800, 655
Number of bands: 1
Data types: float32
Coordinate system: ['WGS 84 / UTM zone 45N']
Nodata value: None
Pixel interpretation: Point
Pixel size: 30.0, 30.0
Upper left corner: 478000.0, 3108140.0
Lower right corner: 502000.0, 3088490.0
Driver: GTiff
Filename: /home/docs/checkouts/geoutils/checkouts/latest/geoutils/example_data/Everest_Landsat/LE71400412000304SGS00_B4_cropped.tif
Loaded? False
Grid size: 492, 315
Number of bands: 1
Data types: float32
Coordinate system: ['WGS 84 / UTM zone 45N']
Nodata value: None
Pixel interpretation: Area
Pixel size: 30.0, 30.0
Upper left corner: 483430.0, 3102710.0
Lower right corner: 498190.0, 3093260.0
Let’s plot the first raster, with the warped extent of the second one.
rast1.rst.plot(cmap="Blues")
vect_bounds_rast2 = gu.GeoDataFrameVectorAccessor.from_bounds_projected(rast2)
vect_bounds_rast2.vct.plot(fc="none", ec="r", lw=2)

First option: using the second raster as a reference to match, we reproject the first one. We simply have to pass the second raster
as single argument to reproject(). See Match-reference operations for more details.
By default, a “bilinear” resampling algorithm is used. Any string or Resampling can be passed.
Note
The rasters have different pixel interpretations (“Point” and “Area”), which raises a UserWarning.
Correct the metadata if needed, or configure this warning through Configuration.
Now the shape and georeferencing should be the same as that of the second raster, shown above.
Driver: GTiff
Filename: /home/docs/checkouts/geoutils/checkouts/latest/geoutils/example_data/Everest_Landsat/LE71400412000304SGS00_B4.tif
Loaded? True
Grid size: 492, 315
Number of bands: 1
Data types: float32
Coordinate system: ['WGS 84 / UTM zone 45N']
Nodata value: -99999
Pixel interpretation: Point
Pixel size: 30.0, 30.0
Upper left corner: 483430.0, 3102710.0
Lower right corner: 498190.0, 3093260.0
We can plot the two rasters next to one another
rast1_warped.rst.plot(ax="new", cmap="Reds")
rast2.rst.plot(ax="new", cmap="Blues")
Second option: we can pass any georeferencing argument to reproject(), such as dst_size and dst_crs, and will only
deduce other parameters from the raster from which it is called (for dst_crs, an EPSG code can be passed directly as int).
# Ensure the right nodata value is set
rast2.rst.set_nodata(0)
# Pass the desired georeferencing parameters
rast2_warped = rast2.rst.reproject(grid_size=(100, 100), crs=32645)
rast2_warped.rst.info()
Driver: GTiff
Filename: /home/docs/checkouts/geoutils/checkouts/latest/geoutils/example_data/Everest_Landsat/LE71400412000304SGS00_B4_cropped.tif
Loaded? True
Grid size: 100, 100
Number of bands: 1
Data types: float32
Coordinate system: ['WGS 84 / UTM zone 45N']
Nodata value: 0
Pixel interpretation: Area
Pixel size: 147.6, 94.5
Upper left corner: 483430.0, 3102710.0
Lower right corner: 498190.0, 3093260.0
Total running time of the script: (0 minutes 3.617 seconds)

