leaf_engine.utils.shapely

This module contains various pure functions for manipulating shapely geometries.

Attributes

DISTANCE_UNITS

EARTH_RADIUS_IN_MILES

GEOMETRY_ROUNDING_PRECISION

Functions

geojson_to_wkt(→ str)

Converts a geojson object to a shapely Polygon wkt.

geometry_to_lat_lon(→ tuple)

Returns a lat-lon tuple from a geometry string.

get_geometry_diameter(→ float)

Computes the maximum distance between any two points in a geometry string.

get_polygon_diameter(→ float)

Computes the maximum distance between any two points in a Polygon.

haversine(a, b)

Computes the great circle distance between two points.

points_to_geometry(→ shapely.geometry.Polygon)

Converts lat-lon tuples to (x, y) euclidean coordinate pairs.

round_geometry(→ str)

Rounds geometry coordinates to 5 decimal places. This reduces the size of

union_geometries(→ str)

Module Contents

leaf_engine.utils.shapely.geojson_to_wkt(geojson: dict) str

Converts a geojson object to a shapely Polygon wkt.

Parameters:

geojson (dict) – Geojson object.

Returns:

Shapely Polygon wkt.

Return type:

str

leaf_engine.utils.shapely.geometry_to_lat_lon(geometry_string: str) tuple

Returns a lat-lon tuple from a geometry string.

Parameters:

geometry_string (str) – Geometry string.

Returns:

Lat-lon tuple.

Return type:

tuple

leaf_engine.utils.shapely.get_geometry_diameter(geometry: str) float

Computes the maximum distance between any two points in a geometry string.

Parameters:

geometry (str) – Shapely Polygon string.

Returns:

Maximum distance between geometry points.

Return type:

float

leaf_engine.utils.shapely.get_polygon_diameter(polygon: shapely.geometry.Polygon) float

Computes the maximum distance between any two points in a Polygon.

Parameters:

polygon (Polygon) – Shapely polygon.

Returns:

Maximum distance between polygon points.

Return type:

float

leaf_engine.utils.shapely.haversine(a, b)

Computes the great circle distance between two points.

Parameters:
  • a – [lon,lat]

  • b – [lon,lat]

Returns:

Distance in miles between a,b

leaf_engine.utils.shapely.points_to_geometry(lat_lon_records: Iterable) shapely.geometry.Polygon

Converts lat-lon tuples to (x, y) euclidean coordinate pairs.

Parameters:

lat_lon_records (Iterable) – Iterable of lat-lon tuples.

Returns:

Convex hull of polygon described by points.

Return type:

Polygon

leaf_engine.utils.shapely.round_geometry(geometry_string: str, rounding_precision: int = GEOMETRY_ROUNDING_PRECISION) str

Rounds geometry coordinates to 5 decimal places. This reduces the size of geometry strings and makes them usable as spatial indices when comparing them to geometries returned by the analytics API. This is necessary because geometries returned by the API have lower precision than the geometries we work with here.

Note that this does NOT simplify the geometries (i.e., it does not reduce the number of points in the geometry), just reduces unnecessary precision in point coordinates.

The @lru_cache decorator is used to cache calls to this functions in-memory.

Parameters:
  • geometry_string (str) –

  • rounding_precision (int) –

Return type:

str

leaf_engine.utils.shapely.union_geometries(geometries: List[str], rounding_precision: int = GEOMETRY_ROUNDING_PRECISION) str
Parameters:
  • geometries (List[str]) –

  • rounding_precision (int) –

Return type:

str

leaf_engine.utils.shapely.DISTANCE_UNITS = 'miles'
leaf_engine.utils.shapely.EARTH_RADIUS_IN_MILES = 3961.0
leaf_engine.utils.shapely.GEOMETRY_ROUNDING_PRECISION = 5