Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Leaflet map - Draw Line for each row of dataframe

Tags:

r

leaflet

I am trying to create lines on a map with Leaflet between latitude/longitude points. Here is a sample input data:

  segment_id latitude1 longitude1 latitude2 longitude2      len
1          1  48.15387   17.07388  48.15396   17.07387 10.98065
2          1  48.15396   17.07387  48.15404   17.07377 11.31327
3          1  48.15404   17.07377  48.15410   17.07364 11.74550
4          1  48.15410   17.07364  48.15412   17.07349 11.48138
5          1  48.15412   17.07349  48.15412   17.07334 11.63625
6          2  48.15424   17.07307  48.15432   17.07299 10.79304

The result of this should be 6 lines lat1,lng1 -> lat2,lng2. I have a hard time working with addPolylines, it is creating extra unwanted lines and I am not sure why.

enter image description here

This is how it should look like, without the extra lines stacked on top of each other :D

Here's my code so far but it's garbage:

  drawEdges <- function(x) {
    d <- cbind(x$latitude1,x$latitude2)
    s <- rep(1:nrow(x), each = 2) + (0:1) * nrow(x)
    latitudeOut <- d[s]
    e <- cbind(x$longitude1,x$longitude2)
    t <- rep(1:nrow(x), each = 2) + (0:1) * nrow(x)
    longitudeOut <- e[t]
    mymap <<- addPolylines(map = mymap,data = x, lng = ~longitudeOut, lat = ~latitudeOut)
  }

  if (!is.null(edges)){
    segments <- split( edges , f = edges$segment_id )
    segments
    sapply(segments, drawEdges)
}

Thank you for helping

like image 265
ayshelina Avatar asked Oct 17 '22 20:10

ayshelina


1 Answers

To get the lines joining in sequence you need your data reshaped into a long form, with the points in order.

And to do this without using any spatial objects (e.g. from library(sp)) you need to add the lines using a loop.

library(leaflet)

### --- reshaping the data ----
## keep the order - but because we're going to split the data, only use odd numbers
## and we'll combine the even's on later
df$myOrder <- seq(from = 1, to = ((nrow(df) * 2) - 1), by = 2)

## put the data in long form by splitting into two sets and then rbinding them
## I'm renaming the columns using setNames, as we need to `rbind` them
## together later
df1 <- setNames(df[, c("segment_id","latitude1","longitude1", "myOrder")],
                c("segment_id", "lat","lon", "myOrder"))

df2 <- setNames(df[, c("segment_id","latitude2","longitude2", "myOrder")],
                c("segment_id", "lat","lon", "myOrder"))

## make df2's order even
df2$myOrder <- (df2$myOrder + 1)

df <- rbind(df1, df2)

## can now sort the dataframe
df <- df[with(df, order(myOrder)), ]

## and de-dupelicate it
df <- unique(df[, c("segment_id", "lat","lon")])
### -----------------------------


## ----- plotting ---------------
map <- leaflet(data = df) %>%
  addTiles() %>%
  addCircles()

## without using any spatial objects, you add different lines in a loop
for(i in unique(df$segment_id)){
  map <- addPolylines(map, data = df[df$segment_id == i,], 
                      lat = ~lat, lng = ~lon, group = ~segment_id)
}
map

enter image description here

like image 102
SymbolixAU Avatar answered Oct 20 '22 11:10

SymbolixAU