forked from MapServer/mapcache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapcache_seed.c
1540 lines (1414 loc) · 49.9 KB
/
mapcache_seed.c
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
* $Id: mapcache_seed.c 13201 2012-03-05 13:50:45Z tbonfort $
*
* Project: MapServer
* Purpose: MapCache utility program for seeding and pruning caches
* Author: Thomas Bonfort and the MapServer team.
*
******************************************************************************
* Copyright (c) 1996-2011 Regents of the University of Minnesota.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies of this Software or works derived from this Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
#include "mapcache-util-config.h"
#include "mapcache.h"
#include <apr_thread_proc.h>
#include <apr_thread_mutex.h>
#include <apr_getopt.h>
#include <signal.h>
#include <time.h>
#ifndef _WIN32
#include <unistd.h>
#include <sys/stat.h>
#define USE_FORK
#include <sys/time.h>
#endif
#include <apr_time.h>
#include <apr_strings.h>
#ifdef USE_FORK
int msqid;
#include <sys/ipc.h>
#include <sys/msg.h>
#include <errno.h>
#endif
#include <apr_queue.h>
apr_queue_t *work_queue;
apr_queue_t *log_queue;
#if defined(USE_OGR) && defined(USE_GEOS)
#define USE_CLIPPERS
#endif
#ifdef USE_CLIPPERS
#include "ogr_api.h"
#include "geos_c.h"
int nClippers = 0;
const GEOSPreparedGeometry **clippers=NULL;
#endif
mapcache_tileset *tileset;
mapcache_tileset *tileset_transfer;
mapcache_cfg *cfg;
mapcache_context ctx;
apr_array_header_t *dimensions=NULL;
int minzoom=-1;
int maxzoom=-1;
mapcache_grid_link *grid_link;
int nthreads=0;
int nprocesses=0;
int quiet = 0;
int verbose = 0;
int force = 0;
int sig_int_received = 0;
int error_detected = 0;
double percent_failed_allowed = 1.0;
int n_metatiles_tot = 0;
int n_nodata_tot = 0;
int rate_limit = 0;
FILE *failed_log = NULL, *retry_log = NULL;
#define FAIL_BACKLOG_COUNT 1000
apr_time_t age_limit = 0;
struct mctimeval starttime;
typedef enum {
MAPCACHE_CMD_SEED,
MAPCACHE_CMD_STOP,
MAPCACHE_CMD_DELETE,
MAPCACHE_CMD_SKIP,
MAPCACHE_CMD_TRANSFER,
MAPCACHE_CMD_STOP_RECURSION
} cmd;
typedef enum {
MAPCACHE_ITERATION_UNSET,
MAPCACHE_ITERATION_DEPTH_FIRST,
MAPCACHE_ITERATION_LEVEL_FIRST,
MAPCACHE_ITERATION_LOG
} mapcache_iteration_mode;
mapcache_iteration_mode iteration_mode = MAPCACHE_ITERATION_UNSET;
struct seed_cmd {
cmd command;
int x;
int y;
int z;
};
typedef enum {
MAPCACHE_STATUS_OK,
MAPCACHE_STATUS_FAIL,
MAPCACHE_STATUS_FINISHED
} s_status;
struct seed_status {
s_status status;
int x,y,z;
int nodata;
char *msg;
};
#ifdef USE_FORK
struct msg_cmd {
long mtype;
struct seed_cmd cmd;
};
#endif
cmd mode = MAPCACHE_CMD_SEED; /* the mode the utility will be running in: either seed or delete */
int push_queue(struct seed_cmd cmd)
{
struct seed_cmd *pcmd;
int retries=0;
int ret;
#ifdef USE_FORK
if(nprocesses > 1) {
struct msg_cmd mcmd;
mcmd.mtype = 1;
mcmd.cmd = cmd;
if (msgsnd(msqid, &mcmd, sizeof(struct seed_cmd), 0) == -1) {
printf("failed to push tile %d %d %d\n",cmd.z,cmd.y,cmd.x);
return APR_EGENERAL;
}
return APR_SUCCESS;
}
#endif
pcmd = calloc(1,sizeof(struct seed_cmd));
*pcmd = cmd;
ret = apr_queue_push(work_queue,pcmd);
while (ret == APR_EINTR && retries < 10) {
retries++;
ret = apr_queue_push(work_queue,pcmd);
}
if(ret == APR_EINTR) {
printf("failed to push tile %d %d %d after 10 retries\n",cmd.z,cmd.y,cmd.x);
return APR_EGENERAL;
}
return ret;
}
int pop_queue(struct seed_cmd *cmd)
{
int ret,retries=0;
struct seed_cmd *pcmd;
#ifdef USE_FORK
if(nprocesses > 1) {
struct msg_cmd mcmd;
if (msgrcv(msqid, &mcmd, sizeof(struct seed_cmd), 1, 0) == -1) {
printf("failed to pop tile\n");
return APR_EGENERAL;
}
*cmd = mcmd.cmd;
return APR_SUCCESS;
}
#endif
ret = apr_queue_pop(work_queue, (void**)&pcmd);
while(ret == APR_EINTR && retries<10) {
retries++;
ret = apr_queue_pop(work_queue, (void**)&pcmd);
}
if(ret == APR_SUCCESS) {
*cmd = *pcmd;
free(pcmd);
}
return ret;
}
int trypop_queue(struct seed_cmd *cmd)
{
int ret,retries=0;
struct seed_cmd *pcmd;
#ifdef USE_FORK
if(nprocesses>1) {
struct msg_cmd mcmd;
ret = msgrcv(msqid, &mcmd, sizeof(struct seed_cmd), 1, IPC_NOWAIT);
if(errno == ENOMSG) return APR_EAGAIN;
if(ret>0) {
*cmd = mcmd.cmd;
return APR_SUCCESS;
} else {
printf("failed to trypop tile\n");
return APR_EGENERAL;
}
}
#endif
ret = apr_queue_trypop(work_queue,(void**)&pcmd);
while(ret == APR_EINTR && retries<10) {
retries++;
ret = apr_queue_trypop(work_queue,(void**)&pcmd);
}
if(ret == APR_SUCCESS) {
*cmd = *pcmd;
free(pcmd);
}
return ret;
}
#define SEEDER_OPT_THREAD_DELAY 256
#define SEEDER_OPT_RATE_LIMIT 257
static const apr_getopt_option_t seed_options[] = {
/* long-option, short-option, has-arg flag, description */
{ "config", 'c', TRUE, "configuration file (/path/to/mapcache.xml)"},
{ "cache", 'C', TRUE, "override cache used by selected tileset (useful for selectively seeding fallback/multitier caches)"},
#ifdef USE_CLIPPERS
{ "ogr-datasource", 'd', TRUE, "ogr datasource to get features from"},
#endif
{ "dimension", 'D', TRUE, "set the value of a dimension (format DIMENSIONNAME=VALUE). Can be used multiple times for multiple dimensions" },
{ "extent", 'e', TRUE, "extent to seed, format: minx,miny,maxx,maxy" },
{ "force", 'f', FALSE, "force tile recreation even if it already exists" },
{ "grid", 'g', TRUE, "grid to seed" },
{ "help", 'h', FALSE, "show help" },
{ "iteration-mode", 'i', TRUE, "either \"drill-down\" or \"scanline\". Default is to use drill-down for g, WGS84 and GoogleMapsCompatible grids, and scanline for others. Use this flag to override." },
#ifdef USE_CLIPPERS
{ "ogr-layer", 'l', TRUE, "layer inside datasource"},
#endif
{ "log-failed", 'L', TRUE, "log failed tiles to [file]"},
{ "mode", 'm', TRUE, "mode: seed (default), delete or transfer" },
{ "metasize", 'M', TRUE, "override metatile size while seeding, eg 8,8" },
{ "nthreads", 'n', TRUE, "number of parallel threads to use (incompatible with -p/--nprocesses)" },
{ "older", 'o', TRUE, "reseed tiles older than supplied date (format: year/month/day hour:minute, eg: 2011/01/31 20:45" },
{ "nprocesses", 'p', TRUE, "number of parallel processes to use (incompatible with -n/--nthreads)" },
{ "percent", 'P', TRUE, "percent of failed requests allowed from the last 1000 before we abort (default: 1(%), set to 0 to abort on first error)" },
{ "quiet", 'q', FALSE, "don't show progress info" },
{ "retry-failed", 'R', TRUE, "retry failed requests logged to [file] by --log-failed" },
#ifdef USE_CLIPPERS
{ "ogr-sql", 's', TRUE, "sql to filter inside layer"},
#endif
{ "tileset", 't', TRUE, "tileset to seed" },
{ "verbose", 'v', FALSE, "show debug log messages" },
#ifdef USE_CLIPPERS
{ "ogr-where", 'w', TRUE, "filter to apply on layer features"},
#endif
{ "transfer", 'x', TRUE, "tileset to transfer" },
{ "zoom", 'z', TRUE, "min and max zoomlevels to seed, separated by a comma. eg 0,6" },
{ "rate-limit", SEEDER_OPT_RATE_LIMIT, TRUE, "maximum number of tiles/second to seed"},
{ "thread-delay", SEEDER_OPT_THREAD_DELAY, TRUE, "delay in seconds between rendering thread creation (ramp up)"},
{ NULL, 0, 0, NULL }
};
void handle_sig_int(int signal)
{
#define signal_msg "SIGINT received, waiting for threads to finish\npress ctrl-C again to force terminate\n"
if(!sig_int_received) {
int err = write(2,signal_msg,strlen(signal_msg));
if(err) {
//nothing we can really do here
}
sig_int_received = 1;
} else {
exit(signal);
}
}
void seed_log(mapcache_context *ctx, mapcache_log_level level, char *msg, ...)
{
if(level >= MAPCACHE_WARN || verbose) {
va_list args;
va_start(args,msg);
vfprintf(stderr,msg,args);
va_end(args);
fprintf(stderr,"\n");
}
}
void mapcache_context_seeding_log(mapcache_context *ctx, mapcache_log_level level, char *msg, ...)
{
va_list args;
va_start(args,msg);
vfprintf(stderr,msg,args);
va_end(args);
fprintf(stderr,"\n");
}
#ifdef USE_CLIPPERS
int ogr_features_intersect_tile(mapcache_context *ctx, mapcache_tile *tile)
{
mapcache_metatile *mt = mapcache_tileset_metatile_get(ctx,tile);
GEOSCoordSequence *mtbboxls = GEOSCoordSeq_create(5,2);
GEOSGeometry *mtbbox = GEOSGeom_createLinearRing(mtbboxls);
GEOSGeometry *mtbboxg = GEOSGeom_createPolygon(mtbbox,NULL,0);
int i;
int intersects = 0;
GEOSCoordSeq_setX(mtbboxls,0,mt->map.extent.minx);
GEOSCoordSeq_setY(mtbboxls,0,mt->map.extent.miny);
GEOSCoordSeq_setX(mtbboxls,1,mt->map.extent.maxx);
GEOSCoordSeq_setY(mtbboxls,1,mt->map.extent.miny);
GEOSCoordSeq_setX(mtbboxls,2,mt->map.extent.maxx);
GEOSCoordSeq_setY(mtbboxls,2,mt->map.extent.maxy);
GEOSCoordSeq_setX(mtbboxls,3,mt->map.extent.minx);
GEOSCoordSeq_setY(mtbboxls,3,mt->map.extent.maxy);
GEOSCoordSeq_setX(mtbboxls,4,mt->map.extent.minx);
GEOSCoordSeq_setY(mtbboxls,4,mt->map.extent.miny);
for(i=0; i<nClippers; i++) {
const GEOSPreparedGeometry *clipper = clippers[i];
if(GEOSPreparedIntersects(clipper,mtbboxg)) {
intersects = 1;
break;
}
}
GEOSGeom_destroy(mtbboxg);
return intersects;
}
#endif
cmd examine_tile(mapcache_context *ctx, mapcache_tile *tile)
{
int action = MAPCACHE_CMD_SKIP;
int tile_exists;
#ifdef USE_CLIPPERS
/* check we are in the requested features before checking the tile */
if(nClippers > 0 && ogr_features_intersect_tile(ctx,tile) == 0)
return MAPCACHE_CMD_STOP_RECURSION;
#endif
if(mode != MAPCACHE_CMD_TRANSFER && force) {
if(mode == MAPCACHE_CMD_DELETE) {
tile_exists = 1;
} else {
tile_exists = 0;
}
} else {
int i;
if(tile->tileset->dimension_assembly_type != MAPCACHE_DIMENSION_ASSEMBLY_NONE) {
for(i=0; i<tile->dimensions->nelts; i++) {
mapcache_requested_dimension *rdim = APR_ARRAY_IDX(tile->dimensions,i,mapcache_requested_dimension*);
rdim->cached_value = rdim->requested_value;
}
} else {
if(tile->dimensions) {
mapcache_extent extent;
mapcache_grid_get_tile_extent(ctx,tile->grid_link->grid,tile->x,tile->y,tile->z,&extent);
for(i=0; i<tile->dimensions->nelts; i++) {
apr_array_header_t *rdim_vals;
mapcache_requested_dimension *rdim = APR_ARRAY_IDX(tile->dimensions,i,mapcache_requested_dimension*);
rdim_vals = rdim->dimension->get_entries_for_value(ctx,rdim->dimension,rdim->requested_value, tile->tileset, NULL, tile->grid_link->grid);
if(GC_HAS_ERROR(ctx)) {
return MAPCACHE_CMD_SKIP;
}
if(rdim_vals->nelts > 1) {
ctx->set_error(ctx,500,"dimension (%s) for tileset (%s) returned invalid number of subdimensions (1 expected)",rdim->dimension->name, tile->tileset->name);
return MAPCACHE_CMD_SKIP;
}
if(rdim_vals->nelts == 0) {
ctx->set_error(ctx,404,"dimension (%s) for tileset (%s) returned no subdimensions (1 expected)",rdim->dimension->name, tile->tileset->name);
return MAPCACHE_CMD_SKIP;
}
rdim->cached_value = APR_ARRAY_IDX(rdim_vals,0,char*);
}
}
}
tile_exists = mapcache_cache_tile_exists(ctx,tileset->_cache,tile);
}
/* if the tile exists and a time limit was specified, check the tile modification date */
if(tile_exists) {
if(age_limit) {
if(mapcache_cache_tile_get(ctx,tileset->_cache, tile) == MAPCACHE_SUCCESS) {
if(tile->mtime && tile->mtime<age_limit) {
/* the tile modification time is older than the specified limit */
if(mode == MAPCACHE_CMD_SEED || mode == MAPCACHE_CMD_TRANSFER) {
mapcache_tileset_tile_delete(ctx,tile,MAPCACHE_TRUE);
/* if we are in mode transfer, delete it from the dst tileset */
if (mode == MAPCACHE_CMD_TRANSFER) {
tile->tileset = tileset_transfer;
if (mapcache_cache_tile_exists(ctx,tile->tileset->_cache, tile)) {
mapcache_tileset_tile_delete(ctx,tile,MAPCACHE_TRUE);
}
tile->tileset = tileset;
}
action = mode;
} else { //if(action == MAPCACHE_CMD_DELETE)
action = MAPCACHE_CMD_DELETE;
}
}
} else {
//BUG: tile_exists returned true, but tile_get returned a failure. not sure what to do.
action = MAPCACHE_CMD_SKIP;
}
} else {
if(mode == MAPCACHE_CMD_DELETE) {
//the tile exists and we are in delete mode: delete it
action = MAPCACHE_CMD_DELETE;
} else if (mode == MAPCACHE_CMD_TRANSFER) {
/* the tile exists in the source tileset,
check if the tile exists in the destination cache */
tile->tileset = tileset_transfer;
if (!force && mapcache_cache_tile_exists(ctx,tile->tileset->_cache, tile)) {
action = MAPCACHE_CMD_SKIP;
} else {
action = MAPCACHE_CMD_TRANSFER;
}
tile->tileset = tileset;
} else {
// the tile exists and we are in seed mode, skip to next one
action = MAPCACHE_CMD_SKIP;
}
}
} else {
// the tile does not exist
if (mode == MAPCACHE_CMD_SEED) {
action = mode;
} else {
action = MAPCACHE_CMD_SKIP;
}
}
return action;
}
double rate_limit_last_time = 0;
double rate_limit_delay = 0.0;
void rate_limit_sleep() {
if(rate_limit > 0) {
struct mctimeval now;
double now_time;
mapcache_gettimeofday(&now,NULL);
now_time = now.tv_sec + now.tv_usec / 1000000.0;
if((now_time - rate_limit_last_time) < rate_limit_delay) {
apr_sleep((int)((rate_limit_delay - (now_time - rate_limit_last_time)) * 1000000));
/*last_time = now_time + delay - (now_time - last_time); //now plus the time we slept */
rate_limit_last_time = rate_limit_delay + rate_limit_last_time;
} else {
rate_limit_last_time = now_time;
}
}
}
void cmd_recurse(mapcache_context *cmd_ctx, mapcache_tile *tile)
{
cmd action;
int curx, cury, curz;
int blchildx,trchildx,blchildy,trchildy;
int minchildx,maxchildx,minchildy,maxchildy;
mapcache_extent bboxbl,bboxtr;
double epsilon;
apr_pool_clear(cmd_ctx->pool);
if(sig_int_received || error_detected) { //stop if we were asked to stop by hitting ctrl-c
//remove all items from the queue
struct seed_cmd entry;
while (trypop_queue(&entry)!=APR_EAGAIN) /*do nothing*/;
return;
}
action = examine_tile(cmd_ctx, tile);
if(action == MAPCACHE_CMD_SEED || action == MAPCACHE_CMD_DELETE || action == MAPCACHE_CMD_TRANSFER) {
//current x,y,z needs seeding, add it to the queue
struct seed_cmd cmd;
cmd.x = tile->x;
cmd.y = tile->y;
cmd.z = tile->z;
cmd.command = action;
if(rate_limit > 0)
rate_limit_sleep();
push_queue(cmd);
}
if(action == MAPCACHE_CMD_STOP_RECURSION)
return;
//recurse into our 4 child metatiles
curx = tile->x;
cury = tile->y;
curz = tile->z;
tile->z += 1;
if(tile->z > maxzoom) {
tile->z -= 1;
return;
}
/*
* compute the x,y limits of the next zoom level that intersect the
* current metatile
*/
mapcache_grid_get_tile_extent(cmd_ctx, grid_link->grid,
curx, cury, curz, &bboxbl);
mapcache_grid_get_tile_extent(cmd_ctx, grid_link->grid,
curx+tileset->metasize_x-1, cury+tileset->metasize_y-1, curz, &bboxtr);
epsilon = (bboxbl.maxx-bboxbl.minx)*0.01;
mapcache_grid_get_xy(cmd_ctx,grid_link->grid,
bboxbl.minx + epsilon,
bboxbl.miny + epsilon,
tile->z,&blchildx,&blchildy);
mapcache_grid_get_xy(cmd_ctx,grid_link->grid,
bboxtr.maxx - epsilon,
bboxtr.maxy - epsilon,
tile->z,&trchildx,&trchildy);
minchildx = (MAPCACHE_MIN(blchildx,trchildx) / tileset->metasize_x)*tileset->metasize_x;
minchildy = (MAPCACHE_MIN(blchildy,trchildy) / tileset->metasize_y)*tileset->metasize_y;
maxchildx = (MAPCACHE_MAX(blchildx,trchildx) / tileset->metasize_x + 1)*tileset->metasize_x;
maxchildy = (MAPCACHE_MAX(blchildy,trchildy) / tileset->metasize_y + 1)*tileset->metasize_y;
for(tile->x = minchildx; tile->x < maxchildx; tile->x += tileset->metasize_x) {
if(tile->x >= grid_link->grid_limits[tile->z].minx && tile->x < grid_link->grid_limits[tile->z].maxx) {
for(tile->y = minchildy; tile->y < maxchildy; tile->y += tileset->metasize_y) {
if(tile->y >= grid_link->grid_limits[tile->z].miny && tile->y < grid_link->grid_limits[tile->z].maxy) {
cmd_recurse(cmd_ctx,tile);
}
}
}
}
tile->x = curx;
tile->y = cury;
tile->z = curz;
}
void feed_worker()
{
int n;
mapcache_tile *tile;
int z = minzoom;
int x = grid_link->grid_limits[z].minx;
int y = grid_link->grid_limits[z].miny;
mapcache_context cmd_ctx = ctx;
int nworkers = nthreads;
if(nprocesses >= 1) nworkers = nprocesses;
apr_pool_create(&cmd_ctx.pool,ctx.pool);
tile = mapcache_tileset_tile_create(ctx.pool, tileset, grid_link);
tile->dimensions = mapcache_requested_dimensions_clone(ctx.pool,dimensions);
if(rate_limit > 0) {
/* compute time between seed commands accounting for max rate-limit and current metasize */
rate_limit_delay = (tileset->metasize_x * tileset->metasize_y) / (double)rate_limit;
}
if(iteration_mode == MAPCACHE_ITERATION_DEPTH_FIRST) {
do {
tile->x = x;
tile->y = y;
tile->z = z;
cmd_recurse(&cmd_ctx,tile);
x += tileset->metasize_x;
if( x >= grid_link->grid_limits[z].maxx ) {
y += tileset->metasize_y;
if( y < grid_link->grid_limits[z].maxy) {
x = grid_link->grid_limits[z].minx;
}
}
} while (
x < grid_link->grid_limits[z].maxx
&&
y < grid_link->grid_limits[z].maxy
);
} else {
while(1) {
int action;
apr_pool_clear(cmd_ctx.pool);
if(sig_int_received || error_detected) { //stop if we were asked to stop by hitting ctrl-c
//remove all items from the queue
struct seed_cmd entry;
while (trypop_queue(&entry)!=APR_EAGAIN) /* do nothing */;
break;
}
if(iteration_mode == MAPCACHE_ITERATION_LOG) {
if(3 != fscanf(retry_log,"%d,%d,%d\n",&x,&y,&z)) {
break;
} else {
printf("from log: %d %d %d\n",x,y,z);
}
}
tile->x = x;
tile->y = y;
tile->z = z;
action = examine_tile(&cmd_ctx, tile);
if(action == MAPCACHE_CMD_SEED || action == MAPCACHE_CMD_DELETE || action == MAPCACHE_CMD_TRANSFER) {
//current x,y,z needs seeding, add it to the queue
struct seed_cmd cmd;
cmd.x = x;
cmd.y = y;
cmd.z = z;
cmd.command = action;
if(rate_limit > 0)
rate_limit_sleep();
push_queue(cmd);
}
//compute next x,y,z
x += tileset->metasize_x;
if(x >= grid_link->grid_limits[z].maxx) {
//x is too big, increment y
y += tileset->metasize_y;
if(y >= grid_link->grid_limits[z].maxy) {
//y is too big, increment z
z += 1;
if(z > maxzoom) break; //we've finished seeding
y = grid_link->grid_limits[z].miny; //set y to the smallest value for current z
}
x = grid_link->grid_limits[z].minx; //set x to smallest value for current z
}
}
}
//instruct rendering threads to stop working
for(n=0; n<nworkers; n++) {
struct seed_cmd cmd;
cmd.command = MAPCACHE_CMD_STOP;
push_queue(cmd);
}
}
void seed_worker()
{
mapcache_tile *tile;
mapcache_context seed_ctx = ctx;
apr_pool_t *tpool;
seed_ctx.log = seed_log;
apr_pool_create(&seed_ctx.pool,ctx.pool);
apr_pool_create(&tpool,ctx.pool);
tile = mapcache_tileset_tile_create(tpool, tileset, grid_link);
if(dimensions) {
tile->dimensions = mapcache_requested_dimensions_clone(tpool,dimensions);
}
while(1) {
struct seed_cmd cmd;
apr_status_t ret;
apr_pool_clear(seed_ctx.pool);
ret = pop_queue(&cmd);
if(ret != APR_SUCCESS || cmd.command == MAPCACHE_CMD_STOP) break;
tile->x = cmd.x;
tile->y = cmd.y;
tile->z = cmd.z;
tile->nodata = 0;
tile->encoded_data = NULL;
tile->raw_image = NULL;
if(tile->dimensions) {
int i;
if(tileset->dimension_assembly_type == MAPCACHE_DIMENSION_ASSEMBLY_NONE) {
mapcache_extent extent;
mapcache_grid_get_tile_extent(&seed_ctx,tile->grid_link->grid,tile->x,tile->y,tile->z,&extent);
for(i=0; i<tile->dimensions->nelts; i++) {
apr_array_header_t *rdim_vals;
mapcache_requested_dimension *rdim = APR_ARRAY_IDX(tile->dimensions,i,mapcache_requested_dimension*);
rdim_vals = rdim->dimension->get_entries_for_value(&seed_ctx,rdim->dimension,rdim->requested_value, tile->tileset, NULL, tile->grid_link->grid);
GC_CHECK_ERROR(&seed_ctx);
if(rdim_vals->nelts > 1) {
seed_ctx.set_error(&seed_ctx,500,"dimension (%s) for tileset (%s) returned invalid number of subdimensions (1 expected)",rdim->dimension->name, tile->tileset->name);
return;
}
if(rdim_vals->nelts == 0) {
seed_ctx.set_error(&seed_ctx,404,"dimension (%s) for tileset (%s) returned no subdimensions (1 expected)",rdim->dimension->name, tile->tileset->name);
return;
}
rdim->cached_value = APR_ARRAY_IDX(rdim_vals,0,char*);
}
} else {
for(i=0; i<tile->dimensions->nelts; i++) {
mapcache_requested_dimension *rdim = APR_ARRAY_IDX(tile->dimensions,i,mapcache_requested_dimension*);
rdim->cached_value = NULL;
}
}
}
if(cmd.command == MAPCACHE_CMD_SEED) {
if(!tile->dimensions || tileset->dimension_assembly_type == MAPCACHE_DIMENSION_ASSEMBLY_NONE) {
mapcache_metatile *mt = mapcache_tileset_metatile_get(&seed_ctx, tile);
/* this will query the source to create the tiles, and save them to the cache */
mapcache_tileset_render_metatile(&seed_ctx, mt);
} else {
mapcache_tileset_tile_set_get_with_subdimensions(&seed_ctx,tile);
}
} else if (cmd.command == MAPCACHE_CMD_TRANSFER) {
mapcache_tileset_tile_get(&seed_ctx, tile);
if(!tile->nodata && !GC_HAS_ERROR(&seed_ctx)) {
mapcache_tileset *tmp_tileset = tile->tileset;
tile->tileset = tileset_transfer;
mapcache_cache_tile_set(&seed_ctx, tile->tileset->_cache, tile);
tile->tileset = tmp_tileset;
}
} else { //CMD_DELETE
mapcache_tileset_tile_delete(&seed_ctx,tile,MAPCACHE_TRUE);
}
{
struct seed_status *st = calloc(1,sizeof(struct seed_status));
int retries=0;
st->x=tile->x;
st->y=tile->y;
st->z=tile->z;
st->nodata = tile->nodata;
if(seed_ctx.get_error(&seed_ctx)) {
st->status = MAPCACHE_STATUS_FAIL;
st->msg = strdup(seed_ctx.get_error_message(&seed_ctx));
seed_ctx.clear_errors(&seed_ctx);
} else {
st->status = MAPCACHE_STATUS_OK;
}
ret = apr_queue_push(log_queue,(void*)st);
while( ret == APR_EINTR && retries < 10) {
retries++;
ret = apr_queue_push(log_queue,(void*)st);
}
if( ret == APR_EINTR) {
printf("FATAL ERROR: unable to log progress after 10 retries, aborting\n");
break;
}
if(ret != APR_SUCCESS)
{
printf("FATAL ERROR: unable to log progress\n");
break;
}
}
}
}
#ifdef USE_FORK
int seed_process() {
seed_worker();
return 0;
}
#endif
static void* APR_THREAD_FUNC seed_thread(apr_thread_t *thread, void *data) {
seed_worker();
return NULL;
}
static void* APR_THREAD_FUNC feed_thread_fn(apr_thread_t *thread, void *data) {
//start the thread that will populate the queue.
feed_worker();
return NULL;
}
static void* APR_THREAD_FUNC log_thread_fn(apr_thread_t *thread, void *data) {
size_t cur;
double last_time;
double now_time;
int i;
int nfailed;
int ntotal;
double pct;
char failed[FAIL_BACKLOG_COUNT];
memset(failed,-1,FAIL_BACKLOG_COUNT);
cur=0;
last_time=0;
while(1) {
int retries = 0;
struct seed_status *st;
apr_status_t ret = apr_queue_pop(log_queue, (void**)&st);
while(ret == APR_EINTR && retries<10) {
retries++;
ret = apr_queue_pop(log_queue, (void**)&st);
}
if(ret != APR_SUCCESS || !st) break;
if(st->status == MAPCACHE_STATUS_FINISHED)
return NULL;
if(st->status == MAPCACHE_STATUS_OK) {
failed[cur]=0;
n_metatiles_tot++;
if(st->nodata) {
n_nodata_tot++;
}
if(!quiet) {
struct mctimeval now;
mapcache_gettimeofday(&now,NULL);
now_time = now.tv_sec + now.tv_usec / 1000000.0;
if((now_time - last_time) > 1.0) {
printf(" \r");
printf("seeded %d tiles, now at z%d x%d y%d\r",n_metatiles_tot*tileset->metasize_x*tileset->metasize_y, st->z,st->x,st->y);
fflush(stdout);
last_time = now_time;
}
}
} else {
/* count how many errors and successes we have */
failed[cur]=1;
nfailed=0;
ntotal=0;
if(failed_log) {
fprintf(failed_log,"%d,%d,%d\n",st->x,st->y,st->z);
}
for(i=0; i<FAIL_BACKLOG_COUNT; i++) {
if(failed[i]>=0) ntotal++;
if(failed[i]==1) nfailed++;
}
ctx.log(&ctx, MAPCACHE_WARN, "failed to seed tile z%d,x%d,y%d:\n%s\n", st->z,st->x,st->y,st->msg);
pct = ((double)nfailed / (double)ntotal) * 100;
if(pct > percent_failed_allowed) {
ctx.log(&ctx, MAPCACHE_ERROR, "aborting seed as %.1f%% of the last %d requests failed\n", pct, FAIL_BACKLOG_COUNT);
error_detected = 1;
}
}
if(st->msg) free(st->msg);
free(st);
cur++;
cur %= FAIL_BACKLOG_COUNT;
}
return NULL;
}
void
notice(const char *fmt, ...)
{
va_list ap;
fprintf( stdout, "NOTICE: ");
va_start (ap, fmt);
vfprintf( stdout, fmt, ap);
va_end(ap);
fprintf( stdout, "\n" );
}
void
log_and_exit(const char *fmt, ...)
{
va_list ap;
fprintf( stdout, "ERROR: ");
va_start (ap, fmt);
vfprintf( stdout, fmt, ap);
va_end(ap);
fprintf( stdout, "\n" );
exit(1);
}
int usage(const char *progname, char *msg, ...)
{
int i=0;
if(msg) {
va_list args;
va_start(args,msg);
printf("%s\n",progname);
vprintf(msg,args);
printf("\noptions:\n");
va_end(args);
}
else
printf("usage: %s options\n",progname);
while(seed_options[i].name) {
if(seed_options[i].has_arg==TRUE) {
printf("-%c|--%s [value]: %s\n",seed_options[i].optch,seed_options[i].name, seed_options[i].description);
} else {
printf("-%c|--%s: %s\n",seed_options[i].optch,seed_options[i].name, seed_options[i].description);
}
i++;
}
apr_terminate();
return 1;
}
static int isPowerOfTwo(int x)
{
return (x & (x - 1)) == 0;
}
int main(int argc, const char **argv)
{
/* initialize apr_getopt_t */
apr_getopt_t *opt;
const char *configfile=NULL;
apr_thread_t **seed_threads;
apr_thread_t *log_thread,*feed_thread;
const char *tileset_name=NULL;
const char *tileset_transfer_name=NULL;
const char *grid_name = NULL;
const char *cache_override = NULL;
int *zooms = NULL;//[2];
mapcache_extent *extent = NULL;//[4];
int optch;
int rv,n;
const char *old = NULL;
const char *optarg;
apr_table_t *argdimensions;
char *dimkey=NULL, *dimvalue=NULL,*key, *last, *optargcpy=NULL;
int keyidx;
int *metasizes = NULL;//[2];
int metax=-1,metay=-1;
double *extent_array = NULL;
double thread_delay = 0.0;
#ifdef USE_CLIPPERS
OGRFeatureH hFeature;
GEOSWKTReader *geoswktreader;
const char *ogr_where = NULL;
const char *ogr_layer = NULL;
const char *ogr_sql = NULL;
const char *ogr_datasource = NULL;
#endif
apr_initialize();
(void) signal(SIGINT,handle_sig_int);
apr_pool_create(&ctx.pool,NULL);
mapcache_context_init(&ctx);
cfg = mapcache_configuration_create(ctx.pool);
ctx.config = cfg;
ctx.log= mapcache_context_seeding_log;
apr_getopt_init(&opt, ctx.pool, argc, argv);
mapcache_gettimeofday(&starttime,NULL);
argdimensions = apr_table_make(ctx.pool,3);
/* parse the all options based on opt_option[] */
while ((rv = apr_getopt_long(opt, seed_options, &optch, &optarg)) == APR_SUCCESS) {
switch (optch) {
case 'h':
return usage(argv[0],NULL);
break;
case 'f':
force = 1;
break;
case 'q':
quiet = 1;
break;
case 'v':
verbose = 1;
break;
case 'c':
configfile = optarg;
break;
case 'C':
cache_override = optarg;
break;
case 'g':
grid_name = optarg;
break;
case 't':
tileset_name = optarg;
break;
case 'x':
tileset_transfer_name = optarg;
break;
case 'i':
if(!strcmp(optarg,"drill-down")) {
iteration_mode = MAPCACHE_ITERATION_DEPTH_FIRST;
} else if(!strcmp(optarg,"level-by-level") || !strcmp(optarg, "scanline")) {
iteration_mode = MAPCACHE_ITERATION_LEVEL_FIRST;
} else {
return usage(argv[0],"invalid iteration mode, expecting \"drill-down\" or \"scanline\"");
}
break;
case 'L':
failed_log = fopen(optarg,"w");
if(!failed_log) {
return usage(argv[0],"failed to open -L|--log-failed file for writing");
}
break;
case 'R':
retry_log = fopen(optarg,"r");
if(!retry_log) {
return usage(argv[0],"failed to open -R|--retry_failed file for writing");
}
break;
case 'm':
if(!strcmp(optarg,"delete")) {
mode = MAPCACHE_CMD_DELETE;
} else if(!strcmp(optarg,"transfer")) {
mode = MAPCACHE_CMD_TRANSFER;
} else if(strcmp(optarg,"seed")) {