Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ authors = ["Jack Chan <[email protected]>"]
version = "0.3.1"

[deps]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
GeoInterface = "cf35fbd7-0cd7-5166-be24-54bfbe79505f"
GeoInterfaceRecipes = "0329782f-3d07-4b52-b9f6-d3137cf03c7a"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
Expand All @@ -13,6 +16,7 @@ MetaGraphs = "626554b9-1ddb-594c-aa3c-2596fe9399a5"
NearestNeighbors = "b8a86587-4115-5ab1-83bc-aa920d37bbce"
Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
QuickHeaps = "30b38841-0f52-47f8-a5f8-18d5d4064379"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
SimpleWeightedGraphs = "47aef6b3-ad0c-573a-a1e2-d07658019622"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
SpatialIndexing = "d4ead438-fe20-5cc5-a293-4fd39a41b74c"
Expand Down
38 changes: 38 additions & 0 deletions example.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using LightOSM, Plots, GeoInterfaceRecipes
using StatsBase: sample

g = graph_from_download(
:place_name,
place_name="tiergarten, berlin germany",
network_type=:drive
)
sg = simplify_graph(g)

# Set plot size
size = (1920, 1080)

# Show original nodes
plot(g; size)
savefig("original_nodes")

# Show relevant nodes
plot(sg; size)
savefig("relevant_nodes")



osm_ids = sample(collect(values(sg.nodes)), 200)
for source in osm_ids
for target in osm_ids
path = shortest_path(g, source, target)
path_simplified = shortest_path(sg, source, target)
if isnothing(path) || isnothing(path_simplified)
continue
end
path_length = total_path_weight(g, path)
path_simplified_length = total_path_weight(sg, path_simplified)
if !isapprox(path_length, path_simplified_length)
error("Path from $source to $target is $path_length in the original graph and $path_simplified_length in the simplified graph")
end
end
end
16 changes: 14 additions & 2 deletions src/LightOSM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using DataStructures: DefaultDict, OrderedDict, MutableLinkedList
using QuickHeaps: BinaryHeap, FastMin
using Statistics: mean
using SparseArrays: SparseMatrixCSC, sparse, findnz
using Graphs: AbstractGraph, DiGraph, nv, outneighbors, weakly_connected_components, vertices
using Graphs: AbstractGraph, DiGraph, nv, outneighbors, weakly_connected_components, vertices, all_neighbors, indegree, outdegree, add_edge!
using StaticGraphs: StaticDiGraph
using SimpleWeightedGraphs: SimpleWeightedDiGraph
using MetaGraphs: MetaDiGraph, set_prop!
Expand All @@ -15,9 +15,14 @@ using JSON
using LightXML
using StaticArrays
using SpatialIndexing
using DataFrames
using GeoInterface: MultiLineString, LineString, Point
using RecipesBase

export GeoLocation,
AbstractOSMGraph,
OSMGraph,
SimplifiedOSMGraph,
Node,
Way,
EdgePoint,
Expand Down Expand Up @@ -67,7 +72,11 @@ export index_to_node_id,
set_dijkstra_state_with_index!,
set_dijkstra_state_with_node_id!,
maxspeed_from_index,
maxspeed_from_node_id
maxspeed_from_node_id,
simplify_graph,
node_gdf,
edge_gdf,
way_gdf

include("types.jl")
include("constants.jl")
Expand All @@ -83,5 +92,8 @@ include("nearest_node.jl")
include("nearest_way.jl")
include("buildings.jl")
include("subgraph.jl")
include("simplification.jl")
include("geodataframes.jl")
include("plotrecipes.jl")

end # module
24 changes: 24 additions & 0 deletions src/geodataframes.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Point(node::Node) = Point(node.location.lon, node.location.lat)
LineString(g::AbstractOSMGraph, way::Way) = LineString(Point.(g.parent.nodes[i] for i in way.nodes))

function node_gdf(g::AbstractOSMGraph)
ids = collect(keys(g.nodes))
nodes = (g.nodes[id] for id in ids)
return DataFrame(;id=ids, tags=getproperty.(nodes, :tags), geom=Point.(nodes))
end

function way_gdf(g::AbstractOSMGraph)
ids = collect(keys(g.ways))
ways = (g.ways[id] for id in ids)
return DataFrame(;id=ids, tags=getproperty.(ways, :tags), geom=LineString.(Ref(g), ways))
end

