pixelgrid¶
Utility class PixelGridDefn, which defines a pixel grid, plus useful operations on it.
Utility function findCommonRegion() to find the union or intersection region of a list GDAL datasets, in a reference grid.
- class rios.pixelgrid.PixelGridDefn(geotransform=None, nrows=None, ncols=None, projection=None, xMin=None, xMax=None, yMin=None, yMax=None, xRes=None, yRes=None)[source]¶
Definition of a pixel grid, including the size and extent, and by implication the resolution and alignment.
Methods are defined for relationships with other instances, including:
intersection()
union()
reproject()
alignedWith()
isComparable()
Attributes defined on the object:
xMin
xMax
yMin
yMax
xRes
yRes
projection
The bounds define the external corners of the image, i.e. the top-left corner of the top-left pixel, through to the bottom-right corner of the bottom-right pixel. This is consistent with GDAL conventions.
The projection is given as a WKT string.
The constructor takes the projection, and EITHER a complete GDAL geotransform tuple, with the number of rows and columns, OR a grid specified with all the extent limits and the pixel resolutions (xRes and yRes). If the geotransform is given, then the xMin, xMax, xRes and so on are calculated from it.
- alignedWith(other)[source]¶
Returns True if self is aligned with other. This means that they represent the same grid, with different extents.
Alignment is checked within a small tolerance, so that exact floating point matches are not required. However, notionally it is possible to get a match which shouldn’t be. The tolerance is calculated as:
tolerance = 0.01 * pixsize / npix
and if a mis-alignment is <= tolerance, it is assumed to be zero. For further details, read the source code.
- static densifyInterval(x1, y1, x2, y2, n)[source]¶
Create a densely sampled sequence of points along an interval, given the end points of the interval, (x1, y1) and (x2, y2). The total number of points desired is n, including the given end points.
This is an internal utility function, used by reproject().
Returns a single array of shape (n, 2), with x in the first column.
- equalPixSize(other)[source]¶
Returns True if pixel size of self is equal to that of other. Currently only checks absolute equality, probably should work out a tolerance.
- equalProjection(other)[source]¶
Returns True if the projection of self is the same as the projection of other
- equivalentProjection(otherspatialref, pixtolerance)[source]¶
Similar to equalProjection but does a less accurate test by checking converting coordinates from projection of self to otherspatialref and checking they are within pixtolerance pixels of each other. The coordinates used for this are the four corners and centre of image.
- getDimensions()[source]¶
Utility method which returns the number of rows and columns in the grid. Returns a tuple:
(nrows, ncols)
calculated from the min/max/res values.
- static getNumPix(gridMax, gridMin, gridRes)[source]¶
Works out how many pixels lie between the given min and max, at the given resolution. This is for internal use only.
- isComparable(other)[source]¶
Checks whether self is comparable with other. Returns True or False. Grids are comparable if they have equal pixel size, and the same projection.
- reproject(targetGrid, snap=True)[source]¶
Returns a new instance which is the reprojection of self to be in the same projection and pixel size as targetGrid. By default, the new coordinates are snapped to a multiple of the target pixel size, but this can be disabled with snap=False.
The bounding box is created by reprojecting the densely-sampled edges, so that the extent will fully cover reprojections which curve an edge outwards from the source bounding box. For small extents this is usually trivial, but for large extents it can become important (new in 2.1.0, previously only corners were used).
The edge curves are sampled at many points along each edge, which obviously does not absolutely guarantee the fullest extent. Ideally one would do a gradient search to find the local extreme point along the edge, but I was not convinced that there could never be multiple local extremes, depending on the combination of projections and extents involved, so I concluded that a fairly dense sampling was sufficiently good. However, it should be noted that a large region, with a suitable combination of projections, may still clip off small parts of the bounding box.
- static roundAway(x)[source]¶
Simulates Python 2 round behavour This is what we want as it rounds away from 0. The decimal module seems to be the only way to do this properly
- static snapToGrid(val, valOnGrid, res)[source]¶
Returns the nearest value to val which is a whole multiple of res away from valOnGrid, so that val is effectively on the same grid as valOnGrid. This is for internal use only.
- rios.pixelgrid.findCommonRegion(gridList, refGrid, combine=0)[source]¶
Returns a PixelGridDefn for the combination of all the grids in the given gridList. The output grid is in the same coordinate system as the reference grid.
The combine parameter controls whether UNION, INTERSECTION or BOUNDS_FROM_REFERENCE is performed.
- rios.pixelgrid.pixelGridFromFile(filename)[source]¶
Create a PixelGridDefn object for the given image file
- rios.pixelgrid.removeSurrounding(gridList)[source]¶
Check a list of PixelGridDefn objects for any which completely surround all of the others. If that is true, the outer one can never contribute to an intersection calculation, so it is removed.
The check is done in geographic coordinates (i.e. lat/long), because any other projection can be validly transformed into this.
Return a new list with all such outer pixgrids removed.