| 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243 |
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
6×
6×
1×
1×
1×
1×
6×
6×
6×
6×
6×
6×
6×
6×
6×
6×
6×
6×
6×
6×
6×
1×
1×
1×
1×
1×
1×
1×
1×
1×
2×
2×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
2×
2×
2×
2×
1×
7×
7×
7×
7×
1×
1×
1×
1×
1×
1×
4×
4×
4×
4×
4×
4×
1×
| // FIXME add typedef for stack state objects
goog.provide('ol.format.OSMXML');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('goog.dom.NodeType');
goog.require('goog.object');
goog.require('ol.Feature');
goog.require('ol.format.Feature');
goog.require('ol.format.XMLFeature');
goog.require('ol.geom.GeometryLayout');
goog.require('ol.geom.LineString');
goog.require('ol.geom.Point');
goog.require('ol.geom.Polygon');
goog.require('ol.proj');
goog.require('ol.xml');
/**
* @classdesc
* Feature format for reading data in the
* [OSMXML format](http://wiki.openstreetmap.org/wiki/OSM_XML).
*
* @constructor
* @extends {ol.format.XMLFeature}
* @api stable
*/
ol.format.OSMXML = function() {
goog.base(this);
/**
* @inheritDoc
*/
this.defaultDataProjection = ol.proj.get('EPSG:4326');
};
goog.inherits(ol.format.OSMXML, ol.format.XMLFeature);
/**
* @const
* @type {Array.<string>}
* @private
*/
ol.format.OSMXML.EXTENSIONS_ = ['.osm'];
/**
* @inheritDoc
*/
ol.format.OSMXML.prototype.getExtensions = function() {
return ol.format.OSMXML.EXTENSIONS_;
};
/**
* @param {Node} node Node.
* @param {Array.<*>} objectStack Object stack.
* @private
*/
ol.format.OSMXML.readNode_ = function(node, objectStack) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT,
'node.nodeType should be ELEMENT');
goog.asserts.assert(node.localName == 'node', 'localName should be node');
var options = /** @type {olx.format.ReadOptions} */ (objectStack[0]);
var state = /** @type {Object} */ (objectStack[objectStack.length - 1]);
var id = node.getAttribute('id');
var coordinates = /** @type {Array.<number>} */ ([
parseFloat(node.getAttribute('lon')),
parseFloat(node.getAttribute('lat'))
]);
state.nodes[id] = coordinates;
var values = ol.xml.pushParseAndPop({
tags: {}
}, ol.format.OSMXML.NODE_PARSERS_, node, objectStack);
Eif (!goog.object.isEmpty(values.tags)) {
var geometry = new ol.geom.Point(coordinates);
ol.format.Feature.transformWithOptions(geometry, false, options);
var feature = new ol.Feature(geometry);
feature.setId(id);
feature.setProperties(values.tags);
state.features.push(feature);
}
};
/**
* @param {Node} node Node.
* @param {Array.<*>} objectStack Object stack.
* @private
*/
ol.format.OSMXML.readWay_ = function(node, objectStack) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT,
'node.nodeType should be ELEMENT');
goog.asserts.assert(node.localName == 'way', 'localName should be way');
var options = /** @type {olx.format.ReadOptions} */ (objectStack[0]);
var id = node.getAttribute('id');
var values = ol.xml.pushParseAndPop({
ndrefs: [],
tags: {}
}, ol.format.OSMXML.WAY_PARSERS_, node, objectStack);
var state = /** @type {Object} */ (objectStack[objectStack.length - 1]);
var flatCoordinates = /** @type {Array.<number>} */ ([]);
for (var i = 0, ii = values.ndrefs.length; i < ii; i++) {
var point = state.nodes[values.ndrefs[i]];
goog.array.extend(flatCoordinates, point);
}
var geometry;
Iif (values.ndrefs[0] == values.ndrefs[values.ndrefs.length - 1]) {
// closed way
geometry = new ol.geom.Polygon(null);
geometry.setFlatCoordinates(ol.geom.GeometryLayout.XY, flatCoordinates,
[flatCoordinates.length]);
} else {
geometry = new ol.geom.LineString(null);
geometry.setFlatCoordinates(ol.geom.GeometryLayout.XY, flatCoordinates);
}
ol.format.Feature.transformWithOptions(geometry, false, options);
var feature = new ol.Feature(geometry);
feature.setId(id);
feature.setProperties(values.tags);
state.features.push(feature);
};
/**
* @param {Node} node Node.
* @param {Array.<*>} objectStack Object stack.
* @private
* @return {ol.Feature|undefined} Track.
*/
ol.format.OSMXML.readNd_ = function(node, objectStack) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT,
'node.nodeType should be ELEMENT');
goog.asserts.assert(node.localName == 'nd', 'localName should be nd');
var values = /** @type {Object} */ (objectStack[objectStack.length - 1]);
values.ndrefs.push(node.getAttribute('ref'));
};
/**
* @param {Node} node Node.
* @param {Array.<*>} objectStack Object stack.
* @private
* @return {ol.Feature|undefined} Track.
*/
ol.format.OSMXML.readTag_ = function(node, objectStack) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT,
'node.nodeType should be ELEMENT');
goog.asserts.assert(node.localName == 'tag', 'localName should be tag');
var values = /** @type {Object} */ (objectStack[objectStack.length - 1]);
values.tags[node.getAttribute('k')] = node.getAttribute('v');
};
/**
* @const
* @private
* @type {Array.<string>}
*/
ol.format.OSMXML.NAMESPACE_URIS_ = [
null
];
/**
* @const
* @type {Object.<string, Object.<string, ol.xml.Parser>>}
* @private
*/
ol.format.OSMXML.WAY_PARSERS_ = ol.xml.makeStructureNS(
ol.format.OSMXML.NAMESPACE_URIS_, {
'nd': ol.format.OSMXML.readNd_,
'tag': ol.format.OSMXML.readTag_
});
/**
* @const
* @type {Object.<string, Object.<string, ol.xml.Parser>>}
* @private
*/
ol.format.OSMXML.PARSERS_ = ol.xml.makeStructureNS(
ol.format.OSMXML.NAMESPACE_URIS_, {
'node': ol.format.OSMXML.readNode_,
'way': ol.format.OSMXML.readWay_
});
/**
* @const
* @type {Object.<string, Object.<string, ol.xml.Parser>>}
* @private
*/
ol.format.OSMXML.NODE_PARSERS_ = ol.xml.makeStructureNS(
ol.format.OSMXML.NAMESPACE_URIS_, {
'tag': ol.format.OSMXML.readTag_
});
/**
* Read all features from an OSM source.
*
* @function
* @param {Document|Node|Object|string} source Source.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @return {Array.<ol.Feature>} Features.
* @api stable
*/
ol.format.OSMXML.prototype.readFeatures;
/**
* @inheritDoc
*/
ol.format.OSMXML.prototype.readFeaturesFromNode = function(node, opt_options) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT,
'node.nodeType should be ELEMENT');
var options = this.getReadOptions(node, opt_options);
Eif (node.localName == 'osm') {
var state = ol.xml.pushParseAndPop({
nodes: {},
features: []
}, ol.format.OSMXML.PARSERS_, node, [options]);
Eif (state.features) {
return state.features;
}
}
return [];
};
/**
* Read the projection from an OSM source.
*
* @function
* @param {Document|Node|Object|string} source Source.
* @return {ol.proj.Projection} Projection.
* @api stable
*/
ol.format.OSMXML.prototype.readProjection;
|