function edge_gdf(g::SimplifiedOSMGraph)
edge_ids = collect(keys(g.edges))
geom = map(edge_ids) do edge
path = g.edges[edge]
LineString(Point.(g.nodes[path]))
end
u, v, key = map(i -> getindex.(edge_ids, i), 1:3)
return DataFrame(;u, v, key, geom=geom)
end
52 changes: 26 additions & 26 deletions src/graph_utilities.jl
Original file line number Diff line number Diff line change
@@ -1,67 +1,67 @@
"""
index_to_node_id(g::OSMGraph, x::DEFAULT_OSM_INDEX_TYPE)
index_to_node_id(g::OSMGraph, x::Vector{<:DEFAULT_OSM_INDEX_TYPE})
index_to_node_id(g::AbstractOSMGraph, x::DEFAULT_OSM_INDEX_TYPE)
index_to_node_id(g::AbstractOSMGraph, x::Vector{<:DEFAULT_OSM_INDEX_TYPE})

Maps node index to node id.
"""
index_to_node_id(g::OSMGraph, x::DEFAULT_OSM_INDEX_TYPE) = g.index_to_node[x]
index_to_node_id(g::OSMGraph, x::Vector{<:DEFAULT_OSM_INDEX_TYPE}) = [index_to_node_id(g, i) for i in x]
index_to_node_id(g::AbstractOSMGraph, x::DEFAULT_OSM_INDEX_TYPE) = g.index_to_node[x]
index_to_node_id(g::AbstractOSMGraph, x::Vector{<:DEFAULT_OSM_INDEX_TYPE}) = [index_to_node_id(g, i) for i in x]

"""
index_to_node(g::OSMGraph, x::DEFAULT_OSM_INDEX_TYPE)
index_to_node(g::OSMGraph, x::Vector{<:DEFAULT_OSM_INDEX_TYPE})
index_to_node(g::AbstractOSMGraph, x::DEFAULT_OSM_INDEX_TYPE)
index_to_node(g::AbstractOSMGraph, x::Vector{<:DEFAULT_OSM_INDEX_TYPE})

Maps node index to node object.
"""
index_to_node(g::OSMGraph, x::DEFAULT_OSM_INDEX_TYPE) = g.nodes[g.index_to_node[x]]
index_to_node(g::OSMGraph, x::Vector{<:DEFAULT_OSM_INDEX_TYPE}) = [index_to_node(g, i) for i in x]
index_to_node(g::AbstractOSMGraph, x::DEFAULT_OSM_INDEX_TYPE) = g.nodes[g.index_to_node[x]]
index_to_node(g::AbstractOSMGraph, x::Vector{<:DEFAULT_OSM_INDEX_TYPE}) = [index_to_node(g, i) for i in x]

"""
node_id_to_index(g::OSMGraph, x::DEFAULT_OSM_ID_TYPE)
node_id_to_index(g::OSMGraph, x::Vector{<:DEFAULT_OSM_ID_TYPE})
node_id_to_index(g::AbstractOSMGraph, x::DEFAULT_OSM_ID_TYPE)
node_id_to_index(g::AbstractOSMGraph, x::Vector{<:DEFAULT_OSM_ID_TYPE})

Maps node id to index.
"""
node_id_to_index(g::OSMGraph, x::DEFAULT_OSM_ID_TYPE) = g.node_to_index[x]
node_id_to_index(g::OSMGraph, x::Vector{<:DEFAULT_OSM_ID_TYPE}) = [node_id_to_index(g, i) for i in x]
node_id_to_index(g::AbstractOSMGraph, x::DEFAULT_OSM_ID_TYPE) = g.node_to_index[x]
node_id_to_index(g::AbstractOSMGraph, x::Vector{<:DEFAULT_OSM_ID_TYPE}) = [node_id_to_index(g, i) for i in x]
"""
node_to_index(g::OSMGraph, x::Node)
node_to_index(g::OSMGraph, x::Vector{Node})
node_to_index(g::AbstractOSMGraph, x::Node)
node_to_index(g::AbstractOSMGraph, x::Vector{Node})

Maps node object to index.
"""
node_to_index(g::OSMGraph, x::Node) = g.node_to_index[x.id]
node_to_index(g::OSMGraph, x::Vector{Node}) = [node_id_to_index(g, i.id) for i in x]
node_to_index(g::AbstractOSMGraph, x::Node) = g.node_to_index[x.id]
node_to_index(g::AbstractOSMGraph, x::Vector{Node}) = [node_id_to_index(g, i.id) for i in x]

