Basic tools for setting up a function to be applied over
a raster processing chain. The
rios.applier.apply() function is the main
point of entry in this module.
Controls for the operation of rios, for use with the
This object starts with default values for all controls, and has methods for setting each of them to something else.
- Attributes are:
- windowxsize X size of rios block (pixels)
- windowysize Y size of rios block (pixels)
- overlap Number of pixels in margin for block overlaps
- drivername GDAL driver short name for output
- creationoptions GDAL creation options for output
- thematic True/False for thematic outputs
- layernames List of layer names for outputs
- referenceImage Image for reference projection and grid that inputs will be resampled to.
- referencePixgrid pixelGrid for reference projection and grid
- loggingstream file-like for logging of messages
- progress progress object
- statsIgnore global stats ignore value for output (i.e. null value)
- statscache stats cache if pre-calculated
- calcStats True/False to signal calculate statistics and pyramids
- omitPyramids True/False to omit pyramids when doing stats
- overviewLevels List of level factors used when calculating output image overviews
- overviewMinDim Minimum dimension of highest overview level
- overviewAggType Aggregation type for calculating overviews
- tempdir Name of directory for temp files (resampling, etc.)
- resampleMethod String for resample method, when required (as per GDAL)
- numThreads Number of parallel threads used for processing each image block
- jobManagerType Which
rios.parallel.jobmanager.JobManagersub-class to use for parallel processing (by name)
- autoColorTableType Type of color table to be automatically added to thematic output rasters
- Options relating to vector input files
- burnvalue Value to burn into raster from vector
- filtersql SQL where clause used to filter vector features
- alltouched Boolean. If True, all pixels touched are included in vector.
- burnattribute Name of vector attribute used to supply burnvalue
- vectorlayer Number (or name) of vector layer
- vectordatatype Numpy datatype to use for raster created from vector
- vectornull Rasterised vector is initialised to this value, before burning
Default values are provided for all attributes, and can then be over-ridden with the ‘set’ methods given.
Some ‘set’ methods take an optional imagename argument. If given, this should be the same internal name used for a given image as in the
rios.applier.FilenameAssociationsobjects. This is the internal name for that image, and the method will set the parameter in question for that specific image, which will over-ride the global value set when no imagename is given.
Returns the value of a particular option for the given imagename. If only the global option has been set, then that is returned, but if a specific value has been set for the given imagename, then use that.
The imagename is the same internal name as used for the image in the
Make a dictionary of resample methods, one for every image name in the given dictionary. This method is for internal use only.
Set which layers are to be read from the input image(s). Default will read all layers. If imagename is given, selection will be for that image only. The layerselection parameter should be a list of layer numbers. Layer numbers follow GDAL conventions, i.e. a layer number of 1 refers to the first layer in the file. Can be much more efficient when only using a small subset of layers from the inputs.
Set boolean value of alltouched attribute. If alltouched is True, then pixels will count as “inside” a vector polygon if they touch the polygon, rather than only if their centre is inside. If vectornmame given, then set only for that vector.
If this option is set, then thematic raster outputs will have a color table automatically generated and attached to them. The type is passed to
rios.rat.genColorTable()to determine what type of automatic color table is generated.
The default type will be taken from $RIOS_DFLT_AUTOCOLORTABLETYPE if it is set. If that is not set, then the default is not to automatically attached any color table to thematic output rasters.
In practise, it is probably simpler to explicitly set the color table using the
rios.rat.setColorTable()function, after creating the file, but this is an alternative.
Note that the imagename parameter can be given, in which case the autoColorTableType will only be applied to that raster.
None of this has any impact on athematic outputs.
Set the vector attribute name from which to get the burn value for each vector feature. If vectorname is given, set only for that vector input. Default is to use burnvalue instead of burnattribute.
Set the burn value to be used when rasterizing the input vector(s). If vectorname given, set only for that vector. Default is 1.
Set True to calc stats, False otherwise. If True, then statistics and pyramid layers are calculated (if supported by the driver
Set a list of GDAL creation options (should match with the driver). Each list element is a string of the form “NAME=VALUE”.
Defaults are suitable for the default driver, and need to be changed if that is changed. However, if an appropriate driver-specific default environment variable ($RIOS_DFLT_CREOPT_<driver>) is given, this will be used.
Set an SQL WHERE clause which will be used to filter vector features. If vectorname is given, then set only for that vector
Set type of footprint, one of INTERSECTION, UNION or BOUNDS_FROM_REFERENCE from this module
Set which type of JobManager is to be used for parallel processing. See
rios.parallel.jobmanagerfor details. Default is taken from $RIOS_DFLT_JOBMGRTYPE.
Set list of layernames to be given to the output file(s)
Set the rios logging stream to the given file-like object.
Set the number of ‘threads’ to be used when processing each block of imagery. Note that these are not threads in the technical sense, but are handled by the JobManager class, and are some form of cooperating parallel processes, depending on the type of job manager sub-class selected. See
rios.parallel.jobmanagerfor full details. Note that this is only worth using on very computationally-intensive tasks. Default is 1, i.e. no parallel processing.
Set True to omit pyramid layers, False otherwise. If True, then when statistics are being calculated, pyramid layers will be omitted, otherwise they will be created at the same time. Usual default is False.
setOptionForImagename(option, imagename, value)¶
Set the given option specifically for the given imagename. This method is for internal use only. If you wish to set a particular attribute, use the corresponding ‘set’ method.
Set the output driver name to the given GDAL shortname.
Note that the GDAL creation options have defaults suitable only for the default driver, so if one sets the output driver, then the creation options should be reviewed too.
In more recent versions of RIOS, the addition of driver-specific default creation options ($RIOS_DFLT_CREOPT_<driver>) allows for multiple default creation optionsto be set up.
Set the overlap to the given value. Overlap is a number of pixels, and is somewhat mis-named. It refers to the amount of margin added to each block of input, so that the blocks will overlap, hence the actual amount of overlap is really more like double this value (allowing for odd and even numbers, etc).
Set the type of aggregation used when computing overview images (i.e. pyramid layers). Normally a thematic image should be aggregated using “NEAREST”, while a continuous image should be aggregated using “AVERAGE”. When the setting is given as None, then a default is used. If using an output format which supports LAYER_TYPE, the default is based on this, but if not, it comes from the value of the environment variable $RIOS_DEFAULT_OVERVIEWAGGREGATIONTYPE.
This method should usually be used to set when writing an output to a format which does not support LAYER_TYPE, and which is not appropriate for the setting given by the environment default.
Set the overview levels to be used on output images (i.e. pyramid layers). Levels are specified as a list of integer factors, with the same meanings as given to the gdaladdo command.
Set minimum dimension allowed for output overview. Overview levels (i.e. pyramid layers) will be calculated as per the overviewLevels list of factors, but only until the minimum dimension falls below the value of overviewMinDim
Set the progress display object. Default is no progress object.
Set the name of the image to use for the reference pixel grid and projection. If neither referenceImage nor referencePixgrid are set, then no resampling will be allowed. Only set one of referenceImage or referencePixgrid.
Note that this is the external filename, not the internal name (which unfortunately is a bit inconsistent with everything else).
Set the reference pixel grid. If neither referenceImage nor referencePixgrid are set, then no resampling will be allowed. Only set one of referenceImage or referencePixgrid. The referencePixgrid argument is of type
Set resample method to be used for all resampling. Possible options are those defined by gdalwarp, i.e. ‘near’, ‘bilinear’, ‘cubic’, ‘cubicspline’, ‘lanczos’.
Set the stats cache, if statistics are known from some other source.
Set the global default value to use as the null value when calculating stats.
Set directory to use for temporary files for resampling, etc.
Set boolean value of thematic flag (may not be supported by the GDAL driver)
Set numpy datatype to use for rasterized vectors If vectorname given, set only for that vector
Set the vector null value. This is used to initialise the rasterised vector, before burning in the burn value. This is of most importance when burning values from a vector attribute column, as this should be a distinct value from any of the values in the column. If this is not so, then polygons can end up blending with the background, resulting in incorrect answers.
Set number/name of vector layer, for vector formats which have multiple layers. Not required for plain shapefiles. Can be either a layer number (start at zero) or a layer name. If vectorname given, set only for that vector.
Set the X size of the blocks used. Images are processed in blocks (windows) of ‘windowxsize’ columns, and ‘windowysize’ rows.
Set the Y size of the blocks used. Images are processed in blocks (windows) of ‘windowxsize’ columns, and ‘windowysize’ rows.
Generic object to store the image blocks used within the applied function. The attributes are named the same way as in the corresponding FilenameAssociations object, but are blocks of image data, instead of filenames. Where lists of filenames were used, lists of image blocks are used here.
Class for associating external image filenames with internal names, which are then the same names used inside a function given to the
Each attribute created on this object should be a filename, or a list of filenames. The corresponding attribute names will appear on the ‘inputs’ or ‘outputs’ objects inside the applied function. Each such attribute will be an image data block or a list of image data blocks, accordingly.
Generic object to store any extra inputs and outputs used inside the function being applied. This class was originally named for inputs, but in fact works just as well for outputs, too. Any items stored on this will be persistent between iterations of the block loop.
RIOSJobInfo(info, inputs, otherargs=None)¶
Class that contains information for parameters to a RIOS function
Return the parameters as a tuple.
When this is run in the subprocess, we can reset the info.loggingstream to something other than None
Return the ouputs parameter
GDAL datasets cannot be pickled and neither stderr (in info.logginstream) so we clean up the info object a bit
apply(userFunction, infiles, outfiles, otherArgs=None, controls=None)¶
Apply the given ‘userFunction’ to the given input and output files.
infiles and outfiles are
rios.applier.FilenameAssociationsobjects to define associations between internal variable names and external filenames, for the raster file inputs and outputs.
otherArgs is an object of extra arguments to be passed to the userFunction, each with a sensible name on the object. These can be either input or output arguments, entirely at the discretion of userFunction(). otherArgs should be in instance of
The userFunction has the following call sequence:
userFunction(info, inputs, outputs)
userFunction(info, inputs, outputs, otherArgs)
if otherArgs is not None. inputs and outputs are objects in which there are named attributes with the same names as those given in the infiles and outfiles objects. In the inputs and outputs objects, available inside userFunction, these attributes contain numpy arrays of data read from/written to the corresponding image file.
If the attributes given in the infiles or outfiles objects are lists of filenames, the the corresponding attributes of the inputs and outputs objects inside the applied function will be lists of image data blocks instead of single blocks.
The numpy arrays are always 3-d arrays, with shape:
(numBands, numRows, numCols)
The datatype of the output image(s) is determined directly from the datatype of the numpy arrays in the outputs object.
The info object contains many useful details about the processing, and will always be passed to the userFunction. It can, of course, be ignored. It is an instance of the
The controls argument, if given, is an instance of the
rios.applier.ApplierControlsclass, which allows control of various aspects of the reading and writing of images. See the class documentation for further details.
There is a page dedicated to Applier Examples.
closeOutputImages(writerdict, outfiles, controls)¶
rios.applier.apply()to close all output image files.
handleInputResampling(infiles, controls, reader)¶
rios.applier.apply()to handle automatic resampling of input rasters. Most of the work is done by the read.allowResample() method.
Make a dictionary with the same image name keys as imagefiles, but with layerselection lists for each entry, as per the controls object. If only some images have a layerselection set, then the remaining entries are None.
Returns a dictionary of
rios.vectorreader.Vectorobjects, with the keys being the attribute names used on the vectorfiles object. This is then ready to go into the
Return True if filename opens as a GDAL raster, False otherwise
Return True if filename opens as an OGR vector, False otherwise
rios.applier.FilenameAssociationsobject, separate out the files which are raster, and the files which are vectors. Returns two
rios.applier.FilenameAssociationsobjects, carrying the same attribute names, but each has only the raster or the vectors.
updateProgress(controls, info, lastpercent)¶
rios.applier.apply()to update progress
writeOutputBlocks(writerdict, outfiles, outputBlocks, controls, info)¶
rios.applier.apply(), to write the output blocks, after they have been created by the user function. For internal use only.
For all names given in outfiles object, look for a data block of the same name in the outputBlocks object. If the given name is a list, then the corresponding name should be a list of blocks.
Use the spatial extent of the reference file
Default minimum dimension of highest overview level calculated
Default agregation type when using formats without LAYER_TYPE
DEFAULT_OVERVIEWLEVELS= [4, 8, 16, 32, 64, 128, 256, 512]¶
Default overview levels on output images
Use the spatial intersection of inputs
Use the spatial union of inputs