如何使用GDAL将GeoTiff的投影设置为WGS 1984

时间:2019-04-01 20:00:56

标签: python-3.x gdal netcdf geotiff

我正在尝试将NASA全局LDAS数据(在默认的-60,90和-180,180子集范围以及所有变量下可用的here的netcdf文件中的数据转换为WGS 1984中的geotiff投影。我正在使用GDAL和Python 3。

我写的代码与示例和此处关于创建Geotiff的其他问题大致相同,并且在我尝试设置投影之前一直有效。

当我尝试设置投影时,我遇到了问题。我试过了 new_geotiff.SetProjection('WGS84')并得到Only OGC WKT Projections supported for writing to GeoTIFF. WGS84 not supported.我已经尝试过

spatialreference = osr.SpatialReference().ImportFromWkt('WGS84')
new_geotiff.SetProjection(spatialreference)

出现错误

TypeError: in method 'Dataset_SetProjection', argument 2 of type 'char const *'

这里是我编写的全部功能:

    import netCDF4
    import numpy
    import gdal, gdalconst
    import osr

    # Reading in data from the netcdf
    nc_obj = netCDF4.Dataset(file_path, 'r')
    var_data = nc_obj.variables[var][:]
    lat = nc_obj.variables['lat'][:]
    lon = nc_obj.variables['lon'][:]

    # format the array of information going to the tiff
    x = numpy.asarray(var_data)[0, :, :]

    # Creates geotiff raster file (filepath, x-dimensions, y-dimensions, number of bands, datatype)
    geotiffdriver = gdal.GetDriverByName('GTiff')
    new_geotiff = geotiffdriver.Create(save_dir_path + 'geotiff.tif', len(lon), len(lat), 1, gdal.GDT_Float32)

    # calculate the geographic transform information and set the projection
    # geotransform = (topleft x, x-width/spacing, orientation angle, topleft y, orientation angle, y-width/spacing)
    yorigin = lat.max()
    xorigin = lon.min()
    xres = lat[1] - lat[0]
    yres = lon[1] - lon[0]
    new_geotiff.SetGeoTransform((xorigin, xres, 0, yorigin, 0, -yres))

    # Set the projection of the geotiff
    new_geotiff.SetProjection('WGS84')             # set the projection for the new geotiff

    # actually write the data array to the tiff file and save it
    new_geotiff.GetRasterBand(1).WriteArray(x)               # write band to the raster (variable array)
    new_geotiff.FlushCache()                                 # write to disk

不确定我要去哪里。我也一直在花时间看地理变换,只是不确定在哪里看

0 个答案:

没有答案