Flow map(Travel Path) Using Lat and Long in R

前端 未结 5 1727
臣服心动
臣服心动 2021-01-19 07:55

I am trying to plot flow map (for singapore) . I have Entry(Lat,Long) and Exit (Lat,long). I am trying to map the flow from entry to exit in singapore map.

s         


        
5条回答
  •  北海茫月
    2021-01-19 08:50

    If you want to plot it on an actual Google Map, and recreate the style of your linked map, you can use my googleway package that uses Google's Maps API. You need an API key to use their maps

    library(googleway)
    
    df$Exit_Station_Lat <- as.numeric(as.character(df$Exit_Station_Lat))
    df$Exit_Station_Long <- as.numeric(as.character(df$Exit_Station_Long))
    
    df$polyline <- apply(df, 1, function(x) {
        lat <- c(x['Entry_Station_Lat'], x['Exit_Station_Lat'])
        lon <- c(x['Entry_Station_Long'], x['Exit_Station_Long'])
        encode_pl(lat = lat, lon = lon)
    })
    
    mapKey <- 'your_api_key'
    
    style <- '[ { "stylers": [{ "visibility": "simplified"}]},{"stylers": [{"color": "#131314"}]},{"featureType": "water","stylers": [{"color": "#131313"},{"lightness": 7}]},{"elementType": "labels.text.fill","stylers": [{"visibility": "on"},{"lightness": 25}]}]'
    
    google_map(key = mapKey, style = style) %>%
        add_polylines(data = df, 
          polyline = "polyline", 
          mouse_over_group = "Entry_Station_Lat",
          stroke_weight = 0.7,  
          stroke_opacity = 0.5, 
          stroke_colour = "#ccffff")
    


    Note, to recreate the map using flight data, see the example given in ?add_polylines


    You can also show other types of routes, for example, driving between the locations by using Google's Directions API to encode the driving routes.

    df$drivingRoute <- lst_directions <- apply(df, 1, function(x){
        orig <- as.numeric(c(x['Entry_Station_Lat'], x['Entry_Station_Long']))
        dest <- as.numeric(c(x['Exit_Station_Lat'], x['Exit_Station_Long']))
    
        dir <- google_directions(origin = orig, destination = dest, key = apiKey)
        dir$routes$overview_polyline$points
    })
    
    
    google_map(key = mapKey, style = style) %>%
        add_polylines(data = df, 
          polyline = "drivingRoute", 
          mouse_over_group = "Entry_Station_Lat",
          stroke_weight = 0.7,  
          stroke_opacity = 0.5, 
          stroke_colour = "#ccffff")
    

提交回复
热议问题