python rasterio projection code example

Example 1: rasterio reproject python

import rasterio
from rasterio.warp import calculate_default_transform, reproject, Resampling
from fiona.crs import from_epsg
dst_crs = from_epsg(3413) #example of crs

with rasterio.open('my_raster.tif') as src:
    transform, width, height = calculate_default_transform(src.crs, dst_crs, 
                                                           src.width, 
                                                           src.height, 
                                                           *src.bounds)
    kwargs = src.meta.copy()
    kwargs.update({'crs': dst_crs,'transform': transform, 'width': width,'height': height})

    with rasterio.open('reprojected_raster.tif', 'w', **kwargs) as dst:
            reproject(source=rasterio.band(src, 1),destination=rasterio.band(dst, 1),
                src_transform=src.transform,
                src_crs=src.crs,
                dst_transform=transform,
                dst_crs=dst_crs,
                resampling=Resampling.nearest)

Example 2: rasterio.warp.reproject

gdalwarp -of GTiff -co TILED=YES -co COMPRESS=LZW -tr 30.0 30.0 -s_srs EPSG:4326 -t_srs "+proj=aea +lat_1=10 +lat_2=-40 +lat_0=-25 +lon_0=-50 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs" AMAZONIA_tiled.tif AMAZONIA_reprojected.tif