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

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);
};