Skip to content

Commit 8df4d38

Browse files
committed
add AbstractOSMGraph and plot recipe
1 parent 15d06c7 commit 8df4d38

File tree

5 files changed

+69
-4
lines changed

5 files changed

+69
-4
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ version = "0.1.19"
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"
1314
LightXML = "9c8b4983-aa76-5018-a973-4c85ecc9e179"
1415
MetaGraphs = "626554b9-1ddb-594c-aa3c-2596fe9399a5"
1516
NearestNeighbors = "b8a86587-4115-5ab1-83bc-aa920d37bbce"
1617
Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
18+
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
1719
SimpleWeightedGraphs = "47aef6b3-ad0c-573a-a1e2-d07658019622"
1820
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
1921
StaticGraphs = "4c8beaf5-199b-59a0-a7f2-21d17de635b6"

src/LightOSM.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ using StaticGraphs: StaticDiGraph
99
using SimpleWeightedGraphs: SimpleWeightedDiGraph
1010
using MetaGraphs: MetaDiGraph
1111
using NearestNeighbors: KDTree, knn
12-
using ArchGDAL: IGeometry, createlinestring, createpoint
1312
using HTTP
1413
using JSON
1514
using LightXML
1615
using DataFrames
16+
using ArchGDAL: createmultilinestring, createlinestring, createpoint, addgeom!
17+
using RecipesBase
1718

1819
export GeoLocation,
20+
AbstractOSMGraph,
1921
OSMGraph,
22+
SimplifiedOSMGraph,
2023
Node,
2124
Way,
2225
Restriction,
@@ -36,7 +39,10 @@ export GeoLocation,
3639
buildings_from_object,
3740
buildings_from_download,
3841
buildings_from_file,
39-
simplify_graph
42+
simplify_graph,
43+
node_gdf,
44+
edge_gdf,
45+
highway_gdf
4046

4147
include("types.jl")
4248
include("constants.jl")
@@ -50,5 +56,7 @@ include("shortest_path.jl")
5056
include("nearest_node.jl")
5157
include("buildings.jl")
5258
include("simplification.jl")
59+
include("geodataframes.jl")
60+
include("plotrecipes.jl")
5361

5462
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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ Container for storing OpenStreetMap node, way, relation and graph related obejct
9999
- `kdtree::Union{KDTree,Nothing}`: KDTree used to calculate nearest nodes.
100100
- `weight_type::Union{Symbol,Nothing}`: Either `:distance`, `:time` or `:lane_efficiency`.
101101
"""
102-
@with_kw mutable struct OSMGraph{U <: Integer,T <: Integer,W <: Real}
102+
103+
abstract type AbstractOSMGraph end
104+
@with_kw mutable struct OSMGraph{U <: Integer,T <: Integer,W <: Real} <: AbstractOSMGraph
103105
nodes::Dict{T,Node{T}} = Dict{T,Node{T}}()
104106
node_coordinates::Vector{Vector{W}} = Vector{Vector{W}}() # needed for astar heuristic
105107
highways::Dict{T,Way{T}} = Dict{T,Way{T}}()
@@ -116,7 +118,7 @@ Container for storing OpenStreetMap node, way, relation and graph related obejct
116118
weight_type::Union{Symbol,Nothing} = nothing
117119
end
118120

119-
struct SimplifiedOSMGraph{U <: Integer,T <: Integer,W <: Real}
121+
struct SimplifiedOSMGraph{U <: Integer,T <: Integer,W <: Real} <: AbstractOSMGraph
120122
parent::OSMGraph{U,T,W}
121123
node_coordinates::Vector{Vector{W}} # needed for astar heuristic
122124
node_to_index::OrderedDict{T,U}

0 commit comments

Comments
 (0)