2

I want to use GCPs list for converting HDF file to GeoTIFF file with lat/lon. The related example on internet is very poor. My code below can't run.

import gdal
from pyhdf.SD import SD, SDC
file="C:/Users/TLQ/Desktop/11/MYD09.A2011093.0410.006.2015217030905.hdf"
f1 = SD(file, SDC.READ)
lat = f1.select('Latitude')
latitude = lat[:,:]
lon = f1.select('Longitude')
longitude = lon[:,:]
b1 = f1.select('BAND31')
AOP = b1[:,:]
GCPList=[]
for i in range(10):#latitude.shape[0]-1
    for j in range(10):#latitude.shape[1]-1
        GCP = gdal.GCP()
        GCP.GCPLine= i*203
        GCP.GCPPixel = j*135
        GCP.GCPX = float(longitude[i*203,j*135])
        GCP.GCPY =float(latitude[i*203,j*135])
        GCPList.append(GCP)
datatype=gdal.GDT_UInt16
driver=gdal.GetDriverByName('GTiff')
dataset=driver.Create("C:/Users/TLQ/Desktop/11/MYD09.A2011093_111.tiff",1354,2030,1,datatype)
dataset.GetRasterBand(1).WriteArray(AOP)
ds=gdal.Open("C:/Users/TLQ/Desktop/11/MYD09.A2011093_111.tiff")
result=gdal.Translate("C:/Users/TLQ/Desktop/11/MYD09.A2011093.tiff",ds,\
                      options=gdal.TranslateOptions(GCPs=GCPList,format='GTiff'))

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.