Skip to content

work with empty nodes/ways #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
54 changes: 26 additions & 28 deletions src/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ function parse_osm_network_dict(osm_network_dict::AbstractDict,

ways = Dict{T,Way{T}}()
highway_nodes = Set{T}([])
for way in osm_network_dict["way"]
for way in get(osm_network_dict, "way", Vector{Dict{String, Any}}())
if haskey(way, "tags") && haskey(way, "nodes")
tags = way["tags"]
if is_highway(tags) && (!filter_network_type || matches_network_type(tags, network_type))
Expand Down Expand Up @@ -240,7 +240,7 @@ function parse_osm_network_dict(osm_network_dict::AbstractDict,
end

nodes = Dict{T,Node{T}}()
for node in osm_network_dict["node"]
for node in get(osm_network_dict, "node", Vector{Dict{String, Any}}())
id = node["id"]
if id in highway_nodes
nodes[id] = Node{T}(
Expand All @@ -252,33 +252,31 @@ function parse_osm_network_dict(osm_network_dict::AbstractDict,
end

restrictions = Dict{T,Restriction{T}}()
if haskey(osm_network_dict, "relation")
for relation in osm_network_dict["relation"]
if haskey(relation, "tags") && haskey(relation, "members")
tags = relation["tags"]
members = relation["members"]

if is_restriction(tags) && is_valid_restriction(members, ways)
restriction_kwargs = DefaultDict(Vector)
for member in members
key = "$(member["role"])_$(member["type"])"
if key == "via_way"
push!(restriction_kwargs[Symbol(key)], member["ref"])
else
restriction_kwargs[Symbol(key)] = member["ref"]
end
for relation in get(osm_network_dict, "relation", Vector{Dict{String, Any}}())
if haskey(relation, "tags") && haskey(relation, "members")
tags = relation["tags"]
members = relation["members"]

if is_restriction(tags) && is_valid_restriction(members, ways)
restriction_kwargs = DefaultDict(Vector)
for member in members
key = "$(member["role"])_$(member["type"])"
if key == "via_way"
push!(restriction_kwargs[Symbol(key)], member["ref"])
else
restriction_kwargs[Symbol(key)] = member["ref"]
end

id = relation["id"]
restrictions[id] = Restriction{T}(
id=id,
tags=tags,
type=haskey(restriction_kwargs, :via_way) ? "via_way" : "via_node",
is_exclusion=occursin("no", tags["restriction"]) ? true : false,
is_exclusive=occursin("only", tags["restriction"]) ? true : false,
;restriction_kwargs...
)
end

id = relation["id"]
restrictions[id] = Restriction{T}(
id=id,
tags=tags,
type=haskey(restriction_kwargs, :via_way) ? "via_way" : "via_node",
is_exclusion=occursin("no", tags["restriction"]) ? true : false,
is_exclusive=occursin("only", tags["restriction"]) ? true : false,
;restriction_kwargs...
)
end
end
end
Expand Down Expand Up @@ -372,7 +370,7 @@ end
Finds the node id type of an osm dict.
"""
function get_id_type(osm_network_dict::AbstractDict)::Type
if isempty(osm_network_dict["node"])
if isempty(osm_network_dict) || isempty(osm_network_dict["node"])
return Int64
end

Expand Down
9 changes: 9 additions & 0 deletions test/data/empty.osm
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<osm version="0.6" generator="LightOSM.jl">
<count id="0">
<tag k="nodes" v="0"/>
<tag k="ways" v="0"/>
<tag k="relations" v="0"/>
<tag k="total" v="0"/>
</count>
</osm>
5 changes: 5 additions & 0 deletions test/parse.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@testset "parse empty osm file" begin
xml = LightXML.parse_file(joinpath(@__DIR__, "data", "empty.osm"))
graph = graph_from_object(xml)
@test isempty(graph.nodes)
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const TEST_OSM_URL = "https://raw.githubusercontent.com/DeloitteOptimalReality/L
@testset "LightOSM Tests" begin
@testset "Constants" begin include("constants.jl") end
@testset "Utilities" begin include("utilities.jl") end
@testset "Parse" begin include("parse.jl") end
@testset "Geometry" begin include("geometry.jl") end
@testset "Download" begin include("download.jl") end
@testset "Nearest Node" begin include("nearest_node.jl") end
Expand Down