@@ -735,11 +735,12 @@ std::vector<long long> parse_max_density(const unsigned char *v) {
735735 return out;
736736}
737737
738- void merge_tiles (char **fnames, size_t n, size_t cpus, sqlite3 *outdb, int zooms) {
738+ void merge_tiles (char **fnames, size_t n, size_t cpus, sqlite3 *outdb, int zooms, std::vector< long long > &zoom_max, double &midlat, double &midlon, double &minlat, double &minlon, double &maxlat, double &maxlon ) {
739739 std::vector<tile_reader> readers;
740740 size_t total_rows = 0 ;
741741 size_t seq = 0 ;
742742 size_t oprogress = 999 ;
743+ size_t biggest = 0 ;
743744
744745 for (size_t i = 0 ; i < n; i++) {
745746 tile_reader r;
@@ -813,8 +814,39 @@ void merge_tiles(char **fnames, size_t n, size_t cpus, sqlite3 *outdb, int zooms
813814 const char *data = (const char *) sqlite3_column_blob (r.stmt , 3 );
814815 size_t len = sqlite3_column_bytes (r.stmt , 3 );
815816 r.data = std::string (data, len);
817+
818+ if (r.zoom == zooms - 1 && r.data .size () > biggest) {
819+ biggest = r.data .size ();
820+ projection->unproject (r.x , r.y (), r.zoom , &midlon, &midlat);
821+ }
822+
816823 readers.push_back (r);
817824 seq++;
825+
826+ // This check is here instead of above so the bounding box is only affected if there are
827+ // tiles in the tileset being merged.
828+
829+ if (sqlite3_prepare_v2 (r.db , " SELECT value from metadata where name = 'bounds';" , -1 , &stmt, NULL ) == SQLITE_OK) {
830+ if (sqlite3_step (stmt) == SQLITE_ROW) {
831+ const unsigned char *bbox = sqlite3_column_text (stmt, 0 );
832+ double o1, a1, o2, a2;
833+ if (sscanf ((const char *) bbox, " %lf,%lf,%lf,%lf" , &o1, &a1, &o2, &a2) == 4 ) {
834+ if (o1 < minlon) {
835+ minlon = o1;
836+ }
837+ if (a1 < minlat) {
838+ minlat = a1;
839+ }
840+ if (o2 > maxlon) {
841+ maxlon = o2;
842+ }
843+ if (a2 > maxlat) {
844+ maxlat = a2;
845+ }
846+ }
847+ }
848+ sqlite3_finalize (stmt);
849+ }
818850 } else {
819851 sqlite3_finalize (r.stmt );
820852
@@ -826,22 +858,21 @@ void merge_tiles(char **fnames, size_t n, size_t cpus, sqlite3 *outdb, int zooms
826858 }
827859
828860 // XXX This doesn't detect if two sources brighten the same pixel together
829- std::vector<long long > global_density;
830861 for (size_t i = 0 ; i < readers.size (); i++) {
831- if (readers[i].max_density .size () > global_density .size ()) {
832- global_density .resize (readers[i].max_density .size ());
862+ if (readers[i].max_density .size () > zoom_max .size ()) {
863+ zoom_max .resize (readers[i].max_density .size ());
833864 }
834865
835866 for (size_t j = 0 ; j < readers[i].max_density .size (); j++) {
836- if (readers[i].max_density [j] > global_density [j]) {
837- global_density [j] = readers[i].max_density [j];
867+ if (readers[i].max_density [j] > zoom_max [j]) {
868+ zoom_max [j] = readers[i].max_density [j];
838869 }
839870 }
840871 }
841872
842873 std::priority_queue<tile_reader> reader_q;
843874 for (size_t i = 0 ; i < readers.size (); i++) {
844- readers[i].global_density = global_density ;
875+ readers[i].global_density = zoom_max ;
845876 reader_q.push (readers[i]);
846877 }
847878 readers.clear ();
@@ -870,6 +901,11 @@ void merge_tiles(char **fnames, size_t n, size_t cpus, sqlite3 *outdb, int zooms
870901 size_t len = sqlite3_column_bytes (r.stmt , 3 );
871902 r.data = std::string (data, len);
872903
904+ if (r.zoom == zooms - 1 && r.data .size () > biggest) {
905+ biggest = r.data .size ();
906+ projection->unproject (r.x , r.y (), r.zoom , &midlon, &midlat);
907+ }
908+
873909 reader_q.push (r);
874910 seq++;
875911
@@ -978,7 +1014,7 @@ int main(int argc, char **argv) {
9781014 }
9791015 sqlite3 *outdb = mbtiles_open (outfile, argv, false );
9801016
981- double minlat = 0 , minlon = 0 , maxlat = 0 , maxlon = 0 , midlat = 0 , midlon = 0 ;
1017+ double minlat = 90 , minlon = 180 , maxlat = - 90 , maxlon = - 180 , midlat = 0 , midlon = 0 ;
9821018 std::vector<long long > zoom_max;
9831019 size_t zooms = 0 ;
9841020
@@ -1169,7 +1205,7 @@ int main(int argc, char **argv) {
11691205 }
11701206 } else {
11711207 fprintf (stderr, " going to merge %zu zoom levels\n " , zooms);
1172- merge_tiles (argv + optind, argc - optind, cpus, outdb, zooms);
1208+ merge_tiles (argv + optind, argc - optind, cpus, outdb, zooms, zoom_max, midlat, midlon, minlat, minlon, maxlat, maxlon );
11731209 }
11741210
11751211 layermap_entry lme (0 );
0 commit comments