How to retrieve WMS image from certain time/date - r

I'm trying to get de precipation radar data from the KNMI loaded in a leaflet in R. The data is located here: http://adaguc.knmi.nl/contents/webservices/WebServices_RADNL_OPER_R___25PCPRR_L3.html
So far, I've managed to get the current radar image to a leaflet layer with the following code:
require("leaflet")
require("dplyr")
leaflet() %>% addTiles() %>%
addWMSTiles(
"http://geoservices.knmi.nl/cgi-bin/RADNL_OPER_R___25PCPRR_L3.cgi",
layers = "RADNL_OPER_R___25PCPRR_L3_KNMI",
options = WMSTileOptions(format = "image/png", transparent = TRUE),
attribution = "KNMI"
)
So far so good...
But what I really would like, is to get a radar image from a given timestamp. The series goes back to 2009 (see example from the KNMI viewer here: http://geoservices.knmi.nl/viewer2.0/?srs=EPSG%3A3857&bbox=-725.7203842048766,6500000,1220725.7203842048,7200000&service=http%3A%2F%2Fgeoservices.knmi.nl%2Fcgi-bin%2FRADNL_OPER_R___25PCPRR_L3.cgi%3F&layer=RADNL_OPER_R___25PCPRR_L3_COLOR%24image%2Fpng%24true%24default%241%240&selected=0&dims=time$current&baselayers=streetmap$world_line ).
On the web, I have found several examples of leaflet that supports dates/times, (like this: http://apps.socib.es/Leaflet.TimeDimension/examples/example14.html), but unfortunalety I lack the skills to convert this example to the R-version of leaflet.
Can this be done? Can I, for example, retrieve the precipation radar image from 2017-01-07T04:00? If yes: where would/could I start?
Or if it cannot be done with leaflet; any other suggestions?

That was easier than expected ;-)
I just had to add a TIME-variable in the WMSTileOptions.
This works:
require("leaflet")
require("dplyr")
leaflet() %>% addTiles() %>%
addWMSTiles(
"http://geoservices.knmi.nl/cgi-bin/RADNL_OPER_R___25PCPRR_L3.cgi",
layers = "RADNL_OPER_R___25PCPRR_L3_COLOR",
options = WMSTileOptions(format = "image/png", transparent = TRUE, TIME = "2017-01-07T04:00:00"),
attribution = "KNMI neerslagradar"
)

Related

Mapview Popup Graph Appear on Hover?

Is there a way to make popup graphs appear on hover (rather than click) in Mapview? Alternatively, is it possible for the graphs to appear open by default? Rather than produce my own reproducible example, I would defer to the example given with the R Mapview documentation.
I am fairly new to R and Mapview so any guidance is greatly appreciated!
I've just pushed an update to package leafpop which provides the popup functionality used in mapview. This should provide what you want (at least partly - as mapview() will still need to be updated). This allows you to now specify tooltip = TRUE in addPopupImages (in addPopupGraphs via ...). Note that it is encouraged to use addPopup* functions over the classic popup* functions because they also work in non-interactive setting, e.g. when saving a map locally.
library(sf)
library(leaflet)
library(lattice)
library(leafpop)
pt = data.frame(x = 174.764474, y = -36.877245)
pt = st_as_sf(pt, coords = c("x", "y"), crs = 4326)
p2 = levelplot(t(volcano), col.regions = terrain.colors(100))
leaflet() %>%
addTiles() %>%
addCircleMarkers(data = pt, group = "pt") %>%
addPopupGraphs(
list(p2)
, group = "pt"
, width = 300
, height = 400
, tooltip = TRUE
)
Not sure when and how to integrate this into mapview() as this is a bit more complicated than the classic popup* functions (because we need to know something about the map object we create with mapview before we create it...). In any case, I hope this is at least partly useful and helps resolve your issue.

How to use WMS in R?

I like to implement a special WMS-layer with leaflet() in R. But I have no success with the WMS-layer I like to use.
library("leaflet")
library("sp")
# this example from the tutorial works
leaflet() %>% addTiles() %>% setView(-93.65, 42.028, zoom = 4) %>%
addWMSTiles(
"http://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r.cgi",
layers = "nexrad-n0r",
options = WMSTileOptions(format = "image/png", transparent = TRUE)
)
# but for my implementation it don't work
leaflet() %>% addTiles() %>% setView(lat = 53.8, lng = 12.6, zoom = 8) %>%
addWMSTiles("http://www.umweltkarten.mv-regierung.de/script/mv_a3_wasserschutz_wms.php?",
layers = "t2_bib_p",
options = WMSTileOptions(format = "image/png")
)
I assume that there is a problem with the Coordinate Refernce System. Because the WMS-layer I like to use, have not a standard CRS. So I try to expermented with this WMSTileOptions():
crs=CRS("+init=epsg:5650")
The idea is to use WMS-Layers as background images for my vector data. The vector data come mainly from shape-files and GPS-data (text files). The purpose is to create maps which i can use in a report.
If you know other possibles beside leaflet to use WMS in R there are also welcome?
I notice that if you investigate that base URL it says
msWMSDispatch(): WMS server error. Incomplete WMS request: VERSION
parameter missing
and by researching the website I determined that the proper format is:
https://www.umweltkarten.mv-regierung.de/script/mv_a3_wasserschutz_wms.php?SERVICE=WMS&REQUEST=GetCapabilities&VERSION=1.3.0
which yields quite a lot of WMS data if you put it in your browser.
We can use "find" to see which layers are available to be queried. There are several, such as t3_ksg, t3_wsg, and lungwms, but it seems that t2_bib_p is not available for mv_a3_wasserschutz_wms.

R Leaflet change layer order

I'm trying to produce a leaflet map in R with a layer (e.g. WMS tile or raster) and some circles. Since there are many circles in my application, I need to change the order, i.e. the layer should be shown on top of the circles.
Is there a way in R to change the order such that I can bring the WMS tile to the front or sent the circles to the background?
My code looks like the following (in a simplified way):
library(leaflet)
leaflet() %>% addTiles() %>% setView(-93.65, 42.0285, zoom = 4) %>%
addCircles(lng=-98,lat=40,radius=100000,color = "red",stroke=FALSE,fillOpacity=1) %>%
addWMSTiles(
baseUrl="http://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r.cgi",
layers ="nexrad-n0r-900913",
options = WMSTileOptions(format = "image/png", transparent = TRUE),
attribution = "Weather data © 2012 IEM Nexrad"
)

How to add a scale bar in the R package leaflet?

I have recently discovered the r package “leaflet” and found a great blog with some basic instruction for creating an interactive map (found here) http://mhermans.net/hiking-gpx-r-leaflet.html.
I have not, however been successful at adding a scale bar on the map. That is, I’d like to add a graphic feature that scales distance as you zoom in and out of the map (e.g. a bar at the bottom of the map that represents 1km). The leaflet site (found here) http://leafletjs.com/reference.html#control-scale-l.control.scale discusses this feature: L.control,scale().
Does anyone know how to add a scale bar??
This is the code for my map so far (The “Add Scale Bar” Does NOT work):
# A map of Track data
Mymap <- leaflet() %>% addTiles() %>%
addPolylines(data=Dofu1) %>%
addPolylines(data=Zak1) %>%
addProviderTiles("Esri.WorldImagery")
# Add a legend
Mymap %>%
addLegend(position = 'topright',
colors = "blue",
labels = 'Buruku Tracks', opacity = 0.5,
title = '')
# Add a Scale Bar
Mymap %>%
addControl(Mymap, "Scale",
position = c("topright"),
layerID = NULL,
className = "Scale",
data - getMapData(Mymap))
This feature has been added to the development version of the leaflet package. See Add support for scale bar. Also, the second argument to addControl expects html as either a character string or html generated from Shiny or htmltools tags. Finally, I think you have a typo in addControl: data - getMapData(Mymap) should be data = getMapData(Mymap)
the scale bar function is implemented and can be added with 'addScaleBar'
leaflet() %>%
addTiles() %>%
addScaleBar()

NASA tiles with leaflet in R

I would like to ask some help with regard to the leaflet package. When you draw an interactive map, you can do something like this.
library(leaflet)
library(magrittr)
m <- leaflet() %>%
setView(lng = -71.0589, lat = 42.3601, zoom = 8) %>%
addTiles()
m
If you want to add a third-party tile, you can do that too. The following link offers options for third-party tiles (http://leaflet-extras.github.io/leaflet-providers/preview/index.html) The following image is created with OpenWeatherMap.Precipitation.
### They work
m %>% addProviderTiles("MtbMap")
m %>% addProviderTiles("HikeBike.HikeBike")
m %>% addProviderTiles("OpenWeatherMap.Precipitation")
Some of the tile options in the link include tiles by NASA. I wanted to use one of them. So I tried the following codes. None of them unfortunately worked.
### The default map appears, then a black layer appears on top of the default layer.
m %>% addProviderTiles("NASAGIBS.ModisTerraTRUEColorCR")
m %>% addProviderTiles("NASAGIBS.ModisTerraBands367CR")
The only option which is working is the following.
m %>% addProviderTiles("NASAGIBS.ViirsEarthAtNight2012")
My next attempt was to use custom URL template with addTiles(). The URL is from the link above. But, this was not successful either; no error message appeared, but no change in tile.
m %>%addTiles(urlTemplate = "http://map1.vis.earthdata.nasa.gov/wmts-webmerc/MODIS_Terra_CorrectedReflectance_Bands367/default/{time}/{tilematrixset}{maxZoom}/{z}/{y}/{x}.{format}",
tileOptions(minZoom = 1, maxZoom = 8))
My final attempt was the following. This showed the default map, but an additional tile did not appear either.
leaflet() %>%
addTiles() %>%
setView(lng = -71.0589, lat = 42.3601, zoom = 8) %>%
addTiles(urlTemplate = "http://map1.vis.earthdata.nasa.gov/wmts-webmerc/MODIS_Terra_CorrectedReflectance_Bands367/default/{time}/{tilematrixset}{maxZoom}/{z}/{y}/{x}.{format}",
tileOptions(minZoom = 1, maxZoom = 8))
My question is whether this is a potential bug specifically with NASA tiles. Alternatively, what revision do I need in these scripts? Thank you for your help in advance.
UPDATE
I found a website which uses same NASA tiles. I specified NASAGIBS.ModisTerraTRUEColorCR and got the following image. The image is showing how a mail traveled from the US to Sweden. As you see, there is no image for both US and Europe. I think this could be the reason why I saw a black tile. I would like to know if anybody knows some details of NASA tiles. I chose the area which I can see the NASA image. But, I had no luck.
### I expected to see Japan area this time.
foo <- leaflet() %>%
setView(lng = 137.37, lat = 35.93, zoom = 5) %>%
addTiles()
foo %>% addProviderTiles("NASAGIBS.ModisTerraTRUEColorCR")
UPDATE 2
Today, I gave one more shot. At this moment, I managed to get the following image. I zoomed out a bit when I captured it. In UPDATE, I provided a map which you cannot see the US and Europe. In the new image, you see West coast of the States is in black. Given all observations, it seems to me that one may not get NASA images of a location all the time. Depending on when you ask NASA tiles, you may/may not have an image you want.
m <- leaflet() %>%
setView(lng = -71.0589, lat = 42.3601, zoom = 8) %>%
addTiles()
m %>% addProviderTiles("NASAGIBS.ModisTerraBands367CR")
Your final conclusion is correct: depending on what location you request imagery for and on the time of the request, the satellite may or may not yet have acquired the image. So you may get an actual image or just an empty one. (This is also stated in the GIBS API documentation.)
However, you can specify what day to request the image for via the 'time' option for addProviderTiles(). By specifying a date in the near past, you can get non-empty images for all locations if that is what you prefer.
This is the syntax:
> library(leaflet)
> library(magrittr)
> m <- leaflet() %>%
setView(lng = 4.5, lat = 51, zoom = 1) %>%
addTiles() %>%
addProviderTiles("NASAGIBS.ModisTerraTrueColorCR",
options = providerTileOptions(time = "2015-08-31", opacity = 0.5))
> m
At the time of writing (2015-08-31) I get this result:
It's cloudy in England, who would have guessed?
Most data happens to be there already, but there is no imagery for Alaska yet. If on the other hand, I specify yesterday's date
options = providerTileOptions(time = "2015-08-30", opacity = 0.5)
we get the full image:
Finally, the reason why
m %>% addProviderTiles("NASAGIBS.ModisTerraTRUEColorCR")
didn't work was probably because of a typo. It should be
m %>% addProviderTiles("NASAGIBS.ModisTerraTrueColorCR")

Resources