1

I found this R code (Search button for Leaflet R map?) and was able to make a interactive map in R:

library(inlmisc)

city <- rgdal::readOGR(system.file("extdata/city.geojson",
                                   package = "inlmisc")[1])
opt <- leaflet::markerClusterOptions(showCoverageOnHover = FALSE)
map <- CreateWebMap("Topo")
map <- leaflet::addMarkers(map, label = ~name, popup = ~name,
                           clusterOptions = opt,
                           clusterId = "cluster",
                           group = "marker", data = city)
map <- AddHomeButton(map)
map <- AddClusterButton(map, clusterId = "cluster")
map <- AddSearchButton(map, group = "marker", zoom = 15,
                       textPlaceholder = "Search city names...")
map

enter image description here

I was curious and wanted to see the format and entries of the "city" file. I was expecting this file to be a "tabular" file (i.e. containing rows and columns, like a data frame), but when I opened the file, it did not appear in this format at all - this file is apparently a "SpatialPointsDataFrame":

> head(city)
class       : SpatialPointsDataFrame 
features    : 6 
extent      : -123.09, -73.8, 31.58, 44.62  (xmin, xmax, ymin, ymax)
crs         : +proj=longlat +datum=WGS84 +no_defs 
variables   : 2
names       :       name, capital 
min values  : Abilene TX,       0 
max values  :  Albany OR,       2 

I then found this post here (How to convert a spatial dataframe back to normal dataframe?) and saw that you can convert a SpatialPointsDataFrame into a regular data frame like this:

DF <- as.data.frame(city)

> head(DF)
        name capital coords.x1 coords.x2
1 Abilene TX       0    -99.74     32.45
2   Akron OH       0    -81.52     41.08
3 Alameda CA       0   -122.26     37.77
4  Albany GA       0    -84.18     31.58
5  Albany NY       2    -73.80     42.67
6  Albany OR       0   -123.09     44.62

But is there a way to convert a regular data frame into a "SpatialDataFrame"? I tried the following code and then tried to plot the results:

#https://stackoverflow.com/questions/29736577/how-to-convert-data-frame-to-spatial-coordinates

library(sf)

city <- st_as_sf(x = DF, 
                        coords = c("coords.x1", "coords.x2"),
                        crs = "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")

map <- CreateWebMap("Topo")
map <- leaflet::addMarkers(map, label = ~name, popup = ~name,
                           clusterOptions = opt,
                           clusterId = "cluster",
                           group = "marker", data = city)
map <- AddHomeButton(map)
map <- AddClusterButton(map, clusterId = "cluster")
map <- AddSearchButton(map, group = "marker", zoom = 15,
                       textPlaceholder = "Search city names...")
map

The code ran, but I get this warning message:

Warning message:
sf layer has inconsistent datum (+proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs).
Need '+proj=longlat +datum=WGS84
  • Am I doing this correctly?

Thank you!

1 Answer 1

1

Leaflet uses a slightly different format of crs than sf with st_as_sf, which you can read more about on GitHub. You have a few options, where first we could use a shortened crs, like here:

library(sf)

city <- st_as_sf(x = DF, 
                        coords = c("coords.x1", "coords.x2"),
                        crs = "+proj=longlat +datum=WGS84 +no_defs")

Or you can use sp::CRS along with your crs definition, so that it is properly read by leaflet:

city <- st_as_sf(x = DF, 
                 coords = c("coords.x1", "coords.x2"),
                 crs = sp::CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))

Or another option would be to use the SRID 4326 as the crs (which will set the Geodetic CRS):

city <- st_as_sf(x = DF, 
                        coords = c("coords.x1", "coords.x2"),
                        crs = 4326) 

map <- CreateWebMap("Topo")
map <- leaflet::addMarkers(map, label = ~name, popup = ~name,
                           clusterOptions = opt,
                           clusterId = "cluster",
                           group = "marker", data = city)
map <- AddHomeButton(map)
map <- AddClusterButton(map, clusterId = "cluster")
map <- AddSearchButton(map, group = "marker", zoom = 15,
                       textPlaceholder = "Search city names...")
map

enter image description here

You will notice that in the answer you are using that they are using mapview, which does work with the format that you give (i.e., crs = "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"). So, it would work with your code, but it won't be in the leaflet style:

city <- st_as_sf(x = DF, 
                 coords = c("coords.x1", "coords.x2"),
                 crs = "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")

mapview::mapview(city)

enter image description here

2

Not the answer you're looking for? Browse other questions tagged or ask your own question.