fix osmxml to read ways before the definition of nodes
This commit is contained in:
+28
-22
@@ -71,34 +71,14 @@ ol.format.OSMXML.readNode_ = function(node, objectStack) {
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
ol.format.OSMXML.readWay_ = function(node, objectStack) {
|
ol.format.OSMXML.readWay_ = function(node, objectStack) {
|
||||||
var options = /** @type {olx.format.ReadOptions} */ (objectStack[0]);
|
|
||||||
var id = node.getAttribute('id');
|
var id = node.getAttribute('id');
|
||||||
var values = ol.xml.pushParseAndPop({
|
var values = ol.xml.pushParseAndPop({
|
||||||
|
id: id,
|
||||||
ndrefs: [],
|
ndrefs: [],
|
||||||
tags: {}
|
tags: {}
|
||||||
}, ol.format.OSMXML.WAY_PARSERS_, node, objectStack);
|
}, ol.format.OSMXML.WAY_PARSERS_, node, objectStack);
|
||||||
var state = /** @type {Object} */ (objectStack[objectStack.length - 1]);
|
var state = /** @type {Object} */ (objectStack[objectStack.length - 1]);
|
||||||
/** @type {Array.<number>} */
|
state.ways.push(values);
|
||||||
var flatCoordinates = [];
|
|
||||||
for (var i = 0, ii = values.ndrefs.length; i < ii; i++) {
|
|
||||||
var point = state.nodes[values.ndrefs[i]];
|
|
||||||
ol.array.extend(flatCoordinates, point);
|
|
||||||
}
|
|
||||||
var geometry;
|
|
||||||
if (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);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -189,8 +169,34 @@ ol.format.OSMXML.prototype.readFeaturesFromNode = function(node, opt_options) {
|
|||||||
if (node.localName == 'osm') {
|
if (node.localName == 'osm') {
|
||||||
var state = ol.xml.pushParseAndPop({
|
var state = ol.xml.pushParseAndPop({
|
||||||
nodes: {},
|
nodes: {},
|
||||||
|
ways: [],
|
||||||
features: []
|
features: []
|
||||||
}, ol.format.OSMXML.PARSERS_, node, [options]);
|
}, ol.format.OSMXML.PARSERS_, node, [options]);
|
||||||
|
// parse nodes in ways
|
||||||
|
for (var j = 0; j < state.ways.length; j++) {
|
||||||
|
var values = /** @type {Object} */ (state.ways[j]);
|
||||||
|
/** @type {Array.<number>} */
|
||||||
|
var flatCoordinates = [];
|
||||||
|
for (var i = 0, ii = values.ndrefs.length; i < ii; i++) {
|
||||||
|
var point = state.nodes[values.ndrefs[i]];
|
||||||
|
ol.array.extend(flatCoordinates, point);
|
||||||
|
}
|
||||||
|
var geometry;
|
||||||
|
if (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(values.id);
|
||||||
|
feature.setProperties(values.tags);
|
||||||
|
state.features.push(feature);
|
||||||
|
}
|
||||||
if (state.features) {
|
if (state.features) {
|
||||||
return state.features;
|
return state.features;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,6 +87,33 @@ describe('ol.format.OSMXML', function() {
|
|||||||
expect(g.getCoordinates()).to.eql([[2, 1], [4, 3]]);
|
expect(g.getCoordinates()).to.eql([[2, 1], [4, 3]]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('can read ways before nodes', function() {
|
||||||
|
var text =
|
||||||
|
'<?xml version="1.0" encoding="UTF-8"?>' +
|
||||||
|
'<osm version="0.6" generator="my hand">' +
|
||||||
|
' <way id="3">' +
|
||||||
|
' <tag k="name" v="3"/>' +
|
||||||
|
' <nd ref="1" />' +
|
||||||
|
' <nd ref="2" />' +
|
||||||
|
' </way>' +
|
||||||
|
' <node id="1" lat="1" lon="2">' +
|
||||||
|
' <tag k="name" v="1"/>' +
|
||||||
|
' </node>' +
|
||||||
|
' <node id="2" lat="3" lon="4">' +
|
||||||
|
' <tag k="name" v="2"/>' +
|
||||||
|
' </node>' +
|
||||||
|
'</osm>';
|
||||||
|
var fs = format.readFeatures(text);
|
||||||
|
expect(fs).to.have.length(3);
|
||||||
|
var line = fs[2];
|
||||||
|
expect(line).to.be.an(ol.Feature);
|
||||||
|
var g = line.getGeometry();
|
||||||
|
expect(g).to.be.an(ol.geom.LineString);
|
||||||
|
expect(g.getCoordinates()).to.eql([[2, 1], [4, 3]]);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
it('can transform and read nodes', function() {
|
it('can transform and read nodes', function() {
|
||||||
var text =
|
var text =
|
||||||
'<?xml version="1.0" encoding="UTF-8"?>' +
|
'<?xml version="1.0" encoding="UTF-8"?>' +
|
||||||
|
|||||||
Reference in New Issue
Block a user