This module contains functionality for subsetting a large segmented image into a smaller one for checking etc. See subsetImage().

exception pyshepseg.subset.PyShepSegSubsetError
pyshepseg.subset.copyColumns(inRat, outRat)

Copy column structure from inRat to outRat. Note that this just creates the empty columns in outRat, it does not copy any data.

inRat, outRatgdal.RasterAttributeTable

Columns found in inRat are created on outRat


Number of integer columns found


Number of float columns found

pyshepseg.subset.copySubsettedSegmentsToNew(inPage, outPagedRat, recodeDict)

Using the recodeDict, copy across the rows inPage to outPage.

inPage is processed and (taking into account of inPage.startSegId) the original input row found. This value is then looked up in recodeDict to find the row in the output RAT to copy the row from the input to.


A page of RAT from the input file


In-memory pages of the output RAT, as created by createPagedRat(). This is modified in-place, creating new pages as required.


Keyed by original segment ID, values are the corresponding segment IDs in the subset

pyshepseg.subset.processSubsetTile(tile, recodeDict, histogramDict, maskData)

Process a tile of the subset area. Returns a new tile with the new codes. Fills in the recodeDict as it goes and also updates histogramDict.

tileshepseg.SegIdType ndarray (tileNrows, tileNcols)

Input tile of segment IDs


Keyed by original segment ID, values are the corresponding segment IDs in the subset


Histogram counts in the subset, keyed by new segment ID

maskDataNone or int ndarray (tileNrows, tileNcols)

If not None, then is a raster mask. Only pixels which are non-zero in the mask will be included in the subset

outDatashepseg.SegIdType ndarray (tileNrows, tileNcols)

Recoded copy of the input tile.

pyshepseg.subset.readColDataIntoPage(page, data, idx, colType, minVal)

Numba function to quickly read a column returned by rat.ReadAsArray() info a RatPage.

pyshepseg.subset.readRATIntoPage(rat, numIntCols, numFloatCols, minVal, maxVal)

Create a new RatPage() object that represents the section of the RAT for a tile of an image. The part of the RAT between minVal and maxVal is read in and a RatPage() instance returned with the startSegId param set to minVal.

pyshepseg.subset.setHistogramFromDictionary(dictn, histArray)

Given a dictionary of pixel counts keyed on index, write these values to the array.

pyshepseg.subset.setSubsetRecodeFromDictionary(dictn, array)

Given the recodeDict write the original values to the array at the new indices.

pyshepseg.subset.subsetImage(inname, outname, tlx, tly, newXsize, newYsize, outformat, creationOptions=[], origSegIdColName=None, maskImage=None)

Subset an image and “compress” the RAT so only values that are in the new image are in the RAT. Note that the image values will be recoded in the process.

gdal_translate seems to have a problem with files that have large RAT’s so while that gets fixed do the subsetting in this function.


Filename of input raster


Filename of output raster

tlx, tlyint

The x & y pixel coordinates (i.e. col & row, respectively) of the top left pixel of the image subset to extract.

newXsize, newYsizeint

The x & y size (in pixels) of the image subset to extract


The GDAL short name of the format driver to use for the output raster file

creationOptionslist of str

The GDAL creation options for the output file

origSegIdColNamestr or None

The name of a RAT column. If not None, this will be created in the output file, and will contain the original segment ID numbers so the new segment IDs can be linked back to the old

maskImagestr or None

If not None, then the filename of a mask raster. Only pixels which are non-zero in this mask image will be included in the subset. This image is assumed to match the shape and position of the output subset

pyshepseg.subset.writeCompletedPagesForSubset(inRAT, outRAT, outPagedRat)

For the subset operation. Writes out any completed pages to outRAT using the inRAT as a template.

inRAT, outRATgdal.RasterAttributeTable

The input and output raster attribute tables.


The paged RAT in memory, as created by createPagedRat()