Search code examples
rshinyr-leaflet

Display Line on Leaflet


I want to display a line on a leaflet (for R) map.

The points of my line:

myPoints < - structure(list(lat = c(52.40684, 52.40682, 52.40681, 52.40679, 
52.40675, 52.40669, 52.40668, 52.40661, 52.40641, 52.40623, 52.40608, 
52.40592, 52.40469, 52.40464, 52.40456, 52.40416, 52.4039, 52.40376, 
52.40359, 52.40352, 52.4033, 52.40291, 52.40289, 52.4029, 52.40245, 
52.40232, 52.40097, 52.40032, 52.40006, 52.39995, 52.39987, 52.39988, 
52.40011, 52.40015, 52.40025, 52.40032, 52.40046, 52.4005, 52.40057, 
52.40062, 52.40063, 52.40065, 52.40082, 52.40091, 52.40094, 52.40102, 
52.40104, 52.4011, 52.40114, 52.40181, 52.40181, 52.40187, 52.40221, 
52.40224, 52.40228, 52.40217, 52.40219, 52.40218, 52.4022, 52.40223, 
52.4023, 52.40232, 52.40238, 52.40245, 52.40249, 52.40252, 52.40251, 
52.40248, 52.40232, 52.40222, 52.40201, 52.40183, 52.40172, 52.40161, 
52.40149, 52.40141, 52.40141, 52.40145, 52.40151, 52.40175, 52.40195, 
52.4021, 52.40205, 52.40202, 52.40201, 52.40204, 52.40208, 52.40211, 
52.40213, 52.40219, 52.4022, 52.40216, 52.40209, 52.40204, 52.40201, 
52.402, 52.40202, 52.40206, 52.40212, 52.4022, 52.40223, 52.40224, 
52.40228, 52.4023, 52.40233, 52.40238, 52.40242, 52.40244, 52.40239, 
52.40234, 52.40231, 52.40234, 52.40238, 52.4024, 52.40248, 52.4025, 
52.40245, 52.40234, 52.4022, 52.4017, 52.40156, 52.4015, 52.40144, 
52.40113, 52.40092, 52.40042, 52.39998, 52.39951, 52.3992, 52.39916, 
52.39909, 52.39906, 52.39902, 52.39901, 52.39902, 52.39906, 52.39909, 
52.39916, 52.39898, 52.39888, 52.39876, 52.39874, 52.39869, 52.39858, 
52.398, 52.39712, 52.39685, 52.39612, 52.39575, 52.39574, 52.39562, 
52.39559, 52.39558, 52.39549, 52.3953, 52.39512, 52.39505, 52.39478, 
52.39463, 52.39451, 52.39281, 52.39277, 52.39267, 52.39258, 52.39247, 
52.39242), lng = c(12.97444, 12.97219, 12.97209, 12.97203, 12.97197, 
12.97194, 12.97162, 12.97164, 12.97174, 12.97187, 12.97193, 12.97196, 
12.97191, 12.97189, 12.97181, 12.97228, 12.97262, 12.97295, 12.97349, 
12.97393, 12.97629, 12.98028, 12.98055, 12.98065, 12.98071, 12.98074, 
12.98075, 12.98075, 12.98073, 12.98069, 12.98067, 12.98128, 12.98624, 
12.98744, 12.99267, 12.99438, 12.99775, 12.99832, 12.9994, 13.00018, 
13.0007, 13.00165, 13.00588, 13.00791, 13.00886, 13.009, 13.00909, 
13.0101, 13.01094, 13.01086, 13.01101, 13.01239, 13.01235, 13.01316, 
13.01429, 13.01431, 13.01493, 13.01502, 13.01519, 13.01521, 13.01523, 
13.0153, 13.01579, 13.01596, 13.01602, 13.01607, 13.01623, 13.01632, 
13.01657, 13.01709, 13.01778, 13.01812, 13.01823, 13.0183, 13.01836, 
13.01839, 13.01857, 13.01976, 13.02107, 13.02707, 13.03184, 13.0357, 
13.03572, 13.03578, 13.03593, 13.03599, 13.03602, 13.03602, 13.0367, 
13.03808, 13.03829, 13.0383, 13.03835, 13.03844, 13.03856, 13.03868, 
13.0388, 13.0389, 13.03897, 13.03901, 13.03901, 13.03924, 13.04057, 
13.04103, 13.04104, 13.04108, 13.04113, 13.04128, 13.04142, 13.04146, 
13.04147, 13.04194, 13.04295, 13.04346, 13.04519, 13.04547, 13.04555, 
13.0457, 13.04589, 13.04663, 13.04689, 13.04698, 13.04701, 13.04717, 
13.04729, 13.04755, 13.04774, 13.04786, 13.04801, 13.04802, 13.04795, 
13.04788, 13.04766, 13.04759, 13.04766, 13.04788, 13.04795, 13.04802, 
13.04811, 13.04818, 13.04833, 13.04842, 13.04842, 13.04842, 13.04843, 
13.04841, 13.04839, 13.04825, 13.05091, 13.05121, 13.05225, 13.05242, 
13.05254, 13.05251, 13.05242, 13.05229, 13.05259, 13.05487, 13.0561, 
13.05704, 13.05646, 13.05654, 13.05735, 13.05803, 13.05881, 13.05924
)), .Names = c("lat", "lng"), row.names = c(NA, -166L), class =     "data.frame")

Thats how I make a SpatialLine out of it

makeLine <- function(myMatrix) {
    library(sp)
    if (nrow(myMatrix) > 1) {
            myLine <- Line(myMatrix)
            myLinesList <- Lines(list(myLine), 1)
            myLineSpatial <- SpatialLines(list(myLinesList))
            proj4string(myLineSpatial) <- CRS("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs")
            return(myLineSpatial)
            
    } else {
            print("Matrix to short")
    }
}

After mySpatialLine <- makeLine(myPoints) I have this

class       : SpatialLines 
features    : 1 
extent      : 52.39242, 52.40684, 12.97162, 13.05924  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs 

Which I then try to bring on my leaflet map.

leafletProxy("myMap") %>% 
    addPolylines(data = mySpatialLine, stroke = TRUE, 
    fillOpacity = 0.5, smoothFactor = 0.5, layerId = "way"
)

That is not working. Nothing gets displayed. I do not even get an error or warning. Do you have any ideas why?


Solution

  • You assign the wrong coordinate reference system. Your point coordinates are latitude and longitude in degrees, yet you assign a webmercator projection which has units meters. This basically means that the line is too small to be seen, even at the highest zoom level.

    Therefore:

    makeLine <- function(myMatrix) {
      library(sp)
      if (nrow(myMatrix) > 1) {
        myLine <- Line(myMatrix)
        myLinesList <- Lines(list(myLine), 1)
        myLineSpatial <- SpatialLines(list(myLinesList))
        proj4string(myLineSpatial) <- CRS("+init=epsg:4326")
        return(myLineSpatial)
    
      } else {
        print("Matrix to short")
      }
    }
    
    leaflet() %>% addTiles() %>%
      addPolylines(data = mySpatialLine, stroke = TRUE, 
                   fillOpacity = 0.5, smoothFactor = 0.5, layerId = "way"
      )
    

    should work as intended.