4

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]]
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Andreas
  • 397
  • 4
  • 18
  • 37
  • 1
    Hi! Can you share a [minimal example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) of your problem? – agila Jun 10 '20 at 14:20
  • Hi @agila i added the code as requested. – Andreas Jun 10 '20 at 19:17
  • Maybe it's a dumb question, but how did you get the "streets_bayern.osm" file? – agila Jun 11 '20 at 14:34
  • no problem. Its a one i filter from bigger files. But you can use the way it is demonstrated in the source i link to: download.file("http://osmar.r-forge.r-project.org/muenchen.osm.gz", "muenchen.osm.gz") system("gzip -d muenchen.osm.gz") ### Import subset based on bounding box: ############################# src <- osmsource_osmosis(file = "muenchen.osm", – Andreas Jun 11 '20 at 16:05
  • The problem is that I should also change all locations, no? Could you share the `streets_bayern.osm` file? Moreover if you are not super interested in using `osmar` I could provide alternative approaches using [`stplanr`](https://cran.r-project.org/web/packages/stplanr/index.html), [`dodgr`](https://cran.r-project.org/web/packages/dodgr/index.html) and [`sfnetworks`](https://github.com/luukvdmeer/sfnetworks). – agila Jun 11 '20 at 19:57
  • 1
    I will upload the file soon. Thank you very much for the suggestion. I would be interested in stplanr and dodgr. – Andreas Jun 12 '20 at 09:07
  • @agila you can find this file at https://send.firefox.com/download/e9af5d4798175d64/#PD-dnLKRtuvMrAM5Frl3lg it is a zip file. – Andreas Jun 20 '20 at 10:38

0 Answers0