Example #1
0
def elevation_byloc(coord: Tuple[float, float], crs: str = DEF_CRS):
    """Get elevation from USGS 3DEP service for a coordinate.

    Parameters
    ----------
    coord : tuple
        Coordinates of the location as a tuple
    crs : str, optional
        The spatial reference of the input coord, defaults to epsg:4326 (lon, lat)

    Returns
    -------
    float
        Elevation in meter
    """
    if not isinstance(coord, tuple) or len(coord) != 2:
        raise InvalidInputType("coord", "tuple of length 2", "(x, y)")

    lon, lat = MatchCRS.coords(([coord[0]], [coord[1]]), crs, DEF_CRS)

    url = "https://nationalmap.gov/epqs/pqs.php"
    payload = {"output": "json", "x": lon[0], "y": lat[0], "units": "Meters"}
    r = RetrySession().get(url, payload)
    root = r.json()["USGS_Elevation_Point_Query_Service"]
    elevation = float(root["Elevation_Query"]["Elevation"])

    if abs(elevation - (-1000000)) < 1e-3:
        raise ValueError(
            f"The elevation of the requested coordinate ({coord[0]}, {coord[1]}) cannot be found."
        )

    return elevation
Example #2
0
def elevation_bycoords(coords: List[Tuple[float, float]],
                       crs: str = DEF_CRS) -> List[int]:
    """Get elevation from Airmap for a list of coordinates.

    Parameters
    ----------
    coords : list of tuples
        Coordinates of the location as a tuple
    crs : str, optional
        The spatial reference of the input coord, defaults to epsg:4326 (lon, lat)

    Returns
    -------
    list of int
        Elevation in meter
    """
    if not isinstance(coords, (list, Iterator)):
        raise InvalidInputType("coord",
                               "list (or iterator) of tuples of length 2",
                               "[(x, y), ...]")

    if isinstance(coords, list) and any(len(c) != 2 for c in coords):
        raise InvalidInputType("coord", "list of tuples of length 2",
                               "[(x, y), ...]")

    coords_reproj = zip(*MatchCRS.coords(tuple(zip(*coords)), crs, DEF_CRS))
    coords_reproj = tlz.partition_all(100, coords_reproj)

    headers = {"Content-Type": "application/json", "charset": "utf-8"}
    elevations = []
    for chunk in coords_reproj:
        payload = {"points": ",".join(f"{lat},{lon}" for lon, lat in chunk)}
        resp = RetrySession().get(ServiceURL().restful.airmap,
                                  payload=payload,
                                  headers=headers)
        elevations.append(resp.json()["data"])

    return list(tlz.concat(elevations))