Cannot run the osmar navigation demo in R. Probably because the demo expects igraph0, which is deprecated

407 views Asked by At

I am trying to run the osmar navigation demo, in R. This demo will use osmar and igraph plot a traffic route around Munich city centre, based on openstreetmap data.

I am using R version 3.1.1 on Lubuntu

The demo, and the osmar library are detailed here http://journal.r-project.org/archive/2013-1/eugster-schlesinger.pdf

To run the demo I type,

library("osmar")
library("igraph")     # The demo tries to call igraph0, but this is 
                      # no-longer available in my version of R, so I
                      # have changed it to "igraph"
demo("navigator")

The demo runs perfectly until it reaches the igraph section.

gr_muc<-as_igraph(hways_muc)      # make a graph of the highways from openstreetmap
summary(gr_muc)

This should return

Vertices: 2381
Edges: 2888
Directed: TRUE
No graph attributes.
Vertex attributes: name.
Edge attributes: weight, name.

But for me it returns

IGRAPH DNW-2385 2894 --
attr: name (v/c), weight (e/n), name (e/n)

I know that gr_muc is a graph because the commands E(gr_muc) and V(gr_muc) return lists of edges and vertices.

Then the demo runs

route <- get.shortest.paths(gr_muc,from = as.character(hway_start_node),to = as.character(hway_end_node))[[1]]

and returns the error

At structural_properties.c:4482 :Couldn't reach some vertices

Which means that it couldn't link the start and end vertices. Then the script fails.

What do I change to make the demo script run, and why is it not working?

1

There are 1 answers

3
Spacedman On BEST ANSWER

There are some one-way streets that prevent the start and end node being connected. Here's some scrappy code that plots the nodes reachable from the hway_start node:

plot(hways_muc)

hway_start # osmar object
plot_nodes(hway_start, add = TRUE, col = "red", pch = 19, cex = 2)

# get reachable nodes, return graph Vertex indexes
n1 = neighborhood(gr_muc,200,V(gr_muc)[as.character(hway_start_node)], mode="out")

# get graph subset
allpts = V(gr_muc)[n1[[1]]]

# use names to subset OSM nodes:
nds = subset(muc, node(allpts$name))
plot_nodes(nds, add=TRUE,col="green",pch=19,cex=1)

accessible nodes

Note that you can't get to the top right which is where the end node is in the demo.

You can work round this if you don't mind driving the wrong way up one-way streets by making the graph undirected:

gru_muc=as.undirected(gr_muc)
route <- get.shortest.paths(gru_muc,
                             from = as.character(hway_start_node),
                             to = as.character(hway_end_node))[[1]]

and now you have a route:

> route
[[1]]
  [2] 1444  491 2055  334  331  481  478  479  [etc]

BUT the return from get_shortest_paths is a list of lists, so you need to get the first component of route to continue the demo code:

route=route[[1]]
route_nodes <- as.numeric(V(gr_muc)[route]$name)

and then plot:

route complete

So I think firstly, the start and end node are not connected along directed ways, and secondly there's a bug in the demo code so that doesn't get the correct element of the return from get_shortest_paths. Its nothing to do with igraph0.