22
22
#include " write_json.hpp"
23
23
#include " jsonpull/jsonpull.h"
24
24
#include " dirtiles.hpp"
25
+ #include " errors.hpp"
25
26
26
27
int minzoom = 0 ;
27
28
int maxzoom = 32 ;
@@ -85,18 +86,18 @@ void do_stats(mvt_tile &tile, size_t size, bool compressed, int z, unsigned x, u
85
86
state.json_write_newline ();
86
87
}
87
88
88
- void handle (std::string message, int z, unsigned x, unsigned y, std::set<std::string> const &to_decode, bool pipeline, bool stats, json_writer &state) {
89
+ void handle (std::string message, int z, unsigned x, unsigned y, std::set<std::string> const &to_decode, bool pipeline, bool stats, json_writer &state, int coordinate_mode ) {
89
90
mvt_tile tile;
90
91
bool was_compressed;
91
92
92
93
try {
93
94
if (!tile.decode (message, was_compressed)) {
94
95
fprintf (stderr, " Couldn't parse tile %d/%u/%u\n " , z, x, y);
95
- exit (EXIT_FAILURE );
96
+ exit (EXIT_MVT );
96
97
}
97
98
} catch (std::exception const &e) {
98
99
fprintf (stderr, " PBF decoding error in tile %d/%u/%u\n " , z, x, y);
99
- exit (EXIT_FAILURE );
100
+ exit (EXIT_PROTOBUF );
100
101
}
101
102
102
103
if (stats) {
@@ -159,7 +160,7 @@ void handle(std::string message, int z, unsigned x, unsigned y, std::set<std::st
159
160
160
161
if (layer.extent <= 0 ) {
161
162
fprintf (stderr, " Impossible layer extent %lld in mbtiles\n " , layer.extent );
162
- exit (EXIT_FAILURE );
163
+ exit (EXIT_IMPOSSIBLE );
163
164
}
164
165
165
166
if (to_decode.size () != 0 && !to_decode.count (layer.name )) {
@@ -202,10 +203,16 @@ void handle(std::string message, int z, unsigned x, unsigned y, std::set<std::st
202
203
// X and Y are unsigned, so no need to check <0
203
204
if (x > (1ULL << z) || y > (1ULL << z)) {
204
205
fprintf (stderr, " Impossible tile %d/%u/%u\n " , z, x, y);
205
- exit (EXIT_FAILURE );
206
+ exit (EXIT_IMPOSSIBLE );
206
207
}
207
208
208
- layer_to_geojson (layer, z, x, y, !pipeline, pipeline, pipeline, false , 0 , 0 , 0 , !force, state);
209
+ double scale = 0 ;
210
+ if (coordinate_mode == 1 ) { // fraction
211
+ scale = layer.extent ;
212
+ } else if (coordinate_mode == 2 ) { // integer
213
+ scale = 1 ;
214
+ }
215
+ layer_to_geojson (layer, z, x, y, !pipeline, pipeline, pipeline, false , 0 , 0 , 0 , !force, state, scale);
209
216
210
217
if (!pipeline) {
211
218
if (true ) {
@@ -223,7 +230,7 @@ void handle(std::string message, int z, unsigned x, unsigned y, std::set<std::st
223
230
}
224
231
}
225
232
226
- void decode (char *fname, int z, unsigned x, unsigned y, std::set<std::string> const &to_decode, bool pipeline, bool stats, std::set<std::string> const &exclude_meta) {
233
+ void decode (char *fname, int z, unsigned x, unsigned y, std::set<std::string> const &to_decode, bool pipeline, bool stats, std::set<std::string> const &exclude_meta, int coordinate_mode ) {
227
234
sqlite3 *db = NULL ;
228
235
bool isdir = false ;
229
236
int oz = z;
@@ -240,12 +247,12 @@ void decode(char *fname, int z, unsigned x, unsigned y, std::set<std::string> co
240
247
if (strcmp (map, " SQLite format 3" ) != 0 ) {
241
248
if (z >= 0 ) {
242
249
std::string s = std::string (map, st.st_size );
243
- handle (s, z, x, y, to_decode, pipeline, stats, state);
250
+ handle (s, z, x, y, to_decode, pipeline, stats, state, coordinate_mode );
244
251
munmap (map, st.st_size );
245
252
return ;
246
253
} else {
247
254
fprintf (stderr, " Must specify zoom/x/y to decode a single pbf file\n " );
248
- exit (EXIT_FAILURE );
255
+ exit (EXIT_ARGS );
249
256
}
250
257
}
251
258
}
@@ -256,7 +263,7 @@ void decode(char *fname, int z, unsigned x, unsigned y, std::set<std::string> co
256
263
}
257
264
if (close (fd) != 0 ) {
258
265
perror (" close" );
259
- exit (EXIT_FAILURE );
266
+ exit (EXIT_CLOSE );
260
267
}
261
268
} else {
262
269
perror (fname);
@@ -272,13 +279,13 @@ void decode(char *fname, int z, unsigned x, unsigned y, std::set<std::string> co
272
279
} else {
273
280
if (sqlite3_open (fname, &db) != SQLITE_OK) {
274
281
fprintf (stderr, " %s: %s\n " , fname, sqlite3_errmsg (db));
275
- exit (EXIT_FAILURE );
282
+ exit (EXIT_OPEN );
276
283
}
277
284
278
285
char *err = NULL ;
279
286
if (sqlite3_exec (db, " PRAGMA integrity_check;" , NULL , NULL , &err) != SQLITE_OK) {
280
287
fprintf (stderr, " %s: integrity_check: %s\n " , fname, err);
281
- exit (EXIT_FAILURE );
288
+ exit (EXIT_SQLITE );
282
289
}
283
290
}
284
291
@@ -299,7 +306,7 @@ void decode(char *fname, int z, unsigned x, unsigned y, std::set<std::string> co
299
306
sqlite3_stmt *stmt2;
300
307
if (sqlite3_prepare_v2 (db, sql2, -1 , &stmt2, NULL ) != SQLITE_OK) {
301
308
fprintf (stderr, " %s: select failed: %s\n " , fname, sqlite3_errmsg (db));
302
- exit (EXIT_FAILURE );
309
+ exit (EXIT_SQLITE );
303
310
}
304
311
305
312
while (sqlite3_step (stmt2) == SQLITE_ROW) {
@@ -308,7 +315,7 @@ void decode(char *fname, int z, unsigned x, unsigned y, std::set<std::string> co
308
315
309
316
if (name == NULL || value == NULL ) {
310
317
fprintf (stderr, " Corrupt mbtiles file: null metadata\n " );
311
- exit (EXIT_FAILURE );
318
+ exit (EXIT_SQLITE );
312
319
}
313
320
314
321
if (exclude_meta.count ((char *) name) == 0 ) {
@@ -361,7 +368,7 @@ void decode(char *fname, int z, unsigned x, unsigned y, std::set<std::string> co
361
368
FILE *f = fopen (fn.c_str (), " rb" );
362
369
if (f == NULL ) {
363
370
perror (fn.c_str ());
364
- exit (EXIT_FAILURE );
371
+ exit (EXIT_OPEN );
365
372
}
366
373
367
374
std::string s;
@@ -372,14 +379,14 @@ void decode(char *fname, int z, unsigned x, unsigned y, std::set<std::string> co
372
379
}
373
380
fclose (f);
374
381
375
- handle (s, tiles[i].z , tiles[i].x , tiles[i].y , to_decode, pipeline, stats, state);
382
+ handle (s, tiles[i].z , tiles[i].x , tiles[i].y , to_decode, pipeline, stats, state, coordinate_mode );
376
383
}
377
384
} else {
378
385
const char *sql = " SELECT tile_data, zoom_level, tile_column, tile_row from tiles where zoom_level between ? and ? order by zoom_level, tile_column, tile_row;" ;
379
386
sqlite3_stmt *stmt;
380
387
if (sqlite3_prepare_v2 (db, sql, -1 , &stmt, NULL ) != SQLITE_OK) {
381
388
fprintf (stderr, " %s: select failed: %s\n " , fname, sqlite3_errmsg (db));
382
- exit (EXIT_FAILURE );
389
+ exit (EXIT_SQLITE );
383
390
}
384
391
385
392
sqlite3_bind_int (stmt, 1 , minzoom);
@@ -407,18 +414,18 @@ void decode(char *fname, int z, unsigned x, unsigned y, std::set<std::string> co
407
414
408
415
if (tz < 0 || tz >= 32 ) {
409
416
fprintf (stderr, " Impossible zoom level %d in mbtiles\n " , tz);
410
- exit (EXIT_FAILURE );
417
+ exit (EXIT_IMPOSSIBLE );
411
418
}
412
419
413
420
ty = (1LL << tz) - 1 - ty;
414
421
const char *s = (const char *) sqlite3_column_blob (stmt, 0 );
415
422
416
423
if (s == NULL ) {
417
424
fprintf (stderr, " Corrupt mbtiles file: null entry in tiles table\n " );
418
- exit (EXIT_FAILURE );
425
+ exit (EXIT_SQLITE );
419
426
}
420
427
421
- handle (std::string (s, len), tz, tx, ty, to_decode, pipeline, stats, state);
428
+ handle (std::string (s, len), tz, tx, ty, to_decode, pipeline, stats, state, coordinate_mode );
422
429
}
423
430
424
431
sqlite3_finalize (stmt);
@@ -443,7 +450,7 @@ void decode(char *fname, int z, unsigned x, unsigned y, std::set<std::string> co
443
450
sqlite3_stmt *stmt;
444
451
if (sqlite3_prepare_v2 (db, sql, -1 , &stmt, NULL ) != SQLITE_OK) {
445
452
fprintf (stderr, " %s: select failed: %s\n " , fname, sqlite3_errmsg (db));
446
- exit (EXIT_FAILURE );
453
+ exit (EXIT_SQLITE );
447
454
}
448
455
449
456
sqlite3_bind_int (stmt, 1 , z);
@@ -456,14 +463,14 @@ void decode(char *fname, int z, unsigned x, unsigned y, std::set<std::string> co
456
463
457
464
if (s == NULL ) {
458
465
fprintf (stderr, " Corrupt mbtiles file: null entry in tiles table\n " );
459
- exit (EXIT_FAILURE );
466
+ exit (EXIT_SQLITE );
460
467
}
461
468
462
469
if (z != oz) {
463
470
fprintf (stderr, " %s: Warning: using tile %d/%u/%u instead of %d/%u/%u\n " , fname, z, x, y, oz, ox, oy);
464
471
}
465
472
466
- handle (std::string (s, len), z, x, y, to_decode, pipeline, stats, state);
473
+ handle (std::string (s, len), z, x, y, to_decode, pipeline, stats, state, coordinate_mode );
467
474
handled = 1 ;
468
475
}
469
476
@@ -477,13 +484,13 @@ void decode(char *fname, int z, unsigned x, unsigned y, std::set<std::string> co
477
484
478
485
if (sqlite3_close (db) != SQLITE_OK) {
479
486
fprintf (stderr, " %s: could not close database: %s\n " , fname, sqlite3_errmsg (db));
480
- exit (EXIT_FAILURE );
487
+ exit (EXIT_CLOSE );
481
488
}
482
489
}
483
490
484
491
void usage (char **argv) {
485
492
fprintf (stderr, " Usage: %s [-s projection] [-Z minzoom] [-z maxzoom] [-l layer ...] file.mbtiles [zoom x y]\n " , argv[0 ]);
486
- exit (EXIT_FAILURE );
493
+ exit (EXIT_ARGS );
487
494
}
488
495
489
496
int main (int argc, char **argv) {
@@ -494,9 +501,12 @@ int main(int argc, char **argv) {
494
501
bool pipeline = false ;
495
502
bool stats = false ;
496
503
std::set<std::string> exclude_meta;
504
+ int coordinate_mode = 0 ;
497
505
498
506
struct option long_options[] = {
499
507
{" projection" , required_argument, 0 , ' s' },
508
+ {" fractional-coordinates" , no_argument, 0 , ' F' },
509
+ {" integer-coordinates" , no_argument, 0 , ' I' },
500
510
{" maximum-zoom" , required_argument, 0 , ' z' },
501
511
{" minimum-zoom" , required_argument, 0 , ' Z' },
502
512
{" layer" , required_argument, 0 , ' l' },
@@ -527,6 +537,14 @@ int main(int argc, char **argv) {
527
537
set_projection_or_exit (optarg );
528
538
break ;
529
539
540
+ case ' F' :
541
+ coordinate_mode = 1 ;
542
+ break ;
543
+
544
+ case ' I' :
545
+ coordinate_mode = 2 ;
546
+ break ;
547
+
530
548
case ' z' :
531
549
maxzoom = atoi (optarg );
532
550
break ;
@@ -561,9 +579,9 @@ int main(int argc, char **argv) {
561
579
}
562
580
563
581
if (argc == optind + 4 ) {
564
- decode (argv[optind ], atoi (argv[optind + 1 ]), atoi (argv[optind + 2 ]), atoi (argv[optind + 3 ]), to_decode, pipeline, stats, exclude_meta);
582
+ decode (argv[optind ], atoi (argv[optind + 1 ]), atoi (argv[optind + 2 ]), atoi (argv[optind + 3 ]), to_decode, pipeline, stats, exclude_meta, coordinate_mode );
565
583
} else if (argc == optind + 1 ) {
566
- decode (argv[optind ], -1 , -1 , -1 , to_decode, pipeline, stats, exclude_meta);
584
+ decode (argv[optind ], -1 , -1 , -1 , to_decode, pipeline, stats, exclude_meta, coordinate_mode );
567
585
} else {
568
586
usage (argv);
569
587
}
0 commit comments