UNPKG

openlayers

Version:

Build tools and sources for developing OpenLayers based mapping applications

1,169 lines (1,104 loc) 134 kB
goog.provide('ol.test.format.KML'); goog.require('ol.array'); goog.require('ol.Feature'); goog.require('ol.format.GeoJSON'); goog.require('ol.format.KML'); goog.require('ol.geom.GeometryCollection'); goog.require('ol.geom.LineString'); goog.require('ol.geom.LinearRing'); goog.require('ol.geom.MultiLineString'); goog.require('ol.geom.MultiPoint'); goog.require('ol.geom.MultiPolygon'); goog.require('ol.geom.Point'); goog.require('ol.geom.Polygon'); goog.require('ol.style.Circle'); goog.require('ol.style.Fill'); goog.require('ol.style.Icon'); goog.require('ol.proj'); goog.require('ol.proj.Projection'); goog.require('ol.proj.transforms'); goog.require('ol.style.Stroke'); goog.require('ol.style.Style'); goog.require('ol.style.Text'); goog.require('ol.xml'); describe('ol.format.KML', function() { var format; describe('using defaultStyle', function() { var dfltStyle = new ol.style.Style(); beforeEach(function() { format = new ol.format.KML({ defaultStyle: [dfltStyle] }); }); it('set constant variables', function() { expect(ol.format.KML.DEFAULT_STYLE_ARRAY_).to.be.an(Array); }); describe('#readFeatures', function() { it('can apply a default style to a feature', function() { var text = '<kml xmlns="http://earth.google.com/kml/2.2">' + ' <Document>' + ' <Placemark/>' + ' </Document>' + '</kml>'; var fs = format.readFeatures(text); expect(fs).to.have.length(1); var f = fs[0]; expect(f).to.be.an(ol.Feature); var styleFunction = f.getStyleFunction(); expect(styleFunction).not.to.be(undefined); var styleArray = styleFunction.call(f, 0); expect(styleArray).to.be.an(Array); expect(styleArray).to.have.length(1); var style = styleArray[0]; expect(style).to.be.an(ol.style.Style); expect(style).to.be(dfltStyle); }); }); }); describe('without parameters', function() { beforeEach(function() { format = new ol.format.KML(); }); it('set constant variables', function() { expect(ol.format.KML.DEFAULT_STYLE_ARRAY_).to.be.an(Array); }); describe('#readProjection', function() { it('returns the default projection from document', function() { var projection = format.readProjectionFromDocument(); expect(projection).to.eql(ol.proj.get('EPSG:4326')); }); it('returns the default projection from node', function() { var projection = format.readProjectionFromNode(); expect(projection).to.eql(ol.proj.get('EPSG:4326')); }); }); describe('#readFeatures', function() { describe('id', function() { it('can read a Feature\'s id', function() { var text = '<kml xmlns="http://earth.google.com/kml/2.2">' + ' <Placemark id="foo"/>' + '</kml>'; var fs = format.readFeatures(text); expect(fs).to.have.length(1); var f = fs[0]; expect(f).to.be.an(ol.Feature); expect(f.getId()).to.be('foo'); }); it('treats a missing id as undefined', function() { var text = '<kml xmlns="http://earth.google.com/kml/2.2">' + ' <Placemark/>' + '</kml>'; var fs = format.readFeatures(text); expect(fs).to.have.length(1); var f = fs[0]; expect(f).to.be.an(ol.Feature); expect(f.getId()).to.be(undefined); }); it('can write a Feature', function() { var features = [new ol.Feature()]; var node = format.writeFeaturesNode(features); var text = '<kml xmlns="http://www.opengis.net/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xsi:schemaLocation="http://www.opengis.net/kml/2.2' + ' https://developers.google.com/kml/schema/kml22gx.xsd">' + ' <Placemark/>' + '</kml>'; expect(node).to.xmleql(ol.xml.parse(text)); }); it('can write a Feature as string', function() { var features = [new ol.Feature()]; var node = format.writeFeatures(features); var text = '<kml xmlns="http://www.opengis.net/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xsi:schemaLocation="http://www.opengis.net/kml/2.2' + ' https://developers.google.com/kml/schema/kml22gx.xsd">' + ' <Placemark/>' + '</kml>'; expect(ol.xml.parse(node)).to.xmleql(ol.xml.parse(text)); }); it('can write a Feature\'s id', function() { var feature = new ol.Feature(); feature.setId('foo'); var features = [feature]; var node = format.writeFeaturesNode(features); var text = '<kml xmlns="http://www.opengis.net/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xsi:schemaLocation="http://www.opengis.net/kml/2.2' + ' https://developers.google.com/kml/schema/kml22gx.xsd">' + ' <Placemark id="foo"/>' + '</kml>'; expect(node).to.xmleql(ol.xml.parse(text)); }); }); describe('geometry', function() { it('treats a missing geometry as null', function() { var text = '<kml xmlns="http://earth.google.com/kml/2.2">' + ' <Placemark/>' + '</kml>'; 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(null); }); it('can write feature with null geometries', function() { var features = [new ol.Feature(null)]; var node = format.writeFeaturesNode(features); var text = '<kml xmlns="http://www.opengis.net/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xsi:schemaLocation="http://www.opengis.net/kml/2.2' + ' https://developers.google.com/kml/schema/kml22gx.xsd">' + ' <Placemark/>' + '</kml>'; expect(node).to.xmleql(ol.xml.parse(text)); }); it('can read Point geometries', function() { var text = '<kml xmlns="http://www.opengis.net/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xsi:schemaLocation="http://www.opengis.net/kml/2.2' + ' https://developers.google.com/kml/schema/kml22gx.xsd">' + ' <Placemark>' + ' <Point>' + ' <coordinates>1,2,3</coordinates>' + ' <extrude>0</extrude>' + ' <altitudeMode>absolute</altitudeMode>' + ' </Point>' + ' </Placemark>' + '</kml>'; 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.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() { var text = '<kml xmlns="http://www.opengis.net/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xsi:schemaLocation="http://www.opengis.net/kml/2.2' + ' https://developers.google.com/kml/schema/kml22gx.xsd">' + ' <Placemark>' + ' <Point>' + ' <coordinates>1,2,3</coordinates>' + ' </Point>' + ' </Placemark>' + '</kml>'; var fs = format.readFeatures(text, { featureProjection: 'EPSG:3857' }); 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.Point); var expectedPoint = ol.proj.transform([1, 2], 'EPSG:4326', 'EPSG:3857'); expectedPoint.push(3); expect(g.getCoordinates()).to.eql(expectedPoint); }); it('can read a single Point geometry', function() { var text = '<kml xmlns="http://www.opengis.net/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xsi:schemaLocation="http://www.opengis.net/kml/2.2' + ' https://developers.google.com/kml/schema/kml22gx.xsd">' + ' <Placemark>' + ' <Point>' + ' <coordinates>1,2,3</coordinates>' + ' </Point>' + ' </Placemark>' + '</kml>'; var f = format.readFeature(text); expect(f).to.be.an(ol.Feature); var g = f.getGeometry(); expect(g).to.be.an(ol.geom.Point); expect(g.getCoordinates()).to.eql([1, 2, 3]); }); it('can transform and read a single Point geometry', function() { var text = '<kml xmlns="http://www.opengis.net/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xsi:schemaLocation="http://www.opengis.net/kml/2.2' + ' https://developers.google.com/kml/schema/kml22gx.xsd">' + ' <Placemark>' + ' <Point>' + ' <coordinates>1,2,3</coordinates>' + ' </Point>' + ' </Placemark>' + '</kml>'; var f = format.readFeature(text, { featureProjection: 'EPSG:3857' }); expect(f).to.be.an(ol.Feature); var g = f.getGeometry(); expect(g).to.be.an(ol.geom.Point); var expectedPoint = ol.proj.transform([1, 2], 'EPSG:4326', 'EPSG:3857'); expectedPoint.push(3); expect(g.getCoordinates()).to.eql(expectedPoint); }); it('can write XY Point geometries', function() { var layout = 'XY'; var point = new ol.geom.Point([1, 2], layout); var features = [new ol.Feature(point)]; var node = format.writeFeaturesNode(features); var text = '<kml xmlns="http://www.opengis.net/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xsi:schemaLocation="http://www.opengis.net/kml/2.2' + ' https://developers.google.com/kml/schema/kml22gx.xsd">' + ' <Placemark>' + ' <Point>' + ' <coordinates>1,2</coordinates>' + ' </Point>' + ' </Placemark>' + '</kml>'; expect(node).to.xmleql(ol.xml.parse(text)); }); it('can write XYZ Point geometries', function() { var layout = 'XYZ'; var point = new ol.geom.Point([1, 2, 3], layout); var features = [new ol.Feature(point)]; var node = format.writeFeaturesNode(features); var text = '<kml xmlns="http://www.opengis.net/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xsi:schemaLocation="http://www.opengis.net/kml/2.2' + ' https://developers.google.com/kml/schema/kml22gx.xsd">' + ' <Placemark>' + ' <Point>' + ' <coordinates>1,2,3</coordinates>' + ' </Point>' + ' </Placemark>' + '</kml>'; expect(node).to.xmleql(ol.xml.parse(text)); }); it('can transform and write XYZ Point geometries', function() { ol.proj.addProjection(new ol.proj.Projection({code: 'double'})); ol.proj.addCoordinateTransforms('EPSG:4326', 'double', function(coordinate) { return [2 * coordinate[0], 2 * coordinate[1]]; }, function(coordinate) { return [coordinate[0] / 2, coordinate[1] / 2]; }); var layout = 'XYZ'; var point = new ol.geom.Point([1, 2, 3], layout).transform( 'EPSG:4326', 'double'); var features = [new ol.Feature(point)]; var node = format.writeFeaturesNode(features, { featureProjection: 'double' }); var text = '<kml xmlns="http://www.opengis.net/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xsi:schemaLocation="http://www.opengis.net/kml/2.2' + ' https://developers.google.com/kml/schema/kml22gx.xsd">' + ' <Placemark>' + ' <Point>' + ' <coordinates>1,2,3</coordinates>' + ' </Point>' + ' </Placemark>' + '</kml>'; expect(node).to.xmleql(ol.xml.parse(text)); ol.proj.transforms.remove( ol.proj.get('EPSG:4326'), ol.proj.get('double')); ol.proj.transforms.remove( ol.proj.get('double'), ol.proj.get('EPSG:4326')); }); it('can write XYM Point geometries', function() { var layout = 'XYM'; var point = new ol.geom.Point([1, 2, 100], layout); var features = [new ol.Feature(point)]; var node = format.writeFeaturesNode(features); var text = '<kml xmlns="http://www.opengis.net/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xsi:schemaLocation="http://www.opengis.net/kml/2.2' + ' https://developers.google.com/kml/schema/kml22gx.xsd">' + ' <Placemark>' + ' <Point>' + ' <coordinates>1,2</coordinates>' + ' </Point>' + ' </Placemark>' + '</kml>'; expect(node).to.xmleql(ol.xml.parse(text)); }); it('can write XYZM Point geometries', function() { var layout = 'XYZM'; var point = new ol.geom.Point([1, 2, 3, 100], layout); var features = [new ol.Feature(point)]; var node = format.writeFeaturesNode(features); var text = '<kml xmlns="http://www.opengis.net/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xsi:schemaLocation="http://www.opengis.net/kml/2.2' + ' https://developers.google.com/kml/schema/kml22gx.xsd">' + ' <Placemark>' + ' <Point>' + ' <coordinates>1,2,3</coordinates>' + ' </Point>' + ' </Placemark>' + '</kml>'; expect(node).to.xmleql(ol.xml.parse(text)); }); it('can read LineString geometries', function() { var text = '<kml xmlns="http://earth.google.com/kml/2.2">' + ' <Placemark>' + ' <LineString>' + ' <coordinates>1,2,3 4,5,6</coordinates>' + ' <extrude>0</extrude>' + ' <altitudeMode>absolute</altitudeMode>' + ' </LineString>' + ' </Placemark>' + '</kml>'; 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.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() { var layout = 'XY'; var lineString = new ol.geom.LineString([[1, 2], [3, 4]], layout); var features = [new ol.Feature(lineString)]; var node = format.writeFeaturesNode(features); var text = '<kml xmlns="http://www.opengis.net/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xsi:schemaLocation="http://www.opengis.net/kml/2.2' + ' https://developers.google.com/kml/schema/kml22gx.xsd">' + ' <Placemark>' + ' <LineString>' + ' <coordinates>1,2 3,4</coordinates>' + ' </LineString>' + ' </Placemark>' + '</kml>'; expect(node).to.xmleql(ol.xml.parse(text)); }); it('can write XYZ LineString geometries', function() { var layout = 'XYZ'; var lineString = new ol.geom.LineString( [[1, 2, 3], [4, 5, 6]], layout); var features = [new ol.Feature(lineString)]; var node = format.writeFeaturesNode(features); var text = '<kml xmlns="http://www.opengis.net/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xsi:schemaLocation="http://www.opengis.net/kml/2.2' + ' https://developers.google.com/kml/schema/kml22gx.xsd">' + ' <Placemark>' + ' <LineString>' + ' <coordinates>1,2,3 4,5,6</coordinates>' + ' </LineString>' + ' </Placemark>' + '</kml>'; expect(node).to.xmleql(ol.xml.parse(text)); }); it('can write XYM LineString geometries', function() { var layout = 'XYM'; var lineString = new ol.geom.LineString( [[1, 2, 100], [3, 4, 200]], layout); var features = [new ol.Feature(lineString)]; var node = format.writeFeaturesNode(features); var text = '<kml xmlns="http://www.opengis.net/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xsi:schemaLocation="http://www.opengis.net/kml/2.2' + ' https://developers.google.com/kml/schema/kml22gx.xsd">' + ' <Placemark>' + ' <LineString>' + ' <coordinates>1,2 3,4</coordinates>' + ' </LineString>' + ' </Placemark>' + '</kml>'; expect(node).to.xmleql(ol.xml.parse(text)); }); it('can write XYZM LineString geometries', function() { var layout = 'XYZM'; var lineString = new ol.geom.LineString( [[1, 2, 3, 100], [4, 5, 6, 200]], layout); var features = [new ol.Feature(lineString)]; var node = format.writeFeaturesNode(features); var text = '<kml xmlns="http://www.opengis.net/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xsi:schemaLocation="http://www.opengis.net/kml/2.2' + ' https://developers.google.com/kml/schema/kml22gx.xsd">' + ' <Placemark>' + ' <LineString>' + ' <coordinates>1,2,3 4,5,6</coordinates>' + ' </LineString>' + ' </Placemark>' + '</kml>'; expect(node).to.xmleql(ol.xml.parse(text)); }); it('can read LinearRing geometries', function() { var text = '<kml xmlns="http://earth.google.com/kml/2.2">' + ' <Placemark>' + ' <LinearRing>' + ' <coordinates>1,2,3 4,5,6 7,8,9</coordinates>' + ' </LinearRing>' + ' </Placemark>' + '</kml>'; 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 write XY LinearRing geometries', function() { var layout = 'XY'; var linearRing = new ol.geom.LinearRing( [[1, 2], [3, 4], [1, 2]], layout); var features = [new ol.Feature(linearRing)]; var node = format.writeFeaturesNode(features); var text = '<kml xmlns="http://www.opengis.net/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xsi:schemaLocation="http://www.opengis.net/kml/2.2' + ' https://developers.google.com/kml/schema/kml22gx.xsd">' + ' <Placemark>' + ' <LinearRing>' + ' <coordinates>1,2 3,4 1,2</coordinates>' + ' </LinearRing>' + ' </Placemark>' + '</kml>'; expect(node).to.xmleql(ol.xml.parse(text)); }); it('can write XYZ LinearRing geometries', function() { var layout = 'XYZ'; var linearRing = new ol.geom.LinearRing( [[1, 2, 3], [4, 5, 6], [1, 2, 3]], layout); var features = [new ol.Feature(linearRing)]; var node = format.writeFeaturesNode(features); var text = '<kml xmlns="http://www.opengis.net/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xsi:schemaLocation="http://www.opengis.net/kml/2.2' + ' https://developers.google.com/kml/schema/kml22gx.xsd">' + ' <Placemark>' + ' <LinearRing>' + ' <coordinates>1,2,3 4,5,6 1,2,3</coordinates>' + ' </LinearRing>' + ' </Placemark>' + '</kml>'; expect(node).to.xmleql(ol.xml.parse(text)); }); it('can write XYM LinearRing geometries', function() { var layout = 'XYM'; var linearRing = new ol.geom.LinearRing( [[1, 2, 100], [3, 4, 200], [1, 2, 100]], layout); var features = [new ol.Feature(linearRing)]; var node = format.writeFeaturesNode(features); var text = '<kml xmlns="http://www.opengis.net/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xsi:schemaLocation="http://www.opengis.net/kml/2.2' + ' https://developers.google.com/kml/schema/kml22gx.xsd">' + ' <Placemark>' + ' <LinearRing>' + ' <coordinates>1,2 3,4 1,2</coordinates>' + ' </LinearRing>' + ' </Placemark>' + '</kml>'; expect(node).to.xmleql(ol.xml.parse(text)); }); it('can write XYZM LinearRing geometries', function() { var layout = 'XYZM'; var linearRing = new ol.geom.LinearRing( [[1, 2, 3, 100], [4, 5, 6, 200], [1, 2, 3, 100]], layout); var features = [new ol.Feature(linearRing)]; var node = format.writeFeaturesNode(features); var text = '<kml xmlns="http://www.opengis.net/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xsi:schemaLocation="http://www.opengis.net/kml/2.2' + ' https://developers.google.com/kml/schema/kml22gx.xsd">' + ' <Placemark>' + ' <LinearRing>' + ' <coordinates>1,2,3 4,5,6 1,2,3</coordinates>' + ' </LinearRing>' + ' </Placemark>' + '</kml>'; expect(node).to.xmleql(ol.xml.parse(text)); }); it('can read Polygon geometries', function() { var text = '<kml xmlns="http://earth.google.com/kml/2.2">' + ' <Placemark>' + ' <Polygon>' + ' <extrude>0</extrude>' + ' <altitudeMode>absolute</altitudeMode>' + ' <outerBoundaryIs>' + ' <LinearRing>' + ' <coordinates>0,0,1 0,5,1 5,5,2 5,0,3</coordinates>' + ' </LinearRing>' + ' </outerBoundaryIs>' + ' </Polygon>' + ' </Placemark>' + '</kml>'; 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( [[[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() { var layout = 'XY'; var polygon = new ol.geom.Polygon( [[[0, 0], [0, 2], [2, 2], [2, 0], [0, 0]]], layout); var features = [new ol.Feature(polygon)]; var node = format.writeFeaturesNode(features); var text = '<kml xmlns="http://www.opengis.net/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xsi:schemaLocation="http://www.opengis.net/kml/2.2' + ' https://developers.google.com/kml/schema/kml22gx.xsd">' + ' <Placemark>' + ' <Polygon>' + ' <outerBoundaryIs>' + ' <LinearRing>' + ' <coordinates>0,0 0,2 2,2 2,0 0,0</coordinates>' + ' </LinearRing>' + ' </outerBoundaryIs>' + ' </Polygon>' + ' </Placemark>' + '</kml>'; expect(node).to.xmleql(ol.xml.parse(text)); }); it('can write XYZ Polygon geometries', function() { var layout = 'XYZ'; var polygon = new ol.geom.Polygon( [[[0, 0, 1], [0, 2, 2], [2, 2, 3], [2, 0, 4], [0, 0, 5]]], layout); var features = [new ol.Feature(polygon)]; var node = format.writeFeaturesNode(features); var text = '<kml xmlns="http://www.opengis.net/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xsi:schemaLocation="http://www.opengis.net/kml/2.2' + ' https://developers.google.com/kml/schema/kml22gx.xsd">' + ' <Placemark>' + ' <Polygon>' + ' <outerBoundaryIs>' + ' <LinearRing>' + ' <coordinates>' + ' 0,0,1 0,2,2 2,2,3 2,0,4 0,0,5' + ' </coordinates>' + ' </LinearRing>' + ' </outerBoundaryIs>' + ' </Polygon>' + ' </Placemark>' + '</kml>'; expect(node).to.xmleql(ol.xml.parse(text)); }); it('can write XYM Polygon geometries', function() { var layout = 'XYM'; var polygon = new ol.geom.Polygon( [[[0, 0, 1], [0, 2, 1], [2, 2, 1], [2, 0, 1], [0, 0, 1]]], layout); var features = [new ol.Feature(polygon)]; var node = format.writeFeaturesNode(features); var text = '<kml xmlns="http://www.opengis.net/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xsi:schemaLocation="http://www.opengis.net/kml/2.2' + ' https://developers.google.com/kml/schema/kml22gx.xsd">' + ' <Placemark>' + ' <Polygon>' + ' <outerBoundaryIs>' + ' <LinearRing>' + ' <coordinates>' + ' 0,0 0,2 2,2 2,0 0,0' + ' </coordinates>' + ' </LinearRing>' + ' </outerBoundaryIs>' + ' </Polygon>' + ' </Placemark>' + '</kml>'; expect(node).to.xmleql(ol.xml.parse(text)); }); it('can write XYZM Polygon geometries', function() { var layout = 'XYZM'; var polygon = new ol.geom.Polygon([ [[0, 0, 1, 1], [0, 2, 2, 1], [2, 2, 3, 1], [2, 0, 4, 1], [0, 0, 5, 1]] ], layout); var features = [new ol.Feature(polygon)]; var node = format.writeFeaturesNode(features); var text = '<kml xmlns="http://www.opengis.net/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xsi:schemaLocation="http://www.opengis.net/kml/2.2' + ' https://developers.google.com/kml/schema/kml22gx.xsd">' + ' <Placemark>' + ' <Polygon>' + ' <outerBoundaryIs>' + ' <LinearRing>' + ' <coordinates>0,0,1 0,2,2 2,2,3 2,0,4 0,0,5</coordinates>' + ' </LinearRing>' + ' </outerBoundaryIs>' + ' </Polygon>' + ' </Placemark>' + '</kml>'; expect(node).to.xmleql(ol.xml.parse(text)); }); it('can read complex Polygon geometries', function() { var text = '<kml xmlns="http://earth.google.com/kml/2.2">' + ' <Placemark>' + ' <Polygon>' + ' <innerBoundaryIs>' + ' <LinearRing>' + ' <coordinates>1,1,0 1,2,0 2,2,0 2,1,0</coordinates>' + ' </LinearRing>' + ' </innerBoundaryIs>' + ' <innerBoundaryIs>' + ' <LinearRing>' + ' <coordinates>3,3,0 3,4,0 4,4,0 4,3,0</coordinates>' + ' </LinearRing>' + ' </innerBoundaryIs>' + ' <outerBoundaryIs>' + ' <LinearRing>' + ' <coordinates>0,0,1 0,5,1 5,5,2 5,0,3</coordinates>' + ' </LinearRing>' + ' </outerBoundaryIs>' + ' </Polygon>' + ' </Placemark>' + '</kml>'; 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([ [[0, 0, 1], [0, 5, 1], [5, 5, 2], [5, 0, 3]], [[1, 1, 0], [1, 2, 0], [2, 2, 0], [2, 1, 0]], [[3, 3, 0], [3, 4, 0], [4, 4, 0], [4, 3, 0]] ]); }); it('can write complex Polygon geometries', function() { var layout = 'XYZ'; var polygon = new ol.geom.Polygon([ [[0, 0, 1], [0, 5, 1], [5, 5, 2], [5, 0, 3]], [[1, 1, 0], [1, 2, 0], [2, 2, 0], [2, 1, 0]], [[3, 3, 0], [3, 4, 0], [4, 4, 0], [4, 3, 0]] ], layout); var features = [new ol.Feature(polygon)]; var node = format.writeFeaturesNode(features); var text = '<kml xmlns="http://www.opengis.net/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xsi:schemaLocation="http://www.opengis.net/kml/2.2' + ' https://developers.google.com/kml/schema/kml22gx.xsd">' + ' <Placemark>' + ' <Polygon>' + ' <innerBoundaryIs>' + ' <LinearRing>' + ' <coordinates>1,1,0 1,2,0 2,2,0 2,1,0</coordinates>' + ' </LinearRing>' + ' </innerBoundaryIs>' + ' <innerBoundaryIs>' + ' <LinearRing>' + ' <coordinates>3,3,0 3,4,0 4,4,0 4,3,0</coordinates>' + ' </LinearRing>' + ' </innerBoundaryIs>' + ' <outerBoundaryIs>' + ' <LinearRing>' + ' <coordinates>0,0,1 0,5,1 5,5,2 5,0,3</coordinates>' + ' </LinearRing>' + ' </outerBoundaryIs>' + ' </Polygon>' + ' </Placemark>' + '</kml>'; expect(node).to.xmleql(ol.xml.parse(text)); }); it('can read MultiPolygon geometries', function() { var text = '<kml xmlns="http://earth.google.com/kml/2.2">' + ' <Placemark>' + ' <MultiGeometry>' + ' <Polygon>' + ' <extrude>0</extrude>' + ' <altitudeMode>absolute</altitudeMode>' + ' <outerBoundaryIs>' + ' <LinearRing>' + ' <coordinates>0,0,0 0,1,0 1,1,0 1,0,0</coordinates>' + ' </LinearRing>' + ' </outerBoundaryIs>' + ' </Polygon>' + ' <Polygon>' + ' <outerBoundaryIs>' + ' <LinearRing>' + ' <coordinates>3,0,0 3,1,0 4,1,0 4,0,0</coordinates>' + ' </LinearRing>' + ' </outerBoundaryIs>' + ' </Polygon>' + ' </MultiGeometry>' + ' </Placemark>' + '</kml>'; 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.MultiPolygon); expect(g.getCoordinates()).to.eql( [[[[0, 0, 0], [0, 1, 0], [1, 1, 0], [1, 0, 0]]], [[[3, 0, 0], [3, 1, 0], [4, 1, 0], [4, 0, 0]]]]); expect(g.get('extrude')).to.be.an('array'); 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('altitudeMode')).to.be.an('array'); expect(g.get('altitudeMode')).to.have.length(2); expect(g.get('altitudeMode')[0]).to.be('absolute'); expect(g.get('altitudeMode')[1]).to.be(undefined); }); it('can write MultiPolygon geometries', function() { var layout = 'XYZ'; var multiPolygon = new ol.geom.MultiPolygon( [[[[0, 0, 0], [0, 1, 0], [1, 1, 0], [1, 0, 0]]], [[[3, 0, 0], [3, 1, 0], [4, 1, 0], [4, 0, 0]]]], layout); var features = [new ol.Feature(multiPolygon)]; var node = format.writeFeaturesNode(features); var text = '<kml xmlns="http://www.opengis.net/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xsi:schemaLocation="http://www.opengis.net/kml/2.2' + ' https://developers.google.com/kml/schema/kml22gx.xsd">' + ' <Placemark>' + ' <MultiGeometry>' + ' <Polygon>' + ' <outerBoundaryIs>' + ' <LinearRing>' + ' <coordinates>0,0,0 0,1,0 1,1,0 1,0,0</coordinates>' + ' </LinearRing>' + ' </outerBoundaryIs>' + ' </Polygon>' + ' <Polygon>' + ' <outerBoundaryIs>' + ' <LinearRing>' + ' <coordinates>3,0,0 3,1,0 4,1,0 4,0,0</coordinates>' + ' </LinearRing>' + ' </outerBoundaryIs>' + ' </Polygon>' + ' </MultiGeometry>' + ' </Placemark>' + '</kml>'; expect(node).to.xmleql(ol.xml.parse(text)); }); it('can read MultiPoint geometries', function() { var text = '<kml xmlns="http://earth.google.com/kml/2.2">' + ' <Placemark>' + ' <MultiGeometry>' + ' <Point>' + ' <coordinates>1,2,3</coordinates>' + ' <extrude>0</extrude>' + ' <altitudeMode>absolute</altitudeMode>' + ' </Point>' + ' <Point>' + ' <coordinates>4,5,6</coordinates>' + ' <extrude>1</extrude>' + ' <altitudeMode>clampToGround</altitudeMode>' + ' </Point>' + ' </MultiGeometry>' + ' </Placemark>' + '</kml>'; 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.MultiPoint); expect(g.getCoordinates()).to.eql([[1, 2, 3], [4, 5, 6]]); expect(g.get('extrude')).to.be.an('array'); expect(g.get('extrude')).to.have.length(2); expect(g.get('extrude')[0]).to.be(false); expect(g.get('extrude')[1]).to.be(true); expect(g.get('altitudeMode')).to.be.an('array'); expect(g.get('altitudeMode')).to.have.length(2); expect(g.get('altitudeMode')[0]).to.be('absolute'); expect(g.get('altitudeMode')[1]).to.be('clampToGround'); }); it('can write MultiPoint geometries', function() { var layout = 'XYZ'; var multiPoint = new ol.geom.MultiPoint( [[1, 2, 3], [4, 5, 6]], layout); var features = [new ol.Feature(multiPoint)]; var node = format.writeFeaturesNode(features); var text = '<kml xmlns="http://www.opengis.net/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xsi:schemaLocation="http://www.opengis.net/kml/2.2' + ' https://developers.google.com/kml/schema/kml22gx.xsd">' + ' <Placemark>' + ' <MultiGeometry>' + ' <Point>' + ' <coordinates>1,2,3</coordinates>' + ' </Point>' + ' <Point>' + ' <coordinates>4,5,6</coordinates>' + ' </Point>' + ' </MultiGeometry>' + ' </Placemark>' + '</kml>'; expect(node).to.xmleql(ol.xml.parse(text)); }); it('can read MultiLineString geometries', function() { var text = '<kml xmlns="http://earth.google.com/kml/2.2">' + ' <Placemark>' + ' <MultiGeometry>' + ' <LineString>' + ' <extrude>0</extrude>' + ' <altitudeMode>absolute</altitudeMode>' + ' <coordinates>1,2,3 4,5,6</coordinates>' + ' </LineString>' + ' <LineString>' + ' <coordinates>7,8,9 10,11,12</coordinates>' + ' </LineString>' + ' </MultiGeometry>' + ' </Placemark>' + '</kml>'; 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.MultiLineString); expect(g.getCoordinates()).to.eql( [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]); expect(g.get('extrude')).to.be.an('array'); 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('altitudeMode')).to.be.an('array'); expect(g.get('altitudeMode')).to.have.length(2); expect(g.get('altitudeMode')[0]).to.be('absolute'); expect(g.get('altitudeMode')[1]).to.be(undefined); }); it('can write MultiLineString geometries', function() { var layout = 'XYZ'; var multiLineString = new ol.geom.MultiLineString( [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]], layout); var features = [new ol.Feature(multiLineString)]; var node = format.writeFeaturesNode(features); var text = '<kml xmlns="http://www.opengis.net/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xsi:schemaLocation="http://www.opengis.net/kml/2.2' + ' https://developers.google.com/kml/schema/kml22gx.xsd">' + ' <Placemark>' + ' <MultiGeometry>' + ' <LineString>' + ' <coordinates>1,2,3 4,5,6</coordinates>' + ' </LineString>' + ' <LineString>' + ' <coordinates>7,8,9 10,11,12</coordinates>' + ' </LineString>' + ' </MultiGeometry>' + ' </Placemark>' + '</kml>'; expect(node).to.xmleql(ol.xml.parse(text)); }); it('can read MultiPolygon geometries', function() { var text = '<kml xmlns="http://earth.google.com/kml/2.2">' + ' <Placemark>' + ' <MultiGeometry>' + ' <Polygon>' + ' <extrude>0</extrude>' + ' <altitudeMode>absolute</altitudeMode>' + ' <outerBoundaryIs>' + ' <LinearRing>' + ' <coordinates>0,0,0 0,1,0 1,1,0 1,0,0</coordinates>' + ' </LinearRing>' + ' </outerBoundaryIs>' + ' </Polygon>' + ' <Polygon>' + ' <outerBoundaryIs>' + ' <LinearRing>' + ' <coordinates>3,0,0 3,1,0 4,1,0 4,0,0</coordinates>' + ' </LinearRing>' + ' </outerBoundaryIs>' + ' </Polygon>' + ' </MultiGeometry>' + ' </Placemark>' + '</kml>'; 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.MultiPolygon); expect(g.getCoordinates()).to.eql([ [[[0, 0, 0], [0, 1, 0], [1, 1, 0], [1, 0, 0]]], [[[3, 0, 0], [3, 1, 0], [4, 1, 0], [4, 0, 0]]] ]); expect(g.get('extrude')).to.be.an('array'); 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('altitudeMode')).to.be.an('array'); expect(g.get('altitudeMode')).to.have.length(2); expect(g.get('altitudeMode')[0]).to.be('absolute'); expect(g.get('altitudeMode')[1]).to.be(undefined); }); it('can write MultiPolygon geometries', function() { var layout = 'XYZ'; var multiPolygon = new ol.geom.MultiPolygon([ [[[0, 0, 0], [0, 1, 0], [1, 1, 0], [1, 0, 0]]], [[[3, 0, 0], [3, 1, 0], [4, 1, 0], [4, 0, 0]]] ], layout); var features = [new ol.Feature(multiPolygon)]; var node = format.writeFeaturesNode(features); var text = '<kml xmlns="http://www.opengis.net/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2"' + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' + ' xsi:schemaLocation="http://www.opengis.net/kml/2.2' + ' https://developers.google.com/kml/schema/kml22gx.xsd">' + ' <Placemark>' + ' <MultiGeometry>' + ' <Polygon>' + ' <outerBoundaryIs>' + ' <LinearRing>' + ' <coordinates>0,0,0 0,1,0 1,1,0 1,0,0</coordinates>' + ' </LinearRing>' + ' </outerBoundaryIs>' + ' </Polygon>' + ' <Polygon>' + ' <outerBoundaryIs>' + ' <LinearRing>' + ' <coordinates>3,0,0 3,1,0 4,1,0 4,0,0</coordinates>' + ' </LinearRing>' + ' </outerBoundaryIs>' + ' </Polygon>' + ' </MultiGeometry>' + ' </Placemark>' + '</kml>'; expect(node).to.xmleql(ol.xml.parse(text)); }); it('can read empty GeometryCollection geometries', function() { var text = '<kml xmlns="http://earth.google.com/kml/2.2">' + ' <Placemark>' + ' <MultiGeometry>' + ' </MultiGeometry>' + ' </Placemark>' + '</kml>'; 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.GeometryCollection); expect(g.getGeometries()).to.be.empty(); }); it('can read heterogeneous GeometryCollection geometries', function() { var text = '<kml xmlns="http://earth.google.com/kml/2.2">' + ' <Placemark>' + ' <MultiGeometry>' + ' <Point>' + ' <coordinates>1,2,3</coordinates>' + ' </Point>' + ' <LineString>' + ' <coordinates>1,2,3 4,5,6</coordinates>' + ' </LineString>' + ' <LinearRing>' + ' <coordinates>1,2,3 4,5,6 7,8,9</coordinates>' + ' </LinearRing>' + ' <Polygon>' + ' <outerBoundaryIs>' + ' <LinearRing>' + ' <coordinates>0,0,0 0,1,0 1,1,0 1,0,0</coordinates>' + ' </LinearRing>' + ' </outerBoundaryIs>' + ' </Polygon>' + ' </MultiGeometry>' + ' </Placemark>' + '</kml>'; 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.GeometryCollection); var gs = g.getGeometries(); 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() { var text = '<kml xmlns="http://earth.google.com/kml/2.2">' + ' <Placemark>' + ' <MultiGeometry>' + ' <MultiGeometry>' + ' </MultiGeometry>' + ' </MultiGeometry>' + ' </Placemark>' + '</kml>'; 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();