Skip to content

Commit a7927d1

Browse files
committed
separate out graph weights generation from assignment to graph.
1 parent 0ffe96e commit a7927d1

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

Diff for: src/graph.jl

+22-10
Original file line numberDiff line numberDiff line change
@@ -355,17 +355,17 @@ function add_indexed_restrictions!(g::OSMGraph{U,T,W}) where {U <: DEFAULT_OSM_I
355355
end
356356

357357
"""
358-
add_weights!(g::OSMGraph, weight_type::Symbol=:distance)
358+
generate_weights(g::OSMGraph; weight_type::Symbol=:distance)
359359
360-
Adds edge weights to `OSMGraph`.
360+
Generates a sparse weights matrix for the graph.
361361
"""
362-
function add_weights!(g::OSMGraph, weight_type::Symbol=:distance)
362+
function generate_weights(g::OSMGraph; weight_type::Symbol=:distance)
363363
n_edges = length(g.edge_to_way)
364364
o_indices = Vector{Int}(undef, n_edges) # edge origin node indices
365365
d_indices = Vector{Int}(undef, n_edges) # edge destination node indices
366366
weights = Vector{Float64}(undef, n_edges)
367367

368-
W = DEFAULT_OSM_EDGE_WEIGHT_TYPE
368+
W = LightOSM.DEFAULT_OSM_EDGE_WEIGHT_TYPE
369369

370370
@inbounds for (i, edge) in enumerate(keys(g.edge_to_way))
371371
o_loc = g.nodes[edge[1]].location
@@ -377,23 +377,35 @@ function add_weights!(g::OSMGraph, weight_type::Symbol=:distance)
377377
dist = distance(o_loc, d_loc, :haversine)
378378
if weight_type == :time || weight_type == :lane_efficiency
379379
highway = g.edge_to_way[edge]
380-
maxspeed = g.ways[highway].tags["maxspeed"]::DEFAULT_OSM_MAXSPEED_TYPE
380+
maxspeed = g.ways[highway].tags["maxspeed"]::LightOSM.DEFAULT_OSM_MAXSPEED_TYPE
381381
if weight_type == :time
382382
weight = dist / maxspeed
383383
else
384-
lanes = g.ways[highway].tags["lanes"]::DEFAULT_OSM_LANES_TYPE
385-
lane_efficiency = get(LANE_EFFICIENCY[], lanes, 1.0)
384+
lanes = g.ways[highway].tags["lanes"]::LightOSM.DEFAULT_OSM_LANES_TYPE
385+
lane_efficiency = get(LightOSM.LANE_EFFICIENCY[], lanes, 1.0)
386386
weight = dist / (maxspeed * lane_efficiency)
387387
end
388-
else
389-
# Distance
388+
elseif weight_type == :distance
390389
weight = dist
390+
else
391+
weight = Float64[]
391392
end
392393
weights[i] = max(weight, eps(W))
393394
end
394395

395396
n = length(g.nodes)
396-
g.weights = sparse(o_indices, d_indices, weights, n, n)
397+
weights_matrix = sparse(o_indices, d_indices, weights, n, n)
398+
399+
weights_matrix
400+
end
401+
402+
"""
403+
add_weights!(g::OSMGraph, weight_type::Symbol=:distance)
404+
405+
Adds edge weights to `OSMGraph`.
406+
"""
407+
function add_weights!(g::OSMGraph, weight_type::Symbol=:distance)
408+
g.weights = generate_weights(g; weight_type)
397409
g.weight_type = weight_type
398410
return g
399411
end

0 commit comments

Comments
 (0)