From 2238ddd8baca5ada7cd625cbe3f7599a335a017c Mon Sep 17 00:00:00 2001 From: Przemyslaw Szufel Date: Fri, 25 Jan 2019 03:25:07 +0100 Subject: [PATCH] code reengineering --- src/parseMap.jl | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/parseMap.jl b/src/parseMap.jl index 74d6a84..ac6c684 100644 --- a/src/parseMap.jl +++ b/src/parseMap.jl @@ -63,23 +63,25 @@ function parseOSM(filename::AbstractString; args...)::OpenStreetMapX.OSMData callbacks.end_element = collect_element data = OpenStreetMapX.DataHandle() LibExpat.parsefile(filename, callbacks, data=data; args...) - data.osm + data.osm end - - """ High level function - parses .osm file and create the road network based on the map data. **Arguments** -* `datapath` : path with an .osm file -* `filename` : name of .osm file +* `filepath` : path with an .osm file (directory or path to a file) +* `filename` : name of the file (when the first argument is a directory) * `road_levels` : a set with the road categories (see: OpenStreetMapX.ROAD_CLASSES for more informations) * `use_cache` : a *.cache file will be crated with a serialized map image in the `datapath` folder """ -function get_map_data(datapath::String,filename::String; road_levels::Set{Int} = Set(1:length(OpenStreetMapX.ROAD_CLASSES)),use_cache::Bool = true,only_intersections=true)::MapData +function get_map_data(filepath::String,filename::Union{String,Nothing}=nothing; road_levels::Set{Int} = Set(1:length(OpenStreetMapX.ROAD_CLASSES)),use_cache::Bool = true,only_intersections=true)::MapData #preprocessing map file + datapath = (filename==nothing) ? dirname(filepath) : filepath; + if filename == nothing + filename = basename(filepath) + end cachefile = joinpath(datapath,filename*".cache") if use_cache && isfile(cachefile) f=open(cachefile,"r"); @@ -96,33 +98,34 @@ function get_map_data(datapath::String,filename::String; road_levels::Set{Int} = roadways = OpenStreetMapX.filter_roadways(highways, levels= road_levels) intersections = OpenStreetMapX.find_intersections(roadways) segments = OpenStreetMapX.find_segments(nodes,roadways,intersections) - + #remove unuseful nodes roadways_nodes = unique(vcat(collect(way.nodes for way in roadways)...)) nodes = Dict(key => nodes[key] for key in roadways_nodes) - + # e - Edges in graph, stored as a tuple (source,destination) # class - Road class of each edgey if only_intersections vals = Dict((segment.node0,segment.node1) => (segment.distance,segment.parent) for segment in segments) - e = collect(keys(vals)) + e = collect(keys(vals)) vals = collect(values(vals)) weights = map(val -> val[1],vals) + classified_roadways = OpenStreetMapX.classify_roadways(roadways) class = [classified_roadways[id] for id in map(val -> val[2],vals)] - else - e,class = OpenStreetMapX.get_edges(nodes,roadways) + else + e,class = OpenStreetMapX.get_edges(nodes,roadways) weights = OpenStreetMapX.distance(nodes,e) end # (node id) => (graph vertex) - v = OpenStreetMapX.get_vertices(e) - edges = [v[id] for id in reinterpret(Int, e)] + v = OpenStreetMapX.get_vertices(e) + edges = [v[id] for id in reinterpret(Int, e)] I = edges[1:2:end] - J = edges[2:2:end] + J = edges[2:2:end] # w - Edge weights, indexed by graph id w = SparseArrays.sparse(I, J, weights, length(v), length(v)) - g = LightGraphs.DiGraph(w) + g = LightGraphs.DiGraph(w) - res = OpenStreetMapX.MapData(bounds,nodes,roadways,intersections,g,v,e,w,class) + res = OpenStreetMapX.MapData(bounds,nodes,roadways,intersections,g,v,e,w,class) if use_cache f=open(cachefile,"w"); Serialization.serialize(f,res);