# Expressing raster rotation with GDAL geotransform (python)

I want to generate a raster which is rotated with respect to its coordinate system. As I understand this requires modifying the geotransform array. I have not been able to find much information about this (though I did find a related SE question in Rotating rasters using GDAL Geotransform (and ArcGIS Desktop), and a thread on the GDAL-dev mailing list here, apparently started by the same person).

Most sites seem to only provide information on the structure of the unrotated geotransform. Based on the linked post & mailing list thread, I understand that the rotated geotransform should have the following elements:

``````[0] x-coordinate of upper-left raster corner
[1] cos(θ) * x_resolution
[2] -sin(θ) * x_resolution
[3] y-coordinate of upper-left raster corner
[4] sin(θ) * y_resolution
[5] cos(θ) * y_resolution
``````

However, I am not sure if this is correct.

I have tried using python to save a rotated raster using the above as a guideline for my geotransform, but the output is not what I expected. I defined my geotransform as:

``````# Define raster array to save
array = np.arange(0,25).reshape(5,5)

# Define rotation & geotransform
rot = rot*math.pi/180
gt = (0,math.cos(rot)*x_res, -math.sin(rot)*x_res, 0, math.sin(rot)*y_res, math.cos(rot)*y_res)

# Save raster
save_raster(outfile, array, srs, gt)
``````

And got the following result:

In a sense the raster has been 'rotated', but it appears my array has been resampled (which I do not want) while the raster cells are still aligned with the coordinate system x/y axes.

Am I just misunderstanding how the geotransform array works? Is there even a way to do what I am trying to do?

• When you save the the image into a new file it is resampled to CRS axis oriented system. By the same the rotation elements get removed as you can see from a gdalinfo report. If you just attach the rotated geotransform into the original image no resampling happens. Then it is up to the using applications to rotate the image when it is shown for example on computer screen. Mar 17, 2018 at 8:35
• Thanks for the advice; however, even when I open an existing file using `GA_Update`, the image is still resampled on setting a new geotransform. Is there a way to override this behavior? Mar 17, 2018 at 14:44
• What do you await really? Rasters are rectangular and if you want to show the original image as rotated on a computer screen then there will appear no-data areas at each corner which must be filled with new pixels. Orientation of pixels remains horizontal-vertical because rotating the image does not rotate the screen. Perhaps an alternative way for visualizing the result could be not to rotate the image at all on screen but draw the South-North and East-West coordinate axes on top of the image as vectors which would then be more or less diagonal. Mar 20, 2018 at 12:46
• My thought was that the raster cells themselves would be rotated within the coordinate reference system, i.e. the rows and columns would not be oriented in the same direction as the unit vectors of the coordinate system. However, maybe this is not possible? Mar 20, 2018 at 15:27
• Parameters in geotransform are the same than in ESRI world file en.wikipedia.org/wiki/World_file. You can rotete the image without touching any pixel by writing a new world file. In the same way you should be able to just write the geotransform metadata into an image without warping it into a north-up raster as in stackoverflow.com/questions/9726637/…. Mar 20, 2018 at 21:09