Common utility functions for dealing with grid spatial indices

pylidar.lidarformats.gridindexutils.CreateSpatialIndex(coordOne, coordTwo, binSize, coordOneMax, coordTwoMin, nRows, nCols, indexDtype, countDtype)[source]

Create a SPD grid spatial index given arrays of the coordinates of the elements.

This can then be used for writing a SPD V3/4 spatial index to file, or to re-bin data to a new grid.

Any elements outside of the new spatial index are ignored and the arrays returned will not refer to them.


  • coordOne is the coordinate corresponding to bin row.
  • coordTwo corresponds to bin col.
    Note that coordOne will always be reversed, in keeping with widespread conventions that a Y coordinate increases going north, but a grid row number increases going south. This same assumption will be applied even when the coordinates are not cartesian (e.g. angles).
  • binSize is the size (in world coords) of each bin. The V3/4 index definition
    requires that bins are square.
  • coordOneMax and coordTwoMin define the top left of the
    spatial index to be built. This is the world coordinate of the top-left corner of the top-left bin
  • nRows, nCols - size of the spatial index
  • indexDtype is the numpy dtype for the index (si_start, below)
  • countDtype is the numpy dtype for the count (si_count, below)


  • mask - a 1d array of bools of the valid elements. This must be applied
    before sortedBins.
  • sortedBins - a 1d array of indices that is used to
    re-sort the data into the correct order for using the created spatial index. Since the spatial index puts all elements in the same bin together this is a very important step!
  • si_start - a 2d array of start indexes into the sorted data (see
  • si_count - the count of elements in each bin.
pylidar.lidarformats.gridindexutils.SNAPMETHOD_GREATER = 2

Constant for use with snapToGrid. Snaps to greater grid value

pylidar.lidarformats.gridindexutils.SNAPMETHOD_LESS = 1

Constant for use with snapToGrid. Snaps to lesser grid value

pylidar.lidarformats.gridindexutils.SNAPMETHOD_NEAREST = 0

Constant for use with snapToGrid. Snaps to nearest grid value

pylidar.lidarformats.gridindexutils.convertSPDIdxToReadIdxAndMaskInfo(start_idx_array, count_array, outSize=None)[source]

Convert either a 2d SPD spatial index or 1d index (pulse to points, pulse to waveform etc) information for reading with h5py and creating a masked array with the indices into the read subset.


  • start_idx_array is the 2 or 1d input array of file start indices from SPD
  • count_array is the 2 or 1d input array of element counts from SPD
  • outSize is the size of the h5py dataset to be read. Set to None to not return the h5space.H5Space object


  • If outSize is not None, A h5space.H5Space object for reading and writing data.
  • A 3 or 2d (depending on if a 2 or 1 array was input) array containing indices into the new subset of the data. This array is arranged so that the first axis contains the indices for each bin (or pulse) and the other axis is the row (and col axis for 3d output) This array can be used to rearrange the data ready from h5py into a ragged array of the correct shape constaining the data from each bin.
  • A 3 or 2d (depending on if a 2 or 1 array was input) bool array that can be used as a mask in a masked array of the ragged array (above) of the actual data.
pylidar.lidarformats.gridindexutils.getSlicesForExtent(siPixGrid, siShape, overlap, xMin, xMax, yMin, yMax)[source]

xMin, xMax, yMin, yMax is the extent snapped to the pixGrid.

pylidar.lidarformats.gridindexutils.snapToGrid(val, valOnGrid, res, method)[source]

Snaps a coordinate (val) to a grid specified by one coord on that grid (valOnGrid). res is the pixel size of that grid and method is on of SNAPMETHOD_NEAREST, SNAPMETHOD_LESS or SNAPMETHOD_GREATER.