GClunies / noaa_coops

A Python wrapper for the NOAA CO-OPS Tides & Currents Data and Metadata APIs.
Apache License 2.0
82 stars 28 forks source link

Selecting stations by bounding box #30

Closed sdat2 closed 1 year ago

sdat2 commented 2 years ago

Hey, relying on the point and click map from the website to find out the station ID's seems a little unsystematic.

I was wondering whether there might not be a use case to select station ID's by a bounding lat-lon box and or time-period of operation, say from processing this list: https://www.tidesandcurrents.noaa.gov/stations.html?type=Historic+Water+Levels

or submitting a url request similar to this user:

https://gis.stackexchange.com/questions/89330/accessing-noaa-co-ops-water-levels-using-sos-with-bounding-box-and-time-extent

Apologies if this has been implemented elsewhere, my cursory email search didn't find anything.

I'd be happy to have a shot at implementing this today if it doesn't exist.

sdat2 commented 2 years ago

Minimal example without time selection on this branch https://github.com/sdat2/noaa_coops/tree/bbox:

from typing import List
import requests

def stationid_from_bbox(bbox: List[float]) -> List[str]:
    """List of stations from bounding box.

    Args:
        bbox (List[float]): Bounding box in lon lat space. E.g. [-74.4751,40.389,-73.7432,40.939].

    Returns:
        List[str]: List of stations.
    """
    data_url = "https://api.tidesandcurrents.noaa.gov/mdapi/prod/webapi/stations.json"
    response = requests.get(data_url)
    json_dict = response.json()

    station_list = []

    for station_dict in json_dict["stations"]:
        if bbox[0]< station_dict["lng"] < bbox[2]:
            if bbox[1]< station_dict["lat"] < bbox[3]:
                station_list.append(station_dict["id"])

    return station_list

print(stationid_from_bbox([-74.4751,40.389,-73.7432,40.9397]))

which outputs

['8516945', '8518750', '8519483', '8531680']
GClunies commented 1 year ago

Addressed in #41