I am trying tu use David Rumsey' Georeferencer tiles in R leaflet package without success (Only the OSM basemap is displayed).
According to the package vignette I tried :
library(leaflet)
# XYZ direct link
leaflet() %>%
setView(0.65, 0, zoom = 5) %>%
addTiles() %>%
addTiles("https://maps.georeferencer.com/georeferences/700526190853/2017-12-30T11:48:27.589686Z/map.json?key=D7AwmpRP1H6pUic6DIK3")
and
library(leaflet)
# WMS tiles
leaflet() %>%
addTiles() %>% setView(0.65, 0, zoom = 5) %>%
addWMSTiles(
"https://maps.georeferencer.com/georeferences/700526190853/2017-12-30T11:48:27.589686Z/wmts?key=D7AwmpRP1H6pUic6DIK3&SERVICE=WMTS&REQUEST=GetCapabilities",
layers = "1", # I assume
options = WMSTileOptions(format = "image/png", transparent = FALSE),
attribution = "")
Notes :
- The access at the map links need free registration.
- I used the 2.1 package release.
- The vignette's addWMSTiles example is working on my computer.
I solve my problem using the addLayersControl function that can allow me to choose the layer to display.
library(leaflet)
m <- leaflet() %>%
addProviderTiles(providers$CartoDB.DarkMatterNoLabels, group = "DarkMatter") %>%
addTiles("https://maps.georeferencer.com/georeferences/700526190853/2017-12-30T11:48:27.589686Z/map/{z}/{x}/{y}.png?key=D7AwmpRP1H6pUic6DIK3", group ="H") %>%
addLayersControl(
baseGroups = c("DarkMatter", "H"), position ="topleft")
m
Related
I am trying to make an interactive graph of the United States using leaflet and the us.cities package in R. I was able to create the map and label put markers on all the US capitals, but I am struggling to label those capitals on the map itself.
The goal is to be able to click on a marker and have the state capital and population appear on the graph. The trouble is steaming from my last section of code that starts with the #. If anyone has any idea why that code will not run, besides the obvious fact that I put a # in front, I am open to any and all guidance!
Thank you!
library(leaflet)
library(dplyr)
library(maps)
leaflet(us.cities) %>% addTiles() %>% addCircleMarkers(data = us.cities)
us.cities <- us.cities %>% filter(capital == 2) %>% mutate(state_info = paste(name, capital == 2, pop))
us.cities$state_info
#leaflet(us.cities) %>% addTiles() %>% addCircleMarkers(data = us.cities, lat = ~lat, lng = ~long, state_info = ~state_info)
I don't know much about the leaflet package, but for the purposes of providing a quick answer...
leaflet(us.cities) %>%
addTiles() %>%
addCircleMarkers(
data = us.cities,
lat = ~lat,
lng = ~long,
popup = paste(us.cities$country.etc, us.cities$pop)
)
I am trying to create a plot with heatmap using plotly add_heatmap() function, but it generates an error message saying Error: Must supply z attribute I referred to this site for addHeatmap() function provided by leaflet.extras package.
The following code only displays dots from addCircles() and outputs a warning message and a map as shown below:
final_df %>%
leaflet() %>%
addTiles() %>%
addCircles(
lng = final_df$long,
lat = final_df$lat,
popup = final_df$station_name
) %>% addHeatmap(lng = final_df$long, lat = final_df$lat, radius=5)
I have tried a different function add_heatmap() from leaflet which does not display any maps and generates an error.
final_df %>%
leaflet() %>%
addTiles() %>%
addCircles(
lng = final_df$long,
lat = final_df$lat,
popup = final_df$station_name
) %>% add_heatmap(lng = final_df$long, lat = final_df$lat)
Anyone faced a similar issue and created a heatmap on R?
Remove addCircles part and try:
library(leaflet.extras)
final_df %>%
leaflet() %>%
addTiles() %>%
addHeatmap(lng = final_df$long, lat = final_df$lat, blur = 40, max = 0.05, radius = 15)
I would like to visualize municipalities in R using Leaflet.
This is my code:
library(leaflet)
library(jsonlite)
geojson <- readLines("https://cartomap.github.io/nl/wgs84/gemeente_2020.geojson", warn = FALSE) %>%
paste(collapse = "\n") %>%
fromJSON(simplifyVector = TRUE)
map <- leaflet() %>%
addTiles() %>%
addGeoJSON(geojson, weight = 1, color = "grey") %>%
setView(5.387740, 52.155499, zoom = 7)
map
Alas it is not working. I don't get an error message, but I don't get a map with municipality borders either. Could somebody please point out to me what I am doing wrong?
The addGeoJSON function is expecting a geojson object but jsolite::fromJSON returns a list. This should work:
library(leaflet)
library(sf)
library(geojsonsf)
url <- "https://cartomap.github.io/nl/wgs84/gemeente_2020.geojson"
sf <- st_read(url)
geojson <- sf_geojson(sf)
map <- leaflet() %>%
addTiles() %>%
addGeoJSON(geojson, weight = 1, color = "grey") %>%
setView(5.387740, 52.155499, zoom = 7)
map
I would like to create a map where I can interactively measure the distance between 2 points. Luckily, leaflet.extras has exactly what I need, however, I'm struggling to get it to produce the outputs in metres (or kilometres) as opposed to feet.
Consider the below piece of code:
library(leaflet)
library(leaflet.extras)
leaflet() %>%
addTiles() %>%
addDrawToolbar(
editOptions=editToolbarOptions(selectedPathOptions=selectedPathOptions())
)
It creates the following map:
However, this example (chunk 3) effectively the same code to create the same measuring tool (polyline), except it works in KM, whereas my example works in feet.
If you have any tips that can help me switch to metres as opposed to feet, I would really appreciate it.
The drawPolylineOptions function does not allow to set the option feet=FALSE.
Hence, I suggest to modify drawPolylineOptions as follows:
library(leaflet)
library(leaflet.extras)
mydrawPolylineOptions <- function (allowIntersection = TRUE,
drawError = list(color = "#b00b00", timeout = 2500),
guidelineDistance = 20, metric = TRUE, feet = FALSE, zIndexOffset = 2000,
shapeOptions = drawShapeOptions(fill = FALSE), repeatMode = FALSE) {
leaflet::filterNULL(list(allowIntersection = allowIntersection,
drawError = drawError, guidelineDistance = guidelineDistance,
metric = metric, feet = feet, zIndexOffset = zIndexOffset,
shapeOptions = shapeOptions, repeatMode = repeatMode)) }
leaflet() %>% setView(10.975342,45.421588,9) %>%
addTiles() %>%
addProviderTiles(providers$OpenStreetMap.Mapnik) %>%
addDrawToolbar(
polylineOptions = mydrawPolylineOptions(metric=TRUE, feet=FALSE),
editOptions=editToolbarOptions(selectedPathOptions=selectedPathOptions())
)
Otherwise, using addMeasures you can add to your map a useful tool to measure distances (see the icon at the top right corner of the map).
It is possibile to specify units used to display length results by the primaryLengthUnit option.
leaflet() %>% setView(10.975342,45.421588,9) %>%
addTiles() %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addDrawToolbar(
editOptions=editToolbarOptions(selectedPathOptions=selectedPathOptions())
) %>%
addMeasure(primaryLengthUnit="kilometers", secondaryLengthUnit="kilometers")
I'm trying to add WMS tiles in the R leaflet package - not a problem using this example geoserver WMS:
leaflet() %>% addTiles() %>% setView(-93.65, 42.0285, zoom = 4) %>% addWMSTiles(
"http://sedac.ciesin.columbia.edu/geoserver/wms",
layers = "energy:energy-pop-exposure-nuclear-plants-locations_plants",
options = WMSTileOptions(format = "image/png", transparent = TRUE),
tileOptions(tms = TRUE),
attribution = "")
However, when I try using WMS from the National Map, I continue to get empty leaflet results despite multiple attempts at trying to set parameters correctly for url and layers:
leaflet() %>% addTiles() %>% setView(-93.65, 42.0285, zoom = 4) %>%addWMSTiles(
"http://basemap.nationalmap.gov/arcgis/services/USGSHydroNHD/MapServer/WmsServer?",
layers = "0",
options = WMSTileOptions(format = "image/png", transparent = TRUE),
attribution = "")
I've not used leaflet before outside the R leaflet package, so this may be a very novice mistake in setting my parameters in leaflet using this type of WMS
You just need to be zoomed in a little further for the layer to show up. Try this:
leaflet() %>% addTiles() %>% setView(-93.65, 42.0285, zoom = 7) %>%addWMSTiles(
"http://basemap.nationalmap.gov/arcgis/services/USGSHydroNHD/MapServer/WMSServer?",
layers = "0",
options = WMSTileOptions(format = "image/png", transparent = TRUE),
attribution = "")