Skip to content

Commit 3537baa

Browse files
committed
Enhance .mbtiles output as per #8
1 parent fe0bc1f commit 3537baa

File tree

3 files changed

+45
-20
lines changed

3 files changed

+45
-20
lines changed

config.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
"include_ids": false,
1313
"name": "Tilemaker example",
1414
"version": "0.1",
15-
"description": "Sample vector tiles for Tilemaker"
15+
"description": "Sample vector tiles for Tilemaker",
16+
"compress": true,
17+
"metadata": {
18+
"json": { "vector_layers": [
19+
{ "id": "roads", "description": "roads", "fields": {"name":"String","type":"String"}},
20+
{ "id": "water", "description": "water", "fields": {}},
21+
{ "id": "buildings", "description": "buildings", "fields": {}}
22+
] }
23+
}
1624
}
1725
}

src/mbtiles.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class MBTiles { public:
1919
}
2020

2121
void saveTile(int zoom, int x, int y, string *data) {
22-
db << "REPLACE INTO tiles (zoom_level, tile_column, tile_row, tile_data) VALUES (?,?,?,?);" << zoom << x << y && *data;
22+
int tmsY = pow(2,zoom) - 1 - y;
23+
db << "REPLACE INTO tiles (zoom_level, tile_column, tile_row, tile_data) VALUES (?,?,?,?);" << zoom << x << tmsY && *data;
2324
}
2425
};

src/tilemaker.cpp

+34-18
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,17 @@ int main(int argc, char* argv[]) {
123123

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

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

146-
baseZoom = document["settings"]["basezoom"].GetUint();
147-
startZoom = document["settings"]["minzoom" ].GetUint();
148-
endZoom = document["settings"]["maxzoom" ].GetUint();
149-
includeID = document["settings"]["include_ids"].GetBool();
150-
projectName = document["settings"]["name"].GetString();
151-
projectVersion = document["settings"]["version"].GetString();
152-
projectDesc = document["settings"]["description"].GetString();
146+
baseZoom = jsonConfig["settings"]["basezoom"].GetUint();
147+
startZoom = jsonConfig["settings"]["minzoom" ].GetUint();
148+
endZoom = jsonConfig["settings"]["maxzoom" ].GetUint();
149+
includeID = jsonConfig["settings"]["include_ids"].GetBool();
150+
compress = jsonConfig["settings"]["compress"].GetBool();
151+
projectName = jsonConfig["settings"]["name"].GetString();
152+
projectVersion = jsonConfig["settings"]["version"].GetString();
153+
projectDesc = jsonConfig["settings"]["description"].GetString();
153154
if (endZoom > baseZoom) { cerr << "maxzoom must be the same or smaller than basezoom." << endl; return -1; }
154155
} catch (...) {
155156
cerr << "Couldn't find expected details in JSON file." << endl;
@@ -177,6 +178,19 @@ int main(int argc, char* argv[]) {
177178
mbtiles.writeMetadata("version",projectVersion);
178179
mbtiles.writeMetadata("description",projectDesc);
179180
mbtiles.writeMetadata("format","pbf");
181+
if (jsonConfig["settings"].HasMember("metadata")) {
182+
const rapidjson::Value &md = jsonConfig["settings"]["metadata"];
183+
for(rapidjson::Value::ConstMemberIterator it=md.MemberBegin(); it != md.MemberEnd(); ++it) {
184+
if (it->value.IsString()) {
185+
mbtiles.writeMetadata(it->name.GetString(), it->value.GetString());
186+
} else {
187+
rapidjson::StringBuffer strbuf;
188+
rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
189+
it->value.Accept(writer);
190+
mbtiles.writeMetadata(it->name.GetString(), strbuf.GetString());
191+
}
192+
}
193+
}
180194
}
181195

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

473487
// Write to file or sqlite
474488

489+
string data, compressed;
475490
if (sqlite) {
476491
// Write to sqlite
477-
string data;
478492
tile.SerializeToString(&data);
479-
mbtiles.saveTile(zoom, bbox.tilex, bbox.tiley, &data);
493+
if (compress) { compressed = compress_string(data); }
494+
mbtiles.saveTile(zoom, bbox.tilex, bbox.tiley, compress ? &compressed : &data);
480495

481496
} else {
482497
// Write to file
@@ -485,13 +500,14 @@ int main(int argc, char* argv[]) {
485500
filename << outputFile << "/" << zoom << "/" << bbox.tilex << "/" << bbox.tiley << ".pbf";
486501
boost::filesystem::create_directories(dirname.str());
487502
fstream outfile(filename.str(), ios::out | ios::trunc | ios::binary);
488-
if (!tile.SerializeToOstream(&outfile)) {
489-
cerr << "Couldn't write to " << filename.str() << endl;
490-
return -1;
503+
if (compress) {
504+
tile.SerializeToString(&data);
505+
outfile << compress_string(data);
506+
} else {
507+
if (!tile.SerializeToOstream(&outfile)) { cerr << "Couldn't write to " << filename.str() << endl; return -1; }
491508
}
492509
outfile.close();
493510
}
494-
495511
}
496512
}
497513

0 commit comments

Comments
 (0)