Skip to content

Commit c30254c

Browse files
ltokarevacaptchanjack
ltokareva
authored andcommitted
[FEAT] Changed id type to Union{Int, String}
1 parent c5a0b8f commit c30254c

21 files changed

+420
-236
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "LightOSM"
22
uuid = "d1922b25-af4e-4ba3-84af-fe9bea896051"
33
authors = ["Jack Chan <jchan2@deloitte.com.au>"]
4-
version = "0.2.12"
4+
version = "0.3.0"
55

66
[deps]
77
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"

src/constants.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""
22
Default data types used to construct OSMGraph object.
33
"""
4-
const DEFAULT_OSM_ID_TYPE = Int64
54
const DEFAULT_OSM_INDEX_TYPE = Int32
5+
const DEFAULT_OSM_ID_TYPE = Union{Integer, String}
66
const DEFAULT_OSM_EDGE_WEIGHT_TYPE = Float64
77
const DEFAULT_OSM_MAXSPEED_TYPE = Int16
88
const DEFAULT_OSM_LANES_TYPE = Int8

src/download.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ function download_osm_network(download_method::Symbol;
439439

440440
if !(save_to_file_location isa Nothing)
441441
save_to_file_location = validate_save_location(save_to_file_location, download_format)
442-
write(save_to_file_location, data)
442+
Base.write(save_to_file_location, data)
443443
@info "Saved osm network data to disk: $save_to_file_location"
444444
end
445445

src/graph.jl

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,11 @@ end
199199

200200

201201
"""
202-
add_node_and_edge_mappings!(g::OSMGraph{U,T,W}) where {U <: Integer,T <: Integer,W <: Real}
202+
add_node_and_edge_mappings!(g::OSMGraph{U,T,W}) where {U <: DEFAULT_OSM_INDEX_TYPE,T <: DEFAULT_OSM_ID_TYPE,W <: Real}
203203
204204
Adds mappings between nodes, edges and ways to `OSMGraph`.
205205
"""
206-
function add_node_and_edge_mappings!(g::OSMGraph{U,T,W}) where {U <: Integer,T <: Integer,W <: Real}
206+
function add_node_and_edge_mappings!(g::OSMGraph{U,T,W}) where {U <: DEFAULT_OSM_INDEX_TYPE,T <: DEFAULT_OSM_ID_TYPE,W <: Real}
207207
for (way_id, way) in g.ways
208208
@inbounds for (i, node_id) in enumerate(way.nodes)
209209
if haskey(g.node_to_way, node_id)
@@ -266,11 +266,11 @@ function add_node_tags!(g::OSMGraph)
266266
end
267267

268268
"""
269-
adjacent_node(g::OSMGraph, node::T, way::T)::Union{T,Vector{<:T}} where T <: Integer
269+
adjacent_node(g::OSMGraph, node::T, way::T)::Union{T,Vector{<:T}} where T <: DEFAULT_OSM_ID_TYPE
270270
271271
Finds the adjacent node id on a given way.
272272
"""
273-
function adjacent_node(g::OSMGraph, node::T, way::T)::Union{T,Vector{<:T}} where T <: Integer
273+
function adjacent_node(g::OSMGraph, node::T, way::T)::Union{T,Vector{<:T}} where T <: DEFAULT_OSM_ID_TYPE
274274
way_nodes = g.ways[way].nodes
275275
if node == way_nodes[1]
276276
return way_nodes[2]
@@ -293,14 +293,14 @@ function adjacent_node(g::OSMGraph, node::T, way::T)::Union{T,Vector{<:T}} where
293293
end
294294

295295
"""
296-
add_indexed_restrictions!(g::OSMGraph{U,T,W}) where {U <: Integer,T <: Integer,W <: Real}
296+
add_indexed_restrictions!(g::OSMGraph{U,T,W}) where {U <: DEFAULT_OSM_INDEX_TYPE, T <: DEFAULT_OSM_ID_TYPE, W <: Real}
297297
298298
Adds restrictions linked lists to `OSMGraph`.
299299
300300
# Example
301301
`[from_way_node_index, ...via_way_node_indices..., to_way_node_index]`
302302
"""
303-
function add_indexed_restrictions!(g::OSMGraph{U,T,W}) where {U <: Integer,T <: Integer,W <: Real}
303+
function add_indexed_restrictions!(g::OSMGraph{U,T,W}) where {U <: DEFAULT_OSM_INDEX_TYPE,T <: DEFAULT_OSM_ID_TYPE,W <: Real}
304304
g.indexed_restrictions = DefaultDict{U,Vector{MutableLinkedList{U}}}(Vector{MutableLinkedList{U}})
305305

306306
for (id, r) in g.restrictions
@@ -321,7 +321,7 @@ function add_indexed_restrictions!(g::OSMGraph{U,T,W}) where {U <: Integer,T <:
321321
from_node = adjacent_node(g, r.via_node, r.from_way)::T
322322
for to_way in restricted_to_ways
323323
to_node_temp = adjacent_node(g, r.via_node, to_way)
324-
to_node = isa(to_node_temp, Integer) ? [to_node_temp] : to_node_temp
324+
to_node = isa(to_node_temp, DEFAULT_OSM_ID_TYPE) ? [to_node_temp] : to_node_temp
325325

326326
for tn in to_node
327327
# only_straight_on restrictions may have multiple to_nodes
@@ -399,19 +399,19 @@ function add_weights!(g::OSMGraph, weight_type::Symbol=:distance)
399399
end
400400

401401
"""
402-
add_graph!(g::OSMGraph, graph_type::Symbol=:static)
402+
add_graph!(g::OSMGraph{U, T, W}, graph_type::Symbol=:static) where {U <: DEFAULT_OSM_INDEX_TYPE, T <: DEFAULT_OSM_ID_TYPE, W <: Real}
403403
404404
Adds a Graphs.AbstractGraph object to `OSMGraph`.
405405
"""
406-
function add_graph!(g::OSMGraph{U, T, W}, graph_type::Symbol=:static) where {U <: Integer, T <: Integer, W <: Real}
406+
function add_graph!(g::OSMGraph{U, T, W}, graph_type::Symbol=:static) where {U <: DEFAULT_OSM_INDEX_TYPE, T <: DEFAULT_OSM_ID_TYPE, W <: Real}
407407
if graph_type == :light
408-
g.graph = DiGraph{T}(g.weights)
408+
g.graph = DiGraph{U}(g.weights)
409409
elseif graph_type == :static
410410
g.graph = StaticDiGraph{U,U}(StaticDiGraph(DiGraph(g.weights)))
411411
elseif graph_type == :simple_weighted
412412
g.graph = SimpleWeightedDiGraph{U,W}(g.weights)
413413
elseif graph_type == :meta
414-
g.graph = MetaDiGraph(DiGraph{T}(g.weights))
414+
g.graph = MetaDiGraph(DiGraph{U}(g.weights))
415415
for (o, d, w) in zip(findnz(copy(transpose(g.weights)))...)
416416
set_prop!(g.graph, o, d, :weight, w)
417417
end
@@ -456,7 +456,7 @@ function trim_to_largest_connected_component!(g::OSMGraph{U, T, W}, graph, weigh
456456
end
457457

458458
"""
459-
add_dijkstra_states!(g::OSMGraph{U,T,W}) where {U <: Integer,T <: Integer,W <: Real}
459+
add_dijkstra_states!(g::OSMGraph{U,T,W}) where {U <: DEFAULT_OSM_INDEX_TYPE,T <: DEFAULT_OSM_ID_TYPE,W <: Real}
460460
461461
Adds precomputed dijkstra states for every source node in `OSMGraph`. Precomputing all dijkstra
462462
states is a O(V² + ElogV) operation, where E is the number of edges and V is the number of vertices,
@@ -465,9 +465,9 @@ may not be possible for larger graphs. Not recommended for graphs with greater t
465465
Note: Not using `cost_adjustment`, i.e. not consdering restrictions in dijkstra computation,
466466
consider adding in the future.
467467
"""
468-
function add_dijkstra_states!(g::OSMGraph{U,T,W}) where {U <: Integer,T <: Integer,W <: Real}
468+
function add_dijkstra_states!(g::OSMGraph{U,T,W}) where {U <: DEFAULT_OSM_INDEX_TYPE,T <: DEFAULT_OSM_ID_TYPE,W <: Real}
469469
@warn "Precomputing all dijkstra states is a O(V² + ElogV) operation, may not be possible for larger graphs."
470-
g.dijkstra_states = Vector{Vector{U}}(undef, n)
470+
g.dijkstra_states = Vector{Vector{U}}(undef, nv(g.graph))
471471
set_dijkstra_state!(g, collect(vertices(g.graph)))
472472
end
473473

@@ -480,7 +480,7 @@ Returns a 3-by-n matrix where each column is the `xyz` coordinates of a node. Co
480480
correspond to the `g.graph` vertex indices.
481481
"""
482482
function get_cartesian_locations(g::OSMGraph)
483-
node_locations = [index_to_node(g, index).location for index in 1:nv(g.graph)]
483+
node_locations = [index_to_node(g, index).location for index in DEFAULT_OSM_INDEX_TYPE(1):DEFAULT_OSM_INDEX_TYPE(nv(g.graph))]
484484
return to_cartesian(node_locations)
485485
end
486486

