Add a parser for reading and writing KML.

This adds ol.parser.ogc.KML which can be used to read and write KML documents.
NetworkLinks are retrieved asynchronously.
Current caveats of the implementation are:

* LabelStyle not yet implemented. Missing support in renderers.
* When using shared structures the parser needs to be configured with
  dimension 2.
* We need a better way to disable fill, currently we use opacity as a
  workaround.
* We cannot really roundtrip documents, since some of the info is not
  preserved in the ol structures. But we can write out most of the important
  info.
This commit is contained in:
Bart van den Eijnden
2013-04-09 15:34:03 +02:00
parent 38b1a59574
commit ed9861317f
23 changed files with 3161 additions and 23 deletions

56
examples/kml.html Normal file
View File

@@ -0,0 +1,56 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
<link rel="stylesheet" href="../resources/bootstrap/css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="../resources/layout.css" type="text/css">
<link rel="stylesheet" href="../resources/bootstrap/css/bootstrap-responsive.min.css" type="text/css">
<title>KML example</title>
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="./">OpenLayers 3 Examples</a>
<ul class="nav pull-right">
<li><iframe class="github-watch-button" src="http://ghbtns.com/github-btn.html?user=openlayers&repo=ol3&type=watch&count=true"
allowtransparency="true" frameborder="0" scrolling="0" height="20" width="90"></iframe></li>
<li><a href="https://twitter.com/share" class="twitter-share-button" data-count="none" data-hashtags="openlayers">&nbsp;</a></li>
<li><div class="g-plusone-wrapper"><div class="g-plusone" data-size="medium" data-annotation="none"></div></div></li>
</ul>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row-fluid">
<div class="span12">
<div id="map" class="map"></div>
</div>
</div>
<div class="row-fluid">
<div class="span4">
<h4 id="title">KML example</h4>
<p id="shortdesc">Example of using the KML parser.</p>
<div id="docs">
<p>See the <a href="kml.js" target="_blank">kml.js source</a> to see how this is done.</p>
</div>
<div id="tags">KML</div>
</div>
</div>
</div>
<script src="jquery.min.js" type="text/javascript"></script>
<script src="loader.js?id=kml" type="text/javascript"></script>
<script src="../resources/social-links.js" type="text/javascript"></script>
</body>
</html>

50
examples/kml.js Normal file
View File

@@ -0,0 +1,50 @@
goog.require('ol.Collection');
goog.require('ol.Map');
goog.require('ol.RendererHint');
goog.require('ol.View2D');
goog.require('ol.layer.TileLayer');
goog.require('ol.layer.Vector');
goog.require('ol.parser.ogc.KML');
goog.require('ol.projection');
goog.require('ol.source.TiledWMS');
goog.require('ol.source.Vector');
var raster = new ol.layer.TileLayer({
source: new ol.source.TiledWMS({
url: 'http://vmap0.tiles.osgeo.org/wms/vmap0',
crossOrigin: null,
params: {
'LAYERS': 'basic',
'VERSION': '1.1.1',
'FORMAT': 'image/jpeg'
}
})
});
var epsg4326 = ol.projection.get('EPSG:4326');
var vector = new ol.layer.Vector({
source: new ol.source.Vector({
projection: epsg4326
})
});
var map = new ol.Map({
layers: new ol.Collection([raster, vector]),
renderer: ol.RendererHint.CANVAS,
target: 'map',
view: new ol.View2D({
projection: epsg4326,
center: [-112.169, 36.099],
zoom: 11
})
});
var kml = new ol.parser.ogc.KML({
maxDepth: 1, dimension: 2, extractStyles: true, extractAttributes: true});
$.ajax({
url: 'kml/lines.kml'
}).done(function(data) {
vector.parseFeatures(data, kml, epsg4326);
});

275
examples/kml/lines.kml Normal file
View File

