leaf_engine.utils
Submodules
Attributes
Functions
|
|
|
Returns a lat-lon tuple from a geometry string. |
|
Computes the maximum distance between any two points in a geometry string. |
|
Computes the maximum distance between any two points in a Polygon. |
|
Returns ULID for arbitrary set of arguments. |
|
Generates a random UUID4. lru_cache decorator makes calls with the same |
|
|
|
|
|
Converts a set of H3 indices to shapely Polygon or WKT. Raises an exception |
|
Computes the great circle distance between two points. |
|
Returns True if value is valid UUID. |
|
|
|
|
|
Returns the mode of a Series as a single value. |
|
Converts lat-lon tuples to (x, y) euclidean coordinate pairs. |
|
|
|
Rounds geometry coordinates to 5 decimal places. This reduces the size of |
|
Package Contents
- leaf_engine.utils.geometry_to_h3_indices(geometry: str | shapely.geometry.Polygon, resolution: int = H3_RESOLUTION) Iterable
- leaf_engine.utils.geometry_to_lat_lon(geometry_string: str) tuple
Returns a lat-lon tuple from a geometry string.
- leaf_engine.utils.get_geometry_diameter(geometry: str) float
Computes the maximum distance between any two points in a geometry string.
- 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:
- 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:
- 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:
- leaf_engine.utils.h3_index_to_geometry(h3_index: str, as_string=False) str | shapely.geometry.Polygon
- 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.
- 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.
- 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.
- leaf_engine.utils.union_geometries(geometries: List[str], rounding_precision: int = GEOMETRY_ROUNDING_PRECISION) 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