src/graph_utilities.jl

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
"""
2-
index_to_node_id(g::OSMGraph, x::Integer)
3-
index_to_node_id(g::OSMGraph, x::Vector{<:Integer})
2+
index_to_node_id(g::OSMGraph, x::DEFAULT_OSM_INDEX_TYPE)
3+
index_to_node_id(g::OSMGraph, x::Vector{<:DEFAULT_OSM_INDEX_TYPE})
44
55
Maps node index to node id.
66
"""
7-
index_to_node_id(g::OSMGraph, x::Integer) = g.index_to_node[x]
8-
index_to_node_id(g::OSMGraph, x::Vector{<:Integer}) = [index_to_node_id(g, i) for i in x]
7+
index_to_node_id(g::OSMGraph, x::DEFAULT_OSM_INDEX_TYPE) = g.index_to_node[x]
8+
index_to_node_id(g::OSMGraph, x::Vector{<:DEFAULT_OSM_INDEX_TYPE}) = [index_to_node_id(g, i) for i in x]
99

1010
"""
11-
index_to_node(g::OSMGraph, x::Integer)
12-
index_to_node(g::OSMGraph, x::Vector{<:Integer})
11+
index_to_node(g::OSMGraph, x::DEFAULT_OSM_INDEX_TYPE)
12+
index_to_node(g::OSMGraph, x::Vector{<:DEFAULT_OSM_INDEX_TYPE})
1313
1414
Maps node index to node object.
1515
"""
16-
index_to_node(g::OSMGraph, x::Integer) = g.nodes[g.index_to_node[x]]
17-
index_to_node(g::OSMGraph, x::Vector{<:Integer}) = [index_to_node(g, i) for i in x]
16+
index_to_node(g::OSMGraph, x::DEFAULT_OSM_INDEX_TYPE) = g.nodes[g.index_to_node[x]]
17+
index_to_node(g::OSMGraph, x::Vector{<:DEFAULT_OSM_INDEX_TYPE}) = [index_to_node(g, i) for i in x]
1818

