/
osgearth_seed.cpp
311 lines (257 loc) · 9.05 KB
/
osgearth_seed.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
* Copyright 2008-2012 Pelican Mapping
* http://osgearth.org
*
* osgEarth is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#include <osgDB/FileNameUtils>
#include <osgDB/FileUtils>
#include <osg/io_utils>
#include <osgEarth/Common>
#include <osgEarth/Map>
#include <osgEarth/Cache>
#include <osgEarth/CacheSeed>
#include <osgEarth/MapNode>
#include <osgEarth/Registry>
#include <iostream>
#include <sstream>
#include <iterator>
using namespace osgEarth;
#define LC "[osgearth_cache] "
int list( osg::ArgumentParser& args );
int seed( osg::ArgumentParser& args );
int purge( osg::ArgumentParser& args );
int usage( const std::string& msg );
int message( const std::string& msg );
int
main(int argc, char** argv)
{
osg::ArgumentParser args(&argc,argv);
if ( args.read( "--seed") )
return seed( args );
else if ( args.read( "--list" ) )
return list( args );
else if ( args.read( "--purge" ) )
return purge( args );
else
return usage("");
}
int
usage( const std::string& msg )
{
if ( !msg.empty() )
{
std::cout << msg << std::endl;
}
std::cout
<< std::endl
<< "USAGE: osgearth_cache" << std::endl
<< std::endl
<< " --list file.earth ; Lists info about the cache in a .earth file" << std::endl
<< std::endl
<< " --seed file.earth ; Seeds the cache in a .earth file" << std::endl
<< " [--min-level level] ; Lowest LOD level to seed (default=0)" << std::endl
<< " [--max-level level] ; Highest LOD level to seed (defaut=highest available)" << std::endl
<< " [--bounds xmin ymin xmax ymax] ; Geospatial bounding box to seed" << std::endl
<< " [--cache-path path] ; Overrides the cache path in the .earth file" << std::endl
<< " [--cache-type type] ; Overrides the cache type in the .earth file" << std::endl
<< std::endl
<< " --purge file.earth ; Purges a layer cache in a .earth file (interactive)" << std::endl
<< std::endl;
return -1;
}
int message( const std::string& msg )
{
if ( !msg.empty() )
{
std::cout << msg << std::endl << std::endl;
}
return 0;
}
int
seed( osg::ArgumentParser& args )
{
//Read the min level
unsigned int minLevel = 0;
while (args.read("--min-level", minLevel));
//Read the max level
unsigned int maxLevel = 5;
while (args.read("--max-level", maxLevel));
//Read the bounds
Bounds bounds(0, 0, 0, 0);
while (args.read("--bounds", bounds.xMin(), bounds.yMin(), bounds.xMax(), bounds.yMax()));
while (args.read("-b", bounds.xMin(), bounds.yMin(), bounds.xMax(), bounds.yMax()));
//Read the cache override directory
std::string cachePath;
while (args.read("--cache-path", cachePath));
//Read the cache type
std::string cacheType;
while (args.read("--cache-type", cacheType));
bool verbose = args.read("--verbose");
//Read in the earth file.
osg::ref_ptr<osg::Node> node = osgDB::readNodeFiles( args );
if ( !node.valid() )
return usage( "Failed to read .earth file." );
MapNode* mapNode = MapNode::findMapNode( node.get() );
if ( !mapNode )
return usage( "Input file was not a .earth file" );
CacheSeed seeder;
seeder.setMinLevel( minLevel );
seeder.setMaxLevel( maxLevel );
seeder.setBounds( bounds );
if (verbose)
{
seeder.setProgressCallback(new ConsoleProgressCallback);
}
seeder.seed( mapNode->getMap() );
return 0;
}
int
list( osg::ArgumentParser& args )
{
osg::ref_ptr<osg::Node> node = osgDB::readNodeFiles( args );
if ( !node.valid() )
return usage( "Failed to read .earth file." );
MapNode* mapNode = MapNode::findMapNode( node.get() );
if ( !mapNode )
return usage( "Input file was not a .earth file" );
Map* map = mapNode->getMap();
const Cache* cache = map->getCache();
if ( !cache )
return message( "Earth file does not contain a cache." );
std::cout
<< "Cache config: " << std::endl
<< cache->getCacheOptions().getConfig().toJSON(true) << std::endl;
MapFrame mapf( mapNode->getMap() );
TerrainLayerVector layers;
std::copy( mapf.imageLayers().begin(), mapf.imageLayers().end(), std::back_inserter(layers) );
std::copy( mapf.elevationLayers().begin(), mapf.elevationLayers().end(), std::back_inserter(layers) );
for( TerrainLayerVector::iterator i =layers.begin(); i != layers.end(); ++i )
{
TerrainLayer* layer = i->get();
TerrainLayer::CacheBinMetadata meta;
if ( layer->getCacheBinMetadata( map->getProfile(), meta ) )
{
Config conf = meta.getConfig();
std::cout << "Layer \"" << layer->getName() << "\", cache metadata =" << std::endl
<< conf.toJSON(true) << std::endl;
}
else
{
std::cout << "Layer \"" << layer->getName() << "\": no cache information"
<< std::endl;
}
}
return 0;
}
struct Entry
{
bool _isImage;
std::string _name;
osg::ref_ptr<CacheBin> _bin;
};
int
purge( osg::ArgumentParser& args )
{
//return usage( "Sorry, but purge is not yet implemented." );
osg::ref_ptr<osg::Node> node = osgDB::readNodeFiles( args );
if ( !node.valid() )
return usage( "Failed to read .earth file." );
MapNode* mapNode = MapNode::findMapNode( node.get() );
if ( !mapNode )
return usage( "Input file was not a .earth file" );
Map* map = mapNode->getMap();
if ( !map->getCache() )
return message( "Earth file does not contain a cache." );
std::vector<Entry> entries;
ImageLayerVector imageLayers;
map->getImageLayers( imageLayers );
for( ImageLayerVector::const_iterator i = imageLayers.begin(); i != imageLayers.end(); ++i )
{
CacheBin* bin = i->get()->getCacheBin( map->getProfile() );
if ( bin )
{
entries.push_back(Entry());
entries.back()._isImage = true;
entries.back()._name = i->get()->getName();
entries.back()._bin = bin;
}
}
ElevationLayerVector elevationLayers;
map->getElevationLayers( elevationLayers );
for( ElevationLayerVector::const_iterator i = elevationLayers.begin(); i != elevationLayers.end(); ++i )
{
CacheBin* bin = i->get()->getCacheBin( map->getProfile() );
if ( bin )
{
entries.push_back(Entry());
entries.back()._isImage = false;
entries.back()._name = i->get()->getName();
entries.back()._bin = bin;
}
}
if ( entries.size() > 0 )
{
std::cout << std::endl;
for( unsigned i=0; i<entries.size(); ++i )
{
std::cout << (i+1) << ") " << entries[i]._name << " (" << (entries[i]._isImage? "image" : "elevation" ) << ")" << std::endl;
}
std::cout
<< std::endl
<< "Enter number of cache to purge, or <enter> to quit: "
<< std::flush;
std::string input;
std::getline( std::cin, input );
if ( !input.empty() )
{
unsigned k = as<unsigned>(input, 0L);
if ( k > 0 && k <= entries.size() )
{
Config meta = entries[k-1]._bin->readMetadata();
if ( !meta.empty() )
{
std::cout
<< std::endl
<< "Cache METADATA:" << std::endl
<< meta.toJSON()
<< std::endl << std::endl;
}
std::cout
<< "Are you sure (y/N)? "
<< std::flush;
std::getline( std::cin, input );
if ( input == "y" || input == "Y" )
{
std::cout << "Purging.." << std::flush;
entries[k-1]._bin->purge();
}
else
{
std::cout << "No action taken." << std::endl;
}
}
else
{
std::cout << "Invalid choice." << std::endl;
}
}
else
{
std::cout << "No action taken." << std::endl;
}
}
return 0;
}