@@ -0,0 +1,275 @@
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.0">
<Document>
<name>KML Samples</name>
<open>1</open>
<description>Unleash your creativity with the help of these examples!</description>
<Style id="downArrowIcon">
<IconStyle>
<Icon>
<href>http://maps.google.com/mapfiles/kml/pal4/icon28.png</href>
</Icon>
</IconStyle>
</Style>
<Style id="globeIcon">
<IconStyle>
<Icon>
<href>http://maps.google.com/mapfiles/kml/pal3/icon19.png</href>
</Icon>
</IconStyle>
<LineStyle>
<width>2</width>
</LineStyle>
</Style>
<Style id="transPurpleLineGreenPoly">
<LineStyle>
<color>7fff00ff</color>
<width>4</width>
</LineStyle>
<PolyStyle>
<color>7f00ff00</color>
</PolyStyle>
</Style>
<Style id="yellowLineGreenPoly">
<LineStyle>
<color>7f00ffff</color>
<width>4</width>
</LineStyle>
<PolyStyle>
<color>7f00ff00</color>
</PolyStyle>
</Style>
<Style id="thickBlackLine">
<LineStyle>
<color>87000000</color>
<width>10</width>
</LineStyle>
</Style>
<Style id="redLineBluePoly">
<LineStyle>
<color>ff0000ff</color>
</LineStyle>
<PolyStyle>
<color>ffff0000</color>
</PolyStyle>
</Style>
<Style id="blueLineRedPoly">
<LineStyle>
<color>ffff0000</color>
</LineStyle>
<PolyStyle>
<color>ff0000ff</color>
</PolyStyle>
</Style>
<Style id="transRedPoly">
<LineStyle>
<width>1.5</width>
</LineStyle>
<PolyStyle>
<color>7d0000ff</color>
</PolyStyle>
</Style>
<Style id="transBluePoly">
<LineStyle>
<width>1.5</width>
</LineStyle>
<PolyStyle>
<color>7dff0000</color>
</PolyStyle>
</Style>
<Style id="transGreenPoly">
<LineStyle>
<width>1.5</width>
</LineStyle>
<PolyStyle>
<color>7d00ff00</color>
</PolyStyle>
</Style>
<Style id="transYellowPoly">
<LineStyle>
<width>1.5</width>
</LineStyle>
<PolyStyle>
<color>7d00ffff</color>
</PolyStyle>
</Style>
<Style id="noDrivingDirections">
<BalloonStyle>
<text><![CDATA[
<b>$[name]</b>
<br /><br />
$[description]
]]></text>
</BalloonStyle>
</Style>
<Folder>
<name>Paths</name>
<visibility>0</visibility>
<description>Examples of paths. Note that the tessellate tag is by default
set to 0. If you want to create tessellated lines, they must be authored
(or edited) directly in KML.</description>
<Placemark>
<name>Tessellated</name>
<visibility>0</visibility>
<description><![CDATA[If the <tessellate> tag has a value of 1, the line will contour to the underlying terrain]]></description>
<LookAt>
<longitude>-112.0822680013139</longitude>
<latitude>36.09825589333556</latitude>
<altitude>0</altitude>
<range>2889.145007690472</range>
<tilt>62.04855796276328</tilt>
<heading>103.8120432044965</heading>
</LookAt>
<LineString>
<tessellate>1</tessellate>
<coordinates> -112.0814237830345,36.10677870477137,0
-112.0870267752693,36.0905099328766,0 </coordinates>
</LineString>
</Placemark>
<Placemark>
<name>Untessellated</name>
<visibility>0</visibility>
<description><![CDATA[If the <tessellate> tag has a value of 0, the line follow a simple straight-line path from point to point]]></description>
<LookAt>
<longitude>-112.0822680013139</longitude>
<latitude>36.09825589333556</latitude>
<altitude>0</altitude>
<range>2889.145007690472</range>
<tilt>62.04855796276328</tilt>
<heading>103.8120432044965</heading>
</LookAt>
<LineString>
<tessellate>0</tessellate>
<coordinates> -112.080622229595,36.10673460007995,0
-112.085242575315,36.09049598612422,0 </coordinates>
</LineString>
</Placemark>
<Placemark>
<name>Absolute</name>
<visibility>0</visibility>
<description>Transparent purple line</description>
<LookAt>
<longitude>-112.2719329043177</longitude>
<latitude>36.08890633450894</latitude>
<altitude>0</altitude>
<range>2569.386744398339</range>
<tilt>44.60763714063257</tilt>
<heading>-106.8161545998597</heading>
</LookAt>
<styleUrl>#transPurpleLineGreenPoly</styleUrl>
<LineString>
<tessellate>1</tessellate>
<altitudeMode>absolute</altitudeMode>
<coordinates> -112.265654928602,36.09447672602546,2357
-112.2660384528238,36.09342608838671,2357
-112.2668139013453,36.09251058776881,2357
-112.2677826834445,36.09189827357996,2357
-112.2688557510952,36.0913137941187,2357
-112.2694810717219,36.0903677207521,2357
-112.2695268555611,36.08932171487285,2357
-112.2690144567276,36.08850916060472,2357
-112.2681528815339,36.08753813597956,2357
-112.2670588176031,36.08682685262568,2357
-112.2657374587321,36.08646312301303,2357 </coordinates>
</LineString>
</Placemark>
<Placemark>
<name>Absolute Extruded</name>
<visibility>0</visibility>
<description>Transparent green wall with yellow outlines</description>
<LookAt>
<longitude>-112.2643334742529</longitude>
<latitude>36.08563154742419</latitude>
<altitude>0</altitude>
<range>4451.842204068102</range>
<tilt>44.61038665812578</tilt>
<heading>-125.7518698668815</heading>
</LookAt>
<styleUrl>#yellowLineGreenPoly</styleUrl>
<LineString>
<extrude>1</extrude>
<tessellate>1</tessellate>
<altitudeMode>absolute</altitudeMode>
<coordinates> -112.2550785337791,36.07954952145647,2357
-112.2549277039738,36.08117083492122,2357
-112.2552505069063,36.08260761307279,2357
-112.2564540158376,36.08395660588506,2357
-112.2580238976449,36.08511401044813,2357
-112.2595218489022,36.08584355239394,2357
-112.2608216347552,36.08612634548589,2357
-112.262073428656,36.08626019085147,2357
-112.2633204928495,36.08621519860091,2357
-112.2644963846444,36.08627897945274,2357
-112.2656969554589,36.08649599090644,2357 </coordinates>
</LineString>
</Placemark>
<Placemark>
<name>Relative</name>
<visibility>0</visibility>
<description>Black line (10 pixels wide), height tracks terrain</description>
<LookAt>
<longitude>-112.2580438551384</longitude>
<latitude>36.1072674824385</latitude>
<altitude>0</altitude>
<range>2927.61105910266</range>
<tilt>44.61324882043339</tilt>
<heading>4.947421249553717</heading>
</LookAt>
<styleUrl>#thickBlackLine</styleUrl>
<LineString>
<tessellate>1</tessellate>
<altitudeMode>relativeToGround</altitudeMode>
<coordinates> -112.2532845153347,36.09886943729116,645
-112.2540466121145,36.09919570465255,645
-112.254734666947,36.09984998366178,645
-112.255493345654,36.10051310621746,645
-112.2563157098468,36.10108441943419,645
-112.2568033076439,36.10159722088088,645
-112.257494011321,36.10204323542867,645
-112.2584106072308,36.10229131995655,645
-112.2596588987972,36.10240001286358,645
-112.2610581199487,36.10213176873407,645
-112.2626285262793,36.10157011437219,645 </coordinates>
</LineString>
</Placemark>
<Placemark>
<name>Relative Extruded</name>
<visibility>0</visibility>
<description>Opaque blue walls with red outline, height tracks terrain</description>
<LookAt>
<longitude>-112.2683594333433</longitude>
<latitude>36.09884362144909</latitude>
<altitude>0</altitude>
<range>2184.193522571467</range>
<tilt>44.60855445139561</tilt>
<heading>-72.24271551768405</heading>
</LookAt>
<styleUrl>#redLineBluePoly</styleUrl>
<LineString>
<extrude>1</extrude>
<tessellate>1</tessellate>
<altitudeMode>relativeToGround</altitudeMode>
<coordinates> -112.2656634181359,36.09445214722695,630
-112.2652238941097,36.09520916122063,630
-112.2645079986395,36.09580763864907,630
-112.2638827428817,36.09628572284063,630
-112.2635746835406,36.09679275951239,630
-112.2635711822407,36.09740038871899,630
-112.2640296531825,36.09804913435539,630
-112.264327720538,36.09880337400301,630
-112.2642436562271,36.09963644790288,630
-112.2639148687042,36.10055381117246,630
-112.2626894973474,36.10149062823369,630 </coordinates>
</LineString>
</Placemark>
<Placemark>
<name>Blue Icon</name>
<description>Just another blue icon.</description>
<styleUrl>kml/styles.kml#blueIcons</styleUrl>
<Point>
<coordinates>-112.292238941097,36.09520916122063,630</coordinates>
</Point>
</Placemark>
</Folder>
</Document>
</kml>

