1

I was wondering how is the weight value calculated while creating igraph route object from osmar object? Is there a maximum?

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)

Thank you! Best regards.

Andreas
  • 397
  • 4
  • 18
  • 37

1 Answers1

1

For most Edges, Weight is simply the distance (in meters) between the Nodes of the Edge:

# 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

This result shows that for 96% of Edges, Weight = distance between Nodes.
I don't know the reason why this is not the case for the remaining 4%.

Waldi
  • 39,242
  • 6
  • 30
  • 78
  • there is one problem with the code: edges_data_frame is not found in calling scope and it is not a column name either. In the line datafrom <- nodes.coords[edges_data_frame,on = .(id = from)][,.(lonfrom = lon,latfrom=lat,weight,id,to)] – Andreas Jun 19 '20 at 21:48
  • 1
    @Andreas, I thought you had this from our previous exchanges. I updated the answer. – Waldi Jun 19 '20 at 21:52
  • sorry did not thought that far :) i have a seperate script for every question. – Andreas Jun 19 '20 at 21:54
  • interesting: i get this result [1] 0.01246048 based on coordinates and bbox in my example. – Andreas Jun 19 '20 at 22:00
  • regarding difference: maybe its because the dist function calculates 'great-circle-distance' or 'as the crow flies' distance and open street map data has the actual mesured distances? – Andreas Jun 19 '20 at 22:04
  • Good idea. However not sure because over 95% of the weights are exactly equal to the distance, and I think that there are more than just 5% of streets with curves in Munich... – Waldi Jun 19 '20 at 22:09
  • True but from what i understand some users are contributing to the data while actualy driving and recording their trips through apps/devices that feed back to the database and since i think those are not so many .. they contribute. Just an idea .. so this is mesured data and the rest ist guessed based on the "crow" calculation – Andreas Jun 19 '20 at 22:10
  • Possible, but I don't really know : I discovered OSM through your questions :) – Waldi Jun 19 '20 at 22:14
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/216306/discussion-between-andreas-and-waldi). – Andreas Jun 19 '20 at 22:54