-
Notifications
You must be signed in to change notification settings - Fork 18
/
parseMap.jl
217 lines (191 loc) · 6.32 KB
/
parseMap.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
### Julia OpenStreetMap Package ###
### MIT License ###
### Copyright 2014 ###
type OSMattributes
oneway::Bool
oneway_override::Bool
oneway_reverse::Bool
visible::Bool
lanes::Int
name::UTF8String
class::UTF8String
detail::UTF8String
cycleway::UTF8String
sidewalk::UTF8String
bicycle::UTF8String
# XML elements
element::Symbol # :None, :Node, :Way, :Tag[, :Relation]
parent::Symbol # :Building, :Feature, :Highway
way_nodes::Vector{Int} # for buildings and highways
id::Int # Uninitialized
lat::Float64 # Uninitialized
lon::Float64 # Uninitialized
OSMattributes() = new(false,false,false,false,1,
"","","","","","",:None,:None,[])
end
type OSMdata
nodes::Dict{Int,LLA}
highways::Dict{Int,Highway}
buildings::Dict{Int,Building}
features::Dict{Int,Feature}
attr::OSMattributes
OSMdata() = new(Dict(),Dict(),Dict(),Dict(),OSMattributes())
end
function reset_attributes!(osm::OSMattributes)
osm.oneway = osm.oneway_override = osm.oneway_reverse = osm.visible = false
osm.lanes = 1
osm.name = osm.class = osm.detail = osm.cycleway = osm.sidewalk = osm.bicycle = ""
osm.element = osm.parent = :None
empty!(osm.way_nodes)
end
### PARSE XML ELEMENTS ###
function parse_node(attr::OSMattributes, attrs_in::Dict{String,String})
attr.visible = true
attr.element = :Node
if haskey(attrs_in, "id")
attr.id = int(attrs_in["id"])
attr.lat = float(attrs_in["lat"])
attr.lon = float(attrs_in["lon"])
end
end
function parse_way(attr::OSMattributes, attrs_in::Dict{String,String})
attr.visible = true
attr.element = :Way
if haskey(attrs_in, "id")
attr.id = int(attrs_in["id"])
end
end
function parse_nd(attr::OSMattributes, attrs_in::Dict{String,String})
if haskey(attrs_in, "ref")
push!(attr.way_nodes, int64(attrs_in["ref"]))
end
end
function parse_tag(attr::OSMattributes, attrs_in::Dict{String,String})
if haskey(attrs_in, "k") && haskey(attrs_in, "v")
k, v = attrs_in["k"], attrs_in["v"]
if k == "name"
if isempty(attr.name)
attr.name = v # applicable to roads (highways), buildings, features
end
elseif attr.element == :Way
if k == "building"
parse_building(attr, v)
else
parse_highway(attr, k, v) # for other highway tags
end
elseif attr.element == :Node
if haskey(FEATURE_CLASSES, k)
parse_feature(attr, k, v)
end
end
else
# Nothing to be done here?
end
end
### PARSE OSM ENTITIES ###
function parse_highway(attr::OSMattributes, k::String, v::String)
if k == "highway"
attr.class = v
if v == "services" # Highways marked "services" are not traversable
attr.visible = false
return
end
if v == "motorway" || v == "motorway_link"
attr.oneway = true # motorways default to oneway
end
elseif k == "oneway"
if v == "-1"
attr.oneway = true
attr.oneway_reverse = true
elseif v == "false" || v == "no" || v == "0"
attr.oneway = false
attr.oneway_override = true
elseif v == "true" || v == "yes" || v == "1"
attr.oneway = true
end
elseif k == "junction" && v == "roundabout"
attr.oneway = true
elseif k == "cycleway"
attr.cycleway = v
elseif k == "sidewalk"
attr.sidewalk = v
elseif k == "bicycle"
attr.bicycle = v
elseif k == "lanes" && length(v)==1 && '1' <= v[1] <= '9'
attr.lanes = int(v)
else
return
end
attr.parent = :Highway
end
function parse_building(attr::OSMattributes, v::String)
attr.parent = :Building
if isempty(attr.class)
attr.class = v
end
end
function parse_feature(attr::OSMattributes, k::String, v::String)
attr.parent = :Feature
attr.class = k
attr.detail = v
end
### LibExpat.XPStreamHandlers ###
function parseElement(handler::LibExpat.XPStreamHandler, name::String, attrs_in::Dict{String,String})
attr = handler.data.attr::OSMattributes
if attr.visible
if name == "nd"
parse_nd(attr, attrs_in)
elseif name == "tag"
parse_tag(attr, attrs_in)
end
elseif !(haskey(attrs_in, "visible") && attrs_in["visible"] == "false")
if name == "node"
parse_node(attr, attrs_in)
elseif name == "way"
parse_way(attr, attrs_in)
end
end # no work done for "relations" yet
end
function collectValues(handler::LibExpat.XPStreamHandler, name::String)
# println(typeof(name))
osm = handler.data::OSMdata
attr = osm.attr::OSMattributes
if name == "node"
osm.nodes[attr.id] = LLA(attr.lat, attr.lon)
if attr.parent == :Feature
osm.features[attr.id] = Feature(attr.class, attr.detail, attr.name)
end
elseif name == "way"
if attr.parent == :Building
osm.buildings[attr.id] = Building(attr.class, attr.name, copy(attr.way_nodes))
elseif attr.parent == :Highway
if attr.oneway_reverse
reverse!(attr.way_nodes)
end
osm.highways[attr.id] = Highway(attr.class, attr.lanes,
(attr.oneway && !attr.oneway_override),
attr.sidewalk, attr.cycleway, attr.bicycle,
attr.name, copy(attr.way_nodes))
end
else # :Tag or :Nd (don't reset values!)
return
end
reset_attributes!(osm.attr)
end
### Parse the data from an openStreetMap XML file ###
function parseMapXML(filename::String)
# Parse the file
street_map = LightXML.parse_file(filename)
if LightXML.name(LightXML.root(street_map)) != "osm"
throw(ArgumentError("Not an OpenStreetMap datafile."))
end
return street_map
end
function getOSMData(filename::String; args...)
osm = OSMdata()
callbacks = LibExpat.XPCallbacks()
callbacks.start_element = parseElement
callbacks.end_element = collectValues
LibExpat.parsefile(filename, callbacks, data=osm; args...)
osm.nodes, osm.highways, osm.buildings, osm.features
end