From 132634f10cf9d3548e5457627375f7c2edae9ab7 Mon Sep 17 00:00:00 2001 From: oterral Date: Wed, 17 May 2017 14:26:39 +0200 Subject: [PATCH 1/2] Add parsing of Tessellate tag in KML fomat --- src/ol/format/kml.js | 11 +++++++++-- test/spec/ol/format/kml.test.js | 7 +++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/ol/format/kml.js b/src/ol/format/kml.js index 0e2b7bbf62..eff0966db2 100644 --- a/src/ol/format/kml.js +++ b/src/ol/format/kml.js @@ -1065,19 +1065,25 @@ ol.format.KML.setCommonGeometryProperties_ = function(multiGeometry, geometries) { var ii = geometries.length; var extrudes = new Array(geometries.length); + var tessellates = new Array(geometries.length); var altitudeModes = new Array(geometries.length); - var geometry, i, hasExtrude, hasAltitudeMode; - hasExtrude = hasAltitudeMode = false; + var geometry, i, hasExtrude, hasTessellate, hasAltitudeMode; + hasExtrude = hasTessellate = hasAltitudeMode = false; for (i = 0; i < ii; ++i) { geometry = geometries[i]; extrudes[i] = geometry.get('extrude'); + tessellates[i] = geometry.get('tessellate'); altitudeModes[i] = geometry.get('altitudeMode'); hasExtrude = hasExtrude || extrudes[i] !== undefined; + hasTessellate = hasTessellate || tessellates[i] !== undefined; hasAltitudeMode = hasAltitudeMode || altitudeModes[i]; } if (hasExtrude) { multiGeometry.set('extrude', extrudes); } + if (hasTessellate) { + multiGeometry.set('tessellate', tessellates); + } if (hasAltitudeMode) { multiGeometry.set('altitudeMode', altitudeModes); } @@ -1371,6 +1377,7 @@ ol.format.KML.LOD_PARSERS_ = ol.xml.makeStructureNS( ol.format.KML.EXTRUDE_AND_ALTITUDE_MODE_PARSERS_ = ol.xml.makeStructureNS( ol.format.KML.NAMESPACE_URIS_, { 'extrude': ol.xml.makeObjectPropertySetter(ol.format.XSD.readBoolean), + 'tessellate': ol.xml.makeObjectPropertySetter(ol.format.XSD.readBoolean), 'altitudeMode': ol.xml.makeObjectPropertySetter(ol.format.XSD.readString) }); diff --git a/test/spec/ol/format/kml.test.js b/test/spec/ol/format/kml.test.js index fc2ea80bd1..02d20dbdaa 100644 --- a/test/spec/ol/format/kml.test.js +++ b/test/spec/ol/format/kml.test.js @@ -412,6 +412,7 @@ describe('ol.format.KML', function() { ' ' + ' 1,2,3 4,5,6' + ' 0' + + ' 1' + ' absolute' + ' ' + ' ' + @@ -424,6 +425,7 @@ describe('ol.format.KML', function() { 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('tessellate')).to.be(true); expect(g.get('altitudeMode')).to.be('absolute'); }); @@ -964,6 +966,7 @@ describe('ol.format.KML', function() { ' ' + ' ' + ' 0' + + ' 0' + ' absolute' + ' 1,2,3 4,5,6' + ' ' + @@ -985,6 +988,10 @@ describe('ol.format.KML', function() { expect(g.get('extrude')).to.have.length(2); expect(g.get('extrude')[0]).to.be(false); expect(g.get('extrude')[1]).to.be(undefined); + expect(g.get('tessellate')).to.be.an('array'); + expect(g.get('tessellate')).to.have.length(2); + expect(g.get('tessellate')[0]).to.be(false); + expect(g.get('tessellate')[1]).to.be(undefined); expect(g.get('altitudeMode')).to.be.an('array'); expect(g.get('altitudeMode')).to.have.length(2); expect(g.get('altitudeMode')[0]).to.be('absolute'); From ca90157e9f8f34f63c5b4916b227690ab982843c Mon Sep 17 00:00:00 2001 From: oterral Date: Wed, 17 May 2017 16:08:39 +0200 Subject: [PATCH 2/2] Write all geometry's properties in KML --- src/ol/format/kml.js | 39 ++++++++++++++++++++------------- test/spec/ol/format/kml.test.js | 27 +++++++++++++++++++++++ 2 files changed, 51 insertions(+), 15 deletions(-) diff --git a/src/ol/format/kml.js b/src/ol/format/kml.js index eff0966db2..20f0f3509e 100644 --- a/src/ol/format/kml.js +++ b/src/ol/format/kml.js @@ -2475,10 +2475,16 @@ ol.format.KML.writePrimitiveGeometry_ = function(node, geometry, objectStack) { var /** @type {ol.XmlNodeStackItem} */ context = {node: node}; context['layout'] = geometry.getLayout(); context['stride'] = geometry.getStride(); - ol.xml.pushSerializeAndPop(context, - ol.format.KML.PRIMITIVE_GEOMETRY_SERIALIZERS_, - ol.format.KML.COORDINATES_NODE_FACTORY_, - [flatCoordinates], objectStack); + + // serialize properties (properties unknown to KML are not serialized) + var properties = geometry.getProperties(); + properties.coordinates = flatCoordinates; + + var parentNode = objectStack[objectStack.length - 1].node; + var orderedKeys = ol.format.KML.PRIMITIVE_GEOMETRY_SEQUENCE_[parentNode.namespaceURI]; + var values = ol.xml.makeSequence(properties, orderedKeys); + ol.xml.pushSerializeAndPop(context, ol.format.KML.PRIMITIVE_GEOMETRY_SERIALIZERS_, + ol.xml.OBJECT_PROPERTY_NODE_FACTORY, values, objectStack, orderedKeys); }; @@ -2639,7 +2645,6 @@ ol.format.KML.GEOMETRY_TYPE_TO_NODENAME_ = { 'GeometryCollection': 'MultiGeometry' }; - /** * @const * @type {Object.>} @@ -2815,6 +2820,17 @@ ol.format.KML.PLACEMARK_SERIALIZERS_ = ol.xml.makeStructureNS( }); +/** + * @const + * @type {Object.>} + * @private + */ +ol.format.KML.PRIMITIVE_GEOMETRY_SEQUENCE_ = ol.xml.makeStructureNS( + ol.format.KML.NAMESPACE_URIS_, [ + 'extrude', 'tessellate', 'altitudeMode', 'coordinates' + ]); + + /** * @const * @type {Object.>} @@ -2822,6 +2838,9 @@ ol.format.KML.PLACEMARK_SERIALIZERS_ = ol.xml.makeStructureNS( */ ol.format.KML.PRIMITIVE_GEOMETRY_SERIALIZERS_ = ol.xml.makeStructureNS( ol.format.KML.NAMESPACE_URIS_, { + 'extrude': ol.xml.makeChildAppender(ol.format.XSD.writeBooleanTextNode), + 'tessellate': ol.xml.makeChildAppender(ol.format.XSD.writeBooleanTextNode), + 'altitudeMode': ol.xml.makeChildAppender(ol.format.XSD.writeStringTextNode), 'coordinates': ol.xml.makeChildAppender( ol.format.KML.writeCoordinatesTextNode_) }); @@ -2933,16 +2952,6 @@ ol.format.KML.GEOMETRY_NODE_FACTORY_ = function(value, objectStack, ol.format.KML.COLOR_NODE_FACTORY_ = ol.xml.makeSimpleNodeFactory('color'); -/** - * A factory for creating coordinates nodes. - * @const - * @type {function(*, Array.<*>, string=): (Node|undefined)} - * @private - */ -ol.format.KML.COORDINATES_NODE_FACTORY_ = - ol.xml.makeSimpleNodeFactory('coordinates'); - - /** * A factory for creating Data nodes. * @const diff --git a/test/spec/ol/format/kml.test.js b/test/spec/ol/format/kml.test.js index 02d20dbdaa..e929c80d54 100644 --- a/test/spec/ol/format/kml.test.js +++ b/test/spec/ol/format/kml.test.js @@ -192,6 +192,33 @@ describe('ol.format.KML', function() { expect(node).to.xmleql(ol.xml.parse(text)); }); + + it('can write properties', function() { + var lineString = new ol.geom.LineString([[1, 2], [3, 4]]); + lineString.set('extrude', false); + lineString.set('tessellate', true); + lineString.set('altitudeMode', 'clampToGround'); + lineString.set('unsupportedProperty', 'foo'); + var features = [new ol.Feature(lineString)]; + var node = format.writeFeaturesNode(features); + var text = + '' + + ' ' + + ' ' + + ' 0' + + ' 1' + + ' clampToGround' + + ' 1,2 3,4' + + ' ' + + ' ' + + ''; + expect(node).to.xmleql(ol.xml.parse(text)); + }); + it('can read Point geometries', function() { var text = '