# 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 | Broken clouds. Mild. | 23.00 | 65 | 3.71 | 0
Lugano | 670 | Passing clouds. Warm. | 29.00 | 51 | 9.27 | 190
Chailly | 340 | Mild. | 22.50 | 70 | 7.41 | 170
Kleinhüningen | 430 | Scattered clouds. Warm. | 27.00 | 48 | 20.39 | 250
Kehrsatz | 620 | Partly sunny. Mild. | 23.00 | 65 | 7.41 | 150
Zürich (Kreis 6) / Oberstrass | 820 | Overcast. Mild. | 22.00 | 69 | 9.27 | 240
Geneva | 490 | Broken clouds. Warm. | 26.00 | 51 | 16.68 | 220
Vaduz | 940 | Mild. | 24.28 | 60 | 12.97 | 340

Print the weather observation information on an interactive leaflet map:

    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:

    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-06-28 | 5:34AM | 9:26PM | 1:36PM | 1:33AM | First Quarter
2020-06-29 | 5:34AM | 9:26PM | 2:53PM | 1:57AM | First Quarter
2020-06-30 | 5:35AM | 9:25PM | 4:10PM | 2:20AM | Waxing gibbous
2020-07-01 | 5:35AM | 9:25PM | 5:29PM | 2:47AM | Waxing gibbous
2020-07-02 | 5:36AM | 9:25PM | 6:46PM | 3:18AM | Waxing gibbous
2020-07-03 | 5:37AM | 9:25PM | 7:59PM | 3:54AM | Waxing gibbous
2020-07-04 | 5:37AM | 9:24PM | 9:05PM | 4:39AM | Full moon
2020-07-05 | 5:38AM | 9:24PM | 10:01PM | 5:34AM | Full moon

## 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

- Destination Weather API