diff --git a/src/ol/format/osmxml.js b/src/ol/format/osmxml.js index 7ac33ead43..f5a133f286 100644 --- a/src/ol/format/osmxml.js +++ b/src/ol/format/osmxml.js @@ -71,34 +71,14 @@ ol.format.OSMXML.readNode_ = function(node, objectStack) { * @private */ ol.format.OSMXML.readWay_ = function(node, objectStack) { - var options = /** @type {olx.format.ReadOptions} */ (objectStack[0]); var id = node.getAttribute('id'); var values = ol.xml.pushParseAndPop({ + id: id, ndrefs: [], tags: {} }, ol.format.OSMXML.WAY_PARSERS_, node, objectStack); var state = /** @type {Object} */ (objectStack[objectStack.length - 1]); - /** @type {Array.} */ - 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); + state.ways.push(values); }; @@ -189,8 +169,34 @@ ol.format.OSMXML.prototype.readFeaturesFromNode = function(node, opt_options) { if (node.localName == 'osm') { var state = ol.xml.pushParseAndPop({ nodes: {}, + ways: [], features: [] }, 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.} */ + 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) { return state.features; } diff --git a/test/spec/ol/format/osmxml.test.js b/test/spec/ol/format/osmxml.test.js index 11dfa5b99a..a00b56cd25 100644 --- a/test/spec/ol/format/osmxml.test.js +++ b/test/spec/ol/format/osmxml.test.js @@ -87,6 +87,33 @@ describe('ol.format.OSMXML', function() { expect(g.getCoordinates()).to.eql([[2, 1], [4, 3]]); }); + + it('can read ways before nodes', function() { + var text = + '' + + '' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ''; + 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() { var text = '' +