imagereader

Contains the ImageReader class

class rios.imagereader.ImageIterator(reader)[source]

Class to allow iteration across an ImageReader instance. Do not instantiate this class directly - it is created by ImageReader.__iter__().

See http://docs.python.org/library/stdtypes.html#typeiter for a description of how this works. There is another way, see: http://docs.python.org/reference/expressions.html#yieldexpr but it seemed too much like Windows 3.1 programming which scared me!

Returns a tuple containing an ReaderInfo class, plus a numpy array for each iteration

next()[source]
class rios.imagereader.ImageReader(imageContainer, footprint=0, windowxsize=256, windowysize=256, overlap=0, statscache=None, loggingstream=<open file '<stdout>', mode 'w'>, layerselection=None)[source]

Class that reads a single file, a list or dictionary of files and iterates through them block by block

Example

import sys
from rios.imagereader import ImageReader

reader = ImageReader(sys.argv[1]) 
for (info, block) in reader:     
    block2 = block * 2
allowResample(resamplemethod='near', refpath=None, refgeotrans=None, refproj=None, refNCols=None, refNRows=None, refPixgrid=None, tempdir='.', useVRT=False, allowOverviewsGdalwarp=False)[source]

By default, resampling is disabled (all datasets must match). Calling this enables it. Either refgeotrans, refproj, refNCols and refNRows must be passed, or refpath passed and the info read from that file.

tempdir is the temporary directory where the resampling happens. By default the current directory.

resamplemethod is the method used - must be supported by gdalwarp. This can be a single string if all files are to be resampled by the same method, or a list or dictionary (to match what passed to the constructor) contain the methods for each file.

If resampling is needed it will happen before the call returns.

close()[source]

Closes all open datasets

prepare(workingGrid=None)[source]

Prepare to read from images. These steps are not done in the constructor, but are done just before reading in case allowResample() is called which will resample the inputs.

The pixelGrid instance to use as the working grid can be passed in case it is not to be derived from the images to be read or is different from that passed to allowResample

readBlock(nblock)[source]

Read a block. This is normally called from the __getitem__ method when this class is indexed, or from the ImageIterator when this class is being iterated through.

A block is read from each image and returned in a tuple along with a ReaderInfo instance.

nblock is a single index, and will be converted to row/column.

static readBlockWithMargin(ds, xoff, yoff, xsize, ysize, datatype, margin=0, nullValList=None, layerselection=None)[source]

A ‘drop-in’ look-alike for the ReadAsArray function in GDAL, but with the option of specifying a margin width, such that the block actually read and returned will be larger by that many pixels. The returned array will ALWAYS contain these extra rows/cols, and if they do not exist in the file (e.g. because the margin would push off the edge of the file) then they will be filled with the given nullVal. Otherwise they will be read from the file along with the rest of the block.

Variables within this function which have _margin as suffix are intended to designate variables which include the margin, as opposed to those without.

This routine will cope with any specified region, even if it is entirely outside the given raster. The returned block would, in that case, be filled entirely with the null value.