0

I'm trying to export numpy ndarray that I have to TIFF using rasterio. I have the transform of the image:

print(transf)
>>>(583932.1992386435, 10.0, 0, 2223772.1491772938, 0, -10.0)

When I use this when I save the raster I recieve error :

    with rasterio.open('test.tiff', 
                       'w',
                       driver='GTiff',
                       height=img.shape[0],
                       width=img.shape[1],
                       count=3,
                       dtype=img.dtype,
                       crs=rasterio.crs.CRS.from_dict(init='epsg:4326'),
                       nodata=None, # change if data has nodata value
                       transform=transf) as dst:
        dst.write(vi[:,:,0], 1)
        dst.write(vi[:,:,1], 2)
        dst.write(vi[:,:,2], 3)

TypeError: GDAL-style transforms have been deprecated. This exception will be raised for a period of time to highlight potentially confusing errors, but will eventually be removed.

I haven't found any posts in regard to this error. How can I change it? How can I save my result ndarray int TIFF given the transform?

2 Answers 2

4

Use the Affine.from_gdal method to convert any existing GDAL-style transforms:

from affine import Affine

transf = (583932.1992386435, 10.0, 0, 2223772.1491772938, 0, -10.0)
afn = Affine.from_gdal(*transf)

Now pass afn to rasterio's transform parameter.

1

Explanation of the why at https://github.com/mapbox/rasterio/blob/master/docs/topics/migrating-to-v1.rst#migrating-to-rasterio-10

Explanation of how to fix your code with this issue comment https://github.com/mapbox/rasterio/issues/1830#issuecomment-601446758. First link is useful for order of args between old style e.g GDAL geotransform and new style e.g affine.Affine

In your case,

#(c, a, b, f, d, e)
transf = (583932.1992386435, 10.0, 0, 2223772.1491772938, 0, -10.0)

should become

from rasterio import Affine as A

#affine.Affine(a, b, c,
#              d, e, f)

transf = A(10.0, 0.0, 583932.1992386435,
            0.0, -10.0, 2223772.1491772938,)

In the rasterio.open, continue to use transform=transf as it accepts an Affine object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.