pyoints.grid package

Submodules

pyoints.grid.grid module

Handling of grided data like voxels or rasters.

class pyoints.grid.grid.Grid[source]

Bases: pyoints.georecords.GeoRecords

Grid class extends GeoRecords to ease handling of matrices like rasters or voxels.

Parameters:
  • proj (pyoints.projection.Proj) – Projection object provides the geograpic projection of the grid.
  • rec (np.recarray) – Multidimensional array of objects. Each cell of the matrix represents a geo-object with k dimensional coordinates.
  • T (array_like(Number, shape=(k+1, k+1))) – A linear transformation matrix to transform the coordinates. The translation represents the origin, the rotation represents the orientation, and the scale represents the pixel size of the matrix.

Examples

>>> from pyoints import (
...     transformation,
...     projection,
... )

Create a raster with a projection and a transformation matrix.

>>> proj = projection.Proj()
>>> data = np.recarray((4, 3), dtype=[('values', int)])
>>> data['values'] = np.arange(np.product(data.shape)).reshape(data.shape)
>>> T = transformation.matrix(t=[10, 20], s=[0.5, 0.4], order='rst')
>>> raster = Grid(proj, data, T)
>>> print(sorted(raster.dtype.names))
['coords', 'values']
>>> print_rounded(raster.shape)
(4, 3)
>>> print_rounded(raster.dim)
2
>>> print_rounded(raster.t.origin)
[ 10.  20.]

Get cell data of the raster.

>>> print_rounded(raster.coords)
[[[ 10.25  20.2 ]
  [ 10.75  20.2 ]
  [ 11.25  20.2 ]]
<BLANKLINE>
 [[ 10.25  20.6 ]
  [ 10.75  20.6 ]
  [ 11.25  20.6 ]]
<BLANKLINE>
 [[ 10.25  21.  ]
  [ 10.75  21.  ]
  [ 11.25  21.  ]]
<BLANKLINE>
 [[ 10.25  21.4 ]
  [ 10.75  21.4 ]
  [ 11.25  21.4 ]]]
>>> print_rounded(raster.values)
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]

Convert coordinates to indices and reverse.

>>> print_rounded(raster.coords_to_keys(raster.t.origin))
[0 0]
>>> print_rounded(raster.keys_to_coords([-0.5, -0.5]))
[ 10.  20.]
>>> print_rounded(raster.coords_to_keys(raster.coords))
[[[0 0]
  [0 1]
  [0 2]]
<BLANKLINE>
 [[1 0]
  [1 1]
  [1 2]]
<BLANKLINE>
 [[2 0]
  [2 1]
  [2 2]]
<BLANKLINE>
 [[3 0]
  [3 1]
  [3 2]]]
>>> print_rounded(raster.keys_to_coords(raster.keys))
[[[ 10.25  20.2 ]
  [ 10.75  20.2 ]
  [ 11.25  20.2 ]]
<BLANKLINE>
 [[ 10.25  20.6 ]
  [ 10.75  20.6 ]
  [ 11.25  20.6 ]]
<BLANKLINE>
 [[ 10.25  21.  ]
  [ 10.75  21.  ]
  [ 11.25  21.  ]]
<BLANKLINE>
 [[ 10.25  21.4 ]
  [ 10.75  21.4 ]
  [ 11.25  21.4 ]]]

Usage of the spatial index.

>>> dists, indices = raster.indexKD().knn(raster.t.origin, k=5)
>>> print_rounded(dists)
[ 0.32  0.65  0.78  0.96  1.03]
>>> print_rounded(indices)
[0 3 1 4 6]
>>> print_rounded(raster.indices_to_keys(indices))
[[0 0]
 [1 0]
 [0 1]
 [1 1]
 [2 0]]

See also

GeoRecords

coords_to_coords(coords)[source]

Sets coordinate to closest grid coordinate.

See also

poynts.grid.coords_to_coords()

coords_to_keys(coords)[source]

Converts coordinates to cell indices.

See also

poynts.grid.coords_to_keys()

corners
classmethod from_corners(proj, corners, scale)[source]

Alternative constructor using grid corners.

Parameters:
  • proj (pyoints.projection.Proj) – Projection object provides the geographic projection of the grid.
  • corners (array_like(Number, shape=(n, k))) – Desired k dimensional corners of the grid.
  • scale (array_like(int, shape=(k))) – Desired scale of the pixel cells.

