KML Support in the Web World Wind

The visualized KML Example
One of the formats we support to some extent in Web World Wind is KML at version 2.2. The support isn’t full. The full extent to which is the KML supported is explained in the relevant README.MD.
How do you parse the KML document and display it using the default styling and the styling provided in the KML document. The code is in the Example, but for clarity:

var layer = new WorldWind.RenderableLayer("KML");
var kmlFilePromise = new WorldWind.KmlFile('data/KML_Samples.kml');
kmlFilePromise.then(function (kmlFile) {
    layer.addRenderable(kmlFile);

    wwd.addLayer(renderableLayer);
});

This is a very simple example showing the default code necessary to add KML file to the Web World Wind. The main precondition is that Web World Wind is already initialized and stored in the wwd variable. The KML File itself is Renderable and as such can be added to any RenderableLayer. In this example, we create new and add it to the Web World Wind instance.
The potentially confusing part is the creation of the KmlFile. The KmlFile actually returns Promise. When the promise is resolved it means that the File is parsed and ready to be rendered. The renderable to add to the layer is provided as a parameter to this function.

Customization

There are three main reasons to customize the current implementation. The first is that you want to support more of the functionality than currently supported, that you want to change way something is currently supported or you want to change the default styling.
The core component is the class KmlElements. This class is a singleton which is used for the retrieving of relevant class for parsing and rendering the elements of the KmlFile. All the Kml Elements has its representation in the https://github.com/NASAWorldWind/WebWorldWind/tree/develop/src/formats/kml. The following code snippet changes the class that will be used to handle the Polygon element of the KML.

KmlElements.addKey('Polygon', KmlSurfaceOnlyPolygon);

In order for the KmlSurfaceOnlyPolygonto be working correctly, it is necessary to provide render method. In this method, you have DrawContext available. This way it is possible to generate any Renderable and render it using the context and pass the properties in the parsing tree. It is for example possible to update the Polygon to be always represented as SurfacePolygon.

KmlSurfaceOnlyPolygon.prototype.render = function(dc, kmlOptions) {
    KmlGeometry.prototype.render.call(this, dc, kmlOptions);

    if(!this._renderable) {
        this._renderable = new SurfacePolygon(this.kmlOuterBoundary.kmlPositions, new WorldWind.ShapeAttributes();
        dc.redrawRequested = true;
    }

    this._renderable.render(dc);
};

If you want to change the default styles, it is necessary to provide the updated KmlStyle with changed generate function. This way it should be possible for you to update any behavior that KML implementation currently exhibits. If you got lost when trying, leave it in comments and I will try to help.

Komentáře