Search code examples
pythonimage-processinggisrasterrasterio

How to export a rasterio window to an image without geo-reference information?


I would like to export a rasterio window to an image.

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)

Unfortunately, because I am not specifying a transform key in the profile above, I get the following warning:

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

NotGeoreferencedWarning: Dataset has no geotransform set. The identity matrix may be returned.

Is there a way to export a non-georeferenced image from a rasterio window without getting warnings?


Solution

  • You need to set the transform, if you set it to an identity, the warning goes away:

    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)