From d20a8eac3f074297fb726a8ad84465ddbda41f01 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Mon, 6 Jan 2014 10:41:38 +0100 Subject: [PATCH] Read KML LinearRings as Polygons --- src/ol/format/kmlformat.js | 25 +++++++++++++++++++++++++ test/spec/ol/format/kmlformat.test.js | 24 +++++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/ol/format/kmlformat.js b/src/ol/format/kmlformat.js index 46b85ecfc2..95ab5c65d1 100644 --- a/src/ol/format/kmlformat.js +++ b/src/ol/format/kmlformat.js @@ -706,6 +706,28 @@ ol.format.KML.readLineString_ = function(node, objectStack) { }; +/** + * @param {Node} node Node. + * @param {Array.<*>} objectStack Object stack. + * @private + * @return {ol.geom.Polygon|undefined} Polygon. + */ +ol.format.KML.readLinearRing_ = function(node, objectStack) { + goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT); + goog.asserts.assert(node.localName == 'LinearRing'); + 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]); + return polygon; + } else { + return undefined; + } +}; + + /** * @param {Node} node Node. * @param {Array.<*>} objectStack Object stack. @@ -1206,6 +1228,7 @@ ol.format.KML.LINE_STYLE_PARSERS_ = ol.xml.makeParsersNS( ol.format.KML.MULTI_GEOMETRY_PARSERS_ = ol.xml.makeParsersNS( ol.format.KML.NAMESPACE_URIS_, { 'LineString': ol.xml.makeArrayPusher(ol.format.KML.readLineString_), + 'LinearRing': ol.xml.makeArrayPusher(ol.format.KML.readLinearRing_), 'MultiGeometry': ol.xml.makeArrayPusher(ol.format.KML.readMultiGeometry_), 'Point': ol.xml.makeArrayPusher(ol.format.KML.readPoint_), 'Polygon': ol.xml.makeArrayPusher(ol.format.KML.readPolygon_) @@ -1255,6 +1278,8 @@ ol.format.KML.PLACEMARK_PARSERS_ = ol.xml.makeParsersNS( ol.format.KML.readMultiGeometry_, 'geometry'), 'LineString': ol.xml.makeObjectPropertySetter( ol.format.KML.readLineString_, 'geometry'), + 'LinearRing': ol.xml.makeObjectPropertySetter( + ol.format.KML.readLinearRing_, 'geometry'), 'Point': ol.xml.makeObjectPropertySetter( ol.format.KML.readPoint_, 'geometry'), 'Polygon': ol.xml.makeObjectPropertySetter( diff --git a/test/spec/ol/format/kmlformat.test.js b/test/spec/ol/format/kmlformat.test.js index 01a8bea472..68ec2c5007 100644 --- a/test/spec/ol/format/kmlformat.test.js +++ b/test/spec/ol/format/kmlformat.test.js @@ -89,6 +89,24 @@ describe('ol.format.KML', function() { expect(g.getCoordinates()).to.eql([[1, 2, 3], [4, 5, 6]]); }); + it('can read LinearRing geometries', function() { + var text = + '' + + ' ' + + ' ' + + ' 1,2,3 4,5,6 7,8,9' + + ' ' + + ' ' + + ''; + var fs = format.readFeatures(text); + expect(fs).to.have.length(1); + var f = fs[0]; + expect(f).to.be.an(ol.Feature); + var g = f.getGeometry(); + expect(g).to.be.an(ol.geom.Polygon); + expect(g.getCoordinates()).to.eql([[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]); + }); + it('can read Polygon geometries', function() { var text = '' + @@ -255,6 +273,9 @@ describe('ol.format.KML', function() { ' ' + ' 1,2,3 4,5,6' + ' ' + + ' ' + + ' 1,2,3 4,5,6 7,8,9' + + ' ' + ' ' + ' ' + ' ' + @@ -272,10 +293,11 @@ describe('ol.format.KML', function() { var g = f.getGeometry(); expect(g).to.be.an(ol.geom.GeometryCollection); var gs = g.getGeometries(); - expect(gs).to.have.length(3); + expect(gs).to.have.length(4); expect(gs[0]).to.be.an(ol.geom.Point); expect(gs[1]).to.be.an(ol.geom.LineString); expect(gs[2]).to.be.an(ol.geom.Polygon); + expect(gs[3]).to.be.an(ol.geom.Polygon); }); it('can read nested GeometryCollection geometries', function() {