【问题标题】:How to crop a raster image by coordinates in python?如何通过python中的坐标裁剪光栅图像?
【发布时间】:2016-11-09 14:37:39
【问题描述】:

我在 UTM32 中有一个 GeoTiff,在 UTM32 中有一个矩形的坐标。 (这个投影可能不总是这样,但投影总是一样的)

我只需要使用矩形裁剪图像

矩形由下式给出:(xmin, xmax, ymin, ymax)

699934.584491, 700160.946739, 6168703.00544, 6169364.0093

我知道如何从点创建一个多边形,如何从多边形创建一个 shapefile,并且我知道如何使用这些点创建一个蒙面的 numpy 数组。但是,我不知道如何使用多边形、shapefile 或蒙版来实际裁剪图像。

我已经在以下位置查看了描述: https://pcjericks.github.io/py-gdalogr-cookbook/raster_layers.html#clip-a-geotiff-with-shapefile

但是,我并不真正理解它,而且它似乎过于复杂。 (就像我不知道直方图拉伸应该在那里做什么,除了令人困惑)

【问题讨论】:

    标签: python image numpy crop raster


    【解决方案1】:

    尝试使用 bbox = (xmin,ymin,xmax,ymax)

    from osgeo import gdal
    
    bbox = (xmin,ymin,xmax,ymax)
    
    gdal.Translate('output_crop_raster.tif', 'input_raster.tif', projWin = bbox)
    

    【讨论】:

    • 此答案基于gdal_translate documented hereprojwin 参数。 xminymin实际上是upper_left_xupper_left_y坐标,而xmaxymax应该命名为lower_right_xlower_right_y。您可以使用 gdalinfo input_raster.tif 查看输入文件的这些坐标。
    【解决方案2】:

    Akin 的回答大部分是正确的,但没有提供完整的解释。

    你可以使用gdal_translate裁剪一个gdal文件,可以通过gdal.Translate在python中使用。

    最佳选择:projwin

    最简单的方法是使用projwin 标志,它有4 个值:

    window = (upper_left_x, upper_left_y, lower_right_x, lower_right_y)

    这些值在地图坐标中。输入文件的边界可以在命令行通过gdalinfo input_raster.tif获得。

    注意:对于许多坐标系,ymax 实际上小于ymin,因此使用“upper_left”和“lower_right”来标识坐标很重要,而不是“max”和“分钟。”由于这种差异,Akin 的回答对我不起作用。

    完整的解决方案是:

    from osgeo import gdal
    
    upper_left_x = 699934.584491
    upper_left_y = 6169364.0093
    lower_right_x = 700160.946739
    lower_right_y = 6168703.00544
    window = (upper_left_x,upper_left_y,lower_right_x,lower_right_y)
    
    gdal.Translate('output_crop_raster.tif', 'input_raster.tif', projWin = window)
    

    附加选项:srcwin

    srcwin 是另一个 gdal_translate 标志,类似于projwin,但通过偏移量和大小接收像素和线窗口,而不是使用地图坐标边界。你可以这样使用它:

    window = (offset_x, offset_y, size_x, size_y)
    gdal.Translate('output_crop_raster.tif', 'input_raster.tif', srcWin = window)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-29
      • 2022-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-18
      • 2016-12-09
      相关资源
      最近更新 更多