Skip to content

Commit 9593c88

Browse files
committed
add AbstractOSMGraph and plot recipe
1 parent 0a728bb commit 9593c88

File tree

5 files changed

+71
-3
lines changed

5 files changed

+71
-3
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ version = "0.3.1"
77
ArchGDAL = "c9ce4bd3-c3d5-55b8-8973-c0e20141b8c3"
88
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
99
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
10+
GeoInterface = "cf35fbd7-0cd7-5166-be24-54bfbe79505f"
1011
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
1112
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
1213
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
@@ -15,6 +16,7 @@ MetaGraphs = "626554b9-1ddb-594c-aa3c-2596fe9399a5"
1516
NearestNeighbors = "b8a86587-4115-5ab1-83bc-aa920d37bbce"
1617
Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
1718
QuickHeaps = "30b38841-0f52-47f8-a5f8-18d5d4064379"
19+
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
1820
SimpleWeightedGraphs = "47aef6b3-ad0c-573a-a1e2-d07658019622"
1921
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
2022
SpatialIndexing = "d4ead438-fe20-5cc5-a293-4fd39a41b74c"

src/LightOSM.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ using StaticArrays
1717
using SpatialIndexing
1818
using ArchGDAL: IGeometry, createlinestring, createpoint
1919
using DataFrames
20+
using ArchGDAL: createmultilinestring, createlinestring, createpoint, addgeom!
21+
using RecipesBase
2022

2123
export GeoLocation,
24+
AbstractOSMGraph,
2225
OSMGraph,
26+
SimplifiedOSMGraph,
2327
Node,
2428
Way,
2529
EdgePoint,
@@ -70,7 +74,10 @@ export index_to_node_id,
7074
set_dijkstra_state_with_node_id!,
7175
maxspeed_from_index,
7276
maxspeed_from_node_id
73-
simplify_graph
77+
simplify_graph,
78+
node_gdf,
79+
edge_gdf,
80+
highway_gdf
7481

7582
include("types.jl")
7683
include("constants.jl")
@@ -87,5 +94,7 @@ include("nearest_way.jl")
8794
include("buildings.jl")
8895
include("subgraph.jl")
8996
include("simplification.jl")
97+
include("geodataframes.jl")
98+
include("plotrecipes.jl")
9099

91100
end # module

src/geodataframes.jl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
coordinates(node::Node) = node.location.lon, node.location.lat
2+
3+
function node_gdf(g::OSMGraph)
4+
ids = collect(keys(g.nodes))
5+
geom = map(ids) do id
6+
coordinates(g.nodes[id])
7+
end
8+
return DataFrame(;id=ids, geom=createpoint.(geom))
9+
end
10+
11+
function highway_gdf(g::OSMGraph)
12+
ids = collect(keys(g.highways))
13+
_way_coordinates(way) = map(way.nodes) do id
14+
coordinates(g.nodes[id])
15+
end
16+
geom = map(id -> _way_coordinates(g.highways[id]), ids)
17+
return DataFrame(;id=ids, geom=createlinestring.(geom))
18+
end
19+
20+
function node_gdf(sg::SimplifiedOSMGraph)
21+
ids = collect(keys(sg.node_to_index))
22+
geom = map(ids) do id
23+
coordinates(sg.parent.nodes[id])
24+
end
25+
return DataFrame(;id=ids, geom=createpoint.(geom))
26+
end
27+
28+
highway_gdf(sg::SimplifiedOSMGraph) = highway_gdf(sg.parent)
29+
30+
function edge_gdf(sg::SimplifiedOSMGraph)
31+
edge_ids = collect(keys(sg.edges))
32+
geom = map(edge_ids) do edge
33+
path = sg.edges[edge]
34+
reverse.(sg.parent.node_coordinates[path])
35+
end
36+
u, v, key = map(i -> getindex.(edge_ids, i), 1:3)
37+
return DataFrame(;u, v, key, geom=createlinestring.(geom))
38+
end

src/plotrecipes.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function aspect_ratio(g::OSMGraph)
2+
max_y, min_y = extrema(first, g.node_coordinates)
3+
mid_y = max_y/2 + min_y/2
4+
return 1/cos(mid_y * pi/180)
5+
end
6+
aspect_ratio(sg::SimplifiedOSMGraph) = aspect_ratio(sg.parent)
7+
8+
9+
RecipesBase.@recipe function f(g::AbstractOSMGraph)
10+
color --> :black
11+
aspect_ratio --> aspect_ratio(g)
12+
highways = createmultilinestring()
13+
addgeom!.(Ref(highways), highway_gdf(g).geom)
14+
return highways
15+
end

src/types.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ Container for storing OpenStreetMap node, way, relation and graph related obejct
132132
- `kdtree::Union{RTree,Nothing}`: R-tree used to calculate nearest nodes.
133133
- `weight_type::Union{Symbol,Nothing}`: Either `:distance`, `:time` or `:lane_efficiency`.
134134
"""
135-
@with_kw mutable struct OSMGraph{U <: Integer,T <: Union{Integer, String},W <: Real}
135+
136+
137+
abstract type AbstractOSMGraph end
138+
@with_kw mutable struct OSMGraph{U <: Integer,T <: Union{Integer, String},W <: Real} <: AbstractOSMGraph
136139
nodes::Dict{T,Node{T}} = Dict{T,Node{T}}()
137140
node_coordinates::Vector{Vector{W}} = Vector{Vector{W}}() # needed for astar heuristic
138141
ways::Dict{T,Way{T}} = Dict{T,Way{T}}()
@@ -164,8 +167,9 @@ function Base.getproperty(g::OSMGraph, field::Symbol)
164167
else
165168
return getfield(g, field)
166169
end
170+
end
167171

168-
struct SimplifiedOSMGraph{U <: Integer, T <: Union{Integer, String}, W <: Real}
172+
struct SimplifiedOSMGraph{U <: Integer, T <: Union{Integer, String}, W <: Real} <: AbstractOSMGraph
169173
parent::OSMGraph{U,T,W}
170174
node_coordinates::Vector{Vector{W}} # needed for astar heuristic
171175
node_to_index::OrderedDict{T,U}

0 commit comments

Comments
 (0)