pszufe / OpenStreetMapX.jl

OpenStreetMap (*.osm) support for Julia 1.0 and up
MIT License
119 stars 24 forks source link

what is the logic of the following function "find_segments"? #23

Open bsnyh opened 4 years ago

bsnyh commented 4 years ago
function find_segments(nodes::Dict{Int,T}, highways::Vector{OpenStreetMapX.Way}, intersections::Dict{Int,Set{Int}}) where T<:Union{OpenStreetMapX.ENU,OpenStreetMapX.ECEF}
    segments = OpenStreetMapX.Segment[]
    intersect = Set(keys(intersections))
    for highway in highways
        firstNode = 1
        for j = 2:length(highway.nodes)
            if highway.nodes[firstNode] != highway.nodes[j] && (in(highway.nodes[j], intersect)|| j == length(highway.nodes))
                if !reverseway(highway)
                    seg = OpenStreetMapX.Segment(highway.nodes[firstNode],highway.nodes[j],highway.nodes[firstNode:j], OpenStreetMapX.distance(nodes, highway.nodes[firstNode:j]), highway.id)
                    push!(segments,seg)
                else
                    seg = OpenStreetMapX.Segment(highway.nodes[j],highway.nodes[firstNode],reverse(highway.nodes[firstNode:j]), OpenStreetMapX.distance(nodes, highway.nodes[firstNode:j]), highway.id)
                    push!(segments,seg)
                end
                if !oneway(highway)
                    seg = OpenStreetMapX.Segment(highway.nodes[j],highway.nodes[firstNode],reverse(highway.nodes[firstNode:j]), OpenStreetMapX.distance(nodes, highway.nodes[firstNode:j]), highway.id)
                    push!(segments,seg)
                end
                firstNode = j
            end
        end
    end
    return segments
end

Hi, I have a question about the logic of the first if statement. I do not quite understand how it works. I mean, why is it exactly like this? Based on my understanding, highway.nodes[firstNode] is always not the same as highway.nodes[j], right? Moreover, why do we require highway.nodes[j] in the set intersect? It is rather strange to me at least. Could you kindly clarify a little bit?

pszufe commented 4 years ago

Since Ways represent any kind of shape I understand that we can have a situation where the same nodes can occur more than once in a single Way. From graph routing point of view this means having an unnecessary cycle and hence should be avoided. Perhaps @bartoszpankratz can elaborate more on this.