-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathPackagerExtensions.cpp
448 lines (366 loc) · 12.9 KB
/
PackagerExtensions.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include "PackagerExtensions"
#include <osg/Notify>
#include <osgDB/FileUtils>
#include <osgDB/FileNameUtils>
#include <osgEarthDrivers/gdal/GDALOptions>
#include <osgEarth/CompositeTileSource>
#include <osgEarth/CacheEstimator>
#include <osgEarthUtil/TMSPackager>
using namespace osgEarth::Cef;
using namespace osgEarth;
using namespace osgEarth::Util;
using namespace osgEarth::Drivers;
class RunCallbackTask: public CefTask
{
public:
CefRefPtr< CefV8Value > _callback;
CefRefPtr< CefV8Context > _context;
double _current;
double _total;
int _currentStage;
int _totalStages;
std::string _msg;
RunCallbackTask(CefRefPtr< CefV8Context > context,
CefRefPtr< CefV8Value > callback,
double current,
double total,
unsigned currentStage,
unsigned totalStages,
const std::string& msg
):
_context(context),
_callback(callback),
_current( current ),
_total( total ),
_currentStage( currentStage ),
_totalStages( totalStages ),
_msg( msg )
{
}
virtual void Execute() OVERRIDE
{
CefV8ValueList args;
args.push_back(CefV8Value::CreateDouble(_current));
args.push_back(CefV8Value::CreateDouble(_total));
args.push_back(CefV8Value::CreateInt(_currentStage));
args.push_back(CefV8Value::CreateInt(_totalStages));
args.push_back(CefV8Value::CreateString(_msg));
_callback->ExecuteFunctionWithContext(_context, 0, args);
}
// Provide the reference counting implementation for this class.
IMPLEMENT_REFCOUNTING(RunCallbackTask);
};
class V8ProgressCallback : public ProgressCallback
{
public:
V8ProgressCallback(CefRefPtr< CefV8Context > context,
CefRefPtr< CefV8Value > callback,
CefRefPtr< CefTaskRunner > taskRunner):
_context( context ),
_callback( callback ),
_taskRunner( taskRunner )
{
}
virtual bool reportProgress(
double current,
double total,
unsigned currentStage,
unsigned totalStages,
const std::string& msg )
{
_taskRunner->PostTask( new RunCallbackTask( _context, _callback, current, total, currentStage, totalStages, msg ) );
return false;
}
CefRefPtr< CefV8Context > _context;
CefRefPtr< CefV8Value > _callback;
CefRefPtr< CefTaskRunner > _taskRunner;
};
/**
* User data for a package task
*/
class PackagerUserData : public CefBase, public OpenThreads::Thread
{
public:
PackagerUserData(CefRefPtr< CefV8Value > opt )
{
// Get the list of filenames to process
std::vector< std::string > filenames;
if (opt->HasValue("filenames")) {
CefRefPtr< CefV8Value > v8filenames = opt->GetValue("filenames");
for (int i = 0; i < v8filenames->GetArrayLength(); i++)
{
std::string filename = v8filenames->GetValue(i)->GetStringValue().ToString();
filenames.push_back( filename );
}
}
osg::ref_ptr< TileVisitor > visitor;
int cores = 0;
if (opt->HasValue("cores"))
{
cores = opt->GetValue("cores")->GetIntValue();
}
cores = max(1, cores);
if (cores > 1)
{
MultithreadedTileVisitor* v = new MultithreadedTileVisitor();
v->setNumThreads(cores);
visitor = v;
}
else
{
visitor = new TileVisitor();
}
packager.setVisitor( visitor );
std::string extension;
// Get the extension
if (opt->HasValue("extension"))
{
extension = opt->GetValue("extension")->GetStringValue().ToString();
}
// Setup the write options so that it will compress elevation using lzw
osg::ref_ptr< osgDB::Options > options = new osgDB::Options("tiff_compression=lzw");
packager.setWriteOptions( options.get() );
bool elevation = false;
if (extension == "tif16")
{
elevation = true;
packager.setExtension("tif");
packager.setElevationPixelDepth(16);
}
else if (extension == "tif32")
{
elevation = true;
packager.setExtension("tif");
packager.setElevationPixelDepth(32);
}
else
{
packager.setExtension( extension );
}
// Get the min level
if (opt->HasValue("min_level"))
{
visitor->setMinLevel( opt->GetValue("min_level")->GetIntValue() );
}
// Get the max level
if (opt->HasValue("max_level"))
{
visitor->setMaxLevel( opt->GetValue("max_level")->GetIntValue() );
}
// Get the extents
double minLon = -180.0;
double minLat = -90.0;
double maxLon = 180.0;
double maxLat = 90.0;
if (opt->HasValue("extents"))
{
CefRefPtr< CefV8Value > extents = opt->GetValue("extents");
minLon = extents->GetValue(0)->GetIntValue();
minLat = extents->GetValue(1)->GetIntValue();
maxLon = extents->GetValue(2)->GetIntValue();
maxLat = extents->GetValue(3)->GetIntValue();
}
visitor->addExtent(GeoExtent(SpatialReference::create("epsg:4326"), minLon, minLat, maxLon, maxLat));
std::string destination = "tiles";
if ( opt->HasValue("destination"))
{
destination = opt->GetValue("destination")->GetStringValue().ToString();
}
packager.setDestination( destination );
packager.setApplyAlphaMask(true);
// create a folder for the output
osgDB::makeDirectory( destination );
std::string output;
if (opt->HasValue("output"))
{
output = opt->GetValue("output")->GetStringValue().ToString();
if (output == "mbtiles")
{
Config outConf;
outConf.set("driver", "mbtiles");
outConf.set("format", packager.getExtension());
outConf.set("filename", osgDB::concatPaths(destination, "package.db"));
// set the output profile.
osg::ref_ptr<const osgEarth::Profile> outputProfile = osgEarth::Profile::create(
"epsg:4326",
-180, -90, 180, 90,
"",
2, 1 );
ProfileOptions profileOptions = outputProfile->toProfileOptions();
outConf.add("profile", profileOptions.getConfig());
TileSourceOptions outOptions(outConf);
osg::ref_ptr<TileSource> output = TileSourceFactory::create(outOptions);
if ( output.valid() )
{
TileSource::Status outputStatus = output->open(
TileSource::MODE_WRITE | TileSource::MODE_CREATE );
if ( !outputStatus.isError() )
{
packager.setTileSource(output);
}
else
{
OE_WARN << "Failed to initialize output TileSource: " << outputStatus.message() << std::endl;
}
}
else
{
OE_WARN << "Failed to create output TileSource" << std::endl;
}
}
}
CompositeTileSourceOptions compositeOpt;
for (unsigned int i = 0; i < filenames.size(); i++)
{
GDALOptions layerOpt;
layerOpt.url() = filenames[i];
if (elevation)
{
compositeOpt.add(ElevationLayerOptions(filenames[i], layerOpt));
}
else
{
compositeOpt.add(ImageLayerOptions(filenames[i], layerOpt));
}
}
if (elevation)
{
_layer = new osgEarth::ElevationLayer( ElevationLayerOptions("layer", compositeOpt) );
}
else
{
_layer = new osgEarth::ImageLayer( ImageLayerOptions("layer", compositeOpt) );
}
_map = new Map();
_context = CefV8Context::GetCurrentContext();
_taskRunner = CefTaskRunner::GetForCurrentThread();
if (opt->HasValue("progress")) {
_progress = new V8ProgressCallback(_context,
opt->GetValue("progress"),
_taskRunner);
visitor->setProgressCallback( _progress.get() );
}
if (opt->HasValue("complete")) {
_completeCallback = opt->GetValue("complete");
}
}
bool getDone() { return _done;}
void setDone( bool done) { _done = done; }
virtual void run()
{
packager.run( _layer, _map );
if (_completeCallback)
{
_taskRunner->PostTask( new RunCallbackTask( _context, _completeCallback, 0, 0, 0, 0, ""));
}
_progress = 0L;
}
void cancelPackager()
{
if (_progress.valid())
_progress->cancel();
}
volatile bool _done;
TMSPackager packager;
osg::ref_ptr< V8ProgressCallback > _progress;
osg::ref_ptr< TerrainLayer > _layer;
osg::ref_ptr< Map > _map;
CefRefPtr< CefV8Context > _context;
CefRefPtr< CefV8Value > _completeCallback;
CefRefPtr< CefTaskRunner > _taskRunner;
IMPLEMENT_REFCOUNTING(PackagerUserData);
};
PackagerV8Handler::PackagerV8Handler()
{
}
bool PackagerV8Handler::Execute(const CefString& name,
CefRefPtr<CefV8Value> object,
const CefV8ValueList& arguments,
CefRefPtr<CefV8Value>& retval,
CefString& exception)
{
if (name == "Package")
{
CefRefPtr< CefV8Value > opt = arguments[0];
CefRefPtr< PackagerUserData > userData = new PackagerUserData( opt );
userData->startThread();
retval = CefV8Value::CreateObject(0);
retval->SetUserData(userData);
//CefRefPtr<ProgressHandler> progressHandler = new ProgressHandler();
//retval->SetValue("cancel", CefV8Value::CreateFunction("cancel", progressHandler), V8_PROPERTY_ATTRIBUTE_NONE);
return true;
}
else if (name == "CancelPackage")
{
CefRefPtr< CefV8Value > opt = arguments[0];
CefRefPtr< PackagerUserData > userData = dynamic_cast< PackagerUserData* >(opt->GetUserData().get());
if (userData.get())
{
if (!userData->getDone())
{
userData->cancelPackager();
}
return true;
}
return true;
}
else if (name == "Estimate")
{
CefRefPtr< CefV8Value > opt = arguments[0];
CacheEstimator est;
if (opt->HasValue("min_level"))
{
est.setMinLevel( opt->GetValue("min_level")->GetIntValue() );
}
if (opt->HasValue("max_level"))
{
est.setMaxLevel( opt->GetValue("max_level")->GetIntValue() );
}
double minLon = -180.0;
double minLat = -90.0;
double maxLon = 180.0;
double maxLat = 90.0;
if (opt->HasValue("extents"))
{
CefRefPtr< CefV8Value > extents = opt->GetValue("extents");
minLon = extents->GetValue(0)->GetIntValue();
minLat = extents->GetValue(1)->GetIntValue();
maxLon = extents->GetValue(2)->GetIntValue();
maxLat = extents->GetValue(3)->GetIntValue();
}
est.addExtent(GeoExtent(osgEarth::SpatialReference::create("epsg:4326"), minLon, minLat, maxLon, maxLat));
retval = CefV8Value::CreateObject(0);
retval->SetValue("tiles", CefV8Value::CreateUInt(est.getNumTiles()), V8_PROPERTY_ATTRIBUTE_NONE);
retval->SetValue("size", CefV8Value::CreateDouble(est.getSizeInMB()), V8_PROPERTY_ATTRIBUTE_NONE);
retval->SetValue("seconds", CefV8Value::CreateDouble(est.getTotalTimeInSeconds()), V8_PROPERTY_ATTRIBUTE_NONE);
return true;
}
return false;
}
namespace
{
std::string code =
"var osgearth;"
"if (!osgearth) {"
" osgearth = {};"
"}"
"(function() {"
" osgearth.package = function(options) {"
" native function Package();"
" return Package(options);"
" };"
" osgearth.estimate = function(options) {"
" native function Estimate();"
" return Estimate(options);"
" };"
" osgearth.cancelPackage = function(options) {"
" native function CancelPackage();"
" return CancelPackage(options);"
" };"
"})();";
}
void PackagerAPI::AddExtensions(CefRefPtr<CefV8Value> global)
{
CefRefPtr< PackagerV8Handler > handler = new PackagerV8Handler();
CefRegisterExtension("packager", code, handler);
}