Examples

Derive a raster form corners with a positive scale.

>>> corners = [(1, 2), (4, 2), (4, 10), (1, 10)]
>>> scale = [1, 2]
>>> raster = Grid.from_corners(projection.Proj(), corners, scale)
>>> print_rounded(raster.shape)
(4, 3)
>>> print(sorted(raster.dtype.descr))
[('coords', '<f8', (2,))]
>>> print_rounded(raster.t)
[[ 1.  0.  1.]
 [ 0.  2.  2.]
 [ 0.  0.  1.]]
>>> print_rounded(raster.coords)
[[[ 1.5  3. ]
  [ 2.5  3. ]
  [ 3.5  3. ]]
<BLANKLINE>
 [[ 1.5  5. ]
  [ 2.5  5. ]
  [ 3.5  5. ]]
<BLANKLINE>
 [[ 1.5  7. ]
  [ 2.5  7. ]
  [ 3.5  7. ]]
<BLANKLINE>
 [[ 1.5  9. ]
  [ 2.5  9. ]
  [ 3.5  9. ]]]
>>> print_rounded(raster.corners)
[[  1.   2.]
 [  4.   2.]
 [  4.  10.]
 [  1.  10.]]
>>> keys = raster.t.to_global(corners)
>>> print_rounded(keys)
[[ 0.  0.]
 [ 3.  0.]
 [ 3.  4.]
 [ 0.  4.]]
>>> print_rounded(raster.t.to_local(keys))
[[  1.   2.]
 [  4.   2.]
 [  4.  10.]
 [  1.  10.]]

Example with inverted axes.

>>> from pyoints import transformation
>>> corners = [(1, 1), (3, 1), (3, 4), (1, 4)]
>>> raster = Grid.from_corners(projection.Proj(), corners, [-0.5, -1])
>>> print_rounded(raster.shape)
(3, 4)
>>> print_rounded(raster.records().coords.min(0))
[ 1.25  1.5 ]
>>> print_rounded(raster.records().coords.max(0))
[ 2.75  3.5 ]
>>> t, r, s, det = transformation.decomposition(raster.t)
>>> print_rounded(t)
[ 3.  4.]
>>> print_rounded(s)
[ 0.5  1. ]
>>> print_rounded(raster.coords)
[[[ 2.75  3.5 ]
  [ 2.25  3.5 ]
  [ 1.75  3.5 ]
  [ 1.25  3.5 ]]
<BLANKLINE>
 [[ 2.75  2.5 ]
  [ 2.25  2.5 ]
  [ 1.75  2.5 ]
  [ 1.25  2.5 ]]
<BLANKLINE>
 [[ 2.75  1.5 ]
  [ 2.25  1.5 ]
  [ 1.75  1.5 ]
  [ 1.25  1.5 ]]]
>>> print_rounded(raster.corners)
[[ 3.  4.]
 [ 1.  4.]
 [ 1.  1.]
 [ 3.  1.]]
>>> keys = raster.t.to_global(corners)
>>> print_rounded(keys)
[[ 4.  3.]
 [ 0.  3.]
 [ 0.  0.]
 [ 4.  0.]]
>>> print_rounded(raster.t.to_local(keys))
[[ 1.  1.]
 [ 3.  1.]
 [ 3.  4.]
 [ 1.  4.]]
classmethod from_extent(proj, ext, scale)[source]

Alternative constructor using grid corners.

Parameters:
  • proj (pyoints.projection.Proj) – Projection object provides the geographic projection of the grid.
  • ext (array_like(Number, shape=(2 * k)) or array_like(Number, shape=(n, k))) – Desired k dimensional extent of the gird. You can also specify the extent by providing coordinates.
  • scale (array_like(int, shape=(k))) – Desired scale of the pixel cells.

Examples

Defining a raster using a two dimensional extent.

