leaf_engine.utils

Submodules

Attributes

DISTANCE_UNITS

EARTH_RADIUS_IN_MILES

GEOMETRY_ROUNDING_PRECISION

H3_POLYFILL_BUFFER

H3_RESOLUTION

Functions

geometry_to_h3_indices(→ Iterable)

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.

get_ulid(→ str)

Returns ULID for arbitrary set of arguments.

get_uuid(→ str)

Generates a random UUID4. lru_cache decorator makes calls with the same

h3_index_to_geometry(→ Union[str, ...)

h3_index_to_lat_lon(→ Tuple[float, float])

h3_indices_to_geometry(→ Union[str, ...)

Converts a set of H3 indices to shapely Polygon or WKT. Raises an exception

haversine(a, b)

Computes the great circle distance between two points.

is_uuid(→ bool)

Returns True if value is valid UUID.

lat_lon_to_geometry(→ str)

lat_lon_to_h3_index(→ str)

mode(→ Any)

Returns the mode of a Series as a single value.

points_to_geometry(→ shapely.geometry.Polygon)

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

polygon_to_h3_indices(polygon[, resolution])

round_geometry(→ str)

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

union_geometries(→ str)

Package Contents

leaf_engine.utils.geometry_to_h3_indices(geometry: str | shapely.geometry.Polygon, resolution: int = H3_RESOLUTION) Iterable
Parameters:
  • geometry (Union[str, shapely.geometry.Polygon]) –

  • resolution (int) –

Return type:

Iterable

leaf_engine.utils.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.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.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.get_ulid(*args) str

Returns ULID for arbitrary set of arguments.

ULIDs are computed as a timestamp encoding + some random bytes. See here for more info: https://blog.daveallie.com/ulid-primary-keys. Calling this function with the same arguments returns the same ULID value.

Attempts to implement ULID as currently implemented in Postgres - see here: https://bitbucket.org/leaflogistics/migrations/src/production/archive/setup/extensions/generate_ulid.sql

``` SELECT (lpad(to_hex(floor(extract(epoch FROM clock_timestamp()) * 1000)::bigint), 12, ‘0’) ||

encode(gen_random_bytes(10), ‘hex’))::uuid;

```

Returns:

ULID string.

Return type:

str

leaf_engine.utils.get_uuid(*args) str

Generates a random UUID4. lru_cache decorator makes calls with the same args return the same UUID. Args must be hashable.

>>> get_uuid(1, "a")
>>> df[column].apply(get_uuid, 'RANDOM_ARG')
Return type:

str

leaf_engine.utils.h3_index_to_geometry(h3_index: str, as_string=False) str | shapely.geometry.Polygon
Parameters:

h3_index (str) –

Return type:

Union[str, shapely.geometry.Polygon]

leaf_engine.utils.h3_index_to_lat_lon(h3_index: str) Tuple[float, float]
Parameters:

h3_index (str) –

Return type:

Tuple[float, float]

leaf_engine.utils.h3_indices_to_geometry(indices: Iterable, as_string: bool = False) str | shapely.geometry.Polygon

Converts a set of H3 indices to shapely Polygon or WKT. Raises an exception if the conversion produces a MultiPolygon instead of contiguous Polygon.

Parameters:
  • indices (Iterable) – Set of H3 indices.

  • as_string (bool, optional) – Return WKT instead of Polygon object. Defaults to False.

Returns:

Polygon or WKT.

Return type:

Union[str, Polygon]

leaf_engine.utils.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.is_uuid(value: str | uuid.UUID, version=4) bool

Returns True if value is valid UUID.

Parameters:
  • value (Union[str, uuid.UUID]) – String or UUID object.

  • version (int, optional) – UUID version. Defaults to 4.

Returns:

Value is UUID.

Return type:

bool

leaf_engine.utils.lat_lon_to_geometry(lat: float, lon: float) str
Parameters:
Return type:

str

leaf_engine.utils.lat_lon_to_h3_index(lat: float, lon: float, resolution=H3_RESOLUTION) str
Parameters:
Return type:

str

leaf_engine.utils.mode(series: pandas.Series) Any

Returns the mode of a Series as a single value.

Parameters:

series (pd.Series) – Series of values.

Returns:

Most frequent value, if multiple values have the same frequency, the first one in natural sorting order.

Return type:

Any

leaf_engine.utils.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.polygon_to_h3_indices(polygon: shapely.geometry.Polygon, resolution=H3_RESOLUTION)
Parameters:

polygon (shapely.geometry.Polygon) –

leaf_engine.utils.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.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.DISTANCE_UNITS = 'miles'
leaf_engine.utils.EARTH_RADIUS_IN_MILES = 3961.0
leaf_engine.utils.GEOMETRY_ROUNDING_PRECISION = 5
leaf_engine.utils.H3_POLYFILL_BUFFER
leaf_engine.utils.H3_RESOLUTION = 5