geo_adjacency package

Submodules

geo_adjacency.adjacency module

The adjacency module implements the AdjacencyEngine class, which allows us to calculate adjacency relationships. Adjacency relationships are between a set of source geometries, or between source geometries and a second set of target geometries. Obstacle geometries can be passed in to stand between sources or sources and targets, but they are not included in the output.

For example, if we wanted to know what trees in a forest are adjacent to the shore of a lake, we could pass in a set of Point geometries to the trees, a Polygon to represent the lake, and a LineString to represent a road passing between some of the trees and the shore.

AdjacencyEngine utilizes a Voronoi diagram of all the vertices in all the geometries combined to determine which geometries are adjacent to each other. See

class geo_adjacency.adjacency.AdjacencyEngine(source_geoms: List[shapely.geometry.base.BaseGeometry], target_geoms: List[shapely.geometry.base.BaseGeometry] | None = None, obstacle_geoms: List[shapely.geometry.base.BaseGeometry] | None = None, **kwargs)

Bases: object

A class for calculating the adjacency of a set of geometries to another geometry or set of geometries, given a set of obstacles, within a given radius.

First, the Voronoi diagram is generated for each geometry and obstacle. Then, we check which voronoi shapes intersect one another. If they do, then the two underlying geometries are adjacent.

property all_coordinates

All source, target, and obstacle coordinates in a single list. The order of this list must not be changed. This property cannot be set manually.

Returns:

A list of coordinate tuples.

Return type:

List[tuple[float, float]]

property all_features

All source, target, and obstacle features in a single list. The order of this list must not be changed. This property cannot be set manually.

Returns:

A list of _Features.

Return type:

List[_Feature]

get_adjacency_dict() Dict[int, List[int]]

Returns a dictionary of indices. They keys are the indices of feature_geoms. The values are the indices of any target geometries which are adjacent to the feature_geoms.

If no targets were specified, then calculate adjacency between source features and other source features.

Returns:

A dictionary of indices. The keys are the indices of feature_geoms. The values are the indices of any adjacent features.

Return type:

dict

get_feature_from_coord_index(coord_index: int) _Feature

A list which is the length of self._all_coordinates. For each coordinate, we add the index of the corresponding feature from the list self.all_features. This is used to determine which coordinate belongs to which feature after we calculate the voronoi diagram.

Parameters:

coord_index (int) – The index of the coordinate in self._all_coordinates

Returns:

A _Feature at the given index.

Return type:

_Feature

property obstacle_features: Tuple[_Feature]

Features which can prevent source and target features from being adjacent. They Do not participate in the adjacency_dict.

Returns:

A list of _Features.

Return type:

List[_Feature]

plot_adjacency_dict() None

Plot the adjacency linkages between the source and target with pyplot. Runs the analysis if it has not already been run.

Returns:

None

property source_features: Tuple[_Feature]

Features which will be the keys in the adjacency_dict.

Returns:

A list of _Features.

Return type:

List[_Feature]

property target_features: Tuple[_Feature]

Features which will be the values in the adjacency_dict. :returns: A list of _Features. :rtype: List[_Feature]

property vor

The Voronoi diagram object returned by Scipy. Useful primarily for debugging an adjacency analysis.

Returns:

The Scipy Voronoi object.

Return type:

scipy.spatial.Voronoi

geo_adjacency.exception module

Custom exceptions.

exception geo_adjacency.exception.ImmutablePropertyError(message)

Bases: BaseException

Raise when a property is immutable because the setter does nothing.

geo_adjacency.utils module

Utility functions. These are designed for use in the AdjacencyEngine only, and should not be called by end users.

geo_adjacency.utils.add_geometry_to_plot(geoms, color='black')

When updating the test data, it may be useful to visualize it. Add a geometry to the global maplotlib plt object. The next time we call plt.show(), this geometry will be plotted.

Parameters:
  • geoms (List[BaseGeometry]) – A list of Shapely geometries.

  • color (str) – The color we want the geometry to be in the plot.

Returns:

None

geo_adjacency.utils.coords_from_multipolygon(multipolygon: shapely.MultiPolygon) List[Tuple[float, float]]

Convert a MultiPolygon into a list of (x, y) tuples. Does not repeat the first coordinate to close the ring.

Parameters:

multipolygon (MultiPolygon) – A Shapely MultiPolygon.

Returns:

A list of coordinate tuples.

Return type:

List[Tuple[float, float]]

geo_adjacency.utils.coords_from_point(point: shapely.Point) List[Tuple[float, float]]

Convert a Point into a tuple of (x, y). We put this inside a list for consistency with other coordinate methods to allow us to seamlessly merge them later.

Parameters:

point (Point) – A Shapely Point.

Returns:

A list of coordinate tuples.

Return type:

List[Tuple[float, float]]

geo_adjacency.utils.coords_from_polygon(polygon: shapely.Polygon) List[Tuple[float, float]]

Convert a Polygon into a list of (x, y) tuples. Does not repeat the first coordinate to close the ring.

Parameters:

polygon (Polygon) – A Shapely Polygon.

Returns:

A list of coordinate tuples.

Return type:

List[Tuple[float, float]]

geo_adjacency.utils.coords_from_ring(ring: shapely.LineString) List[Tuple[float, float]]

Convert a LinearRing into a list of (x, y) tuples.

Parameters:

ring (LineString) – A Shapely LinearString.

Returns:

A list of coordinate tuples.

Return type:

List[Tuple[float, float]]

geo_adjacency.utils.flatten_list(nested_list) List

Flatten a list of lists. :param nested_list: A list of lists. :type nested_list: List

Returns:

Module contents