From cce9f5680b094cff801054bced5daa6149a92409 Mon Sep 17 00:00:00 2001 From: Wikunia Date: Sun, 16 Feb 2025 19:00:40 +0100 Subject: [PATCH] work with empty nodes/ways --- src/parse.jl | 54 ++++++++++++++++++++++----------------------- test/data/empty.osm | 9 ++++++++ test/parse.jl | 5 +++++ test/runtests.jl | 1 + 4 files changed, 41 insertions(+), 28 deletions(-) create mode 100644 test/data/empty.osm create mode 100644 test/parse.jl diff --git a/src/parse.jl b/src/parse.jl index 82aafa8..b284bd3 100644 --- a/src/parse.jl +++ b/src/parse.jl @@ -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)) @@ -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}( @@ -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 @@ -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 diff --git a/test/data/empty.osm b/test/data/empty.osm new file mode 100644 index 0000000..070cb03 --- /dev/null +++ b/test/data/empty.osm @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/test/parse.jl b/test/parse.jl new file mode 100644 index 0000000..e000e9f --- /dev/null +++ b/test/parse.jl @@ -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 \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index e09d0e5..5a27262 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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