繁体   English   中英

R:连接 Map 上的点

[英]R: Connecting Dots on a Map

我正在使用 R 编程语言。

使用“传单”库,我为这 5 个城市制作了以下 map:

 library(dplyr) library(leaflet) map_data <- data.frame("Lat" = c(43.6426, 43.6424, 43.6544, 43.6452, 43.6629), "Long" = c(-79.3871, -79.3860, -79.3807, -79.3806,-79.3957 ), type = c(1,2,3,4,5)) map_data$type = as.factor(map_data$type) leaflet(map_data) %>% addTiles() %>% addCircleMarkers(stroke = FALSE, label = ~type,fillOpacity = 0.8, labelOptions = labelOptions(direction = "center",style = list('color' = "white"),noHide = TRUE, offset=c(0,0), fill = TRUE, opacity = 1, weight = 10, textOnly = TRUE))

在上面我创建的 map 上,我现在想根据它们的“编号”(例如,将 1 与 2 连接,2 与 3 连接)在 map(在路线中)上“连接”所有这些“点”(即城市) , 3 与 4, 4 与 5, 5 与 1), 以及 output 路线的“总距离”。 我发现以前的帖子显示了如何执行此操作: How to show path and distance on map with leaflet, shiny 应用程序?

我试图修改这篇文章中的代码以适应我的问题:

 library(osrm) route = osrmRoute(c(-79.3871, -79.3860, -79.3807, -79.3806,-79.3957 ), c(43.6426, 43.6424, 43.6544, 43.6452, 43.6629), overview = 'full') route_summary = osrmRoute(c(-79.3871, -79.3860, -79.3807, -79.3806,-79.3957 ), c(43.6426, 43.6424, 43.6544, 43.6452, 43.6629), overview = FALSE) leaflet() %>% addTiles() %>% addCircleMarkers(c(-79.3871, -79.3860, -79.3807, -79.3806,-79.3957 ), c(43.6426, 43.6424, 43.6544, 43.6452, 43.6629), stroke = FALSE, label = ~type,fillOpacity = 0.8, labelOptions = labelOptions(direction = "center",style = list('color' = "white"),noHide = TRUE, offset=c(0,0), fill = TRUE, opacity = 1, weight = 10, textOnly = TRUE)) %>% addPolylines(route$lon,route$lat, label = paste(round(route_summary[1]/60), 'hr - ', round(route_summary[2]), 'km'), labelOptions = labelOptions(noHide = TRUE))

但这会返回以下错误:

 Error in UseMethod("metaData"): no applicable method for 'metaData' applied to an object of class "NULL"

有人可以告诉我如何解决这个问题吗?

我想使用“传单”而不是“rshiny”来做到这一点。 最后,我希望最终的 map 看起来像这样(这应该代表旅行推销员问题的“单一路径”):

[ [在此处输入图片描述][2]][2]

注意:我开始认为问题可能是“osrmRoute()”function 可能无法工作超过 2 点?

一种方法是让您拨打 API 电话:

https://github.com/Project-OSRM/osrm-backend/blob/master/docs/http.md

我将概述您如何做到这一点:

数据

df <- data.frame( lon = c(-79.3871, -79.3860, -79.3807, -79.3806,-79.3957), lat = c(43.6426, 43.6424, 43.6544, 43.6452, 43.6629) )

url 来电

root <- "http://router.project-osrm.org/route/v1/driving/" options <- c( continue_straight = "true", overview = "full", annotations = "true", steps = "true" ) coords <- df %>% slice(c(seq_len(n()), 1)) %>% pmap_chr(str_c, sep = ",") %>% str_c(collapse = ";") options <- options %>% imap_chr(~str_c(.y, "=",.x)) %>% str_c(collapse = "&") %>% str_c("?",.) res <- rjson::fromJSON(file = str_c(root, coords, options))

请注意,我已将第一个点添加为第 6 行以制作圆形路线。

