如何在没有地理参考信息的情况下将光栅窗口导出到图像?

时间:2019-10-15 11:07:53

标签: python image-processing gis raster rasterio

我想将rasterio窗口导出到图像。

import rasterio

def export_window(w, path):
    number_of_bands, height, width = w.shape

    profile = {
        "driver": "JPEG",
        "count": number_of_bands,
        "height": height,
        "width": width,
        'dtype': 'uint8'
    }
    with rasterio.open(path, 'w', **profile) as dst:
        dst.write(w)

不幸的是,由于我没有在上面的transform中指定profile键,因此出现以下警告:

>>> raster = rasterio.open("/tmp/geo.tif")
>>> w = raster.read(window=rasterio.windows.Window(0, 0, 500, 500))
>>> export_window(w, "/tmp/export.jpg")
  

NotGeoreferencedWarning:数据集没有地理转换集。可以返回单位矩阵。

是否有一种方法可以从rasterio窗口中导出未地理参考的图像而不会收到警告?

1 个答案:

答案 0 :(得分:0)

您需要设置转换,如果将其设置为标识,则警告消失:

import rasterio

def export_window(w, path):
    number_of_bands, height, width = w.shape

    profile = {
        "driver": "JPEG",
        "count": number_of_bands,
        "height": height,
        "width": width,
        'dtype': 'uint8',
        'transform': rasterio.Affine(1, 0, 0, 0, 1, 0),
    }
    with rasterio.open(path, 'w', **profile) as dst:
        dst.write(w)