-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
OSMXML.js
204 lines (191 loc) · 5.23 KB
/
OSMXML.js
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
/**
* @module ol/format/OSMXML
*/
// FIXME add typedef for stack state objects
import Feature from '../Feature.js';
import LineString from '../geom/LineString.js';
import Point from '../geom/Point.js';
import Polygon from '../geom/Polygon.js';
import XMLFeature from './XMLFeature.js';
import {extend} from '../array.js';
import {get as getProjection} from '../proj.js';
import {isEmpty} from '../obj.js';
import {makeStructureNS, pushParseAndPop} from '../xml.js';
import {transformGeometryWithOptions} from './Feature.js';
/**
* @const
* @type {Array<null>}
*/
const NAMESPACE_URIS = [null];
/**
* @const
* @type {Object<string, Object<string, import("../xml.js").Parser>>}
*/
// @ts-ignore
const WAY_PARSERS = makeStructureNS(NAMESPACE_URIS, {
'nd': readNd,
'tag': readTag,
});
/**
* @const
* @type {Object<string, Object<string, import("../xml.js").Parser>>}
*/
// @ts-ignore
const PARSERS = makeStructureNS(NAMESPACE_URIS, {
'node': readNode,
'way': readWay,
});
/**
* @classdesc
* Feature format for reading data in the
* [OSMXML format](https://wiki.openstreetmap.org/wiki/OSM_XML).
*
* @api
*/
class OSMXML extends XMLFeature {
constructor() {
super();
/**
* @type {import("../proj/Projection.js").default}
*/
this.dataProjection = getProjection('EPSG:4326');
}
/**
* @protected
* @param {Element} node Node.
* @param {import("./Feature.js").ReadOptions} [options] Options.
* @return {Array<import("../Feature.js").default>} Features.
* @override
*/
readFeaturesFromNode(node, options) {
options = this.getReadOptions(node, options);
if (node.localName == 'osm') {
const state = pushParseAndPop(
{
nodes: {},
ways: [],
features: [],
},
PARSERS,
node,
[options],
);
// parse nodes in ways
for (let j = 0; j < state.ways.length; j++) {
const values = /** @type {Object} */ (state.ways[j]);
/** @type {Array<number>} */
const flatCoordinates = values.flatCoordinates;
if (!flatCoordinates.length) {
for (let i = 0, ii = values.ndrefs.length; i < ii; i++) {
const point = state.nodes[values.ndrefs[i]];
extend(flatCoordinates, point);
}
}
let geometry;
if (values.ndrefs[0] == values.ndrefs[values.ndrefs.length - 1]) {
// closed way
geometry = new Polygon(flatCoordinates, 'XY', [
flatCoordinates.length,
]);
} else {
geometry = new LineString(flatCoordinates, 'XY');
}
transformGeometryWithOptions(geometry, false, options);
const feature = new Feature(geometry);
if (values.id !== undefined) {
feature.setId(values.id);
}
feature.setProperties(values.tags, true);
state.features.push(feature);
}
if (state.features) {
return state.features;
}
}
return [];
}
}
/**
* @const
* @type {Object<string, Object<string, import("../xml.js").Parser>>}
*/
// @ts-ignore
const NODE_PARSERS = makeStructureNS(NAMESPACE_URIS, {
'tag': readTag,
});
/**
* @param {Element} node Node.
* @param {Array<*>} objectStack Object stack.
*/
function readNode(node, objectStack) {
const options = /** @type {import("./Feature.js").ReadOptions} */ (
objectStack[0]
);
const state = /** @type {Object} */ (objectStack[objectStack.length - 1]);
const id = node.getAttribute('id');
/** @type {import("../coordinate.js").Coordinate} */
const coordinates = [
parseFloat(node.getAttribute('lon')),
parseFloat(node.getAttribute('lat')),
];
state.nodes[id] = coordinates;
const values = pushParseAndPop(
{
tags: {},
},
NODE_PARSERS,
node,
objectStack,
);
if (!isEmpty(values.tags)) {
const geometry = new Point(coordinates);
transformGeometryWithOptions(geometry, false, options);
const feature = new Feature(geometry);
if (id !== undefined) {
feature.setId(id);
}
feature.setProperties(values.tags, true);
state.features.push(feature);
}
}
/**
* @param {Element} node Node.
* @param {Array<*>} objectStack Object stack.
*/
function readWay(node, objectStack) {
const id = node.getAttribute('id');
const values = pushParseAndPop(
{
id: id,
ndrefs: [],
flatCoordinates: [],
tags: {},
},
WAY_PARSERS,
node,
objectStack,
);
const state = /** @type {Object} */ (objectStack[objectStack.length - 1]);
state.ways.push(values);
}
/**
* @param {Element} node Node.
* @param {Array<*>} objectStack Object stack.
*/
function readNd(node, objectStack) {
const values = /** @type {Object} */ (objectStack[objectStack.length - 1]);
values.ndrefs.push(node.getAttribute('ref'));
if (node.hasAttribute('lon') && node.hasAttribute('lat')) {
values.flatCoordinates.push(parseFloat(node.getAttribute('lon')));
values.flatCoordinates.push(parseFloat(node.getAttribute('lat')));
}
}
/**
* @param {Element} node Node.
* @param {Array<*>} objectStack Object stack.
*/
function readTag(node, objectStack) {
const values = /** @type {Object} */ (objectStack[objectStack.length - 1]);
values.tags[node.getAttribute('k')] = node.getAttribute('v');
}
export default OSMXML;