Closed
Description
Polygon/polyline data can be provided as lat/lng vectors with the ring/line groupings indicated by NA
values.
# Dummy data
group <- c(219233,219233,167168,167168,208233,208233,219238,219238,145210,145210)
lng <- c(-84.14247,-84.21969,-84.33130,-84.34574,-84.51261,-84.21969,-84.14247,-84.12763,-84.36784,-84.31505)
lat <- c(34.10217,33.95469,34.04140,34.01926,33.91128,33.95469,34.10217,33.95239,33.91493,33.93641)
df <- data.frame(group=group, lng=lng, lat=lat)
# Demo of the problem--all points are considered part of same polyline
library(leaflet)
pal <- colorFactor("Accent", NULL)
leaflet(df) %>% addPolylines(lng = ~lng, lat = ~lat, color = pal(1:5))
# Helper function to split numeric vector with NA, based on a separate group vector
library(purrr)
grouped_coords <- function(coord, group) {
data.frame(coord = coord, group = group) %>%
slice_rows("group") %>%
by_slice(~c(.$coord, NA), .to = "output") %>%
.$output %>% unlist()
}
# Use the helper function to split the lines
library(leaflet)
pal <- colorFactor("Accent", NULL)
leaflet(df) %>% addPolylines(
lng = ~grouped_coords(lng, group),
lat = ~grouped_coords(lat, group),
color = pal(1:5))
Warning message:
In validateCoords(lng, lat, funcName) :
Data contains 5 rows with either missing or invalid lat/lon values and will be ignored
Metadata
Metadata
Assignees
Labels
No labels