21
examples/kml/styles.kml Normal file
View File

@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
This file contains styles referenced by other KML files in this project.
http://kml-samples.googlecode.com/svn/trunk/kml/Style/styles.kml
-->
<kml xmlns="http://earth.google.com/kml/2.1">
<Document id="globalStyles">
<Style id="blueIcons">
<IconStyle>
<color>ffff0000</color>
<Icon>
<href>http://maps.google.com/mapfiles/kml/shapes/sunny.png</href>
</Icon>
</IconStyle>
</Style>
</Document>
</kml>

View File

@@ -326,37 +326,48 @@ ol.layer.Vector.prototype.parseFeatures = function(data, parser, projection) {
var callback = function(feature, type) {
return lookup[type];
};
var addFeatures = function(features) {
var sourceProjection = this.getSource().getProjection();
var transform = ol.projection.getTransform(sourceProjection, projection);
transform(
this.pointVertices_.coordinates,
this.pointVertices_.coordinates,
this.pointVertices_.getDimension());
transform(
this.lineVertices_.coordinates,
this.lineVertices_.coordinates,
this.lineVertices_.getDimension());
transform(
this.polygonVertices_.coordinates,
this.polygonVertices_.coordinates,
this.polygonVertices_.getDimension());
this.addFeatures(features);
};
if (goog.isString(data)) {
goog.asserts.assert(goog.isFunction(parser.readFeaturesFromString),
'Expected a parser with readFeaturesFromString method.');
features = parser.readFeaturesFromString(data, {callback: callback});
addFeatures.call(this, features);
} else if (goog.isObject(data)) {
goog.asserts.assert(goog.isFunction(parser.readFeaturesFromObject),
'Expected a parser with a readFeaturesFromObject method.');
features = parser.readFeaturesFromObject(data, {callback: callback});
if (goog.isFunction(parser.readFeaturesFromObjectAsync)) {
parser.readFeaturesFromObjectAsync(data, goog.bind(addFeatures, this),
{callback: callback});
} else {
goog.asserts.assert(goog.isFunction(parser.readFeaturesFromObject),
'Expected a parser with a readFeaturesFromObject method.');
features = parser.readFeaturesFromObject(data, {callback: callback});
addFeatures.call(this, features);
}
} else {
// TODO: parse more data types
throw new Error('Data type not supported: ' + data);
}
var sourceProjection = this.getSource().getProjection();
var transform = ol.projection.getTransform(sourceProjection, projection);
transform(
this.pointVertices_.coordinates,
this.pointVertices_.coordinates,
this.pointVertices_.getDimension());
transform(
this.lineVertices_.coordinates,
this.lineVertices_.coordinates,
this.lineVertices_.getDimension());
transform(
this.polygonVertices_.coordinates,
this.polygonVertices_.coordinates,
this.polygonVertices_.getDimension());
this.addFeatures(features);
};

