7

first of all I'm new to R so please bear with me.

What I'm eventually trying to accomplish is to show an interactive map of Amsterdam with Leaflet. For that I'm using RGDAL to read shapefiles.

This link contains the shapefiles of amsterdam.

I'm using the following code to read the shapefiles and to show the map.

amsterdam <- readOGR(".", layer = "sd2010zw_region", verbose = FALSE)

leaflet(amsterdam) %>%
  addProviderTiles("CartoDB.Positron", options= providerTileOptions(opacity = 0.99)) %>%
    addPolygons(
      stroke = FALSE, fillOpacity = 0.5, smoothFactor = 0.5
    )

What I get is the map from CartoDB.Positron but not the 'polygonmap' as the second layer. What I get is a SpatialPolygonsDataFrame containing all sorts of data.

When I use the plot method on the other hand I get the map of Amsterdam

plot(amsterdam, axes=TRUE, border="gray")

But I don't want to use plots, I want to use Leaflet :)

What am I doing wrong here?

JDH
  • 165
  • 2
  • 8

2 Answers2

18

The problem is the projection. You need to project your data to longlat using spTransform from either rgdal or sp. Also, provide your SpatialPolygonsDataFrame in the addPolygons() call.

library(leaflet)
library(rgdal)

amsterdam <- readOGR(".", layer = "sd2010zw_region", verbose = FALSE)

ams_ll <- spTransform(amsterdam, CRS("+init=epsg:4326"))

leaflet() %>%
  addProviderTiles("CartoDB.Positron", options= providerTileOptions(opacity = 0.99)) %>%
  addPolygons(data = ams_ll,
    stroke = FALSE, fillOpacity = 0.5, smoothFactor = 0.5
  )
timelyportfolio
  • 6,479
  • 30
  • 33
TimSalabim
  • 5,604
  • 1
  • 25
  • 36
  • Question: how do you know which CRS you need to use? And where do you get it from? @TimSalabim – JDH Jun 01 '16 at 22:48
  • CRSs are not a trivial topic. In short, web mapping is basically done using [web mercator](http://spatialreference.org/ref/sr-org/7483/). Leaflet, however, needs [non-projected latitude and longitude coordinates](http://spatialreference.org/ref/epsg/4326/) as input and internally transforms them to web mercator (bad design in my opinion, but we need to live with that). So for leaflet, you basically just need to know that they need to be in '+init=epsg:4326' (or '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs' to be more precise). – TimSalabim Jun 02 '16 at 07:01
  • I was wondering if you could help me with this issue : [link](http://stackoverflow.com/questions/37595952/cannot-clear-polygon-with-leaflet-for-r) @TimSalabim. Thanks for the info about CRS BTW! – JDH Jun 02 '16 at 15:22
  • Sorry I'm no shiny expert but **leaflet** includes methods to remove objects. See `?removeControl` – TimSalabim Jun 03 '16 at 11:44
  • Getting the correct projections also helps with problems in popups in leaflet. For some reason HTML does not render in popups with mismatched projections. – dca May 07 '17 at 21:44
3

Tip: Don't forget to add the data = ... variable name in addPolygons() call.

This does not work:

leaflet() %>%
  addTiles() %>%
  addPolygons(ams_ll)

This works:

leaflet() %>%
  addTiles() %>%
  addPolygons(data = ams_ll)

I have spent a few hours looking for a solution, hopefully this will be helpful for others.

Lennert
  • 964
  • 10
  • 16