gdal reprojectimage切断图像

时间:2015-07-14 20:24:52

标签: python gdal

我使用gdal python重新投影一个grib文件。我做这个python而不是gdalwarp是因为我不想接受编写新文件并在python脚本中读回来进行进一步处理。并且运行gdalwarp非常慢并且创建非常大的文件。我目前正在使用http://jgomezdans.github.io/gdal_notes/reprojection.html的重投影代码。

osng = osr.SpatialReference ()
osng.ImportFromEPSG ( epsg_to )
wgs84 = osr.SpatialReference ()
wgs84.ImportFromEPSG ( epsg_from )
tx = osr.CoordinateTransformation ( wgs84, osng )
# Up to here, all  the projection have been defined, as well as a 
# transformation from the from to the  to :)
# We now open the dataset
g = gdal.Open ( dataset )
# Get the Geotransform vector
geo_t = g.GetGeoTransform ()
x_size = g.RasterXSize # Raster xsize
y_size = g.RasterYSize # Raster ysize
# Work out the boundaries of the new dataset in the target projection
(ulx, uly, ulz ) = tx.TransformPoint( geo_t[0], geo_t[3])
(lrx, lry, lrz ) = tx.TransformPoint( geo_t[0] + geo_t[1]*x_size, \
                                      geo_t[3] + geo_t[5]*y_size )
# See how using 27700 and WGS84 introduces a z-value!
# Now, we create an in-memory raster
mem_drv = gdal.GetDriverByName( 'MEM' )
# The size of the raster is given the new projection and pixel spacing
# Using the values we calculated above. Also, setting it to store one band
# and to use Float32 data type.
dest = mem_drv.Create('', int((lrx - ulx)/pixel_spacing), \
        int((uly - lry)/pixel_spacing), 1, gdal.GDT_Float32)
# Calculate the new geotransform
new_geo = ( ulx, pixel_spacing, geo_t[2], \
            uly, geo_t[4], -pixel_spacing )
# Set the geotransform
dest.SetGeoTransform( new_geo )
dest.SetProjection ( osng.ExportToWkt() )
# Perform the projection/resampling 
res = gdal.ReprojectImage( g, dest, \
            wgs84.ExportToWkt(), osng.ExportToWkt(), \
            gdal.GRA_Bilinear )

问题在于它似乎正在切断我的数据。我不确定如何更改重新投影图像的范围以包含所有数据。 它看起来像这样: result

它看起来像是什么样的 expected

1 个答案:

答案 0 :(得分:0)

我必须找到最大/最小X和最大/最小Y.使用它而不是lr,ul点。

修改: 基本上你不能在计算新的地理变换时使用右下角,左上角,因为在你的网格中间可能有一个点,当重新投影/变换时你的角落范围之外(lr,ul)。因此,您需要重新投影边界上的所有点并获得最小值和最大值(x,y)。然后使用min和max来计算新的地理变换。