map

 res$routes[[1]]$geometry %>% googlePolylines::decode() %>%.[[1]] %>% leaflet() %>% addTiles() %>% addPolylines(lng = ~lon, lat = ~lat) %>% addCircleMarkers( data = df, stroke = FALSE, label = seq_len(nrow(df)), fillOpacity = 0.8, labelOptions = labelOptions( direction = "center", style = list('color' = "white"), noHide = TRUE, offset=c(0,0), fill = TRUE, opacity = 1, weight = 10, textOnly = TRUE ) )

距离

res$routes[[1]]$distance

这是以米为单位(文档)

编辑

可能有更好的方法来标记折线,但我现在没有时间:

 library(sf) segment_df <- df %>% rbind(df[1,]) d <- segment_df %>% st_as_sf(coords = c("lon", "lat"), crs = 4326) %>% {st_distance(.[-6,],.[-1,], by_element = TRUE)} %>% as.vector() %>% round() m <- leaflet() %>% addTiles() for(i in seq_len(nrow(segment_df) - 1)) m <- m %>% addPolylines( data = segment_df[i:(i+1),], lng = ~lon, lat = ~lat, color = "red", label = paste(d[[i]], "m"), labelOptions(noHide = TRUE, direction = 'top') ) m <- m %>% addCircleMarkers( data = df, stroke = FALSE, label = seq_len(nrow(df)), fillOpacity = 0.8, labelOptions = labelOptions( direction = "center", style = list('color' = "white"), noHide = TRUE, offset=c(0,0), fill = TRUE, opacity = 1, weight = 10, textOnly = TRUE ) )

如果您只想显示总距离,那么这更容易并且不需要循环,只需将循环替换为:

 segment_df %>% leaflet() %>% addTiles() %>% addPolylines( lng = ~lon, lat = ~lat, color = "red", label = paste(sum(d), "m"), labelOptions = labelOptions(noHide = TRUE, direction = 'top') )

我希望你明白(并从地图上看到)这是不可驾驶的。

这是我根据@det 的回答尝试的答案:

 library(sf) library(geosphere) library(dplyr) library(leaflet) library(data.table) library(VPF) #add a 6th row that is equal to the 1st row - so that the path loops back map_data <- data.frame("Lat" = c(43.6426, 43.6424, 43.6544, 43.6452, 43.6629, 43.6426), "Long" = c(-79.3871, -79.3860, -79.3807, -79.3806,-79.3957, -79.3871 ), type = c(1,2,3,4,5,1)) map_data$type = as.factor(map_data$type) m1 = leaflet(map_data) %>% addTiles() %>% addCircleMarkers(stroke = FALSE, label = ~type,fillOpacity = 0.8, color = ~ifelse(type==1,"red","blue"), labelOptions = labelOptions(direction = "center",style = list('color' = "white"), noHide = TRUE, offset=c(0,0), fill = TRUE, opacity = 1, weight = 10, textOnly = TRUE)) m1 %>% addTiles() %>% addPolylines(data = map_data, lng = ~Long, lat = ~Lat, group = ~type)

现在,我想计算行程的总距离并将其显示在 map 上:

 #distances (https://stackoverflow.com/questions/42119438/calculate-distance-between-two-long-lat-coordinates-in-a-dataframe) result = rbind( cbind(map_data[1:nrow(map_data)-1,c(1,2)], map_data[-1,c(1,2)]), cbind(map_data[nrow(map_data), c(1,2)], map_data[1,c(1,2)]) ) colnames(result) <- c("start_lat", "start_long", "end_lat", "end_long") result$id = as.factor(c(1,2,3,4,5,1)) result = data.frame(result) for (i in 1:nrow(result)) { a<-result$start_long[i] b<-result$start_lat[i] c<-result$end_long[i] d<-result$end_lat[i] result$distance[i]<-distm(c(a,b),c(c,d), fun = distHaversine) } #total distance of trip in meters d = result$distance total_d = signif(sum(d),3) m1 %>% addPolylines( data = map_data, lng = ~Long, lat = ~Lat, color = "blue", label = paste0(total_d, " meters"), labelOptions(noHide = TRUE, direction = 'top') )

我想我终于明白了 - 非常感谢@Det

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM