Skip to content

Commit

Permalink
code reengineering
Browse files Browse the repository at this point in the history
  • Loading branch information
pszufe committed Jan 25, 2019
1 parent c64ad1a commit 2238ddd
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions src/parseMap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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);
Expand Down

0 comments on commit 2238ddd

Please sign in to comment.