+
+ # wrapper to access the osm2pgsql node cache
+ class NodeCache
+ # node cache entry
+ class Node
+ attr_reader :lon, :lat
+
+ def initialize(lon, lat)
+ @lat = lat.to_f / 100.0
+ @lon = lon.to_f / 100.0
+ end
+ end
+
+ # open the cache
+ def initialize(filename)
+ @cache = Mmap.new(filename)
+
+ throw "Unexpected format" unless @cache[0..3].unpack("l").first == 1
+ throw "Unexpected ID size" unless @cache[4..7].unpack("l").first == 8
+
+ @max_id = @cache[8..15].unpack("q").first
+ end
+
+ # lookup a node
+ def [](id)
+ if id <= @max_id
+ offset = 16 + id * 8
+
+ lon, lat = @cache[offset..offset + 7].unpack("ll")
+
+ if lon != -2147483648 && lat != -2147483648
+ node = Node.new(lon, lat)
+ end
+ end
+
+ node
+ end
+ end