Search code examples
rleafletline

Single Lines in leaflet


I'm quite new to R and trying to show a district heating system on a leaflet map. From another system I got Gauss-Krueger-Coordinates which I converted to geo-coordinates. The dataset contains basically of datatframe with 4 columns (2 for the startpoint / 2 for the endpoint). I tried to plot the grid with addpolylines in leaflet, but it was just a mess.... This is a part of my data (of several thousand of pipe segments):

 PIPES <- data.frame(longitude_start = c(10.16598309, 10.1306068, 10.13709072, 10.11585874, 10.13122298, 
                                        10.13046081, 10.12480974, 10.09430826, 10.10316342),
                    latitude_start = c(53.59022654, 53.60358092, 53.60814442, 53.59990009, 53.60351254,
                                       53.6035185, 53.60134658, 53.59273843, 53.59777827),
                    longitude_end = c(10.16541999, 10.13051495, 10.13704627, 10.10316342, 10.13117327,
                                      10.12480932, 10.1231769, 10.09388444, 10.09789269),
                    latitude_end = c(53.59022261, 53.60351639, 53.60789653, 53.59777827, 53.6035107,
                                     53.60133096, 53.60104698, 53.59258596, 53.59664915))

This was the code I used:

mydf2 <- data.frame(group = c("max", "min"),
                    lat = c(PIPES$latitude_start, PIPES$latitude_end),
                    long = c(PIPES$longitude_start, PIPES$longitude_end))

leaflet() %>%
addTiles() %>%
addPolylines(data = mydf2, lng = ~long, lat = ~lat, group = ~group)    

Both rows shall be interpreted as single lines, but on the map they are conjunct together: Bad Result

Does anyone know what I can do?

(edited on 20th of April 2020)

Regards Daniel


Solution

  • Hej Daniel,

    look like the group does not work like expected. You may use a loop instead:

    library(magrittr)
    library(leaflet)
    #> Warning: Paket 'leaflet' wurde unter R Version 3.5.3 erstellt
    
    PIPES <- data.frame(longitude_start = c(10.16598309, 10.1306068, 10.13709072, 10.11585874, 10.13122298, 
                                            10.13046081, 10.12480974, 10.09430826, 10.10316342),
                        latitude_start = c(53.59022654, 53.60358092, 53.60814442, 53.59990009, 53.60351254,
                                           53.6035185, 53.60134658, 53.59273843, 53.59777827),
                        longitude_end = c(10.16541999, 10.13051495, 10.13704627, 10.10316342, 10.13117327,
                                          10.12480932, 10.1231769, 10.09388444, 10.09789269),
                        latitude_end = c(53.59022261, 53.60351639, 53.60789653, 53.59777827, 53.6035107,
                                         53.60133096, 53.60104698, 53.59258596, 53.59664915))
    
    m <- leaflet() %>%
      addTiles() 
    
    for (i in 1:nrow(PIPES)) {
      m <- addPolylines(m, lng = c(PIPES$longitude_start[i], PIPES$longitude_end[i]), 
                        lat = c(PIPES$latitude_start[i], PIPES$latitude_end[i]))
    }
    m
    

    Created on 2020-04-21 by the reprex package (v0.3.0)