@@ -55,7 +55,7 @@ function graph_from_object(osm_data_object::Union{XMLDocument,Dict};
5555 g. dijkstra_states = Vector {Vector{U}} (undef, length (g. nodes))
5656 end
5757
58- add_kdtree ! (g)
58+ add_kdtree_and_rtree ! (g)
5959 @info " Created OSMGraph object with kwargs: network_type=$network_type , weight_type=$weight_type , graph_type=$graph_type , precompute_dijkstra_states=$precompute_dijkstra_states , largest_connected_component=$largest_connected_component "
6060 return g
6161end
@@ -454,16 +454,76 @@ function add_dijkstra_states!(g::OSMGraph{U,T,W}) where {U <: Integer,T <: Integ
454454 set_dijkstra_state! (g, collect (vertices (g. graph)))
455455end
456456
457+ """
458+ get_cartesian_locations(g::OSMGraph)
459+
460+ Calculates the Cartesian location of all nodes in the graph.
461+
462+ Returns a 3-by-n matrix where each column is the `xyz` coordinates of a node. Column indices
463+ correspond to the `g.graph` vertex indices.
464+ """
465+ function get_cartesian_locations (g:: OSMGraph )
466+ node_locations = [index_to_node (g, index). location for index in 1 : nv (g. graph)]
467+ return to_cartesian (node_locations)
468+ end
469+
470+ """
471+ add_kdtree_and_rtree!(g::OSMGraph)
472+
473+ Adds k-d tree and R-tree to `OSMGraph` for finding nearest nodes and ways.
474+ """
475+ function add_kdtree_and_rtree! (g:: OSMGraph )
476+ cartesian_locations = get_cartesian_locations (g)
477+ add_kdtree! (g, cartesian_locations)
478+ add_rtree! (g, cartesian_locations)
479+ end
480+
457481"""
458482 add_kdtree!(g::OSMGraph)
483+ add_kdtree!(g::OSMGraph, cartesian_locations::Matrix{Float64})
459484
460485Adds KDTree to `OSMGraph` for finding nearest neighbours.
461486"""
462- function add_kdtree! (g:: OSMGraph )
463- node_locations = [node. location for (id, node) in g. nodes] # node locations must have the same order as node indices
464- cartesian_locations = to_cartesian (node_locations)
487+ function add_kdtree! (g:: OSMGraph , cartesian_locations:: Matrix{Float64} )
465488 g. kdtree = KDTree (cartesian_locations)
466489end
490+ function add_kdtree! (g:: OSMGraph )
491+ cartesian_locations = get_cartesian_locations (g)
492+ add_kdtree! (g, cartesian_locations)
493+ end
494+
495+ """
496+ add_rtree!(g::OSMGraph)
497+ add_rtree!(g::OSMGraph, cartesian_locations::Matrix{Float64})
498+
499+ Adds an R-tree to `OSMGraph` for finding nearest ways.
500+
501+ # Warning
502+ Make sure to suppress outputs!
503+ Behaviour as of SpatialIndexing.jl 0.1.3 will print a line for every single OSM way, which
504+ will flood the terminal if not suppressed. Use with caution for now.
505+ """
506+ function add_rtree! (g:: OSMGraph{U,T,W} , cartesian_locations:: Matrix{Float64} ) where {U,T,W}
507+ # Get bounding box for every way ID
508+ way_ids = collect (keys (g. ways))
509+ data = map (way_ids) do way_id
510+ node_indices = node_id_to_index (g, g. ways[way_id]. nodes)
511+ x = [cartesian_locations[1 ,i] for i in node_indices]
512+ y = [cartesian_locations[2 ,i] for i in node_indices]
513+ z = [cartesian_locations[3 ,i] for i in node_indices]
514+ min_pt = (minimum (x), minimum (y), minimum (z))
515+ max_pt = (maximum (x), maximum (y), maximum (z))
516+ return SpatialElem (SpatialIndexing. Rect (min_pt, max_pt), way_id, nothing )
517+ end
518+
519+ tree = RTree {Float64,3} (T, Nothing)
520+ SpatialIndexing. load! (tree, data)
521+ g. rtree = tree
522+ end
523+ function add_rtree! (g:: OSMGraph )
524+ cartesian_locations = get_cartesian_locations (g)
525+ add_rtree! (g, cartesian_locations)
526+ end
467527
468528"""
469529 get_graph_type(g::OSMGraph)
0 commit comments