0

I'm trying to split a georeferenced TIFF image into several tiles for further processing.

I have managed to calculate a single tile and give it the right CRS, evething fine so far.

Once I load the tile up in QGIS, it shows me that the top left corner of the tile is at 0/0. It should be arround 483544,5579377.

I suppose that somehow I am missing to give the right transformation. The code looks like this:


with rasterio.open(imgPath) as src:
    tile = src.read(1, window=Window(1000, 1000, 500, 300))

test = rasterio.open(imgPath)
print(test.crs)# 25832

with rasterio.open(resPath, 'w',driver='GTiff', width=500, height=300, count=1,
        dtype=tile.dtype, crs=src.crs) as dst:
    dst.write(tile, window=Window(0, 0, 500, 300), indexes=1)
    print(dst.crs)#25832

Before anyone asks, I have build this very same tool using GDAL and calling it via subprocess, but it is a nightmare to bundle up with pyinstaller, so I want to try it this way and hope for the best.

1

It looks like the only piece missing from your write call is a transform:

with rasterio.open(imgPath) as src:
    win = Window(1000, 1000, 500, 300)
    tile = src.read(1, window=win)
    win_transform = src.window_transform(win)

with rasterio.open(
    resPath,
    'w',
    driver='GTiff',
    width=500,
    height=300,
    count=1,
    dtype=tile.dtype,
    crs=src.crs,
    transform=win_transform) as dst:

    dst.write(tile, window=Window(0, 0, 500, 300), indexes=1)

  • You probably don't need the window= kwarg when writing. – snowman2 Jan 7 at 2:49
  • Thank you very much, works like a charm ! – jdoe Jan 7 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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