1

I'm new to GDAL python API, when I tried to rotate a TRMM image, I got a weird result compared to the output of ENVI. I just want to rotate the image with 270 degrees, the result is also flipped vertically.

the output of ENVI desktop enter image description here

the result with using gdal enter image description here

import math
from osgeo import gdal
import numpy as np
from osgeo import osr

# open the file
precip = gdal.Open('HDF4_SDS:UNKNOWN:"/home/wanghl/wxq/3B43.19980101.7.HDF":0')

# get the data of the precipitation dataset
preciparr = precip.ReadAsArray()

# get geotransform
transform = precip.GetGeoTransform()

#rotate

rotation = (math.pi/180) * 270


# set geotif driver
driver = gdal.GetDriverByName('GTiff')

# get x,y dimensions of the map
xs = precip.RasterXSize
ys = precip.RasterYSize

new_geotransform = (transform[0],
                    transform[1],
                    -math.sin(rotation) * transform[1],
                    transform[3],
                    math.sin(rotation) * transform[5],
                    transform[5])
# set output name
outname = "/home/wanghl/wxq/3B43.19980101.7.tif"

# set projection
target = osr.SpatialReference()

# write dataset to disk
outputDataset = driver.Create(outname, xs, ys, 1, gdal.GDT_Float32)
outputDataset.SetGeoTransform(new_geotransform)
outputDataset.SetProjection(target.ExportToWkt())
outputDataset.GetRasterBand(1).WriteArray(preciparr)
outputDataset.GetRasterBand(1).SetNoDataValue(-9999)
outputDataset = None

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.