Skip to content

Commit

Permalink
Merge pull request #9 from nsidc/enrich-station-geojson
Browse files Browse the repository at this point in the history
Enrich station geojson
  • Loading branch information
mfisher87 authored Jul 16, 2024
2 parents 91d5d6b + 8de3ccd commit 96926c2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ submit an HTTP query:
<summary>Example HTTP query</summary>

```
http://localhost:8000/v1/?start=2023-01-01&end=2023-06-01&polygon=POLYGON%20((-159.32130625160698%2069.56469019745796,%20-159.32130625160698%2068.08208920517862,%20-150.17196253090276%2068.08208920517862,%20-150.17196253090276%2069.56469019745796,%20-159.32130625160698%2069.56469019745796))
http://localhost:8000/v1/stations?start=2023-01-01&end=2023-06-01&polygon=POLYGON%20((-159.32130625160698%2069.56469019745796,%20-159.32130625160698%2068.08208920517862,%20-150.17196253090276%2068.08208920517862,%20-150.17196253090276%2069.56469019745796,%20-159.32130625160698%2069.56469019745796))
```
</details>

Expand Down
20 changes: 16 additions & 4 deletions src/aross_stations_db/api/v1/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,26 @@ def stations_query_results_to_geojson(
features=[
Feature(
type="Feature",
properties={
"name": station.name,
"matching_event_count": event_count,
},
geometry=Point(
type="Point",
coordinates=Position2D(longitude=lon, latitude=lat),
),
properties={
"id": station.id,
"name": station.name,
"elevation_meters": station.elevation_meters,
"record_begins": station.record_begins,
"timezone_name": station.timezone_name,
"country_code": station.country_code,
"us_state_abbreviation": station.us_state_abbreviation,
"us_county_name": station.us_county_name,
"weather_forecast_office": station.weather_forecast_office,
"ugc_county_code": station.ugc_county_code,
"ugc_zone_code": station.ugc_zone_code,
"iem_network": station.iem_network,
"iem_climate_site": station.iem_climate_site,
"matching_event_count": event_count,
},
)
for station, lon, lat, event_count in results
],
Expand Down
12 changes: 11 additions & 1 deletion src/aross_stations_db/db/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,21 @@ def load_stations(stations: list[dict[str, str]], *, session: Session) -> None:
Station(
id=station["stid"],
name=station["station_name"],
country_code=station["country"],
# HACK: Passing a string for location is "wrong" here, but it's working.
# Something is being handled implicitly to convert the string to binary
# (WKB).
location=_station_location_wkt(station), # type: ignore[arg-type]
elevation_meters=float(station["elevation"]),
record_begins=dt.datetime.fromisoformat(station["record_begins"]),
timezone_name=station["tzname"],
country_code=station["country"],
us_state_abbreviation=station["state"] or None,
us_county_name=station["county"] or None,
weather_forecast_office=station["wfo"] or None,
ugc_county_code=station["ugc_county"] or None,
ugc_zone_code=station["ugc_zone"] or None,
iem_network=station["iem_network"],
iem_climate_site=station["climate_site"] or None,
)
for station in stations
]
Expand Down
25 changes: 24 additions & 1 deletion src/aross_stations_db/db/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class Station(Base):

id: Mapped[str] = mapped_column(primary_key=True)
name: Mapped[str]
country_code: Mapped[str]

location: Mapped[WKBElement] = mapped_column(
Geometry(
Expand All @@ -31,8 +30,32 @@ class Station(Base):
index=True,
)

elevation_meters: Mapped[float]
record_begins: Mapped[dt.datetime]

timezone_name: Mapped[str]
country_code: Mapped[str]
us_state_abbreviation: Mapped[str | None]
us_county_name: Mapped[str | None]

weather_forecast_office: Mapped[str | None]

# UGC = Universal Geographic Code
# (https://www.weather.gov/media/directives/010_pdfs_archived/pd01017002b.pdf)
ugc_county_code: Mapped[str | None]
ugc_zone_code: Mapped[str | None]

# IEM = Iowa Environmental Mesonet. See:
# https://mesonet.agron.iastate.edu/ASOS/
# https://mesonet.agron.iastate.edu/sites/locate.php?network=AKCLIMATE
iem_network: Mapped[str]
iem_climate_site: Mapped[str | None]

events = relationship("Event", backref="station")

# TODO: More fields:
# ncdc81,ncei91,network,start_year,end_year


class Event(Base):
__tablename__ = "event"
Expand Down

0 comments on commit 96926c2

Please sign in to comment.