Skip to content

Commit

Permalink
Enhance .mbtiles output as per #8
Browse files Browse the repository at this point in the history
  • Loading branch information
systemed committed Jul 8, 2015
1 parent fe0bc1f commit 3537baa
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
10 changes: 9 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
"include_ids": false,
"name": "Tilemaker example",
"version": "0.1",
"description": "Sample vector tiles for Tilemaker"
"description": "Sample vector tiles for Tilemaker",
"compress": true,
"metadata": {
"json": { "vector_layers": [
{ "id": "roads", "description": "roads", "fields": {"name":"String","type":"String"}},
{ "id": "water", "description": "water", "fields": {}},
{ "id": "buildings", "description": "buildings", "fields": {}}
] }
}
}
}
3 changes: 2 additions & 1 deletion src/mbtiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class MBTiles { public:
}

void saveTile(int zoom, int x, int y, string *data) {
db << "REPLACE INTO tiles (zoom_level, tile_column, tile_row, tile_data) VALUES (?,?,?,?);" << zoom << x << y && *data;
int tmsY = pow(2,zoom) - 1 - y;
db << "REPLACE INTO tiles (zoom_level, tile_column, tile_row, tile_data) VALUES (?,?,?,?);" << zoom << x << tmsY && *data;
}
};
52 changes: 34 additions & 18 deletions src/tilemaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,17 @@ int main(int argc, char* argv[]) {

uint baseZoom, startZoom, endZoom;
string projectName, projectVersion, projectDesc;
bool includeID = false;
bool includeID = false, compress = true;
rapidjson::Document jsonConfig;
try {
FILE* fp = fopen(jsonFile.c_str(), "r");
char readBuffer[65536];
rapidjson::FileReadStream is(fp, readBuffer, sizeof(readBuffer));
rapidjson::Document document;
document.ParseStream(is);
if (document.HasParseError()) { cerr << "Invalid JSON file." << endl; return -1; }
jsonConfig.ParseStream(is);
if (jsonConfig.HasParseError()) { cerr << "Invalid JSON file." << endl; return -1; }
fclose(fp);

rapidjson::Value& layerHash = document["layers"];
rapidjson::Value& layerHash = jsonConfig["layers"];
for (rapidjson::Value::MemberIterator it = layerHash.MemberBegin(); it != layerHash.MemberEnd(); ++it) {
// work with (*itr)["status"], etc.
string layerName = it->name.GetString();
Expand All @@ -143,13 +143,14 @@ int main(int argc, char* argv[]) {
cout << "Layer " << layerName << " (z" << minZoom << "-" << maxZoom << ")" << endl;
}

baseZoom = document["settings"]["basezoom"].GetUint();
startZoom = document["settings"]["minzoom" ].GetUint();
endZoom = document["settings"]["maxzoom" ].GetUint();
includeID = document["settings"]["include_ids"].GetBool();
projectName = document["settings"]["name"].GetString();
projectVersion = document["settings"]["version"].GetString();
projectDesc = document["settings"]["description"].GetString();
baseZoom = jsonConfig["settings"]["basezoom"].GetUint();
startZoom = jsonConfig["settings"]["minzoom" ].GetUint();
endZoom = jsonConfig["settings"]["maxzoom" ].GetUint();
includeID = jsonConfig["settings"]["include_ids"].GetBool();
compress = jsonConfig["settings"]["compress"].GetBool();
projectName = jsonConfig["settings"]["name"].GetString();
projectVersion = jsonConfig["settings"]["version"].GetString();
projectDesc = jsonConfig["settings"]["description"].GetString();
if (endZoom > baseZoom) { cerr << "maxzoom must be the same or smaller than basezoom." << endl; return -1; }
} catch (...) {
cerr << "Couldn't find expected details in JSON file." << endl;
Expand Down Expand Up @@ -177,6 +178,19 @@ int main(int argc, char* argv[]) {
mbtiles.writeMetadata("version",projectVersion);
mbtiles.writeMetadata("description",projectDesc);
mbtiles.writeMetadata("format","pbf");
if (jsonConfig["settings"].HasMember("metadata")) {
const rapidjson::Value &md = jsonConfig["settings"]["metadata"];
for(rapidjson::Value::ConstMemberIterator it=md.MemberBegin(); it != md.MemberEnd(); ++it) {
if (it->value.IsString()) {
mbtiles.writeMetadata(it->name.GetString(), it->value.GetString());
} else {
rapidjson::StringBuffer strbuf;
rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
it->value.Accept(writer);
mbtiles.writeMetadata(it->name.GetString(), strbuf.GetString());
}
}
}
}

// ---- Read all PBFs
Expand Down Expand Up @@ -472,11 +486,12 @@ int main(int argc, char* argv[]) {

// Write to file or sqlite

string data, compressed;
if (sqlite) {
// Write to sqlite
string data;
tile.SerializeToString(&data);
mbtiles.saveTile(zoom, bbox.tilex, bbox.tiley, &data);
if (compress) { compressed = compress_string(data); }
mbtiles.saveTile(zoom, bbox.tilex, bbox.tiley, compress ? &compressed : &data);

} else {
// Write to file
Expand All @@ -485,13 +500,14 @@ int main(int argc, char* argv[]) {
filename << outputFile << "/" << zoom << "/" << bbox.tilex << "/" << bbox.tiley << ".pbf";
boost::filesystem::create_directories(dirname.str());
fstream outfile(filename.str(), ios::out | ios::trunc | ios::binary);
if (!tile.SerializeToOstream(&outfile)) {
cerr << "Couldn't write to " << filename.str() << endl;
return -1;
if (compress) {
tile.SerializeToString(&data);
outfile << compress_string(data);
} else {
if (!tile.SerializeToOstream(&outfile)) { cerr << "Couldn't write to " << filename.str() << endl; return -1; }
}
outfile.close();
}

}
}

Expand Down

0 comments on commit 3537baa

Please sign in to comment.