# Destination Weather API

Weather forecasts, reports on current weather conditions, astronomical information and alerts at a specific location based on the ‘HERE Destination Weather’ API.

## Observations

In order to request information about the current weather situation points of interest (POIs) have to be provided. The POIs must be an `sf` object containing geometries of type `POINT` or a `character` vector containing place names (e.g. cities). These POIs are passed to the `weather()` function, whereby the `product` parameter is set to `"observation"`:

    observation <- weather(
      poi = poi,
      product = "observation"
    )

The return value is an `sf` object, which contains the `POINT` geometries of the provided POIs and the most recent record on the observed weather. The measurements are taken from the nearest weather observation stations with respect to the POIs. The distance of the stations to the provided POIs is an indicator for the reliabilty of the weather information at each POI. A table of the observed weather near the example POIs:

station | distance | description | temperature | humidity | windSpeed | windDirection
--- | --- | --- | --- | --- | --- | ---
Rönnimoos | 450 | Light rain. Mostly cloudy. Chilly. | 2.00 | 93 | 7.41 | 350
Lugano | 670 | Passing clouds. Cool. | 10.00 | 88 | 5.56 | 0
Chailly | 340 | Quite cool. | 5.89 | 70 | 18.53 | 30
Kleinhüningen | 430 | Light rain. Mostly cloudy. Chilly. | 3.00 | 93 | 24.09 | 0
Kehrsatz | 620 | Drizzle. Mostly cloudy. Chilly. | 3.00 | 87 | 3.71 | 0
Zürich (Kreis 6) / Oberstrass | 820 | Light rain. Broken clouds. Chilly. | 2.00 | 87 | 3.71 | 0
Geneva | 490 | Broken clouds. Quite cool. | 6.00 | 71 | 22.24 | 30
Vaduz | 940 | Quite cool. | 4.61 | 78 | 20.39 | 340

Print the weather observation information on an interactive leaflet map:

    # NOTE: This code block is temporarily not evaluated due to an issue of the
    # mapview package when plotting multiple SF layers with the new CRS structure of
    # version 0.9-0 (see: https://github.com/r-spatial/mapview/issues/271).
    m <-
      mapview(observation,
              zcol = "temperature",
              cex = observation$humidity/4,
              layer.name = "Observation",
              map.types = c("Esri.WorldTopoMap"),
              homebutton = FALSE
      ) +
      mapview(poi,
              zcol = "city",
              cex = 1,
              col.region = "black",
              legend = FALSE,
              homebutton = FALSE
      )
    m

## Forecast

An hourly forecast of the predicted weather for the following seven days can be obtained by setting the `product` parameter to `"forecast_hourly"`

    forecast <- weather(
      poi = poi,
      product = "forecast_hourly"
    )

Print the weather observation information on an interactive leaflet map with popup graphs for temperature and humidity:

1. Create a list containing the temperature and humidity graphs for every POI:

    g <- lapply(1:nrow(forecast), function(x) {
      fc <- data.frame(
        dt = as.POSIXct(forecast$forecast[[x]]$utcTime, format = "%Y-%m-%dT%H:%M:%OS", tz = "UTC"),
        te = as.numeric(forecast$forecast[[x]]$temperature),
        rh = as.numeric(forecast$forecast[[x]]$humidity)
      )
      ggplot(fc, aes(x = dt)) +
        geom_line(aes(y = te, colour = "Temperature")) +
        geom_line(aes(y = rh/5, colour = "Humidity")) +
        scale_y_continuous(sec.axis = sec_axis(~.*5, name = "Relative humidity [%]")) +
        scale_colour_manual(values = c("blue", "red")) +
        labs(y = "Air temperature [°C]", x = "", colour = "") +
        ggtitle(forecast$station[x]) +
        theme_minimal() +
        theme(legend.position="bottom", panel.background = element_rect(color = NA))
    })

2. Then add list of graphs to the leaflet map using the the `popup` parameter:

    # NOTE: This code block is temporarily not evaluated due to an issue of the
    # mapview package when plotting multiple SF layers with the new CRS structure of
    # version 0.9-0 (see: https://github.com/r-spatial/mapview/issues/271).
    m <-
      mapview(forecast,
              color = "black",
              col.region = "yellow",
              layer.name = "Forecast",
              zcol = "station",
              map.types = c("Esri.WorldTopoMap"),
              homebutton = FALSE,
              legend = FALSE,
              popup = leafpop::popupGraph(g)
      ) +
      mapview(poi,
              zcol = "city",
              cex = 1,
              col.region = "black",
              legend = FALSE,
              homebutton = FALSE
      )
    m

## Astronomy

An astronomical forecast is requested by setting the `product` parameter to `"forecast_astronomy"`:

    astronomy <- weather(
      poi = poi,
      product = "forecast_astronomy"
    )

Print a table for the sun and moon times of the first example POI, where the nearest station is ‘Emmenbrücke’:

date | sunrise | sunset | moonrise | moonset | phase
--- | --- | --- | --- | --- | ---
2020-03-29 | 7:11AM | 7:52PM | 9:42AM | 12:02AM | Waxing crescent
2020-03-30 | 7:09AM | 7:54PM | 10:15AM | 1:07AM | Waxing crescent
2020-03-31 | 7:07AM | 7:55PM | 10:56AM | 2:10AM | Waxing crescent
2020-04-01 | 7:05AM | 7:57PM | 11:46AM | 3:10AM | First Quarter
2020-04-02 | 7:03AM | 7:58PM | 12:47PM | 4:04AM | First Quarter
2020-04-03 | 7:01AM | 7:59PM | 1:57PM | 4:51AM | Waxing gibbous
2020-04-04 | 6:59AM | 8:01PM | 3:14PM | 5:31AM | Waxing gibbous
2020-04-05 | 6:57AM | 8:02PM | 4:34PM | 6:04AM | Waxing gibbous

## Alerts

Current weather alerts, near provided POIs, are obtain by the product `alerts`:

    alerts <- weather(
      poi = poi,
      product = "alerts"
    )

This returns an `sf` object with the POIs and the attribute `"alerts"`, which is a `data.table`, which contains the current weather alerts. If no alerts are recorded near a POI the attribute `"alerts"` is `NULL`.

## API Reference

- Observation
- Forecast
- Astronomy
- Alerts