diff --git a/tests/join-population/joined-i.mbtiles.json b/tests/join-population/joined-i.mbtiles.json index 35a15fd7..e50d42e9 100644 --- a/tests/join-population/joined-i.mbtiles.json +++ b/tests/join-population/joined-i.mbtiles.json @@ -1,6 +1,6 @@ { "type": "FeatureCollection", "properties": { -"antimeridian_adjusted_bounds": "-122.343750,37.857507,-122.255859,37.926868", -"bounds": "-122.343750,37.857507,-122.255859,37.926868", +"antimeridian_adjusted_bounds": "-135.000000,37.881357,-122.280579,40.979898", +"bounds": "-135.000000,37.881357,-122.280579,40.979898", "center": "-122.299805,37.892187,12", "description": "tests/join-population/tabblock_06001420.mbtiles", "format": "pbf", diff --git a/tests/join-population/joined-no-tile-stats.mbtiles.json b/tests/join-population/joined-no-tile-stats.mbtiles.json index 6f7cde11..2e36d31e 100644 --- a/tests/join-population/joined-no-tile-stats.mbtiles.json +++ b/tests/join-population/joined-no-tile-stats.mbtiles.json @@ -1,6 +1,6 @@ { "type": "FeatureCollection", "properties": { -"antimeridian_adjusted_bounds": "-122.343750,37.857507,-122.255859,37.926868", -"bounds": "-122.343750,37.857507,-122.255859,37.926868", +"antimeridian_adjusted_bounds": "-135.000000,37.877021,-122.280579,40.979898", +"bounds": "-135.000000,37.877021,-122.280579,40.979898", "center": "-122.299805,37.892187,12", "description": "tests/join-population/tabblock_06001420.mbtiles", "format": "pbf", diff --git a/tests/join-population/joined-null.mbtiles.json b/tests/join-population/joined-null.mbtiles.json index 8339f194..a2ff7cc4 100644 --- a/tests/join-population/joined-null.mbtiles.json +++ b/tests/join-population/joined-null.mbtiles.json @@ -1,6 +1,6 @@ { "type": "FeatureCollection", "properties": { -"antimeridian_adjusted_bounds": "-122.343750,37.857507,-122.255859,37.926868", -"bounds": "-122.343750,37.857507,-122.255859,37.926868", +"antimeridian_adjusted_bounds": "-135.000000,37.877021,-122.280579,40.979898", +"bounds": "-135.000000,37.877021,-122.280579,40.979898", "center": "-122.299805,37.892187,12", "description": "tests/join-population/tabblock_06001420.mbtiles", "format": "pbf", diff --git a/tests/join-population/joined-tile-stats-attributes-limit.mbtiles.json b/tests/join-population/joined-tile-stats-attributes-limit.mbtiles.json index 37b5326a..89245614 100644 --- a/tests/join-population/joined-tile-stats-attributes-limit.mbtiles.json +++ b/tests/join-population/joined-tile-stats-attributes-limit.mbtiles.json @@ -1,6 +1,6 @@ { "type": "FeatureCollection", "properties": { -"antimeridian_adjusted_bounds": "-122.343750,37.857507,-122.255859,37.926868", -"bounds": "-122.343750,37.857507,-122.255859,37.926868", +"antimeridian_adjusted_bounds": "-135.000000,37.877021,-122.280579,40.979898", +"bounds": "-135.000000,37.877021,-122.280579,40.979898", "center": "-122.299805,37.892187,12", "description": "tests/join-population/tabblock_06001420.mbtiles", "format": "pbf", diff --git a/tests/join-population/joined-tile-stats-sample-values-limit.mbtiles.json b/tests/join-population/joined-tile-stats-sample-values-limit.mbtiles.json index 4b217de0..dad62899 100644 --- a/tests/join-population/joined-tile-stats-sample-values-limit.mbtiles.json +++ b/tests/join-population/joined-tile-stats-sample-values-limit.mbtiles.json @@ -1,6 +1,6 @@ { "type": "FeatureCollection", "properties": { -"antimeridian_adjusted_bounds": "-122.343750,37.857507,-122.255859,37.926868", -"bounds": "-122.343750,37.857507,-122.255859,37.926868", +"antimeridian_adjusted_bounds": "-135.000000,37.877021,-122.280579,40.979898", +"bounds": "-135.000000,37.877021,-122.280579,40.979898", "center": "-122.299805,37.892187,12", "description": "tests/join-population/tabblock_06001420.mbtiles", "format": "pbf", diff --git a/tests/join-population/joined-tile-stats-values-limit.mbtiles.json b/tests/join-population/joined-tile-stats-values-limit.mbtiles.json index 9fa16025..75172022 100644 --- a/tests/join-population/joined-tile-stats-values-limit.mbtiles.json +++ b/tests/join-population/joined-tile-stats-values-limit.mbtiles.json @@ -1,6 +1,6 @@ { "type": "FeatureCollection", "properties": { -"antimeridian_adjusted_bounds": "-122.343750,37.857507,-122.255859,37.926868", -"bounds": "-122.343750,37.857507,-122.255859,37.926868", +"antimeridian_adjusted_bounds": "-135.000000,37.877021,-122.280579,40.979898", +"bounds": "-135.000000,37.877021,-122.280579,40.979898", "center": "-122.299805,37.892187,12", "description": "tests/join-population/tabblock_06001420.mbtiles", "format": "pbf", diff --git a/tests/join-population/joined.mbtiles.json b/tests/join-population/joined.mbtiles.json index 16ad55ee..c1232739 100644 --- a/tests/join-population/joined.mbtiles.json +++ b/tests/join-population/joined.mbtiles.json @@ -1,6 +1,6 @@ { "type": "FeatureCollection", "properties": { -"antimeridian_adjusted_bounds": "-122.343750,37.857507,-122.255859,37.926868", -"bounds": "-122.343750,37.857507,-122.255859,37.926868", +"antimeridian_adjusted_bounds": "-135.000000,37.877021,-122.280579,40.979898", +"bounds": "-135.000000,37.877021,-122.280579,40.979898", "center": "-122.299805,37.892187,12", "description": "tests/join-population/tabblock_06001420.mbtiles", "format": "pbf", diff --git a/tests/join-population/macarthur-6-9-exclude.mbtiles.json b/tests/join-population/macarthur-6-9-exclude.mbtiles.json index 0e191929..3f3f0eea 100644 --- a/tests/join-population/macarthur-6-9-exclude.mbtiles.json +++ b/tests/join-population/macarthur-6-9-exclude.mbtiles.json @@ -1,6 +1,6 @@ { "type": "FeatureCollection", "properties": { -"antimeridian_adjusted_bounds": "-122.343750,37.439974,-121.992188,37.996163", -"bounds": "-122.343750,37.439974,-121.992188,37.996163", +"antimeridian_adjusted_bounds": "-122.294655,37.695231,-122.104111,37.833107", +"bounds": "-122.294655,37.695231,-122.104111,37.833107", "center": "-122.167969,37.833010,9", "description": "tests/join-population/macarthur.mbtiles", "format": "pbf", diff --git a/tests/join-population/macarthur-6-9.mbtiles.json b/tests/join-population/macarthur-6-9.mbtiles.json index 3f79712d..944a79d9 100644 --- a/tests/join-population/macarthur-6-9.mbtiles.json +++ b/tests/join-population/macarthur-6-9.mbtiles.json @@ -1,6 +1,6 @@ { "type": "FeatureCollection", "properties": { -"antimeridian_adjusted_bounds": "-122.343750,37.439974,-121.992188,37.996163", -"bounds": "-122.343750,37.439974,-121.992188,37.996163", +"antimeridian_adjusted_bounds": "-122.294655,37.695231,-122.104111,37.833107", +"bounds": "-122.294655,37.695231,-122.104111,37.833107", "center": "-122.167969,37.833010,9", "description": "tests/join-population/macarthur.mbtiles", "format": "pbf", diff --git a/tests/join-population/merged.mbtiles.json b/tests/join-population/merged.mbtiles.json index e2106996..e7ccaf9c 100644 --- a/tests/join-population/merged.mbtiles.json +++ b/tests/join-population/merged.mbtiles.json @@ -1,6 +1,6 @@ { "type": "FeatureCollection", "properties": { -"antimeridian_adjusted_bounds": "237.656250,37.857507,237.744141,37.926868", -"bounds": "-122.343750,37.695438,-122.104097,37.926868", +"antimeridian_adjusted_bounds": "-135.000000,37.694688,-122.103424,40.979898", +"bounds": "-135.000000,37.694688,-122.103424,40.979898", "center": "-122.299805,37.892187,12", "description": "tests/join-population/tabblock_06001420.mbtiles", "format": "pbf", diff --git a/tile-join.cpp b/tile-join.cpp index cdcabc1e..a6483ac2 100644 --- a/tile-join.cpp +++ b/tile-join.cpp @@ -155,7 +155,29 @@ std::vector> get_joined_rows(sqlite3 *db, const return ret; } -void append_tile(std::string message, int z, unsigned x, unsigned y, std::map &layermap, std::vector &header, std::map> &mapping, sqlite3 *db, std::set &exclude, std::set &include, std::set &keep_layers, std::set &remove_layers, int ifmatched, mvt_tile &outtile, json_object *filter) { +struct arg { + std::map> inputs{}; + std::map outputs{}; + + std::map *layermap = NULL; + + std::vector *header = NULL; + std::map> *mapping = NULL; + sqlite3 *db = NULL; + std::set *exclude = NULL; + std::set *include = NULL; + std::set *keep_layers = NULL; + std::set *remove_layers = NULL; + int ifmatched = 0; + json_object *filter = NULL; + struct tileset_reader *readers = NULL; + + double minlat, minlon; + double maxlat, maxlon; + double minlon2, maxlon2; +}; + +void append_tile(std::string message, int z, unsigned x, unsigned y, std::map &layermap, std::vector &header, std::map> &mapping, sqlite3 *db, std::set &exclude, std::set &include, std::set &keep_layers, std::set &remove_layers, int ifmatched, mvt_tile &outtile, json_object *filter, struct arg *a) { mvt_tile tile; int features_added = 0; bool was_compressed; @@ -247,6 +269,12 @@ void append_tile(std::string message, int z, unsigned x, unsigned y, std::mapsecond.minzoom) { @@ -401,6 +442,38 @@ void append_tile(std::string message, int z, unsigned x, unsigned y, std::mapminlat = std::min(a->minlat, std::min(lat1, lat2)); + a->minlon = std::min(a->minlon, std::min(lon1, lon2)); + a->maxlat = std::max(a->maxlat, std::max(lat1, lat2)); + a->maxlon = std::max(a->maxlon, std::max(lon1, lon2)); + + if (lon1 < 0 || lon2 < 0) { + lon1 += 360; + lon2 += 360; + } + + a->minlon2 = std::min(a->minlon2, std::min(lon1, lon2)); + a->maxlon2 = std::max(a->maxlon2, std::max(lon1, lon2)); + } } if (features_added == 0) { @@ -408,22 +481,6 @@ void append_tile(std::string message, int z, unsigned x, unsigned y, std::map b) { - return a; - } else { - return b; - } -} - struct tilecmp { bool operator()(std::pair const &a, std::pair const &b) { // must match behavior of tileset_reader::operator<() @@ -861,24 +918,6 @@ struct tileset_reader *begin_reading(char *fname) { return r; } -struct arg { - std::map> inputs{}; - std::map outputs{}; - - std::map *layermap = NULL; - - std::vector *header = NULL; - std::map> *mapping = NULL; - sqlite3 *db = NULL; - std::set *exclude = NULL; - std::set *include = NULL; - std::set *keep_layers = NULL; - std::set *remove_layers = NULL; - int ifmatched = 0; - json_object *filter = NULL; - struct tileset_reader *readers = NULL; -}; - void *join_worker(void *v) { arg *a = (arg *) v; @@ -886,7 +925,7 @@ void *join_worker(void *v) { mvt_tile tile; for (size_t i = 0; i < ai->second.size(); i++) { - append_tile(ai->second[i], ai->first.z, ai->first.x, ai->first.y, *(a->layermap), *(a->header), *(a->mapping), a->db, *(a->exclude), *(a->include), *(a->keep_layers), *(a->remove_layers), a->ifmatched, tile, a->filter); + append_tile(ai->second[i], ai->first.z, ai->first.x, ai->first.y, *(a->layermap), *(a->header), *(a->mapping), a->db, *(a->exclude), *(a->include), *(a->keep_layers), *(a->remove_layers), a->ifmatched, tile, a->filter, a); } ai->second.clear(); @@ -921,7 +960,7 @@ void *join_worker(void *v) { return NULL; } -void dispatch_tasks(std::map> &tasks, std::vector> &layermaps, sqlite3 *outdb, const char *outdir, std::vector &header, std::map> &mapping, sqlite3 *db, std::set &exclude, std::set &include, int ifmatched, std::set &keep_layers, std::set &remove_layers, json_object *filter, struct tileset_reader *readers) { +void dispatch_tasks(std::map> &tasks, std::vector> &layermaps, sqlite3 *outdb, const char *outdir, std::vector &header, std::map> &mapping, sqlite3 *db, std::set &exclude, std::set &include, int ifmatched, std::set &keep_layers, std::set &remove_layers, json_object *filter, struct tileset_reader *readers, double *minlat, double *minlon, double *maxlat, double *maxlon, double *minlon2, double *maxlon2) { pthread_t pthreads[CPUS]; std::vector args; @@ -939,6 +978,12 @@ void dispatch_tasks(std::map> &tasks, std::vector< args[i].ifmatched = ifmatched; args[i].filter = filter; args[i].readers = readers; + args[i].minlat = *minlat; + args[i].minlon = *minlon; + args[i].maxlat = *maxlat; + args[i].maxlon = *maxlon; + args[i].minlon2 = *minlon2; + args[i].maxlon2 = *maxlon2; } size_t count = 0; @@ -967,6 +1012,13 @@ void dispatch_tasks(std::map> &tasks, std::vector< for (size_t i = 0; i < CPUS; i++) { void *retval; + *minlat = std::min(*minlat, args[i].minlat); + *minlon = std::min(*minlon, args[i].minlon); + *maxlat = std::max(*maxlat, args[i].maxlat); + *maxlon = std::max(*maxlon, args[i].maxlon); + *minlon2 = std::min(*minlon2, args[i].minlon2); + *maxlon2 = std::max(*maxlon2, args[i].maxlon2); + if (pthread_join(pthreads[i], &retval) != 0) { perror("pthread_join"); } @@ -1085,36 +1137,10 @@ void decode(struct tileset_reader *readers, std::mapall_done()) { std::pair current = readers->current(); - if (current.first.z != zoom_for_bbox) { - // Only use highest zoom for bbox calculation - // to avoid z0 always covering the world - - minlat = minlon = minlon2 = INT_MAX; - maxlat = maxlon = maxlon2 = INT_MIN; - zoom_for_bbox = current.first.z; - } - - double lat1, lon1, lat2, lon2; - tile2lonlat(current.first.x, current.first.y, current.first.z, &lon1, &lat1); - tile2lonlat(current.first.x + 1, current.first.y + 1, current.first.z, &lon2, &lat2); - minlat = min(lat2, minlat); - minlon = min(lon1, minlon); - maxlat = max(lat1, maxlat); - maxlon = max(lon2, maxlon); - - if (lon1 < 0) { - lon1 += 360; - lon2 += 360; - } - - minlon2 = min(lon1, minlon2); - maxlon2 = max(lon2, maxlon2); - if (current.first.z >= minzoom && current.first.z <= maxzoom) { zxy tile = current.first; if (tasks.count(tile) == 0) { @@ -1141,7 +1167,7 @@ void decode(struct tileset_reader *readers, std::mapzoom != current.first.z || readers->x != current.first.x || readers->y != current.first.y) { if (tasks.size() > 100 * CPUS) { - dispatch_tasks(tasks, layermaps, outdb, outdir, header, mapping, db, exclude, include, ifmatched, keep_layers, remove_layers, filter, readers); + dispatch_tasks(tasks, layermaps, outdb, outdir, header, mapping, db, exclude, include, ifmatched, keep_layers, remove_layers, filter, readers, &minlat, &minlon, &maxlat, &maxlon, &minlon2, &maxlon2); tasks.clear(); } } @@ -1160,19 +1186,19 @@ void decode(struct tileset_reader *readers, std::mapminlon = min(minlon, st->minlon); - st->maxlon = max(maxlon, st->maxlon); - st->minlat = min(minlat, st->minlat); - st->maxlat = max(maxlat, st->maxlat); - - st->minlon2 = min(minlon2, st->minlon2); - st->maxlon2 = max(maxlon2, st->maxlon2); - st->minlat2 = min(minlat, st->minlat2); - st->maxlat2 = max(maxlat, st->maxlat2); - - dispatch_tasks(tasks, layermaps, outdb, outdir, header, mapping, db, exclude, include, ifmatched, keep_layers, remove_layers, filter, readers); + dispatch_tasks(tasks, layermaps, outdb, outdir, header, mapping, db, exclude, include, ifmatched, keep_layers, remove_layers, filter, readers, &minlat, &minlon, &maxlat, &maxlon, &minlon2, &maxlon2); layermap = merge_layermaps(layermaps); + st->minlon = std::min(minlon, st->minlon); + st->maxlon = std::max(maxlon, st->maxlon); + st->minlat = std::min(minlat, st->minlat); + st->maxlat = std::max(maxlat, st->maxlat); + + st->minlon2 = std::min(minlon2, st->minlon2); + st->maxlon2 = std::max(maxlon2, st->maxlon2); + st->minlat2 = std::min(minlat, st->minlat2); + st->maxlat2 = std::max(maxlat, st->maxlat2); + struct tileset_reader *next; for (struct tileset_reader *r = readers; r != NULL; r = next) { next = r->next; @@ -1181,14 +1207,14 @@ void decode(struct tileset_reader *readers, std::mapdb, "SELECT value from metadata where name = 'minzoom'", -1, &stmt, NULL) == SQLITE_OK) { if (sqlite3_step(stmt) == SQLITE_ROW) { - int minz = max(sqlite3_column_int(stmt, 0), minzoom); - st->minzoom = min(st->minzoom, minz); + int minz = std::max(sqlite3_column_int(stmt, 0), minzoom); + st->minzoom = std::min(st->minzoom, minz); } sqlite3_finalize(stmt); } if (sqlite3_prepare_v2(r->db, "SELECT value from metadata where name = 'maxzoom'", -1, &stmt, NULL) == SQLITE_OK) { if (sqlite3_step(stmt) == SQLITE_ROW) { - int maxz = min(sqlite3_column_int(stmt, 0), maxzoom); + int maxz = std::min(sqlite3_column_int(stmt, 0), maxzoom); if (!want_overzoom) { if (st->maxzoom >= 0 && maxz != st->maxzoom) { @@ -1196,7 +1222,7 @@ void decode(struct tileset_reader *readers, std::mapmaxzoom = max(st->maxzoom, maxz); + st->maxzoom = std::max(st->maxzoom, maxz); } sqlite3_finalize(stmt); } @@ -1243,20 +1269,6 @@ void decode(struct tileset_reader *readers, std::mapdb, "SELECT value from metadata where name = 'bounds'", -1, &stmt, NULL) == SQLITE_OK) { - if (sqlite3_step(stmt) == SQLITE_ROW) { - const unsigned char *s = sqlite3_column_text(stmt, 0); - if (s != NULL) { - if (sscanf((char *) s, "%lf,%lf,%lf,%lf", &minlon, &minlat, &maxlon, &maxlat) == 4) { - st->minlon = min(minlon, st->minlon); - st->maxlon = max(maxlon, st->maxlon); - st->minlat = min(minlat, st->minlat); - st->maxlat = max(maxlat, st->maxlat); - } - } - } - sqlite3_finalize(stmt); - } if (sqlite3_prepare_v2(r->db, "SELECT value from metadata where name = 'json'", -1, &stmt, NULL) == SQLITE_OK) { if (sqlite3_step(stmt) == SQLITE_ROW) { const unsigned char *s = sqlite3_column_text(stmt, 0);