Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leaflet R derivePolygons missing lat missing long

I am trying to plot the site of some disease-events data on a map.

I use this to import the data:

ByTown<-readOGR(dsn="C:/temp/lyme/Towns", layer="Towns", encoding = "UTF-8", verbose= FALSE)

check the class:

class(ByTown)
#getting this result
[1] "SpatialPolygonsDataFrame"
attr(,"package")
[1] "sp"

Then I convert all of the factors to character data and check to see that I still have a SpatialPolygonsDataFrame using class again, which I do:

Then I format the data I wish to merge into the same title case as the original:

townCount$City<-str_to_title(townCount$City)

Then I geo_join the count data to the spatial polygon data frame:

ByTown<-geo_join(ByTown, townCount,"MCD_NAME", "City")

Then I set the palette and run the mapping:

pal = colorQuantile("PuOr",ByTown$count, n=5 )
map<-leaflet(ByTown) %>%
  addProviderTiles("CartoDB.Positron")%>%
  addPolygons(fillColor = ~pal(count),
            color = "#000000",
            stroke = TRUE,
            weight = 1,
            smoothFactor = 0.5,
            options(viewer = NULL))
map

And I get this error:

Error in derivePolygons(data, lng, lat, missing(lng), missing(lat), "addPolygons") : 
  addPolygons must be called with both lng and lat, or with neither.

I have looked in the coordinates slots and there is data in there...I am baffled by the error and not finding any useful answers online. Here is the head of the first polygon in coordinates slot:

head(nByTown@polygons[[1]]@Polygons[[1]]@coords )

           [,1]     [,2]
[1,] 1036519 916318.7
[2,] 1036039 916355.8
[3,] 1031757 916299.7
[4,] 1027474 916244.5
[5,] 1026709 916198.1
[6,] 1026826 916248.3

Any one every have this issue, identify the root cause and fix it?

like image 510
sconfluentus Avatar asked Feb 14 '17 22:02

sconfluentus


2 Answers

Don't forget to add the data = ... variable name in addPolygons() if you don't provide it in the leaflet() call. I received the same error and spent hours looking for a solution :(.

This does not work:

leaflet() %>%
  addTiles() %>%
  addPolygons(ByTown)

and returns:

Error in derivePolygons(data, lng, lat, missing(lng), missing(lat), "addPolygons") : 
  addPolygons must be called with both lng and lat, or with neither.

This works:

leaflet() %>%
  addTiles() %>%
  addPolygons(data = ByTown)
like image 191
Lennert Avatar answered Sep 23 '22 03:09

Lennert


The addPolygons function either requires you to define the columns of latitude & longitude, OR it will try and derive them from the data you provide.

Given you haven't specified the lat/lon columns, it tries to work out which ones they are. In your case, it couldn't find them. This is mainly due to your data not being in lat/lon format.

Therefore, you'll need to transform your data to use a lat/lon projection, something like (untested)

nByTown_latlon <- spTransform(nByTown, CRS("+proj=longlat +datum=WGS84"))
like image 33
SymbolixAU Avatar answered Sep 21 '22 03:09

SymbolixAU