Skip to content

Helpers

Utility functions for working with altitude and coordinate data.

ALT

cell_to_shapely(h)

Converts an H3 ID into a Shapely Polygon

Parameters:

Name Type Description Default
h str

H3 cell ID

required

Returns:

Name Type Description
polygon Polygon

Polygon of cell boundary in lon,lat

Source code in src/aspiaspace/helpers/alt.py
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
def cell_to_shapely(h):
    """Converts an H3 ID into a Shapely Polygon

    Args:
        h (str): H3 cell ID

    Returns:
        polygon (shapely.geometry.Polygon): Polygon of cell boundary in lon,lat
    """
    b = h3.cell_to_boundary(h,)

    b = [item[::-1] for item in b]

    return Polygon(b)

Coordinates

format_areas(features)

Convert outputs of areas endpoint into GeoPandas GeoDataFrame

Parameters:

Name Type Description Default
features str

Output of areas endpoint

required

Returns:

Type Description

geometry table (geopandas.GeoDataFrame): A GeoDataFrame of features

Source code in src/aspiaspace/helpers/coordinates.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def format_areas(features):
    """Convert outputs of areas endpoint into GeoPandas GeoDataFrame

    Args:
        features (str): Output of areas endpoint

    Returns:
        geometry table (geopandas.GeoDataFrame): A GeoDataFrame of features
    """
    ids = []
    geoms = []
    for row in features:
        geom = from_geojson(json.dumps(row["geometry"]))
        id = row["aspia_uuid"]
        geoms.append(geom)
        ids.append(id)

    shp = gp.GeoDataFrame({"aspia_uuid":ids,"geometry":geoms},crs="EPSG:4326")

    return shp

is_valid_coordinate(coord_str)

Asserts that a coordinate is valid and not out of bounds

Parameters:

Name Type Description Default
coord_str str

Coordinates

required

Returns:

Name Type Description
bool

True if valid, otherwise False

Source code in src/aspiaspace/helpers/coordinates.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def is_valid_coordinate(coord_str):
    """Asserts that a coordinate is valid and not out of bounds

    Args:
        coord_str (str): Coordinates

    Returns:
        bool: True if valid, otherwise False
    """
    pattern = r'^\s*(-?\d+(?:\.\d+)?)\s*,\s*(-?\d+(?:\.\d+)?)\s*$'

    match = re.match(pattern, coord_str)
    if not match:
        return False

    lat = float(match.group(1))
    lon = float(match.group(2))

    # Range check
    if not (-90 <= lat <= 90 and -180 <= lon <= 180):
        return False

    return True