>>> extent = [1, 1, 3, 4]
>>> raster = Grid.from_extent(projection.Proj(), extent, [0.5, 1])
>>> print_rounded(raster.shape)
(3, 4)
>>> print(sorted(raster.dtype.descr))
[('coords', '<f8', (2,))]
>>> print_rounded(raster.coords)
[[[ 1.25  1.5 ]
  [ 1.75  1.5 ]
  [ 2.25  1.5 ]
  [ 2.75  1.5 ]]
<BLANKLINE>
 [[ 1.25  2.5 ]
  [ 1.75  2.5 ]
  [ 2.25  2.5 ]
  [ 2.75  2.5 ]]
<BLANKLINE>
 [[ 1.25  3.5 ]
  [ 1.75  3.5 ]
  [ 2.25  3.5 ]
  [ 2.75  3.5 ]]]
indices_to_keys(indices)[source]

Converts cell indices to keys.

See also

poynts.grid.indices_to_keys()

keys_to_coords(keys)[source]

Converts cell indices to coordinates.

See also

poynts.grid.keys_to_coords()

keys_to_indices(keys)[source]

Converts grid keys to indices.

See also

poynts.grid.keys_to_indices()

transform(T)[source]

Transforms coordinates.

Parameters:T (array_like(Number, shape=(self.dim+1, self.dim+1))) – Transformation matrix to apply.
Returns:
Return type:self

Notes

Overwrites GeoRecords.transform.

See also

GeoRecords.transform()

voxelize(rec, **kwargs)[source]

Converts a point cloud to a voxel or raster.

Parameters:
  • rec (np.recarray(shape=(n, ))) – Numpy record array of n points to voxelize. It requires the two dimensional field ‘coords’ associated with k dimensional coordinates.
  • **kwargs – Arguments passed to voxelize.

See also

voxelize()

Examples

>>> from pyoints import (
...     transformation,
...     projection,
... )

Create Grid.

>>> T = transformation.matrix(s=(2.5, 3), t=[1, 0])
>>> dtype = [('points', np.recarray)]
>>> proj = projection.Proj()
>>> grid = Grid(proj, np.recarray((3, 4), dtype=dtype), T=T)
>>> print_rounded(grid.shape)
(3, 4)
>>> print_rounded(grid.coords)
[[[ 2.25  1.5 ]
  [ 4.75  1.5 ]
  [ 7.25  1.5 ]
  [ 9.75  1.5 ]]
<BLANKLINE>
 [[ 2.25  4.5 ]
  [ 4.75  4.5 ]
  [ 7.25  4.5 ]
  [ 9.75  4.5 ]]
<BLANKLINE>
 [[ 2.25  7.5 ]
  [ 4.75  7.5 ]
  [ 7.25  7.5 ]
  [ 9.75  7.5 ]]]
>>> print_rounded(grid.t.origin)
[ 1.  0.]

Create records to voxelize.

>>> coords = [(0, 0), (1, 0.5), (2, 2), (4, 6), (3, 2), (1, 5), (3, 5)]
>>> rec = np.recarray(len(coords), dtype=[('coords', float, 2)])
>>> rec['coords'] = coords

Voxelize the records and update the raster.

>>> voxels = grid.voxelize(rec)
>>> grid['points'] = voxels
>>> print_rounded(grid['points'][0, 0].coords)
[[ 0.   0. ]
 [ 1.   0.5]
 [ 2.   2. ]
 [ 3.   2. ]]
window_by_extent(extent)[source]

Gets a subset of the grid within a given extent.

Parameters:extent (Extent) – Extent defining the window corners.
Returns:Desired subset.
Return type:Grid

Examples

>>> from pyoints import transformation

Create Grid.

>>> T = transformation.matrix(s=(1, 2), t=[1, 0])
>>> proj = projection.Proj()
>>> grid = Grid(proj, np.recarray((4, 5), dtype=[]), T=T)
>>> print_rounded(grid.extent())
[ 1.5  1.   5.5  7. ]

Select subset by extent.

>>> subset = grid.window_by_extent([3, 2, 7, 6])
>>> print_rounded(subset.shape)
(2, 3)
>>> print_rounded(subset.coords)
[[[ 3.5  3. ]
  [ 4.5  3. ]
  [ 5.5  3. ]]
<BLANKLINE>
 [[ 3.5  5. ]
  [ 4.5  5. ]
  [ 5.5  5. ]]]
pyoints.grid.grid.voxelize(rec, T, shape=None, agg_func=None, dtype=None)[source]

Aggregates a point cloud to a voxel or raster.

