Skip to content

Commit 27b7f2a

Browse files
committed
refactor
1 parent b3dca29 commit 27b7f2a

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

example.jl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using LightOSM, GeoInterface, Plots, DataFrames
2+
using ArchGDAL: createmultilinestring
3+
4+
g = graph_from_download(
5+
:place_name, place_name="moabit, berlin germany",
6+
network_type=:bike
7+
)
8+
# reverse coordinates for plotting
9+
reverse!.(g.node_coordinates)
10+
11+
g_simple, weights, node_gdf, edge_gdf = simplify_graph(g)
12+
13+
# join edges in mulitlinestring for faster plotting
14+
all_edges = createmultilinestring(coordinates.(edge_gdf.geom))
15+
16+
# node validation
17+
18+
# nodes from original graph
19+
plot(all_edges, color=:black, size=(1200,800))
20+
scatter!(first.(g.node_coordinates), last.(g.node_coordinates), color=:red)
21+
22+
# nodes from simplified graph
23+
plot(all_edges, color=:black, size=(1200,800))
24+
scatter!(node_gdf.geom, color=:green)
25+
26+
27+
# edge validation
28+
29+
function highway_gdf(osmg::OSMGraph)
30+
function _geometrize_way(way)
31+
createlinestring(map(id -> coordinates(osmg.nodes[id]), way.nodes))
32+
end
33+
geom = map(way -> _geometrize_way(way), values(osmg.highways))
34+
return DataFrame(; id = collect(keys(osmg.highways)), geom)
35+
end
36+

src/simplification.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ function path_to_endpoint(g::AbstractGraph, (ep, ep_succ)::Tuple{T,T}) where {T<
4545
return path
4646
end
4747

48+
function total_weight(g::OSMGraph, path::Vector{<:Integer})
49+
sum((g.weights[path[[i, i+1]]...] for i in 1:length(path)-1))
50+
end
4851
"""
4952
Build a new graph which simplifies the topology of osmg.graph.
5053
The resulting graph only contains intersections and dead ends from the original graph.
@@ -79,17 +82,17 @@ function simplify_graph(osmg::OSMGraph)
7982
for path in paths_to_reduce(g)
8083
u = index_mapping[first(path)]
8184
v = index_mapping[last(path)]
82-
edge_weight = sum((osmg.weights[i, i+1] for i in 1:length(path)-1))
85+
path_weight = total_weight(osmg, path)
8386
geo = createlinestring(osmg.node_coordinates[path])
8487

8588
if add_edge!(G_simplified, (u, v))
8689
key = 0
87-
weights[u, v] = edge_weight
90+
weights[u, v] = path_weight
8891
else # parallel edge
8992
key = sum((edge_gdf.u .== u) .& (edge_gdf.v .== v))
90-
weights[u, v] = min(edge_weight, weights[u, v])
93+
weights[u, v] = min(path_weight, weights[u, v])
9194
end
92-
push!(edge_gdf, (u, v, key, edge_weight, geo))
95+
push!(edge_gdf, (u, v, key, path_weight, geo))
9396
end
9497

9598
return G_simplified, weights, node_gdf, edge_gdf

0 commit comments

Comments
 (0)