I have a GeoTIFF file of cropped agricultural plots that I am trying to rotate so that they are perpendicular to the image edges. The source image looks like this:
However, when I rotate the image using the Affine functions, the dimensions of the source image are retained, thereby cutting off part of the image as shown:
I have been unable to find a solution that adjusts the dimensions of the output image so that parts are not cut off. Is there a method for resolving this?
My code used is as follows:
import rasterio from rasterio.plot import show import rasterio.warp from affine import Affine from pprint import pprint from rasterio.warp import calculate_default_transform, reproject, Resampling # Takes GeoTIFF file and outputs center pixels def get_center_pixel(img): height, width = img.height, img.width x_mid_pixel = width // 2 y_mid_pixel = height // 2 return x_mid_pixel, y_mid_pixel raster = "clip.tif" angle = 65 scale = 1 with rasterio.open(raster) as src: assert src.crs == 'EPSG:32617', 'Raster must have CRS=EPSG:4326' pivot = get_center_pixel(src) show(src) dst_transform = src.transform * Affine.rotation(angle, pivot) * Affine.scale(1) dst_meta = src.meta dst_meta['transform'] = dst_transform with rasterio.open("rotate.tif", 'w', **dst_meta) as dst: for i in range(1, src.count + 1): reproject( source=rasterio.band(src,i), destination=rasterio.band(dst, i), src_transform=src.transform, src_crs=src.crs, dst_transform=dst_transform, dst_crs=src.crs, resampling=Resampling.nearest )