Skip to content

Commit

Permalink
upgrade LightGraphs.jl to Graphs.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
pszufe committed Jan 9, 2022
1 parent 8974b37 commit aaf89af
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 29 deletions.
8 changes: 4 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
name = "OpenStreetMapX"
uuid = "86cd37e6-c0ff-550b-95fe-21d72c8d4fc9"
authors = ["Przemyslaw Szufel <[email protected]>", "Bartosz Pankratz <[email protected]>", "Anna Szczurek <[email protected]>", "Bogumil Kaminski <[email protected]>", "Pawel Pralat <[email protected]>"]
version = "0.2.7"
version = "0.3.0"

[deps]
CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
LibExpat = "522f3ed2-3f36-55e3-b6df-e94fee9b0c07"
LightGraphs = "093fc24a-ae57-5d10-9952-331d41423f4d"
ProtoBuf = "3349acd9-ac6a-5e09-bcdb-63829b23a429"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
Expand All @@ -18,12 +18,12 @@ StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[compat]
CodecZlib = "^0.7.0"
DataStructures = "^0.17.0, ^0.18.0"
HTTP = "^0.7.0, ^0.8.0, ^0.9.0"
JSON = "^0.20.0, ^0.21.0"
LibExpat = "^0.6.0"
LightGraphs = "^1.3.3"
CodecZlib = "^0.7.0"
Graphs = "^1.4.1"
ProtoBuf = "=0.11.3"
StableRNGs = "^1.0.0"
julia = "^1.3.0"
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* Package for spatial analysis, simulation and visualization of Open Street Map data
* The plotting functionality is provided via a separate package [`OpenStreetMapXPlot.jl`](https://github.com/pszufe/OpenStreetMapXPlot.jl)

The goal of this package is to provide a backbone for multi-agent simulation of cities.
The goal of this package is to provide a backbone for multi-agent modelling and simulation of cities.

The package can parse `*.osm` and `*.pbf` (contributed by [@blegat](https://github.com/blegat/)) files and generate a LightGraphs representation along the metadata.
The package can parse `*.osm` and `*.pbf` (contributed by [@blegat](https://github.com/blegat/)) files and generate a Graphs.jl representation along the metadata.


| **Documentation** | **Build Status** |
Expand Down Expand Up @@ -94,7 +94,7 @@ In order to obtain the `*.osm` file follow the steps below:

Compared to the original package major changes include among many others:

- `LightGraphs.jl` is used for map data storage
- Nwe `Graphs.jl` is used for map data storage
- Several changes with routing algorithm (currently finding a route in a 1 million people city takes around 150ms)
- Added support for using Google Maps API for routing
- Data structure adjustment to make the library more suitable to run simulations of cities.
Expand Down
2 changes: 1 addition & 1 deletion samples/plotting_with_folium.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using OpenStreetMapX, LightGraphs, PyCall
using OpenStreetMapX, Graphs, PyCall

# This code assumes that folium has benn installed

Expand Down
2 changes: 1 addition & 1 deletion src/OpenStreetMapX.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module OpenStreetMapX

using LibExpat
using LightGraphs
using Graphs
using SparseArrays
using DataStructures
using Serialization
Expand Down
10 changes: 5 additions & 5 deletions src/a_star.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ end
a_star_algorithm(g::AbstractGraph{U},
s::Integer,
t::Integer,
distmx::AbstractMatrix{T}=LightGraphs.weights(g),
distmx::AbstractMatrix{T}=Graphs.weights(g),
heuristic::Function = (u,v) -> zero(T)) where {T, U}
High level function - implementation of A star search algorithm:
(https://en.wikipedia.org/wiki/A*_search_algorithm).
Based on the implementation in LightGraphs library,
Based on the implementation in Graphs library,
however significantly improved in terms of performance.
**Arguments**
Expand All @@ -52,10 +52,10 @@ however significantly improved in terms of performance.
* `distmx` : distance matrix
* `heuristic` : search heuristic function; by default returns zero
"""
function a_star_algorithm(g::LightGraphs.AbstractGraph{U}, # the g
function a_star_algorithm(g::Graphs.AbstractGraph{U}, # the g
s::Integer, # the start vertex
t::Integer, # the end vertex
distmx::AbstractMatrix{T}=LightGraphs.weights(g),
distmx::AbstractMatrix{T}=Graphs.weights(g),
heuristic::Function = (u,v) -> zero(T)) where {T, U}
nvg = nv(g)
checkbounds(distmx, Base.OneTo(nvg), Base.OneTo(nvg))
Expand All @@ -72,7 +72,7 @@ function a_star_algorithm(g::LightGraphs.AbstractGraph{U}, # the g
u = dequeue!(frontier)
cost_so_far = dists[u]
u == t && (return OpenStreetMapX.extract_a_star_route(parents,s,u), cost_so_far)
for v in LightGraphs.outneighbors(g, u)
for v in Graphs.outneighbors(g, u)
col = colormap[v]
if col < UInt8(2)
dist = distmx[u, v]
Expand Down
4 changes: 2 additions & 2 deletions src/parseMap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,13 @@ function MapData(mapdata::OSMData, road_levels::Set{Int}, only_intersections::Bo
J = edges[2:2:end]
# w - Edge weights, indexed by graph id
w = SparseArrays.sparse(I, J, weight_vals, length(v), length(v))
g = LightGraphs.DiGraph(length(v))
g = Graphs.DiGraph(length(v))
for edge in e
add_edge!(g,v[edge[1]], v[edge[2]])
end

if trim_to_connected_graph
conn_components = sort!(LightGraphs.strongly_connected_components(g),
conn_components = sort!(Graphs.strongly_connected_components(g),
lt=(x,y)->length(x)<length(y), rev=true)
remove_nodes = Set{Int}()
I = 2:length(conn_components)
Expand Down
14 changes: 7 additions & 7 deletions src/routing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ end
Dijkstra's Algorithm
"""
function dijkstra(m::MapData, w::SparseArrays.SparseMatrixCSC{Float64,Int64}, start_vertex::Int)
return LightGraphs.dijkstra_shortest_paths(m.g, start_vertex, w)
return Graphs.dijkstra_shortest_paths(m.g, start_vertex, w)
end

"""
Expand Down Expand Up @@ -102,7 +102,7 @@ end
"""
Extract route from Dijkstra results object
"""
function extract_route(dijkstra::LightGraphs.DijkstraState{Float64,Int64}, startIndex::Int, finishIndex::Int)
function extract_route(dijkstra::Graphs.DijkstraState{Float64,Int64}, startIndex::Int, finishIndex::Int)
route = Int[]
distance = dijkstra.dists[finishIndex]
if distance != Inf
Expand Down Expand Up @@ -284,8 +284,8 @@ Find waypoint minimizing the route
Approximate solution
"""
function find_optimal_waypoint_approx(m::MapData, weights::SparseArrays.SparseMatrixCSC{Float64,Int64}, node0::Int, node1::Int, waypoints::Dict{Int,Int})
dists_start_waypoint = LightGraphs.dijkstra_shortest_paths(m.g, m.v[node0], weights).dists
dists_waypoint_fin = LightGraphs.dijkstra_shortest_paths(m.g, m.v[node1], weights).dists
dists_start_waypoint = Graphs.dijkstra_shortest_paths(m.g, m.v[node0], weights).dists
dists_waypoint_fin = Graphs.dijkstra_shortest_paths(m.g, m.v[node1], weights).dists
node_id = NaN
min_dist = Inf
for (key,value) in waypoints
Expand All @@ -303,11 +303,11 @@ Find waypoint minimizing the route
Exact solution
"""
function find_optimal_waypoint_exact(m::MapData, weights::SparseArrays.SparseMatrixCSC{Float64,Int64}, node0::Int, node1::Int, waypoints::Dict{Int,Int})
dists_start_waypoint = LightGraphs.dijkstra_shortest_paths(m.g, m.v[node0], weights).dists
dists_start_waypoint = Graphs.dijkstra_shortest_paths(m.g, m.v[node0], weights).dists
node_id = NaN
min_dist = Inf
for (key,value) in waypoints
dist_to_fin = LightGraphs.dijkstra_shortest_paths(m.g, m.v[value], weights).dists[m.v[node1]]
dist_to_fin = Graphs.dijkstra_shortest_paths(m.g, m.v[value], weights).dists[m.v[node1]]
dist = dists_start_waypoint[m.v[value]] + dist_to_fin
if dist < min_dist
min_dist = dist
Expand All @@ -323,7 +323,7 @@ end

### Bellman Ford's Algorithm ###
function bellman_ford(m::MapData, w::SparseArrays.SparseMatrixCSC{Float64,Int64}, start_vertices::Vector{Int})
return LightGraphs.bellman_ford_shortest_paths(m.g, start_vertices, w)
return Graphs.bellman_ford_shortest_paths(m.g, start_vertices, w)
end

### Filter vertices from bellman_fordStates object ###
Expand Down
6 changes: 3 additions & 3 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ This is the main data structure used fot map data analytics.
* `nodes` : dictionary of nodes representing all the objects on the map (with coordinates in East, North, Up system)
* `roadways` : unique roads stored as a OpenStreetMapX.Way objects
* `intersections` : roads intersections
* `g` : `LightGraphs` directed graph representing a road network
* `g` : `Graphs` directed graph representing a road network
* `v` : vertices in the road network (node id .=> graph vertex)
* `n` : vector of OpenStreetMap node ids for each corresponding graph vertex
* `e` : vector of edges in the graph represented as a tuple (source,destination)
Expand All @@ -291,11 +291,11 @@ mutable struct MapData
roadways::Array{Way,1}
intersections::Dict{Int,Set{Int}}
# Transporation network graph data and helpers to increase routing speed
g::LightGraphs.SimpleGraphs.SimpleDiGraph{Int64} # Graph object
g::Graphs.SimpleGraphs.SimpleDiGraph{Int64} # Graph object
v::Dict{Int,Int} # (node id) => (graph vertex)
n::Vector{Int} # (graph vertex) => (node id)
e::Vector{Tuple{Int,Int}} # Edges in graph, stored as a tuple (source,destination)
w::SparseArrays.SparseMatrixCSC{Float64, Int} # Edge weights, indexed by graph id
class::Vector{Int} # Road class of each edge
#MapData(bounds, nodes, roadways, intersections) = new(bounds, nodes, roadways, intersections, LightGraphs.SimpleGraphs.SimpleDiGraph{Int64}(), Dict{Int,Int}(),Int[], Tuple{Int64,Int64}[], SparseMatrixCSC(Matrix{Float64}(undef,0,0)),Int[])
#MapData(bounds, nodes, roadways, intersections) = new(bounds, nodes, roadways, intersections, Graphs.SimpleGraphs.SimpleDiGraph{Int64}(), Dict{Int,Int}(),Int[], Tuple{Int64,Int64}[], SparseMatrixCSC(Matrix{Float64}(undef,0,0)),Int[])
end
6 changes: 3 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Test, OpenStreetMapX
using Random, StableRNGs
import LightGraphs
import Graphs

@testset "$ext" for ext in ["osm", "pbf"]
pth = joinpath(dirname(pathof(OpenStreetMapX)),"..","test","data","reno_east3.$ext")
Expand Down Expand Up @@ -68,13 +68,13 @@ import LightGraphs
#Returns seem to be equal yet returning false (?)
@test sort(distance(m.nodes,OpenStreetMapX.get_edges(m.nodes,m.roadways[1:2])[1])) sort([30.2013937293296, 7.243941886194111, 35.492758006997796, 12.29992029473937, 11.290063259013777])

conn_components = sort!(LightGraphs.strongly_connected_components(m.g),
conn_components = sort!(Graphs.strongly_connected_components(m.g),
lt=(x,y)->length(x)<length(y), rev=true)
@test length(conn_components)>1
@test length(conn_components[1])==1799

m2 = OpenStreetMapX.get_map_data(pth,use_cache = false, trim_to_connected_graph=true);
conn_components2 = sort!(LightGraphs.strongly_connected_components(m2.g),
conn_components2 = sort!(Graphs.strongly_connected_components(m2.g),
lt=(x,y)->length(x)<length(y), rev=true)
@test length(conn_components2)==1
@test length(conn_components[1])==length(conn_components2[1])
Expand Down

2 comments on commit aaf89af

@pszufe
Copy link
Owner Author

@pszufe pszufe commented on aaf89af Jan 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/51970

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.0 -m "<description of version>" aaf89afa16f0f055ab273e5bfe948ac1568e7963
git push origin v0.3.0

Please sign in to comment.