澄清:我以某种方式忽略了关键方面:不使用os.system或子进程-仅使用python API。
我正在尝试将NOAA GTX偏移网格的一部分转换为垂直基准面转换,而不是完全遵循在GDAL中使用python进行此操作的方法。我想要一个网格(在本例中为“测深归因网格”,但它可能是一个geotif),并将其用作我想要做的模板。如果我能做到这一点,我感觉它将极大地帮助人们使用此类数据。
这就是我绝对不能正常工作的东西。在生成的目标数据集(dst_ds)上运行gdalinfo时,它与源网格BAG不匹配。
from osgeo import gdal, osr bag = gdal.Open(bag_filename) gtx = gdal.Open(gtx_filename) bag_srs = osr.SpatialReference() bag_srs.ImportFromWkt(bag.GetProjection()) vrt = gdal.AutoCreateWarpedVRT(gtx, None, bag_srs.ExportToWkt(), gdal.GRA_Bilinear, 0.125) dst_ds = gdal.GetDriverByName('GTiff').Create(out_filename, bag.RasterXSize, bag.RasterYSize, 1, gdalconst.GDT_Float32) dst_ds.SetProjection(bag_srs.ExportToWkt()) dst_ds.SetGeoTransform(vrt.GetGeoTransform()) def warp_progress(pct, message, user_data): return 1 gdal.ReprojectImage(gtx, dst_ds, None, None, gdal.GRA_NearestNeighbour, 0, 0.125, warp_progress, None)
示例文件(但重叠的两个网格但投影位置不同的两个网格都可以):
命令行等效于我要执行的操作:
gdalwarp -tr 2 -2 -te 369179 4773093 372861 4775259 -of VRT -t_srs EPSG:2960 \ MENHMAgome01_8301/mllw.gtx mllw-2960-crop-resample.vrt gdal_translate mllw-2960-crop-resample.{vrt,tif}
感谢杰米的答案。
#!/usr/bin/env python from osgeo import gdal, gdalconst # Source src_filename = 'MENHMAgome01_8301/mllw.gtx' src = gdal.Open(src_filename, gdalconst.GA_ReadOnly) src_proj = src.GetProjection() src_geotrans = src.GetGeoTransform() # We want a section of source that matches this: match_filename = 'F00574_MB_2m_MLLW_2of3.bag' match_ds = gdal.Open(match_filename, gdalconst.GA_ReadOnly) match_proj = match_ds.GetProjection() match_geotrans = match_ds.GetGeoTransform() wide = match_ds.RasterXSize high = match_ds.RasterYSize # Output / destination dst_filename = 'F00574_MB_2m_MLLW_2of3_mllw_offset.tif' dst = gdal.GetDriverByName('GTiff').Create(dst_filename, wide, high, 1, gdalconst.GDT_Float32) dst.SetGeoTransform( match_geotrans ) dst.SetProjection( match_proj) # Do the work gdal.ReprojectImage(src, dst, src_proj, match_proj, gdalconst.GRA_Bilinear) del dst # Flush