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
20#ifndef OSGEARTH_FEATURES_IMAGE_TO_FEATURE_LAYER_H
21#define OSGEARTH_FEATURES_IMAGE_TO_FEATURE_LAYER_H 1
22
23#include <osgEarthFeatures/FeatureSourceLayer>
24#include <osgEarth/LayerListener>
25
26namespace osgEarth {
27    class ImageLayer;
28}
29
30namespace osgEarth { namespace Features
31{
32    /** Options structure for serialization of the FeatureSourceLayer */
33    class ImageToFeatureLayerOptions : public FeatureSourceLayerOptions
34    {
35    public:
36        ImageToFeatureLayerOptions(const ConfigOptions& co = ConfigOptions()) : FeatureSourceLayerOptions(co),
37            _level(0u),
38            _attribute("value")
39        {
40            fromConfig(_conf);
41        }
42
43        //! Name of the image layer to convert to feature data
44        optional<std::string>& imageLayer() { return _imageLayerName; }
45        const optional<std::string>& imageLayer() const { return _imageLayerName; }
46
47        //! Level of detail from which to extract features
48        optional<unsigned>& level() { return _level; }
49        const optional<unsigned>& level() const { return _level; }
50
51        //! Attribute name to set with feature value (default is "value")
52        optional<std::string>& attribute() { return _attribute; }
53        const optional<std::string>& attribute() const { return _attribute; }
54
55    public:
56        Config getConfig() const {
57            Config conf = FeatureSourceLayerOptions::getConfig();
58            conf.set("image", _imageLayerName);
59            conf.set("level", _level);
60            conf.set("attribute", _attribute);
61            return conf;
62        }
63
64        void fromConfig(const Config& conf) {
65            conf.get("image", _imageLayerName);
66            conf.get("level", _level);
67            conf.get("attribute", _attribute);
68        }
69
70        virtual void mergeConfig( const Config& conf ) {
71            FeatureSourceLayerOptions::mergeConfig( conf );
72            fromConfig( conf );
73        }
74
75    private:
76        optional<std::string> _imageLayerName;
77        optional<unsigned> _level;
78        optional<std::string> _attribute;
79    };
80
81
82    /**
83     * A FeatureSourceLayer that extracts features from raster data in
84     * an image layer.
85     */
86    class OSGEARTHFEATURES_EXPORT ImageToFeatureLayer : public FeatureSourceLayer
87    {
88    public:
89        META_Layer(osgEarth, ImageToFeatureLayer, ImageToFeatureLayerOptions, image_to_feature);
90
91        //! Create an empty layer
92        ImageToFeatureLayer();
93
94        //! Create a new layer with initialization options
95        ImageToFeatureLayer(const ImageToFeatureLayerOptions& options);
96
97        //! Image layer to use to convert raster data to features
98        void setImageLayer(ImageLayer* layer);
99
100    public: // Layer
101
102        virtual void init();
103
104        virtual void addedToMap(const Map*);
105
106        virtual void removedFromMap(const Map*);
107
108    protected:
109
110        LayerListener<ImageToFeatureLayer, ImageLayer> _imageLayerListener;
111    };
112
113} } // namespace osgEarth::Features
114
115#endif // OSGEARTH_FEATURES_IMAGE_TO_FEATURE_LAYER_H
116