1/* -*-c++-*- */
2/* osgEarth - Geospatial SDK for OpenSceneGraph
3 * Copyright 2019 Pelican Mapping
4 * http://osgearth.org
5 *
6 * osgEarth is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program.  If not, see <http://www.gnu.org/licenses/>
18 */
19#ifndef OSGEARTH_DRIVER_KML
20#define OSGEARTH_DRIVER_KML 1
21
22#include <osgEarth/MapNode>
23#include <osgEarth/URI>
24#include <osgEarth/Registry>
25#include <osgDB/ReaderWriter>
26#include <osgDB/FileNameUtils>
27#include <osgDB/Registry>
28#include "KMLOptions"
29
30namespace osgEarth { namespace Drivers
31{
32    using namespace osgEarth;
33
34    class KML // header-only (no export)
35    {
36    public:
37        /**
38         * Loads KML or KMZ from a URI.
39         *
40         * @param[in ] uri     URI from which to load the KML or KMZ
41         * @param[in ] mapNode MapNode to which to attach KML elements
42         * @param[in ] options Optional KML options
43         */
44        static osg::Node* load(const URI&            uri,
45                               MapNode*              mapNode,
46                               const osgDB::Options* dbOptions,
47                               const KMLOptions&     kmlOptions)
48        {
49            // ensure the library is loaded.
50            osgDB::Registry::instance()->addArchiveExtension( "kmz" );
51            if ( osgDB::getLowerCaseFileExtension(uri.full()) == "kmz" )
52            {
53                OE_INFO << "[KML] Preloading KML plugin\n";
54                std::string libName = osgDB::Registry::instance()->createLibraryNameForExtension("kml");
55                osgDB::Registry::LoadStatus status = osgDB::Registry::instance()->loadLibrary(libName);
56                if ( status == osgDB::Registry::NOT_LOADED )
57                {
58                    OE_WARN << "[KML] FAILED!\n";
59                }
60                URI newURI( uri.full() + "/doc.kml", uri.context() );
61                return load( newURI, mapNode, dbOptions, kmlOptions );
62            }
63
64            if ( !mapNode ) {
65                OE_WARN << "[KML] " << "MapNode instance required" << std::endl;
66                return 0L;
67            }
68            osg::ref_ptr<osgDB::Options> options = Registry::instance()->cloneOrCreateOptions( dbOptions );
69            options->setPluginData( "osgEarth::MapNode",    mapNode );
70            options->setPluginData( "osgEarth::KMLOptions", (void*)&kmlOptions );
71
72            return uri.getNode( options.get() );
73        }
74
75        static osg::Node* load(const URI&            uri,
76                               MapNode*              mapNode,
77                               const KMLOptions&     kmlOptions =KMLOptions() )
78        {
79            return load(uri, mapNode, 0L, kmlOptions);
80        }
81    };
82
83} } // namespace osgEarth::Drivers
84
85#endif // OSGEARTH_DRIVER_KML
86