9

I find rasterio much easier to use that plain ogr/gdal, so I'd prefer using it. However, I need to process large rasters, so reading rasters by block as explained in the Python GDAL course is important for me.

Does rasterio support this or should I handle the large, tilable rasters with GDAL instead?

0

1 Answer 1

17

Reading Rasters by block can be done in rasterio and I'd argue it's easier than in GDAL. There is even a tutorial on windowed read/write over at GitHub.

Let's take a look at the read functions arguments, which allows you to set a window to read data from:

def read(self, indexes=None, out=None, window=None, masked=False,
        boundless=False):
"""Read raster bands as a multidimensional array
    Parameters
    ----------
    window : a pair (tuple) of pairs of ints, optional
        The optional `window` argument is a 2 item tuple. The first
        item is a tuple containing the indexes of the rows at which
        the window starts and stops and the second is a tuple
        containing the indexes of the columns at which the window
        starts and stops. For example, ((0, 2), (0, 2)) defines
        a 2x2 window at the upper left of the raster dataset.
"""

So all you need now is a way to create the window tuples. That's what rasterios block_windows function is for:

def block_windows(self, bidx=0):
    """Returns an iterator over a band's block windows and their
    indexes.
    [....]
    The primary use of this function is to obtain windows to pass to
    read_band() for highly efficient access to raster block data.
    """

So in your case I guess the code would look something like this:

with rasterio.open("your/data/geo.tif") as src:
     for block_index, window in src.block_windows(1):
         block_array = src.read(window=window)
         result_block = some_calculation(block_array)
3
  • 4
    How do you then reassemble result_block into a full result array with the same dimensions as src?
    – user32882
    Commented Oct 22, 2018 at 7:40
  • 1
    How would I do something similar in R? I have a very big (in terms of pixels) raster that I want to divide into blocks and iterate through the blocks to do my analysis.
    – tg110
    Commented Dec 3, 2018 at 20:13
  • 2
    @tg110 that might be better suited as its own question.
    – Kersten
    Commented Dec 3, 2018 at 21:26

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.