简体   繁体   English

如何投影和重新采样网格以与GDAL python匹配另一个网格?

[英]How to project and resample a grid to match another grid with GDAL python?

Clarification: I somehow left out the key aspect: not using os.system or subprocess - just the python API. 澄清:我在某种程度上忽略了关键方面:不使用os.system或subprocess - 只是python API。

I'm trying to convert a section of a NOAA GTX offset grid for vertical datum transformations and not totally following how to do this in GDAL with python. 我正在尝试转换NOAA GTX偏移网格的一部分以进行垂直基准转换,而不是完全遵循如何在GDAL中使用python执行此操作。 I'd like to take a grid (in this case a Bathymetry Attributed Grid, but it could be a geotif) and use it as the template that I'd like to do to. 我想采用一个网格(在这种情况下是一个Bathymetry归属网格,但它可能是一个geotif)并将其用作我想要做的模板。 If I can do this right, I have a feeling that it will greatly help people make use of this type of data. 如果我能做到这一点,我觉得它将极大地帮助人们使用这种类型的数据。

Here is what I have that is definitely not working. 这就是我所拥有的绝对无效的东西。 When I run gdalinfo on the resulting destination dataset (dst_ds), it does not match the source grid BAG. 当我在生成的目标数据集(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)

Example files (but any two grids where they overlap, but are in different projections would do): 示例文件(但它们重叠的任何两个网格,但在不同的投影中都会这样做):

The command line equivalent to what I'm trying to do: 命令行等同于我正在尝试做的事情:

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}

Thanks to Jamie for the answer. 感谢Jamie的回答。

#!/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

If I understand the question correctly, you could accomplish your goal by running gdalwarp and gdal_translate as subprocesses. 如果我正确理解了这个问题,你可以通过运行gdalwarp和gdal_translate作为子进程来实现目标。 Just assemble your options then do the following for example: 只需组装您的选项,然后执行以下操作:

import subprocess

param = ['gdalwarp',option1,option2...]
cmd = ' '.join(param)
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout = ''.join(process.stdout.readlines())
stderr = ''.join(process.stderr.readlines())

if len(stderr) > 0:
    raise IOError(stderr)

It may not be the most elegant solution, but it will get the job done. 它可能不是最优雅的解决方案,但它可以完成工作。 Once it is run, just load your data into numpy using gdal and carry on your way. 一旦运行,只需使用gdal将数据加载到numpy并继续运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM