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

前端 未结 2 1423
慢半拍i
慢半拍i 2020-12-23 10:58

Clarification: I somehow left out the key aspect: not using os.system or subprocess - just the python API.

I\'m trying to convert a section of a NOAA GTX offset gri

相关标签:
2条回答
  • 2020-12-23 11:32

    Thanks to Jamie for the answer.

    #!/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
    
    0 讨论(0)
  • 2020-12-23 11:36

    If I understand the question correctly, you could accomplish your goal by running gdalwarp and gdal_translate as subprocesses. 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.

    0 讨论(0)
提交回复
热议问题