UNPKG

openlayers

Version:

Build tools and sources for developing OpenLayers based mapping applications

1,211 lines (1,147 loc) 114 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.style.Stroke'); goog.require('ol.style.Style'); goog.require('ol.style.Text'); goog.require('ol.xml'); describe('ol.format.KML', function() { var format; beforeEach(function() { format = new ol.format.KML(); }); 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.removeTransform( ol.proj.get('EPSG:4326'), ol.proj.get('double')); ol.proj.removeTransform( 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 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(); expect(g).to.be.an(ol.geom.GeometryCollection); var gs = g.getGeometries(); expect(gs).to.have.length(1); expect(gs[0]).to.be.an(ol.geom.GeometryCollection); }); it('can write GeometryCollection geometries', function() { var collection = new ol.geom.GeometryCollection([ new ol.geom.Point([1,2]), new ol.geom.LineString([[1,2],[3,4]]), new ol.geom.Polygon([[[1,2],[3,4],[3,2],[1,2]]]) ]); var features = [new ol.Feature(collection)]; 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</coordinates>' + ' </Point>' + ' <LineString>' + ' <coordinates>1,2 3,4</coordinates>' + ' </LineString>' + ' <Polygon>' + ' <outerBoundaryIs>' + ' <LinearRing>' + ' <coordinates>1,2 3,4 3,2 1,2</coordinates>' + ' </LinearRing>' + ' </outerBoundaryIs>' + ' </Polygon>' + ' </MultiGeometry>' + ' </Placemark>' + '</kml>'; expect(node).to.xmleql(ol.xml.parse(text)); }); it('can read gx:Track', function() { var text = '<kml xmlns="http://earth.google.com/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2">' + ' <Placemark>' + ' <gx:Track>' + ' <when>2014-01-06T19:38:55Z</when>' + ' <when>2014-01-06T19:39:03Z</when>' + ' <when>2014-01-06T19:39:10Z</when>' + ' <gx:coord>8.1 46.1 1909.9</gx:coord>' + ' <gx:coord>8.2 46.2 1925.2</gx:coord>' + ' <gx:coord>8.3 46.3 1926.2</gx:coord>' + ' </gx:Track>' + ' </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); }); it('can read gx:MultiTrack', function() { var text = '<kml xmlns="http://earth.google.com/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2">' + ' <Placemark>' + ' <gx:MultiTrack>' + ' <gx:Track>' + ' <when>2014-01-06T19:38:55Z</when>' + ' <gx:coord>8.1 46.1 1909.9</gx:coord>' + ' </gx:Track>' + ' <gx:Track>' + ' <when>2014-01-06T19:38:55Z</when>' + ' <when>2014-01-06T19:39:10Z</when>' + ' <gx:coord>8.1 46.1 1909.9</gx:coord>' + ' <gx:coord>8.2 46.2 1925.2</gx:coord>' + ' </gx:Track>' + ' </gx:MultiTrack>' + ' </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); var gs = g.getLineStrings(); expect(gs).to.have.length(2); expect(gs[0]).to.be.an(ol.geom.LineString); }); it('can read dateTime', function() { var text = '<kml xmlns="http://earth.google.com/kml/2.2"' + ' xmlns:gx="http://www.google.com/kml/ext/2.2">' + ' <Placemark>' + ' <gx:Track>' + ' <when>2014</when>' + ' <when>2014-02</when>' + ' <when>2014-02-06</when>' + ' <when>2014-02-06T19:39:03Z</when>' + ' <when>2014-02-06T19:39:10+03:00</when>' + ' <gx:coord>8.1 46.1 1909.9</gx:coord>' + ' <gx:coord>8.2 46.2 1925.2</gx:coord>' + ' <gx:coord>8.3 46.3 1926.2</gx:coord>' + ' <gx:coord>8.4 46.4 1927.2</gx:coord>' + ' <gx:coord>8.5 46.5 1928.2</gx:coord>' + ' </gx:Track>' + ' </Placemark>' + '</kml>'; var fs = format.readFeatures(text); var f = fs[0]; var g = f.getGeometry(); var flatCoordinates = g.flatCoordinates; expect(flatCoordinates[3]).to.be.eql(Date.UTC(2014, 0, 1, 0, 0, 0)); expect(flatCoordinates[7]).to.be.eql(Date.UTC(2014, 1, 1, 0, 0, 0)); expect(flatCoordinates[11]).to.be.eql(Date.UTC(2014, 1, 6, 0, 0, 0)); expect(flatCoordinates[15]).to.be.eql(Date.UTC(2014, 1, 6, 19, 39, 3)); expect(flatCoordinates[19]).to.be.eql( Date.UTC(2014, 1, 6, 16, 39, 10) ); }); }); describe('attributes', function() { it('can read boolean attributes', function() { var text = '<kml xmlns="http://earth.google.com/kml/2.2">' + ' <Placemark>' + ' <open>1</open>' + ' <visibility>0</visibility>' + ' </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.get('open')).to.be(true); expect(f.get('visibility')).to.be(false); }); it('can read string attributes', function() { var text = '<kml xmlns="http://earth.google.com/kml/2.2">' + ' <Placemark>' + ' <address>My address</address>' + ' <description>My description</description>' + ' <name>My name</name>' + ' <phoneNumber>My phone number</phoneNumber>' + ' </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.get('address')).to.be('My address'); expect(f.get('description')).to.be('My description'); expect(f.get('name')).to.be('My name'); expect(f.get('phoneNumber')).to.be('My phone number'); }); it('strips leading and trailing whitespace in strings', function() { var text = '<kml xmlns="http://earth.google.com/kml/2.2">' + ' <Placemark>' +