Parameters:
  • rec (np.recarray(shape=(n, )) or GeoRecords) – Numpy record array of n points to voxelize. It requires a two dimensional field ‘coords’ associated with k dimensional coordinates.
  • T (array_like(Number, shape=(m+1, m+1))) – Transformation matrix defining the m <= k dimensional voxel system.
  • shape (optional, array_like(int, shape=(m))) – Shape of the output voxel space. If None, shape is fit to rec.coords.
  • agg_func (optional, callable) – Function to aggregate the record array. If None, agg_func set to lambda ids: rec[ids].
  • dtype (optional, np.dtype) – Output data type. If None, set to automatically.
Returns:

Desired m dimensional matrix. If rec is an instance of GeoRecords and dtype has named fields, an instance of Grid is returned. If no point falls within T, None is returned.

Return type:

np.ndarray or np.recarray(dtype=dtype) or Grid

See also

Grid(), np.apply_function()

Examples

>>> from pyoints import transformation

Create record array with coordinates.

>>> coords = [(0, 0), (1, 0.5), (2, 2), (4, 6), (3, 2), (1, 5), (3, 0)]
>>> rec = np.recarray(len(coords), dtype=[('coords', float, 2)])
>>> rec['coords'] = coords

Voxelize records.

>>> T = transformation.matrix(s=(2.5, 3), t=[0, 0])
>>> grid = voxelize(rec, T)
>>> print(grid.dtype)
object
>>> print_rounded(grid.shape)
(3, 2)
>>> print_rounded(grid[0, 0].coords)
[[ 0.   0. ]
 [ 1.   0.5]
 [ 2.   2. ]]

Voxelize with aggregation function.

>>> dtype = [('points', type(rec)), ('count', int)]
>>> agg_func = lambda ids: (rec[ids], len(ids))
>>> grid = voxelize(rec, T, agg_func=agg_func, dtype=dtype)
>>> print(sorted(grid.dtype.names))
['count', 'points']
>>> print_rounded(grid.shape)
(3, 2)
>>> print_rounded(grid.count)
[[3 2]
 [1 0]
 [0 1]]
>>> print_rounded(grid[0, 0].points.coords)
[[ 0.   0. ]
 [ 1.   0.5]
 [ 2.   2. ]]

Voxelize three dimensional coordinates to receive a two dimensional raster.

>>> coords = [(0, 0, 1), (-2, 0.3, 5), (2, 2, 3), (4, 6, 2), (3, 2, 1)]
>>> rec = np.recarray(len(coords), dtype=[('coords', float, 3)])
>>> rec['coords'] = coords
>>> T = transformation.matrix(s=(2.5, 3), t=[0, 0])
>>> grid = voxelize(rec, T)
>>> print_rounded(grid.shape)
(3, 2)
>>> print_rounded(grid[0, 0].coords)
[[ 0.   0.   1. ]
 [-2.   0.3  5. ]
 [ 2.   2.   3. ]]

pyoints.grid.transformation module

Conversion of coordinates to cell indices and reverse by applying transformations.

pyoints.grid.transformation.coords_to_coords(T, coords)[source]

Aligns coordinates with a raster grid.

Parameters:
  • T (array_like(Number, shape=(k+1, k+1))) – The transformation matrix of a k dimensional raster.
  • coords (array_like(Number, shape=(n, k))) – Coordinates to align with a raster grid.
Returns:

coords – Desired coordinates aligned with the grid.

Return type:

array_like(Number, shape=(n, k))

pyoints.grid.transformation.coords_to_keys(T, coords)[source]

Transforms coordinates to matrix indices.

Parameters:
  • T (array_like(Number, shape=(k+1,k+1))) – A linear transformation matrix to transform the coordinates. The translation represents the origin, the rotation represents the orientation and the scale represents the pixel size of a raster.
  • coords (array_like(Number, shape=(n, k))) – Coordinates with at least k dimensions to convert to indices.
Returns:

keys – Indices of the coordinates within the grid.

Return type:

np.ndarray(int, shape=(n, k))

pyoints.grid.transformation.corners_to_transform(corners, scale=None)[source]

Creates a transformation matrix using the corners of a raster.

Parameters:
  • corners (array_like(Number, shape=(2**k, k))) – Corners of a k dimensional grid in a k dimensional space.
  • scale (optional, array_like(Number, shape=(k))) – Optional scale to define the pixel resolution of a raster.