1919
"""
20-
node_id_to_index(g::OSMGraph, x::Integer)
21-
node_id_to_index(g::OSMGraph, x::Vector{<:Integer})
20+
node_id_to_index(g::OSMGraph, x::DEFAULT_OSM_ID_TYPE)
21+
node_id_to_index(g::OSMGraph, x::Vector{<:DEFAULT_OSM_ID_TYPE})
2222
2323
Maps node id to index.
2424
"""
25-
node_id_to_index(g::OSMGraph, x::Integer) = g.node_to_index[x]
26-
node_id_to_index(g::OSMGraph, x::Vector{<:Integer}) = [node_id_to_index(g, i) for i in x]
27-
25+
node_id_to_index(g::OSMGraph, x::DEFAULT_OSM_ID_TYPE) = g.node_to_index[x]
26+
node_id_to_index(g::OSMGraph, x::Vector{<:DEFAULT_OSM_ID_TYPE}) = [node_id_to_index(g, i) for i in x]
2827
"""
2928
node_to_index(g::OSMGraph, x::Node)
3029
node_to_index(g::OSMGraph, x::Vector{Node})
@@ -35,38 +34,34 @@ node_to_index(g::OSMGraph, x::Node) = g.node_to_index[x.id]
3534
node_to_index(g::OSMGraph, x::Vector{Node}) = [node_id_to_index(g, i.id) for i in x]
3635

3736
"""
38-
index_to_dijkstra_state(g::OSMGraph, x::Integer)
37+
index_to_dijkstra_state(g::OSMGraph, x::DEFAULT_OSM_INDEX_TYPE)
3938
4039
Maps node index to dijkstra state (parents).
4140
"""
42-
index_to_dijkstra_state(g::OSMGraph, x::Integer) = g.dijkstra_states[x]
43-
41+
index_to_dijkstra_state(g::OSMGraph, x::DEFAULT_OSM_INDEX_TYPE) = g.dijkstra_states[x]
4442
"""
45-
node_id_to_dijkstra_state(g::OSMGraph, x::Integer)
43+
node_id_to_dijkstra_state(g::OSMGraph, x::DEFAULT_OSM_ID_TYPE)
4644
4745
Maps node id to dijkstra state (parents).
4846
"""
49-
node_id_to_dijkstra_state(g::OSMGraph, x::Integer) = g.dijkstra_states[node_id_to_index(g, x)]
50-
47+
node_id_to_dijkstra_state(g::OSMGraph, x::DEFAULT_OSM_ID_TYPE) = g.dijkstra_states[node_id_to_index(g, x)]
5148
"""
52-
set_dijkstra_state_with_index!(g::OSMGraph, index::Integer, state)
49+
set_dijkstra_state_with_index!(g::OSMGraph, index::DEFAULT_OSM_INDEX_TYPE, state)
5350
5451
Set dijkstra state (parents) with node index.
5552
"""
56-
set_dijkstra_state_with_index!(g::OSMGraph, index::Integer, state) = push!(g.dijkstra_states, index, state)
57-
53+
set_dijkstra_state_with_index!(g::OSMGraph, index::DEFAULT_OSM_INDEX_TYPE, state) = push!(g.dijkstra_states, index, state)
5854
"""
59-
set_dijkstra_state_with_node_id!(g::OSMGraph, index::Integer, state)
55+
set_dijkstra_state_with_node_id!(g::OSMGraph, index::DEFAULT_OSM_ID_TYPE, state)
6056
6157
Set dijkstra state (parents) with node id.
6258
"""
63-
set_dijkstra_state_with_node_id!(g::OSMGraph, node_id::Integer, state) = push!(g.dijkstra_states, node_id_to_index(g, node_id), state)
64-
59+
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)
6560
"""
66-
maxspeed_from_index(g, x::Integer)
67-
maxspeed_from_node_id(g, x::Integer)
61+
maxspeed_from_index(g, x::DEFAULT_OSM_INDEX_TYPE)
62+
maxspeed_from_node_id(g, x::DEFAULT_OSM_ID_TYPE)
6863
6964
Get maxspeed from index id or node id.
7065
"""
71-
maxspeed_from_index(g, x::Integer) = index_to_node(g, x).tags["maxspeed"]
72-
maxspeed_from_node_id(g, x::Integer) = g.nodes[x].tags["maxspeed"]
66+
maxspeed_from_index(g, x::DEFAULT_OSM_INDEX_TYPE) = index_to_node(g, x).tags["maxspeed"]
67+
maxspeed_from_node_id(g, x::DEFAULT_OSM_ID_TYPE) = g.nodes[x].tags["maxspeed"]

src/nearest_node.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ not included in the results.
4747
Tuple elements are `Vector`sif a `Vector` of nodes is inputted, and numbers if a single point is inputted.
4848
"""
4949
nearest_node(g::OSMGraph, node::Node) = nearest_node(g, node.location, (index)->index==g.node_to_index[node.id])
50-
nearest_node(g::OSMGraph, node_id::Integer) = nearest_node(g, g.nodes[node_id])
50+
nearest_node(g::OSMGraph, node_id::DEFAULT_OSM_ID_TYPE) = nearest_node(g, g.nodes[node_id])
5151
nearest_node(g::OSMGraph, nodes::Vector{<:Node}) = nearest_node(g, [n.id for n in nodes])
52-
function nearest_node(g::OSMGraph, node_ids::AbstractVector{<:Integer})
52+
function nearest_node(g::OSMGraph, node_ids::AbstractVector{<:DEFAULT_OSM_ID_TYPE})
5353
locations = [g.nodes[n].location for n in node_ids]
5454
cartesian_locations = to_cartesian(locations)
5555
idxs, dists = knn(g.kdtree, cartesian_locations, 2, true)
@@ -89,8 +89,8 @@ function nearest_nodes(g::OSMGraph, points::AbstractVector{GeoLocation}, n_neigh
8989
end
9090

9191
"""
92-
nearest_nodes(g::OSMGraph, node_id::Integer, n_neighbours::Integer=1)
93-
nearest_nodes(g::OSMGraph, node_ids::Vector{<:Integer}, n_neighbours::Integer=1)
92+
nearest_nodes(g::OSMGraph, node_id::DEFAULT_OSM_ID_TYPE, n_neighbours::Integer=1)
93+
nearest_nodes(g::OSMGraph, node_ids::Vector{<:DEFAULT_OSM_ID_TYPE}, n_neighbours::Integer=1)
9494
nearest_nodes(g::OSMGraph, node::Node, n_neighbours::Integer=1)
9595
nearest_nodes(g::OSMGraph, nodes::AbstractVector{<:Node}, n_neighbours::Integer=1)
9696
@@ -106,9 +106,9 @@ Finds nearest nodes from a point or `Vector` of points using a `NearestNeighbors
106106
Tuple elements are `Vector{Vector}` if a `Vector` of points is inputted, and `Vector` if a single point is inputted.
107107
"""
108108
nearest_nodes(g::OSMGraph, node::Node, n_neighbours::Integer=1) = nearest_nodes(g, node.location, n_neighbours, (index)->index==g.node_to_index[node.id])
109-
nearest_nodes(g::OSMGraph, node_id::Integer, n_neighbours::Integer=1) = nearest_nodes(g, g.nodes[node_id], n_neighbours)
109+
nearest_nodes(g::OSMGraph, node_id::DEFAULT_OSM_ID_TYPE, n_neighbours::Integer=1) = nearest_nodes(g, g.nodes[node_id], n_neighbours)
110110
nearest_nodes(g::OSMGraph, nodes::Vector{<:Node}, n_neighbours::Integer=1) = nearest_nodes(g, [n.id for n in nodes], n_neighbours)
111-
function nearest_nodes(g::OSMGraph, node_ids::Vector{<:Integer}, n_neighbours::Integer=1)
111+
function nearest_nodes(g::OSMGraph, node_ids::Vector{<:DEFAULT_OSM_ID_TYPE}, n_neighbours::Integer=1)
112112
locations = [g.nodes[n].location for n in node_ids]
113113
n_neighbours += 1 # Closest node is always the input node itself, exclude self from result
114114
cartesian_locations = to_cartesian(locations)

src/nearest_way.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function nearest_ways(g::OSMGraph{U,T,W},
8989
end
9090

9191
"""
92-
nearest_point_on_way(g::OSMGraph, point::GeoLocation, way_id::Integer)
92+
nearest_point_on_way(g::OSMGraph, point::GeoLocation, way_id::DEFAULT_OSM_ID_TYPE)
9393
9494
Finds the nearest position on a way to a given point. Matches to an `EdgePoint`.
9595
@@ -103,7 +103,7 @@ Finds the nearest position on a way to a given point. Matches to an `EdgePoint`.
103103
- `::EdgePoint`: Nearest position along the way between two nodes.
104104
- `::Float64`: Distance from `point` to the nearest position on the way.
105105
"""
106-
function nearest_point_on_way(g::OSMGraph, point::GeoLocation, way_id::Integer)
106+
function nearest_point_on_way(g::OSMGraph, point::GeoLocation, way_id::DEFAULT_OSM_ID_TYPE)
107107
nodes = g.ways[way_id].nodes
108108
min_edge = nothing
109109
min_dist = floatmax()
@@ -122,4 +122,4 @@ function nearest_point_on_way(g::OSMGraph, point::GeoLocation, way_id::Integer)
122122
end
123123
end
124124
return EdgePoint(min_edge[1], min_edge[2], min_pos), min_dist
125-
end
125+
end

src/parse.jl

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ is_restriction(tags::AbstractDict)::Bool = get(tags, "type", "") == "restriction
122122
"""
123123
Determine if a restriction is valid and has usable data.
124124
"""
125-
function is_valid_restriction(members::AbstractArray, ways::AbstractDict{T,Way{T}})::Bool where T <: Integer
125+
function is_valid_restriction(members::AbstractArray, ways::AbstractDict{T,Way{T}})::Bool where T <: DEFAULT_OSM_ID_TYPE
126126
role_counts = DefaultDict(0)
127127
role_type_counts = DefaultDict(0)
128128
ways_set = Set{Int}()
@@ -201,13 +201,14 @@ function parse_osm_network_dict(osm_network_dict::AbstractDict,
201201
network_type::Symbol=:drive;
202202
filter_network_type::Bool=true
203203
)::OSMGraph
204+
204205
U = DEFAULT_OSM_INDEX_TYPE
205-
T = DEFAULT_OSM_ID_TYPE
206+
T = get_id_type(osm_network_dict)
206207
W = DEFAULT_OSM_EDGE_WEIGHT_TYPE
207208
L = DEFAULT_OSM_LANES_TYPE
208209

209210
ways = Dict{T,Way{T}}()
210-
highway_nodes = Set{Int}([])
211+
highway_nodes = Set{T}([])
211212
for way in osm_network_dict["way"]
212213
if haskey(way, "tags") && haskey(way, "nodes")
213214
tags = way["tags"]
@@ -363,3 +364,25 @@ function init_graph_from_object(osm_json_object::AbstractDict,
363364
filter_network_type=filter_network_type
364365
)
365366
end
367+
368+
369+
"""
370+
get_id_type(osm_network_dict::AbstractDict)::Type
371+
372+
Finds the node id type of an osm dict.
373+
"""
374+
function get_id_type(osm_network_dict::AbstractDict)::Type
375+
if isempty(osm_network_dict["node"])
376+
return Int64
377+
end
378+
379+
first_id = osm_network_dict["node"][1]["id"]
380+
381+
if first_id isa Integer
382+
return Int64
383+
elseif first_id isa String
384+
return String
385+
else
386+
throw(ErrorException("OSM ID type not supported: $(typeof(first_id))"))
387+
end
388+
end

0 commit comments

Comments
 (0)