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
17 changes: 14 additions & 3 deletions src/mbtiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ MBTiles::MBTiles():
{}

MBTiles::~MBTiles() {
if (db && inTransaction) db << "COMMIT;"; // commit all the changes if open
// commit all the changes if open (closeForWriting normally does this, and reports errors)
if (db && inTransaction) {
try {
db << "COMMIT;";
} catch (const std::exception &e) {
cerr << "Couldn't commit .mbtiles: " << e.what() << endl;
}
}
}

// ---- Write .mbtiles
Expand Down Expand Up @@ -81,10 +88,10 @@ void MBTiles::flushPendingStatements() {

void MBTiles::saveTile(int zoom, int x, int y, string *data, bool isMerge) {
// If the lock is available, write directly to SQLite.
if (m.try_lock()) {
std::unique_lock<std::mutex> dbLock(m, std::try_to_lock);
if (dbLock.owns_lock()) {
insertOrReplace(zoom, x, y, *data, isMerge);
flushPendingStatements();
m.unlock();
} else {
// Else buffer the write for later, copying its binary blob.
const std::lock_guard<std::mutex> lock(pendingStatementsMutex);
Expand All @@ -96,6 +103,10 @@ void MBTiles::closeForWriting() {
flushPendingStatements();
preparedStatements[0].used(true);
preparedStatements[1].used(true);
if (inTransaction) {
inTransaction = false;
db << "COMMIT;";
}
}

// ---- Read mbtiles
Expand Down
14 changes: 11 additions & 3 deletions src/tile_worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,12 @@ void ProcessObjects(
// Append successive linestrings, then reorder afterwards
while (jt<(ooSameLayerEnd-1) && oo.oo.compatible((jt+1)->oo)) {
jt++;
MultiLinestring to_merge = boost::get<MultiLinestring>(source->buildWayGeometry(jt->oo.geomType, jt->oo.objectID, bbox));
for (auto &ls : to_merge) boost::get<MultiLinestring>(g).emplace_back(ls);
try {
MultiLinestring to_merge = boost::get<MultiLinestring>(source->buildWayGeometry(jt->oo.geomType, jt->oo.objectID, bbox));
for (auto &ls : to_merge) boost::get<MultiLinestring>(g).emplace_back(ls);
} catch (std::out_of_range &err) {
if (verbose) cerr << "Error while processing geometry " << jt->oo.geomType << "," << static_cast<int>(jt->oo.objectID) <<"," << err.what() << endl;
}
}
MultiLinestring reordered;
ReorderMultiLinestring(boost::get<MultiLinestring>(g), reordered);
Expand All @@ -438,7 +442,11 @@ void ProcessObjects(
std::vector<MultiPolygon> mps;
while (jt<(ooSameLayerEnd-1) && oo.oo.compatible((jt+1)->oo)) {
jt++;
mps.emplace_back( boost::get<MultiPolygon>(source->buildWayGeometry(jt->oo.geomType, jt->oo.objectID, bbox)) );
try {
mps.emplace_back( boost::get<MultiPolygon>(source->buildWayGeometry(jt->oo.geomType, jt->oo.objectID, bbox)) );
} catch (std::out_of_range &err) {
if (verbose) cerr << "Error while processing geometry " << jt->oo.geomType << "," << static_cast<int>(jt->oo.objectID) <<"," << err.what() << endl;
}
}
if (!mps.empty()) {
mps.emplace_back(boost::get<MultiPolygon>(g));
Expand Down
57 changes: 43 additions & 14 deletions src/tilemaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,17 @@ int main(const int argc, const char* argv[]) {
sortTileCoordinates(config.baseZoom, options.threadNum, tileCoordinates);

std::atomic<uint64_t> batchCounter(0);
// First error from a tile-writing worker; reported after pool.join()
// (an exception escaping a pool thread would call std::terminate)
std::atomic<bool> tileFailed(false);
std::string tileError;
std::mutex tileErrorMutex;
auto recordTileError = [&](unsigned int zoom, TileCoordinates coords, const std::string& what) {
const std::lock_guard<std::mutex> lock(tileErrorMutex);
if (tileFailed) return;
tileError = "z" + std::to_string(zoom) + "/" + std::to_string(coords.x) + "/" + std::to_string(coords.y) + ": " + what;
tileFailed = true;
};
const uint cutZoom = std::max<uint>(12, config.startZoom);
std::size_t batchSize = 0;
for(std::size_t startIndex = 0; startIndex < tileCoordinates.size(); startIndex += batchSize) {
Expand Down Expand Up @@ -530,13 +541,14 @@ int main(const int argc, const char* argv[]) {
batchSize++;
}

boost::asio::post(pool, [=, &tileCoordinates, &pool, &sharedData, &sources, &attributeStore, &io_mutex, &tilesWritten, &lastTilesWritten, &batchCounter]() {
boost::asio::post(pool, [=, &tileCoordinates, &pool, &sharedData, &sources, &attributeStore, &io_mutex, &tilesWritten, &lastTilesWritten, &batchCounter, &tileFailed, &recordTileError]() {
clipCacheBatchId = ++batchCounter;
// A single-tile batch has no descendants to reuse its clips.
clipCacheNoStore = batchSize == 1;
std::vector<std::string> tileTimings;
std::size_t endIndex = std::min(tileCoordinates.size(), startIndex + batchSize);
for(std::size_t i = startIndex; i < endIndex; ++i) {
if (tileFailed) return;
unsigned int zoom = tileCoordinates[i].first;
TileCoordinates coords = tileCoordinates[i].second;

Expand All @@ -546,11 +558,19 @@ int main(const int argc, const char* argv[]) {
clock_gettime(CLOCK_MONOTONIC, &start);
#endif

std::vector<std::vector<OutputObjectID>> data;
for (auto source : sources) {
data.emplace_back(source->getObjectsForTile(sortOrders, zoom, coords));
try {
std::vector<std::vector<OutputObjectID>> data;
for (auto source : sources) {
data.emplace_back(source->getObjectsForTile(sortOrders, zoom, coords));
}
outputProc(sharedData, sources, attributeStore, data, coords, zoom);
} catch (const std::exception& e) {
recordTileError(zoom, coords, e.what());
return;
} catch (...) {
recordTileError(zoom, coords, "unknown exception");
return;
}
outputProc(sharedData, sources, attributeStore, data, coords, zoom);

#ifdef CLOCK_MONOTONIC
if (options.logTileTimings) {
Expand Down Expand Up @@ -593,18 +613,27 @@ int main(const int argc, const char* argv[]) {
}
// Wait for all tasks in the pool to complete.
pool.join();
if (tileFailed) {
cerr << endl << "Error writing tile " << tileError << endl;
return -1;
}

// ---- Close tileset

if (options.outputMode == OptionsParser::OutputMode::MBTiles) {
sharedData.writeMBTilesMetadata(jsonConfig);
sharedData.mbtiles.closeForWriting();
} else if (options.outputMode == OptionsParser::OutputMode::PMTiles) {
sharedData.writePMTilesBounds();
std::string metadata = sharedData.pmTilesMetadata(jsonConfig);
sharedData.pmtiles.close(metadata);
} else {
sharedData.writeFileMetadata(jsonConfig);
try {
if (options.outputMode == OptionsParser::OutputMode::MBTiles) {
sharedData.writeMBTilesMetadata(jsonConfig);
sharedData.mbtiles.closeForWriting();
} else if (options.outputMode == OptionsParser::OutputMode::PMTiles) {
sharedData.writePMTilesBounds();
std::string metadata = sharedData.pmTilesMetadata(jsonConfig);
sharedData.pmtiles.close(metadata);
} else {
sharedData.writeFileMetadata(jsonConfig);
}
} catch (const std::exception& e) {
cerr << endl << "Error writing " << sharedData.outputFile << ": " << e.what() << endl;
return -1;
}

#ifndef _MSC_VER
Expand Down
Loading