【问题标题】:What is the basis for the weight function of igraph route object created from osmar object?从 osmar 对象创建的 igraph 路由对象的权重函数的基础是什么?
【发布时间】:2020-06-15 20:19:39
【问题描述】:

我想知道从 osmar 对象创建 igraph 路由对象时如何计算权重值? 有上限吗?

library(osmar)
library(igraph)
src <- osmsource_api(url = "https://api.openstreetmap.org/api/0.6/")
muc_bbox <- center_bbox(11.575278, 48.137222, 1000, 1000)
muc <- get_osm(muc_bbox, src)

hways <- subset(muc, way_ids = find(muc, way(tags(k == "highway"))))
hways <- find(hways, way(tags(k == "name")))
hways <- find_down(muc, way(hways))
hways <- subset(muc, ids = hways)

id<-find(muc, node(tags(v %agrep% "Sendlinger Tor")))[1]
hway_start_node <-find_nearest_node(muc, id, way(tags(k == "highway"))) 
hway_start <- subset(muc, node(hway_start_node))

id <- find(muc, node(attrs(lon > 11.58 & lat > 48.15)))[1]
hway_end_node <- find_nearest_node(muc, id, way(tags(k == "highway")))
hway_end <- subset(muc, node(hway_end_node))

### Create street graph ----
gr <- as.undirected(as_igraph(hways))

### Compute shortest route: ----
# Calculate route
route <- function(start_node,end_node) {
get.shortest.paths(gr,
                     from = as.character(start_node),
                     to = as.character(end_node), 
                     mode = "all")[[1]][[1]]}
#get Weight value
r <- route(hway_start_node,hway_end_node)
max(E(gr)[r]$weight)

谢谢! 最好的问候。

【问题讨论】:

    标签: r openstreetmap igraph osmar


    【解决方案1】:

    对于大多数边缘,权重只是边缘节点之间的距离(以米为单位):

    # Get nodes Latitude & Longitude
    nodes.coords <- hways$nodes$attrs[, c("lon", "lat")]
    nodes.coords$id<- as.character(hways$nodes$attrs$id)
    setDT(nodes.coords)
    
    # Put two Nodes from an Edge together
    edges_data_frame <- get.data.frame(gr, what = "edges")
    datafrom <- nodes.coords[edges_data_frame,on = .(id = from)][,.(lonfrom = lon,latfrom=lat,weight,id,to)]
    datafromto <- nodes.coords[datafrom,on = .(id = to)][,.(lonfrom, latfrom,lonto = lon,latto=lat,weight)]
    
    # Calculate distance between nodes
    datafromto[,dist:=geosphere::distHaversine(cbind(lonfrom,latfrom),cbind(lonto,latto))]
    
    # Check percentage of Weights different of distance
    nrow( datafromto[weight!=dist]) / nrow(datafromto)
    
    [1] 0.03979914
    

    这个结果表明,对于 96% 的边,权重 = 节点之间的距离。
    我不知道为什么剩下的 4% 不是这种情况。

    【讨论】:

    • 代码有一个问题:在调用范围内没有找到edges_data_frame,也不是列名。在行 datafrom
    • @Andreas,我以为你从我们之前的交流中得到了这个。我更新了答案。
    • 抱歉没想到那么远 :) 我为每个问题准备了单独的脚本。
    • 有趣:在我的示例中,我根据坐标和 bbox 得到此结果 [1] 0.01246048。
    • 关于差异:可能是因为 dist 函数计算“大圆距离”或“乌鸦飞”的距离,而开放的街道地图数据具有实际测量的距离?
    猜你喜欢
    • 2020-10-08
    • 2020-09-26
    • 2021-06-15
    • 1970-01-01
    • 2017-02-26
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多