From 5bd63f7e2ccbd77fef4e6de3730980ecf6d36318 Mon Sep 17 00:00:00 2001 From: Guillaume Beraudo Date: Mon, 30 Mar 2015 20:21:01 +0200 Subject: [PATCH] Parse KML extrude and altitudeMode of simple geometries Point, LineString and Polygon are handled. LinearRing properties are not handled. An 'extrude' boolean property is set in the geometry properties. An 'altitudeMode' string property is set in the geometry properties. --- src/ol/format/kmlformat.js | 28 +++++++++++++++++++++++++++ test/spec/ol/format/kmlformat.test.js | 12 ++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/ol/format/kmlformat.js b/src/ol/format/kmlformat.js index 841914cc94..8ad3401b7c 100644 --- a/src/ol/format/kmlformat.js +++ b/src/ol/format/kmlformat.js @@ -802,11 +802,15 @@ ol.format.KML.readLineString_ = function(node, objectStack) { 'node.nodeType should be ELEMENT'); goog.asserts.assert(node.localName == 'LineString', 'localName should be LineString'); + var properties = ol.xml.pushParseAndPop(/** @type {Object} */ ({}), + ol.format.KML.EXTRUDE_AND_ALTITUDE_MODE_PARSERS_, node, + objectStack); var flatCoordinates = ol.format.KML.readFlatCoordinatesFromNode_(node, objectStack); if (goog.isDef(flatCoordinates)) { var lineString = new ol.geom.LineString(null); lineString.setFlatCoordinates(ol.geom.GeometryLayout.XYZ, flatCoordinates); + lineString.setProperties(properties); return lineString; } else { return undefined; @@ -825,12 +829,16 @@ ol.format.KML.readLinearRing_ = function(node, objectStack) { 'node.nodeType should be ELEMENT'); goog.asserts.assert(node.localName == 'LinearRing', 'localName should be LinearRing'); + var properties = ol.xml.pushParseAndPop(/** @type {Object} */ ({}), + ol.format.KML.EXTRUDE_AND_ALTITUDE_MODE_PARSERS_, node, + objectStack); var flatCoordinates = ol.format.KML.readFlatCoordinatesFromNode_(node, objectStack); if (goog.isDef(flatCoordinates)) { var polygon = new ol.geom.Polygon(null); polygon.setFlatCoordinates(ol.geom.GeometryLayout.XYZ, flatCoordinates, [flatCoordinates.length]); + polygon.setProperties(properties); return polygon; } else { return undefined; @@ -920,6 +928,9 @@ ol.format.KML.readPoint_ = function(node, objectStack) { goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT, 'node.nodeType should be ELEMENT'); goog.asserts.assert(node.localName == 'Point', 'localName should be Point'); + var properties = ol.xml.pushParseAndPop(/** @type {Object} */ ({}), + ol.format.KML.EXTRUDE_AND_ALTITUDE_MODE_PARSERS_, node, + objectStack); var flatCoordinates = ol.format.KML.readFlatCoordinatesFromNode_(node, objectStack); if (goog.isDefAndNotNull(flatCoordinates)) { @@ -927,6 +938,7 @@ ol.format.KML.readPoint_ = function(node, objectStack) { goog.asserts.assert(flatCoordinates.length == 3, 'flatCoordinates should have a length of 3'); point.setFlatCoordinates(ol.geom.GeometryLayout.XYZ, flatCoordinates); + point.setProperties(properties); return point; } else { return undefined; @@ -945,6 +957,9 @@ ol.format.KML.readPolygon_ = function(node, objectStack) { 'node.nodeType should be ELEMENT'); goog.asserts.assert(node.localName == 'Polygon', 'localName should be Polygon'); + var properties = ol.xml.pushParseAndPop(/** @type {Object} */ ({}), + ol.format.KML.EXTRUDE_AND_ALTITUDE_MODE_PARSERS_, node, + objectStack); var flatLinearRings = ol.xml.pushParseAndPop( /** @type {Array.>} */ ([null]), ol.format.KML.FLAT_LINEAR_RINGS_PARSERS_, node, objectStack); @@ -960,6 +975,7 @@ ol.format.KML.readPolygon_ = function(node, objectStack) { } polygon.setFlatCoordinates( ol.geom.GeometryLayout.XYZ, flatCoordinates, ends); + polygon.setProperties(properties); return polygon; } else { return undefined; @@ -1266,6 +1282,18 @@ ol.format.KML.EXTENDED_DATA_PARSERS_ = ol.xml.makeParsersNS( }); +/** + * @const + * @type {Object.>} + * @private + */ +ol.format.KML.EXTRUDE_AND_ALTITUDE_MODE_PARSERS_ = ol.xml.makeParsersNS( + ol.format.KML.NAMESPACE_URIS_, { + 'extrude': ol.xml.makeObjectPropertySetter(ol.format.XSD.readBoolean), + 'altitudeMode': ol.xml.makeObjectPropertySetter(ol.format.XSD.readString) + }); + + /** * @const * @type {Object.>} diff --git a/test/spec/ol/format/kmlformat.test.js b/test/spec/ol/format/kmlformat.test.js index 1aca2f4053..db7e21423b 100644 --- a/test/spec/ol/format/kmlformat.test.js +++ b/test/spec/ol/format/kmlformat.test.js @@ -133,6 +133,8 @@ describe('ol.format.KML', function() { ' ' + ' ' + ' 1,2,3' + + ' 0' + + ' absolute' + ' ' + ' ' + ''; @@ -143,6 +145,8 @@ describe('ol.format.KML', function() { var g = f.getGeometry(); expect(g).to.be.an(ol.geom.Point); expect(g.getCoordinates()).to.eql([1, 2, 3]); + expect(g.get('extrude')).to.be(false); + expect(g.get('altitudeMode')).to.be('absolute'); }); it('can transform and read Point geometries', function() { @@ -338,6 +342,8 @@ describe('ol.format.KML', function() { ' ' + ' ' + ' 1,2,3 4,5,6' + + ' 0' + + ' absolute' + ' ' + ' ' + ''; @@ -348,6 +354,8 @@ describe('ol.format.KML', function() { var g = f.getGeometry(); expect(g).to.be.an(ol.geom.LineString); expect(g.getCoordinates()).to.eql([[1, 2, 3], [4, 5, 6]]); + expect(g.get('extrude')).to.be(false); + expect(g.get('altitudeMode')).to.be('absolute'); }); it('can write XY LineString geometries', function() { @@ -540,6 +548,8 @@ describe('ol.format.KML', function() { '' + ' ' + ' ' + + ' 0' + + ' absolute' + ' ' + ' ' + ' 0,0,1 0,5,1 5,5,2 5,0,3' + @@ -556,6 +566,8 @@ describe('ol.format.KML', function() { expect(g).to.be.an(ol.geom.Polygon); expect(g.getCoordinates()).to.eql( [[[0, 0, 1], [0, 5, 1], [5, 5, 2], [5, 0, 3]]]); + expect(g.get('extrude')).to.be(false); + expect(g.get('altitudeMode')).to.be('absolute'); }); it('can write XY Polygon geometries', function() {