MAths calculations

area_from_lon_lat_poly(geometry)

For shapely geometry in lon-lat coordinates, returns area in km^2.

Source code in workflow/scripts/functions.py
def area_from_lon_lat_poly(geometry):
    """For shapely geometry in lon-lat coordinates,
    returns area in km^2."""

    project = partial(
        pyproj.transform, pyproj.Proj(init="epsg:4326"), pyproj.Proj(proj="aea")  # Source: Lon-Lat
    )  # Target: Albers Equal Area Conical https://en.wikipedia.org/wiki/Albers_projection
    # TODO fix
    new_geometry = transform(project, geometry)

    # default area is in m^2
    return new_geometry.area / 1e6

haversine(p1, p2)

Calculate the great circle distance in km between two points on the earth (specified in decimal degrees)

Parameters:
  • p1 (Point) –

    location 1 in decimal deg

  • p2 (Point) –

    location 2 in decimal deg

Returns:
  • float( float ) –

    great circle distance in [km]

Source code in workflow/scripts/functions.py
def haversine(p1, p2) -> float:
    """Calculate the great circle distance in km between two points on
    the earth (specified in decimal degrees)

    Args:
        p1 (shapely.Point): location 1 in decimal deg
        p2 (shapely.Point): location 2 in decimal deg

    Returns:
        float: great circle distance in [km]
    """

    # convert decimal degrees to radians
    lon1, lat1, lon2, lat2 = map(radians, [p1[0], p1[1], p2[0], p2[1]])

    # haversine formula
    dlon = lon2 - lon1
    dlat = lat2 - lat1
    a = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2
    c = 2 * asin(sqrt(a))
    r = 6371  # Radius of earth in kilometers. Use 3956 for miles
    return c * r