View File

@@ -1,3 +1,4 @@
goog.provide('ol.parser.AsyncObjectFeatureParser');
goog.provide('ol.parser.DomFeatureParser');
goog.provide('ol.parser.ObjectFeatureParser');
goog.provide('ol.parser.ReadFeaturesOptions');
@@ -54,6 +55,23 @@ ol.parser.StringFeatureParser.prototype.readFeaturesFromString =
goog.abstractMethod;
/**
* @interface
*/
ol.parser.AsyncObjectFeatureParser = function() {};
/**
* @param {Object} obj Object representing features.
* @param {function(Array.<ol.Feature>)} callback Callback which is called
* after parsing.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options.
*/
ol.parser.AsyncObjectFeatureParser.prototype.readFeaturesFromObjectAsync =
goog.abstractMethod;
/**
* @typedef {function(ol.Feature, ol.geom.GeometryType):ol.geom.SharedVertices}
*/

View File

@@ -0,0 +1,3 @@
@exportSymbol ol.parser.ogc.KML
@exportProperty ol.parser.ogc.KML.prototype.read
@exportProperty ol.parser.ogc.KML.prototype.write

1041
src/ol/parser/ogc/kml.js Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -9,6 +9,9 @@ goog.require('ol.parser.Parser');
* @extends {ol.parser.Parser}
*/
ol.parser.XML = function() {
if (window.ActiveXObject) {
this.xmldom = new ActiveXObject('Microsoft.XMLDOM');
}
this.regExes = {
trimSpace: (/^\s*|\s*$/g),
removeSpace: (/\s*/g),
@@ -149,3 +152,97 @@ ol.parser.XML.prototype.getAttributeNS = function(node, uri, name) {
}
return attributeValue;
};
/**
* Create a new element with namespace. This node can be appended to
* another node with the standard node.appendChild method. For
* cross-browser support, this method must be used instead of
* document.createElementNS.
*
* @param {string} name The qualified name of the element (prefix:localname).
* @param {string=} opt_uri Namespace URI for the element.
* @return {Element} A DOM element with namespace.
*/
ol.parser.XML.prototype.createElementNS = function(name, opt_uri) {
var uri = opt_uri ? opt_uri : this.defaultNamespaceURI;
var element;
if (this.xmldom) {
element = this.xmldom.createNode(1, name, uri);
} else {
element = document.createElementNS(uri, name);
}
return element;
};
/**
* Shorthand for applying one of the named writers and appending the
* results to a node.
*
* @param {string} name The name of a node to generate. Only use a local name.
* @param {Object} obj Structure containing data for the writer.
* @param {string=} opt_uri The name space uri to which the node belongs.
* @param {Element=} opt_parent Result will be appended to this node. If no
* parent is supplied, the node will not be appended to anything.
* @return {?Element} The child node.
*/
ol.parser.XML.prototype.writeNode = function(name, obj, opt_uri, opt_parent) {
var child = null;
if (goog.isDef(this.writers)) {
var uri = opt_uri ? opt_uri : this.defaultNamespaceURI;
child = this.writers[uri][name].apply(this, [obj]);
if (opt_parent && child) {
opt_parent.appendChild(child);
}
}
return child;
};
/**
* Create a text node. This node can be appended to another node with
* the standard node.appendChild method. For cross-browser support,
* this method must be used instead of document.createTextNode.
*
* @param {string} text The text of the node.
* @return {Element} A DOM text node.
*/
ol.parser.XML.prototype.createTextNode = function(text) {
var node;
if (this.xmldom) {
node = this.xmldom.createTextNode(text);
} else {
node = document.createTextNode(text);
}
return node;
};
/**
* Adds a new attribute or changes the value of an attribute with the given
* namespace and name.
*
* @param {Element} node Element node on which to set the attribute.
* @param {string} uri Namespace URI for the attribute.
* @param {string} name Qualified name (prefix:localname) for the attribute.
* @param {string} value Attribute value.
*/
ol.parser.XML.prototype.setAttributeNS = function(node, uri, name, value) {
if (node.setAttributeNS) {
node.setAttributeNS(uri, name, value);
} else {
if (this.xmldom) {
if (uri) {
var attribute = node.ownerDocument.createNode(
2, name, uri);
attribute.nodeValue = value;
node.setAttributeNode(attribute);
} else {
node.setAttribute(name, value);
}
} else {
throw new Error('setAttributeNS not implemented');
}
}
};

View File

@@ -119,7 +119,7 @@ ol.style.Icon = function(options) {
* @return {ol.style.IconLiteral} Literal shape symbolizer.
*/
ol.style.Icon.prototype.createLiteral = function(feature) {
var attrs = feature.getAttributes();
var attrs = feature && feature.getAttributes();
var url = /** @type {string} */ (this.url_.evaluate(feature, attrs));
goog.asserts.assert(goog.isString(url) && url != '#', 'url must be a string');

View File

@@ -0,0 +1,289 @@
goog.provide('ol.test.parser.ogc.kml');
describe('ol.parser.kml', function() {
var parser = new ol.parser.ogc.KML();
describe('Test KML parser', function() {
it('Polygon read correctly', function() {
var url = 'spec/ol/parser/ogc/xml/kml_polygon.kml';
afterLoadXml(url, function(xml) {
var obj = parser.read(xml);
var output = parser.write(obj);
var expected = '<kml xmlns="http://www.opengis.net/kml/2.2">' +
'<Document><name>Polygon.kml</name><open>0</open><Placemark>' +
'<name>hollow box</name><Polygon><outerBoundaryIs><LinearRing>' +
'<coordinates>-122.366278,37.818844,30 ' +
'-122.365248,37.819267,30 ' +
'-122.36564,37.819861,30 ' +
'-122.366669,37.819429,30 ' +
'-122.366278,37.818844,30</coordinates></LinearRing>' +
'</outerBoundaryIs><innerBoundaryIs><LinearRing><coordinates>' +
'-122.366212,37.818977,30 ' +
'-122.365424,37.819294,30 ' +
'-122.365704,37.819731,30 ' +
'-122.366488,37.819402,30 ' +
'-122.366212,37.818977,30</coordinates></LinearRing>' +
'</innerBoundaryIs></Polygon></Placemark></Document></kml>';
expect(output).to.eql(expected);
expect(obj.features.length).to.eql(1);
var geom = obj.features[0].getGeometry();
expect(geom instanceof ol.geom.Polygon).to.be.ok();
expect(geom.dimension).to.eql(3);
});
});
it('Linestring read correctly', function() {
var url = 'spec/ol/parser/ogc/xml/kml_linestring.kml';
afterLoadXml(url, function(xml) {
var obj = parser.read(xml);
var output = parser.write(obj);
var expected = '<kml xmlns="http://www.opengis.net/kml/2.2">' +
'<Document><name>LineString.kml</name><open>1</open><Placemark>' +
'<name>unextruded</name><LineString><coordinates>-122.364383,' +
'37.824664,0 -122.364152,37.824322,0</coordinates></LineString>' +
'</Placemark><Placemark><name>extruded</name><LineString>' +
'<coordinates>-122.364167,37.824787,50 -122.363917,37.824423,50' +
'</coordinates></LineString></Placemark></Document></kml>';
expect(output).to.eql(expected);
expect(obj.features.length).to.eql(2);
var geom = obj.features[0].getGeometry();
expect(geom instanceof ol.geom.LineString).to.be.ok();
expect(geom.dimension).to.eql(3);
geom = obj.features[1].getGeometry();
expect(geom instanceof ol.geom.LineString).to.be.ok();
});
});
it('Point read correctly', function() {
var url = 'spec/ol/parser/ogc/xml/kml_point.kml';
afterLoadXml(url, function(xml) {
var obj = parser.read(xml);
var output = parser.write(obj);
var expected = '<kml xmlns="http://www.opengis.net/kml/2.2">' +
'<Document><Placemark><name>Simple placemark</name><description>' +
'Attached to the ground. Intelligently places itself \n' +
' at the height of the underlying terrain.</description>' +
'<Point><coordinates>-122.0822035425683,37.42228990140251,0' +
'</coordinates></Point></Placemark></Document></kml>';
expect(output).to.eql(expected);
expect(obj.features.length).to.eql(1);
var geom = obj.features[0].getGeometry();
expect(geom instanceof ol.geom.Point).to.be.ok();
expect(geom.dimension).to.eql(3);
});
});
it('NetworkLink read correctly', function(done) {
var url = 'spec/ol/parser/ogc/xml/kml_networklink.kml';
afterLoadXml(url, function(xml) {
var p = new ol.parser.ogc.KML({maxDepth: 1});
// we need to supply a callback to get visited NetworkLinks
var obj = p.read(xml, function(features) {
expect(features.length).to.eql(3);
done();
});
});
});
it('NetworkLink read correctly [recursively]', function(done) {
var url = 'spec/ol/parser/ogc/xml/kml_networklink_depth.kml';
afterLoadXml(url, function(xml) {
var p = new ol.parser.ogc.KML({maxDepth: 2});
// we need to supply a callback to get visited NetworkLinks
var obj = p.read(xml, function(features) {
expect(features.length).to.eql(2);
done();
});
});
});
it('NetworkLink maxDepth', function(done) {
var url = 'spec/ol/parser/ogc/xml/kml_networklink_depth.kml';
afterLoadXml(url, function(xml) {
var p = new ol.parser.ogc.KML({maxDepth: 1});
// we need to supply a callback to get visited NetworkLinks
var obj = p.read(xml, function(features) {
// since maxDepth is 1, we will not get to the second feature
expect(features.length).to.eql(1);
done();
});
});
});
it('Extended data read correctly', function() {
var url = 'spec/ol/parser/ogc/xml/kml_extended_data.kml';
afterLoadXml(url, function(xml) {
var obj = parser.read(xml);
expect(obj.features[0].get('name')).to.eql('Extended data placemark');
var description = 'Attached to the ground. Intelligently places ' +
'itself \n at the height of the underlying terrain.';
expect(obj.features[0].get('description')).to.eql(description);
expect(obj.features[0].get('foo')).to.eql('bar');
});
});
it('Extended data read correctly [2]', function() {
var url = 'spec/ol/parser/ogc/xml/kml_extended_data2.kml';
afterLoadXml(url, function(xml) {
var obj = parser.read(xml);
var feature = obj.features[0];
expect(feature.get('TrailHeadName')).to.eql('Pi in the sky');
expect(feature.get('TrailLength')).to.eql('3.14159');
expect(feature.get('ElevationGain')).to.eql('10');
});
});
it('Multi geometry read correctly', function() {
var url = 'spec/ol/parser/ogc/xml/kml_multigeometry.kml';
afterLoadXml(url, function(xml) {
var obj = parser.read(xml);
var geom = obj.features[0].getGeometry();
var expected = '<kml xmlns="http://www.opengis.net/kml/2.2">' +
'<Document><name>Polygon.kml</name><open>0</open><Placemark>' +
'<name>SF Marina Harbor Master</name><MultiGeometry><LineString>' +
'<coordinates>-122.4425587930444,37.80666418607323,0 ' +
'-122.4428379594768,37.80663578323093,0</coordinates>' +
'</LineString><LineString><coordinates>-122.4425509770566,' +
'37.80662588061205,0 -122.4428340530617,37.8065999493009,0' +
'</coordinates></LineString></MultiGeometry></Placemark>' +
'</Document></kml>';
var output = parser.write(obj);
expect(output).to.eql(expected);
expect(geom instanceof ol.geom.MultiLineString).to.be.ok();
});
});
it('Discrete multi geometry read correctly', function() {
var url = 'spec/ol/parser/ogc/xml/kml_multigeometry_discrete.kml';
afterLoadXml(url, function(xml) {
var obj = parser.read(xml);
var geom = obj.features[0].getGeometry();
expect(geom instanceof ol.geom.GeometryCollection).to.be.ok();
expect(geom.components.length).to.eql(2);
expect(geom.components[0] instanceof ol.geom.LineString).to.be.ok();
expect(geom.components[1] instanceof ol.geom.Point).to.be.ok();
});
});
it('Test extract tracks', function() {
var url = 'spec/ol/parser/ogc/xml/kml_macnoise.kml';
afterLoadXml(url, function(xml) {
var p = new ol.parser.ogc.KML({extractStyles: true,
trackAttributes: ['speed', 'num']});
var obj = p.read(xml);
expect(obj.features.length).to.eql(170);
var attr = obj.features[4].getAttributes();
// standard track point attributes
expect(attr['when'] instanceof Date).to.be.ok();
expect(attr['when'].getTime()).to.eql(1272736815000);
expect(attr['altitude']).to.eql(1006);
expect(attr['heading']).to.eql(230);
expect(attr['tilt']).to.eql(0);
expect(attr['roll']).to.eql(0);
expect(attr['name']).to.eql('B752');
expect(attr['adflag']).to.eql('A');
expect(attr['flightid']).to.eql('DAL2973');
expect(attr['speed']).to.eql('166');
expect(attr['num']).to.eql('50');
var geom = obj.features[4].getGeometry();
expect(geom.get(0)).to.eql(-93.0753620391713);
expect(geom.get(1)).to.eql(44.9879724110872);
expect(geom.get(2)).to.eql(1006);
});
});
it('Test CDATA attributes', function() {
var cdata = '<kml xmlns="http://earth.google.com/kml/2.0"><Document>' +
'<Placemark><name><![CDATA[Pezinok]]> </name><description>' +
'<![CDATA[Full of text.]]></description><styleUrl>#rel1.0' +
'</styleUrl><Point> <coordinates>17.266666, 48.283333</coordinates>' +
'</Point></Placemark></Document></kml>';
var obj = parser.read(cdata);
expect(obj.features[0].get('description')).to.eql('Full of text.');
expect(obj.features[0].get('name')).to.eql('Pezinok');
});
it('Test line style', function() {
var test_style = '<kml xmlns="http://earth.google.com/kml/2.0"> ' +
'<Placemark> <Style> <LineStyle> <color>870000ff</color> ' +
'<width>10</width> </LineStyle> </Style> <LineString> ' +
'<coordinates> -112,36 -113,37 </coordinates> </LineString>' +
'</Placemark></kml>';
var p = new ol.parser.ogc.KML({extractStyles: true});
var obj = p.read(test_style);
var output = p.write(obj);
var expected = '<kml xmlns="http://www.opengis.net/kml/2.2">' +
'<Document><Placemark><Style><LineStyle><color>870000ff</color>' +
'<width>10</width></LineStyle></Style><LineString><coordinates>' +
'-112,36 -113,37</coordinates></LineString></Placemark>' +
'</Document></kml>';
expect(output).to.eql(expected);
var symbolizer = obj.features[0].getSymbolizerLiterals()[0];
expect(symbolizer instanceof ol.style.LineLiteral).to.be.ok();
expect(symbolizer.strokeColor).to.eql('#ff0000');
expect(symbolizer.opacity).to.eql(0.5294117647058824);
expect(symbolizer.strokeWidth).to.eql(10);
});
it('Test style fill', function() {
var test_style_fill = '<kml xmlns="http://earth.google.com/kml/2.0"> ' +
'<Placemark> <Style> <PolyStyle> <fill>1</fill> ' +
'<color>870000ff</color> <width>10</width> </PolyStyle> </Style>' +
'<Polygon><outerBoundaryIs><LinearRing><coordinates>' +
'5.001370157823406,49.26855713824488 8.214706453896161,' +
'49.630662409673505 8.397385910100951,48.45172350357396 ' +
'5.001370157823406,49.26855713824488</coordinates></LinearRing>' +
'</outerBoundaryIs></Polygon></Placemark><Placemark> <Style> ' +
'<PolyStyle><fill>0</fill><color>870000ff</color><width>10</width> ' +
'</PolyStyle> </Style>' +
'<Polygon><outerBoundaryIs><LinearRing><coordinates>' +
'5.001370157823406,49.26855713824488 8.214706453896161,' +
'49.630662409673505 8.397385910100951,48.45172350357396 ' +
'5.001370157823406,49.26855713824488</coordinates></LinearRing>' +
'</outerBoundaryIs></Polygon></Placemark></kml>';
var p = new ol.parser.ogc.KML({extractStyles: true});
var obj = p.read(test_style_fill);
var output = p.write(obj);
var expected = '<kml xmlns="http://www.opengis.net/kml/2.2"><Document>' +
'<Placemark><Style><PolyStyle><fill>1</fill><color>870000ff' +
'</color><width>10</width></PolyStyle></Style><Polygon>' +
'<outerBoundaryIs><LinearRing><coordinates>5.001370157823406,' +
'49.26855713824488 8.214706453896161,49.630662409673505 ' +
'8.397385910100951,48.45172350357396 5.001370157823406,' +
'49.26855713824488</coordinates></LinearRing></outerBoundaryIs>' +
'</Polygon></Placemark><Placemark><Style><PolyStyle><fill>0</fill>' +
'<color>00000ff</color><width>10</width></PolyStyle></Style>' +
'<Polygon><outerBoundaryIs><LinearRing><coordinates>' +
'5.001370157823406,49.26855713824488 8.214706453896161,' +
'49.630662409673505 8.397385910100951,48.45172350357396 ' +
'5.001370157823406,49.26855713824488</coordinates></LinearRing>' +
'</outerBoundaryIs></Polygon></Placemark></Document></kml>';
expect(output).to.eql(expected);
var symbolizer1 = obj.features[0].getSymbolizerLiterals()[0];
var symbolizer2 = obj.features[1].getSymbolizerLiterals()[0];
expect(symbolizer1.fillColor).to.eql('#ff0000');
expect(symbolizer2.opacity).to.eql(0);
});
it('Test iconStyle', function() {
var url = 'spec/ol/parser/ogc/xml/kml_iconstyle.kml';
afterLoadXml(url, function(xml) {
var p = new ol.parser.ogc.KML({extractStyles: true});
var obj = p.read(xml);
var output = p.write(obj);
var expected = '<kml xmlns="http://www.opengis.net/kml/2.2">' +
'<Document><Style id="pushpin"><IconStyle id="mystyle">' +
'<Icon><href>http://maps.google.com/mapfiles/kml/pushpin/' +
'ylw-pushpin.png</href></Icon></IconStyle></Style>' +
'<Placemark><name>Pin on a mountaintop</name><styleUrl>' +
'#pushpin</styleUrl><Point><coordinates>170.1435558771009,' +
'-43.60505741890396,0</coordinates></Point></Placemark>' +
'</Document></kml>';
expect(output).to.eql(expected);
var symbolizer = obj.features[0].getSymbolizerLiterals()[0];
var url = 'http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png';
expect(symbolizer.url).to.eql(url);
expect(symbolizer.width).to.eql(32);
expect(symbolizer.height).to.eql(32);
});
});
});
});
goog.require('goog.net.XhrIo');
goog.require('ol.Feature');
goog.require('ol.geom.GeometryCollection');
goog.require('ol.geom.LineString');
goog.require('ol.geom.MultiLineString');
goog.require('ol.geom.Point');
goog.require('ol.geom.Polygon');
goog.require('ol.parser.ogc.KML');
goog.require('ol.style.LineLiteral');

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<NetworkLink><Link><href>spec/ol/parser/ogc/xml/kml_polygon.kml</href></Link></NetworkLink>
</Document>
</kml>

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Placemark>
<name>Extended data placemark</name>
<description>Attached to the ground. Intelligently places itself
at the height of the underlying terrain.</description>
<ExtendedData>
<Data name="foo">
<value>bar</value>
</Data>
</ExtendedData>
<Point>
<coordinates>-122.0822035425683,37.42228990140251,0</coordinates>
</Point>
</Placemark>
</kml>

View File

@@ -0,0 +1,30 @@
<kml xmlns="http://earth.google.com/kml/2.2">
<Document>
<Placemark>
<name>Easy trail</name>
<ExtendedData>
<SchemaData schemaUrl="#TrailHeadTypeId">
<SimpleData name="TrailHeadName">Pi in the sky</SimpleData>
<SimpleData name="TrailLength">3.14159</SimpleData>
<SimpleData name="ElevationGain">10</SimpleData>
</SchemaData>
</ExtendedData>
<Point>
<coordinates>-122.000,37.002</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Difficult trail</name>
<ExtendedData>
<SchemaData schemaUrl="#TrailHeadTypeId">
<SimpleData name="TrailHeadName">Mount Everest</SimpleData>
<SimpleData name="TrailLength">347.45</SimpleData>
<SimpleData name="ElevationGain">10000</SimpleData>
</SchemaData>
</ExtendedData>
<Point>
<coordinates>-122.000,37.002</coordinates>
</Point>
</Placemark>
</Document>
</kml>

View File

@@ -0,0 +1,23 @@
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">
<Document>
<Style id="pushpin">
<IconStyle id="mystyle">
<color>ffffffff</color>
<colorMode>normal</colorMode>
<scale>1</scale>
<heading>0</heading>
<Icon>
<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
</Icon>
<hotSpot x="0.5" y="0.5" xunits="fraction" yunits="fraction"/>
</IconStyle>
</Style>
<Placemark id="mountainpin1">
<name>Pin on a mountaintop</name>
<styleUrl>#pushpin</styleUrl>
<Point>
<coordinates>170.1435558771009,-43.60505741890396,0</coordinates>
</Point>
</Placemark>
</Document>
</kml>

View File

@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>LineString.kml</name>
<open>1</open>
<LookAt>
<longitude>-122.36415</longitude>
<latitude>37.824553</latitude>
<altitude>0</altitude>
<range>150</range>
<tilt>50</tilt>
<heading>0</heading>
</LookAt>
<Placemark>
<name>unextruded</name>
<LineString>
<extrude>1</extrude>
<tessellate>1</tessellate>
<coordinates>
-122.364383,37.824664,0 -122.364152,37.824322,0
</coordinates>
</LineString>
</Placemark>
<Placemark>
<name>extruded</name>
<LineString>
<extrude>1</extrude>
<tessellate>1</tessellate>
<altitudeMode>relativeToGround</altitudeMode>
<coordinates>
-122.364167,37.824787,50 -122.363917,37.824423,50
</coordinates>
</LineString>
</Placemark>
</Document>
</kml>

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>Polygon.kml</name>
<open>0</open>
<Placemark>
<name>SF Marina Harbor Master</name>
<visibility>0</visibility>
<MultiGeometry>
<LineString>
<!-- north wall -->
<coordinates>
-122.4425587930444,37.80666418607323,0
-122.4428379594768,37.80663578323093,0
</coordinates>
</LineString>
<LineString>
<!-- south wall -->
<coordinates>
-122.4425509770566,37.80662588061205,0
-122.4428340530617,37.8065999493009,0
</coordinates>
</LineString>
</MultiGeometry>
</Placemark>
</Document>
</kml>

View File

@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>Polygon.kml</name>
<open>0</open>
<Placemark>
<name>SF Marina Harbor Master</name>
<visibility>0</visibility>
<MultiGeometry>
<LineString>
<!-- north wall -->
<coordinates>
-122.4425587930444,37.80666418607323,0
-122.4428379594768,37.80663578323093,0
</coordinates>
</LineString>
<Point>
<coordinates>
-122.4428340530617,37.8065999493009,0
</coordinates>
</Point>
</MultiGeometry>
</Placemark>
</Document>
</kml>

View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Placemark>
<name>Simple placemark</name>
<description>Attached to the ground. Intelligently places itself
at the height of the underlying terrain.</description>
<Point>
<coordinates>-122.0822035425683,37.42228990140251,0</coordinates>
</Point>
</Placemark>
<NetworkLink><Link><href>spec/ol/parser/ogc/xml/kml_polygon.kml</href></Link></NetworkLink>
<NetworkLink><Link><href>spec/ol/parser/ogc/xml/kml_point.kml</href></Link></NetworkLink>
</Document>
</kml>

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Placemark>
<name>Simple placemark</name>
<description>Attached to the ground. Intelligently places itself
at the height of the underlying terrain.</description>
<Point>
<coordinates>-122.0822035425683,37.42228990140251,0</coordinates>
</Point>
</Placemark>
<NetworkLink><Link><href>spec/ol/parser/ogc/xml/kml_depth.kml</href></Link></NetworkLink>
</Document>
</kml>

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Placemark>
<name>Simple placemark</name>
<description>Attached to the ground. Intelligently places itself
at the height of the underlying terrain.</description>
<Point>
<coordinates>-122.0822035425683,37.42228990140251,0</coordinates>
</Point>
</Placemark>
</kml>

View File

@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>Polygon.kml</name>
<open>0</open>
<Placemark>
<name>hollow box</name>
<Polygon>
<extrude>1</extrude>
<altitudeMode>relativeToGround</altitudeMode>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-122.366278,37.818844,30
-122.365248,37.819267,30
-122.365640,37.819861,30
-122.366669,37.819429,30
-122.366278,37.818844,30
</coordinates>
</LinearRing>
</outerBoundaryIs>
<innerBoundaryIs>
<LinearRing>
<coordinates>
-122.366212,37.818977,30
-122.365424,37.819294,30
-122.365704,37.819731,30
-122.366488,37.819402,30
-122.366212,37.818977,30
</coordinates>
</LinearRing>
</innerBoundaryIs>
</Polygon>
</Placemark>
</Document>
</kml>