|
| 1 | +/* -*-c++-*- */ |
| 2 | +/* osgEarth - Geospatial SDK for OpenSceneGraph |
| 3 | + * Copyright 2020 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 | +#include <osgEarth/CompositeTiledModelLayer> |
| 20 | +#include <osgEarth/NodeUtils> |
| 21 | +#include <osgEarth/SimplePager> |
| 22 | + |
| 23 | +#include <vector> |
| 24 | + |
| 25 | +using namespace osgEarth; |
| 26 | + |
| 27 | +#define LC "[CompositeTiledModelLayer] " |
| 28 | + |
| 29 | +#define OE_TEST OE_NULL |
| 30 | + |
| 31 | +REGISTER_OSGEARTH_LAYER(CompositeTiledModel, CompositeTiledModelLayer); |
| 32 | + |
| 33 | +void CompositeTiledModelLayer::Options::fromConfig(const Config& conf) |
| 34 | +{ |
| 35 | + conf.get("profile", profile()); |
| 36 | + |
| 37 | + const ConfigSet& layers = conf.child("layers").children(); |
| 38 | + for (ConfigSet::const_iterator i = layers.begin(); i != layers.end(); ++i) |
| 39 | + { |
| 40 | + _layers.push_back(ConfigOptions(*i)); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +Config |
| 45 | +CompositeTiledModelLayer::Options::getConfig() const |
| 46 | +{ |
| 47 | + Config conf = TiledModelLayer::Options::getConfig(); |
| 48 | + conf.set("profile", profile()); |
| 49 | + |
| 50 | + if (_layers.empty() == false) |
| 51 | + { |
| 52 | + Config layersConf("layers"); |
| 53 | + for (std::vector<ConfigOptions>::const_iterator i = _layers.begin(); i != _layers.end(); ++i) |
| 54 | + { |
| 55 | + layersConf.add(i->getConfig()); |
| 56 | + } |
| 57 | + conf.set(layersConf); |
| 58 | + } |
| 59 | + |
| 60 | + return conf; |
| 61 | +} |
| 62 | + |
| 63 | +//........................................................................... |
| 64 | + |
| 65 | +CompositeTiledModelLayer::~CompositeTiledModelLayer() |
| 66 | +{ |
| 67 | + //NOP |
| 68 | +} |
| 69 | + |
| 70 | +void CompositeTiledModelLayer::setProfile(const Profile* profile) |
| 71 | +{ |
| 72 | + _profile = profile; |
| 73 | + if (_profile) |
| 74 | + { |
| 75 | + options().profile() = profile->toProfileOptions(); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +Config |
| 80 | +CompositeTiledModelLayer::getConfig() const |
| 81 | +{ |
| 82 | + Config conf = TiledModelLayer::getConfig(); |
| 83 | + return conf; |
| 84 | +} |
| 85 | + |
| 86 | +Status |
| 87 | +CompositeTiledModelLayer::openImplementation() |
| 88 | +{ |
| 89 | + Status parent = super::openImplementation(); |
| 90 | + if (parent.isError()) |
| 91 | + return parent; |
| 92 | + |
| 93 | + _profile = Profile::create(*options().profile()); |
| 94 | + |
| 95 | + // Open all the layers |
| 96 | + for (auto& layerConf : options().layers()) |
| 97 | + { |
| 98 | + osg::ref_ptr< Layer > layer = Layer::create(layerConf); |
| 99 | + TiledModelLayer* tiledLayer = dynamic_cast<TiledModelLayer*>(layer.get()); |
| 100 | + if (tiledLayer) |
| 101 | + { |
| 102 | + tiledLayer->open(); |
| 103 | + tiledLayer->setReadOptions(getReadOptions()); |
| 104 | + _layers.push_back(tiledLayer); |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + OE_WARN << LC << "Layer is not a TiledModelLayer" << std::endl; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return Status::NoError; |
| 113 | +} |
| 114 | + |
| 115 | +void |
| 116 | +CompositeTiledModelLayer::addedToMap(const Map* map) |
| 117 | +{ |
| 118 | + for (auto& layer : _layers) |
| 119 | + { |
| 120 | + layer->addedToMap(map); |
| 121 | + } |
| 122 | + |
| 123 | + // Check to make sure the layers are all using the same profile |
| 124 | + for (auto& layer : _layers) |
| 125 | + { |
| 126 | + if (!layer->getProfile()->isEquivalentTo(_profile)) |
| 127 | + { |
| 128 | + OE_WARN << LC << "Layer " << layer->getName() << " does not have the same profile as the composite layer" << std::endl; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + // Compute the min and max levels |
| 133 | + if (!_layers.empty()) |
| 134 | + { |
| 135 | + _minLevel = 1000; |
| 136 | + _maxLevel = 0; |
| 137 | + for (auto& layer : _layers) |
| 138 | + { |
| 139 | + if (layer->getMinLevel() < _minLevel) |
| 140 | + { |
| 141 | + _minLevel = layer->getMinLevel(); |
| 142 | + } |
| 143 | + if (layer->getMaxLevel() > _maxLevel) |
| 144 | + { |
| 145 | + _maxLevel = layer->getMaxLevel(); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + super::addedToMap(map); |
| 151 | +} |
| 152 | + |
| 153 | +void |
| 154 | +CompositeTiledModelLayer::removedFromMap(const Map* map) |
| 155 | +{ |
| 156 | + super::removedFromMap(map); |
| 157 | + |
| 158 | + for (auto& layer : _layers) |
| 159 | + { |
| 160 | + layer->removedFromMap(map); |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +osg::ref_ptr<osg::Node> |
| 165 | +CompositeTiledModelLayer::createTileImplementation(const TileKey& key, ProgressCallback* progress) const |
| 166 | +{ |
| 167 | + osg::ref_ptr<osg::Group> group = new osg::Group(); |
| 168 | + |
| 169 | + for (unsigned int i = 0; i < this->_layers.size(); i++) |
| 170 | + { |
| 171 | + osg::ref_ptr<osg::Node> node = this->_layers[i]->createTile(key, progress); |
| 172 | + if (node.valid()) |
| 173 | + { |
| 174 | + group->addChild(node); |
| 175 | + } |
| 176 | + } |
| 177 | + return group; |
| 178 | +} |
| 179 | + |
| 180 | +unsigned |
| 181 | +CompositeTiledModelLayer::getMinLevel() const |
| 182 | +{ |
| 183 | + return _minLevel; |
| 184 | +} |
| 185 | + |
| 186 | +unsigned |
| 187 | +CompositeTiledModelLayer::getMaxLevel() const |
| 188 | +{ |
| 189 | + return _maxLevel; |
| 190 | +} |
| 191 | + |
0 commit comments