I am currently trying to extract Web Mercator tiles from a Geotiff raster using rasterio VRT (version 1.0rc5). The tiles sometimes exceed the extent of the raster, which causes me trouble.

Reading the tile with data = vrt.read(window=tile_window, out_shape=tile_shape) gives me corrupted result, instead of a cleanly cropped tile (I would expect to have NoData values out of the raster extent). According to the doc, the parameter boundless=True seems to do what I want, but it does not work for WarpedVRT.

I am trying to find a workaround by adding a NoData buffer around my raster. Is it possible, and, if so, how can I do it?

My simplified code:

with rasterio.open(image) as src:

    extent = ...
    tile_shape = ...

    with WarpedVRT(src, crs='EPSG:3857', resampling=Resampling.bilinear) as vrt:

        # Window for reading the dataset
        tile_window = vrt.window(extent.x_min, extent.y_min, extent.x_max, extent.y_max)

        # Read tile content (boundless mode does not work yet for WarpedVRT)
        data = vrt.read(window=tile_window, out_shape=tile_shape)

Edit to illustrate the issue:

The tile I want to extract (there are 3 or 4 rows of pixels et the very bottom of the tile):

What I get from rasterio.vrt:

  • Have you tried defining src_nodata and dst_nodata? What values do you get where you expect no data values? – Liam G Jul 20 '18 at 0:53
  • Hi Liam, I did not set specific nodata values. I have edited my question to show what I get. – ArMoraer Jul 20 '18 at 6:28

I finally solved my issue, without adding a padding buffer around the raster. The corrupted result was caused by the option out_shape=tile_shape when reading the VRT.

Without this option, vrt.read() returns an array that contains only the raster data located inside the window; in my illustration, its size was something like (4,500). I had to manually resample this array in order to fit the tile size, then fill the rest of the tile with NoData value.

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.