Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/mbtiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class MBTiles {

void openForReading(std::string &filename);
void readBoundingBox(double &minLon, double &maxLon, double &minLat, double &maxLat);
bool readMetadata(const std::string &key, std::string &value);
void readTileList(std::vector<std::tuple<int,int,int>> &tileList);
std::vector<char> readTile(int zoom, int col, int row);
bool readTileAndUncompress(std::string &data, int zoom, int col, int row, bool isCompressed, bool asGzip);
Expand Down
9 changes: 9 additions & 0 deletions src/mbtiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ void MBTiles::readBoundingBox(double &minLon, double &maxLon, double &minLat, do
maxLon = stod(b[2]); maxLat = stod(b[3]);
}

bool MBTiles::readMetadata(const string &key, string &value) {
bool found = false;
db << "SELECT value FROM metadata WHERE name=?" << key >> [&](string v) {
value = v;
found = true;
};
return found;
}

void MBTiles::readTileList(std::vector<std::tuple<int,int,int>> &tileList) {
db << "SELECT zoom_level,tile_column,tile_row FROM tiles" >> [&](int z,int col, int row) {
tileList.emplace_back(std::make_tuple(z,col,row));
Expand Down
53 changes: 50 additions & 3 deletions src/shared_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ void SharedData::writeMBTilesProjectData() {
mbtiles.writeMetadata("version", config.projectVersion);
mbtiles.writeMetadata("description", config.projectDesc);
mbtiles.writeMetadata("format", config.tileFormat == TileFormat::MLT ? "mlt" : "pbf");
mbtiles.writeMetadata("minzoom", to_string(config.startZoom));
mbtiles.writeMetadata("maxzoom", to_string(config.endZoom));
uint minzoom = config.startZoom, maxzoom = config.endZoom;
if (mergeSqlite) {
string existing;
if (mbtiles.readMetadata("minzoom", existing)) minzoom = std::min(minzoom, static_cast<uint>(stoul(existing)));
if (mbtiles.readMetadata("maxzoom", existing)) maxzoom = std::max(maxzoom, static_cast<uint>(stoul(existing)));
}
mbtiles.writeMetadata("minzoom", to_string(minzoom));
mbtiles.writeMetadata("maxzoom", to_string(maxzoom));

ostringstream bounds;
if (mergeSqlite) {
Expand All @@ -47,9 +53,50 @@ void SharedData::writeMBTilesProjectData() {
}
}

// Add the vector_layers of an existing .mbtiles json object to the new one, for --merge:
// zoom ranges are widened, missing fields and layers are added
static string mergeVectorLayers(const string &existingJson, const string &newJson) {
Document existing, merged;
existing.Parse(existingJson.c_str());
merged.Parse(newJson.c_str());
if (existing.HasParseError() || !existing.IsObject() || !existing.HasMember("vector_layers") || !existing["vector_layers"].IsArray()) return newJson;
Document::AllocatorType &allocator = merged.GetAllocator();
Value &newLayers = merged["vector_layers"];

for (auto &oldLayer : existing["vector_layers"].GetArray()) {
if (!oldLayer.IsObject() || !oldLayer.HasMember("id") || !oldLayer["id"].IsString()) continue;
Value *match = nullptr;
for (auto &newLayer : newLayers.GetArray()) {
if (newLayer["id"] == oldLayer["id"]) { match = &newLayer; break; }
}
if (!match) {
newLayers.PushBack(Value(oldLayer, allocator), allocator);
continue;
}
if (oldLayer.HasMember("minzoom") && oldLayer["minzoom"].IsUint())
(*match)["minzoom"] = std::min((*match)["minzoom"].GetUint(), oldLayer["minzoom"].GetUint());
if (oldLayer.HasMember("maxzoom") && oldLayer["maxzoom"].IsUint())
(*match)["maxzoom"] = std::max((*match)["maxzoom"].GetUint(), oldLayer["maxzoom"].GetUint());
if (oldLayer.HasMember("fields") && oldLayer["fields"].IsObject()) {
Value &fields = (*match)["fields"];
for (auto &field : oldLayer["fields"].GetObject()) {
if (!fields.HasMember(field.name)) fields.AddMember(Value(field.name, allocator), Value(field.value, allocator), allocator);
}
}
}

StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
merged.Accept(writer);
return string(buffer.GetString(), buffer.GetSize());
}

void SharedData::writeMBTilesMetadata(rapidjson::Document const &jsonConfig) {
// Write mbtiles 1.3+ json object
mbtiles.writeMetadata("json", layers.serialiseToJSON());
string json = layers.serialiseToJSON();
string existingJson;
if (mergeSqlite && mbtiles.readMetadata("json", existingJson)) json = mergeVectorLayers(existingJson, json);
mbtiles.writeMetadata("json", json);

// Write user-defined metadata
if (jsonConfig["settings"].HasMember("metadata")) {
Expand Down
Loading