"""
index_to_dijkstra_state(g::OSMGraph, x::DEFAULT_OSM_INDEX_TYPE)
index_to_dijkstra_state(g::AbstractOSMGraph, x::DEFAULT_OSM_INDEX_TYPE)

Maps node index to dijkstra state (parents).
"""
index_to_dijkstra_state(g::OSMGraph, x::DEFAULT_OSM_INDEX_TYPE) = g.dijkstra_states[x]
index_to_dijkstra_state(g::AbstractOSMGraph, x::DEFAULT_OSM_INDEX_TYPE) = g.dijkstra_states[x]
"""
node_id_to_dijkstra_state(g::OSMGraph, x::DEFAULT_OSM_ID_TYPE)
node_id_to_dijkstra_state(g::AbstractOSMGraph, x::DEFAULT_OSM_ID_TYPE)

Maps node id to dijkstra state (parents).
"""
node_id_to_dijkstra_state(g::OSMGraph, x::DEFAULT_OSM_ID_TYPE) = g.dijkstra_states[node_id_to_index(g, x)]
node_id_to_dijkstra_state(g::AbstractOSMGraph, x::DEFAULT_OSM_ID_TYPE) = g.dijkstra_states[node_id_to_index(g, x)]
"""
set_dijkstra_state_with_index!(g::OSMGraph, index::DEFAULT_OSM_INDEX_TYPE, state)
set_dijkstra_state_with_index!(g::AbstractOSMGraph, index::DEFAULT_OSM_INDEX_TYPE, state)

Set dijkstra state (parents) with node index.
"""
set_dijkstra_state_with_index!(g::OSMGraph, index::DEFAULT_OSM_INDEX_TYPE, state) = push!(g.dijkstra_states, index, state)
set_dijkstra_state_with_index!(g::AbstractOSMGraph, index::DEFAULT_OSM_INDEX_TYPE, state) = push!(g.dijkstra_states, index, state)
"""
set_dijkstra_state_with_node_id!(g::OSMGraph, index::DEFAULT_OSM_ID_TYPE, state)
set_dijkstra_state_with_node_id!(g::AbstractOSMGraph, index::DEFAULT_OSM_ID_TYPE, state)

Set dijkstra state (parents) with node id.
"""
set_dijkstra_state_with_node_id!(g::OSMGraph, node_id::DEFAULT_OSM_ID_TYPE, state) = push!(g.dijkstra_states, node_id_to_index(g, node_id), state)
set_dijkstra_state_with_node_id!(g::AbstractOSMGraph, node_id::DEFAULT_OSM_ID_TYPE, state) = push!(g.dijkstra_states, node_id_to_index(g, node_id), state)
"""
maxspeed_from_index(g, x::DEFAULT_OSM_INDEX_TYPE)
maxspeed_from_node_id(g, x::DEFAULT_OSM_ID_TYPE)

Get maxspeed from index id or node id.
"""
maxspeed_from_index(g, x::DEFAULT_OSM_INDEX_TYPE) = index_to_node(g, x).tags["maxspeed"]
maxspeed_from_node_id(g, x::DEFAULT_OSM_ID_TYPE) = g.nodes[x].tags["maxspeed"]
maxspeed_from_index(g::AbstractOSMGraph, x::DEFAULT_OSM_INDEX_TYPE) = index_to_node(g, x).tags["maxspeed"]
maxspeed_from_node_id(g::AbstractOSMGraph, x::DEFAULT_OSM_ID_TYPE) = g.nodes[x].tags["maxspeed"]
29 changes: 29 additions & 0 deletions src/plotrecipes.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function aspect_ratio(g::AbstractOSMGraph)
max_y, min_y = extrema(first, g.node_coordinates)
mid_y = (max_y + min_y)/2
return 1/cos(mid_y * pi/180)
end

RecipesBase.@recipe function f(g::AbstractOSMGraph)
# set the aspect ratio
aspect_ratio --> aspect_ratio(g)

# way color and thickness
color --> :black
linewdith --> 1.5
# node color and size
markercolor --> :blue
markersize --> 2

# plot ways
@series begin
seriestype := :path
MultiLineString(way_gdf(g).geom)
end

# plot nodes
@series begin
seriestype := :scatter
node_gdf(g).geom
end
end
Loading