Cate
Cate

Reputation: 431

In Rasterio - how to tile an array representing a geo-referenced image when the Affine object is separate?

I am attempting to tile a large image (.img format, but could be geotiff), however I have already cropped the image using rasterio mask which returns a masked array and a separate Affine object.

from rasterio import mask
import fiona

image = rasterio.open(image_path)
with fiona.open(shapefile_path, 'r') as shapefile:
    cropping_polygon = [polygon['geometry'] for polygon in shapefile]

smaller_image, smaller_image_affine = mask.mask(image, cropping_polygon, crop=True)

Now I want to split the smaller_image into tiles of a fixed size. I have looked at rasterio windowed reading and writing but this seems to rely on the image having the image.affine attribute in order not to lose the geo-referencing.

Is it possible to tile the masked array, and produce a new affine for each tile?

Upvotes: 2

Views: 1905

Answers (1)

jdmcbr
jdmcbr

Reputation: 6406

I think you are looking for rasterio.windows.transform.

tile_window = rasterio.windows.Window(0, 0, 256, 256)
tile_affine = rasterio.windows.transform(tile_window, smaller_image_affine)
tile_image = smaller_image[(slice(None),) + tile_window.toslices()]

Then with tile_image and tile_affine you have all the pieces you need to write this to a new file.

Upvotes: 2

Related Questions