Returns:

  • T (np.matrix(Number, shape=(k+1, k+1))) – Desired transformation matrix.
  • shape (np.ndarray(int, shape=(k))) – Shape of a grid required to cover all corners.

Examples

Create some corners.

>>> T = transformation.matrix(t=[3, 5], s=[10, 20], r=np.pi/2)
>>> coords = Extent([np.zeros(2), np.ones(2)]).corners
>>> corners = transformation.transform(coords, T)
>>> print_rounded(corners)
[[  3.   5.]
 [  3.  15.]
 [-17.  15.]
 [-17.   5.]]

Create transformation matrix without scale.

>>> M, shape = corners_to_transform(corners)
>>> print_rounded(M)
[[ 0. -1.  3.]
 [ 1.  0.  5.]
 [ 0.  0.  1.]]
>>> print_rounded(shape)
[20 10]

Create transformation matrix with a scale.

>>> M, shape = corners_to_transform(corners, [0.5, 2])
>>> print_rounded(M)
[[ 0.  -2.   3. ]
 [ 0.5  0.   5. ]
 [ 0.   0.   1. ]]
>>> print_rounded(shape)
[10 20]
pyoints.grid.transformation.extentinfo(T, extent)[source]

Receives information on a raster subset with given boundaries.

Parameters:
  • T (array_like(Number, shape=(k+1,k+1))) – The transformation matrix of a k dimensional grid.
  • extent (array_like(Number, shape=(2 * k))) – Desired extent.
Returns:

  • T (array_like(Number, shape=(k+1,k+1))) – Extent of the original raster.
  • origin_key (array_like(int, shape=(k))) – Key or index of the origin of the new transformation matrix.
  • shape (np.array(int, shape=(k))) – Shape of the grid subset.

Examples

>>> T = transformation.matrix(t=[100, 200], s=[2, -2])
>>> ext = Extent([150, 250, 200, 300])
>>> M, min_corner_key, shape = extentinfo(T, ext)
>>> print_rounded(M)
[[   2.    0.  150.]
 [   0.   -2.  300.]
 [   0.    0.    1.]]
>>> print_rounded(min_corner_key)
[-50  25]
>>> print_rounded(shape)
[26 26]
pyoints.grid.transformation.indices_to_keys(indices, shape)[source]

Converts indices vector to keys of a matrix.

Parameters:
  • indices (array_like(int, shape=(n))) – Index vector to convert to matrix keys. Each element i specifies the i-th`element in the matrix.
  • shape (array_like(int, shape=(k))) – Shape of output matrix.
Returns:

Desired matrix keys associated with requested indices.

Return type:

np.ndarray(int, shape=shape)

pyoints.grid.transformation.keys_to_coords(T, keys)[source]

Converts indices of raster cells to coordinates.

Parameters:
  • T (array_like(Number, shape=(k+1,k+1))) – The transformation matrix of a k dimensional raster.
  • keys (array_like(int, shape=(n, k))) – Indices of n raster cells.
Returns:

coords – Desired coordinates of the raster cells.

Return type:

array_like(Number, shape=(n, k))

pyoints.grid.transformation.keys_to_indices(keys, shape)[source]

Converts matrix keys to indices.

Parameters:
  • keys (array_like(int)) – Keys of matrix.
  • shape (array_like(int, shape=(k))) – Shape of the input matrix.
Returns:

Desired indices vector associated with the requested keys.

Return type:

np.ndarray(int)

pyoints.grid.transformation.transform_to_corners(T, shape)[source]

Generates the corners of a grid based on a transformation matrix.

Parameters:
  • T (array_like(Number, shape=(k+1, k+1))) – Transformation matrix in a k dimensional space.
  • shape (array_like(int, shape=(k))) – Desired shape of the grid

Examples

>>> T = transformation.matrix(t=[10, 20], s=[0.5, 2])
>>> print_rounded(T)
[[  0.5   0.   10. ]
 [  0.    2.   20. ]
 [  0.    0.    1. ]]
>>> corners = transform_to_corners(T, (100, 200))
>>> print_rounded(corners)
[[  10.   20.]
 [ 110.   20.]
 [ 110.  220.]
 [  10.  220.]]
>>> print_rounded(coords_to_keys(T, corners))
[[  0   0]
 [  0 200]
 [100 200]
 [100   0]]

Module contents

Handling of grid objects like image rasters or voxels.