Make igraph searches faster

Viewed 163

Is it possible to make searches in igraph faster? Adding hash or some other way to do so?

I use igraph in combination with open street map files and searching for nodes in bigger datafiles takes far too long

find(muc, node(attrs(lon > 11.82983 & lon < (11.82983+unschaerfe) & lat > 48.11007 & lat< (48.11007+unschaerfe))))[1]

or

find_nearest_node(muc, id, way(tags(k == "highway")))

What could I do to make those faster? OSM data is already stripped down to bare minimum.

Thank you in advance.

Edit:here is a more elaborate code example. Basically I'm using navigator demo as basis. I need to do 5 search operations on igraph.

  1. find a node near gps coordinates
  2. find a nearest road near found node -> start point of navigation
  3. find a node near gps coordinates
  4. find a nearest road near found node -> end point of navigation
  5. find a shortest path.

Just eliminating the initial search would help. I don't know how since the api requires an entry node.

I was thinking maybe dumping all osm data into a database and use database indices to speed up the search process..

OSM File

library(tidyverse)
library(osmdata)
library(osmar) # (geosphere is inclued in osmar)
library(sf)
library(ggmap)
library(prettymapr)
library(leaflet)
library(igraph)
library(stplanr)
library(rgeos)

src <- osmsource_osmosis(file = "streets_bayern.osm")
muc_bbox <- center_bbox(11.829831, 48.110075, 60000, 60000)
muc <- get_osm(muc_bbox, src)
hways_muc<-muc

gr_muc <- as_igraph(hways_muc)

unschaerfe=0.002
hway_start_node <- local({
  #start in wolfesing
  #id <- find(muc, node(attrs(lon > 11.82983 & lat > 48.11007)))[1]
  id<-find(muc, node(attrs(lon > 11.82983 & lon < (11.82983+unschaerfe) & lat > 48.11007 & lat< (48.11007+unschaerfe))))[1]
  #id<-find(muc, node(tags(v %agrep% "Sendlinger Tor")))[1]
  find_nearest_node(muc, id, way(tags(k == "highway")))
})

hway_start <- subset(muc, node(hway_start_node))

hway_end_node <- local({
  #end in taufkirchen
  #id <- find(muc, node(attrs(lon > 11.85355 & lat > 48.19067)))[1]
  #id<-find(muc, node(attrs(lon > 11.85355 & lon<11.85400 & lat > 48.19067 & lat < 48.19180 )))[1]
  id<-find(muc, node(attrs(lon > 12.20747 & lon<(12.20747+unschaerfe) & lat > 47.83937 & lat < (47.83937+unschaerfe) )))[1]
  
  find_nearest_node(muc, id, way(tags(k == "highway")))
  
})

hway_end <- subset(muc, node(hway_end_node))

route <- get.all.shortest.paths(gr_muc,
                                from = as.character(hway_start_node),
                               to = as.character(hway_end_node),
                               mode = "all")[[1]]
0 Answers
Related