Skip to content

Commit

Permalink
Keep output list as OutputObjects for longer
Browse files Browse the repository at this point in the history
  • Loading branch information
systemed committed Sep 19, 2023
1 parent 22a3e64 commit 2249202
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 52 deletions.
9 changes: 6 additions & 3 deletions include/osm_lua_processing.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,16 @@ class OsmLuaProcessing {
const class Config &config;
class LayerDefinition &layers;

OutputRefsWithAttributes outputs; // All output objects that have been created
OutputObjectsWithAttributes outputs; // All output objects that have been created
boost::container::flat_map<std::string, std::string> currentTags;

void AddAttributesToOutputObjects() {
std::vector<OutputObjectRef> OutputsAsOORefs() {
std::vector<OutputObjectRef> list;
for (auto jt = this->outputs.begin(); jt != this->outputs.end(); ++jt) {
jt->first->setAttributeSet(attributeStore.add(jt->second));
jt->first.setAttributeSet(attributeStore.add(jt->second));
list.emplace_back(osmMemTiles.CreateObject(jt->first));
}
return list;
}
};

Expand Down
2 changes: 1 addition & 1 deletion include/output_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class OutputObjectRef
void reset() { oo = nullptr; }
};

typedef std::deque<std::pair<OutputObjectRef, AttributeSet>> OutputRefsWithAttributes;
typedef std::deque<std::pair<OutputObject, AttributeSet>> OutputObjectsWithAttributes;

// Comparison functions

Expand Down
6 changes: 3 additions & 3 deletions include/tile_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ class TileDataSource {
return &objects.back();
}

void AddGeometryToIndex(Linestring const &geom, OutputRefsWithAttributes const &outputs);
void AddGeometryToIndex(MultiLinestring const &geom, OutputRefsWithAttributes const &outputs);
void AddGeometryToIndex(MultiPolygon const &geom, OutputRefsWithAttributes const &outputs);
void AddGeometryToIndex(Linestring const &geom, std::vector<OutputObjectRef> const &outputs);
void AddGeometryToIndex(MultiLinestring const &geom, std::vector<OutputObjectRef> const &outputs);
void AddGeometryToIndex(MultiPolygon const &geom, std::vector<OutputObjectRef> const &outputs);

void AddObjectToTileIndex(TileCoordinates const &index, OutputObjectRef const &oo) {
std::lock_guard<std::mutex> lock(mutex);
Expand Down
55 changes: 23 additions & 32 deletions src/osm_lua_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,8 @@ void OsmLuaProcessing::Layer(const string &layerName, bool area) {
if(!CorrectGeometry(p)) return;

NodeID id = osmMemTiles.store_point(p);
OutputObjectRef oo = osmMemTiles.CreateObject(OutputObjectPoint(geomType,
layers.layerMap[layerName], id, 0, layerMinZoom));
outputs.push_back(std::make_pair(oo, attributes));
OutputObject oo = OutputObjectPoint(geomType, layers.layerMap[layerName], id, 0, layerMinZoom);
outputs.push_back(std::make_pair(std::move(oo), attributes));
return;
}
else if (geomType==POLYGON_) {
Expand Down Expand Up @@ -362,9 +361,8 @@ void OsmLuaProcessing::Layer(const string &layerName, bool area) {
if(!CorrectGeometry(mp)) return;

NodeID id = osmMemTiles.store_multi_polygon(mp);
OutputObjectRef oo = osmMemTiles.CreateObject(OutputObjectMultiPolygon(geomType,
layers.layerMap[layerName], id, 0, layerMinZoom));
outputs.push_back(std::make_pair(oo, attributes));
OutputObject oo = OutputObjectMultiPolygon(geomType, layers.layerMap[layerName], id, 0, layerMinZoom);
outputs.push_back(std::make_pair(std::move(oo), attributes));
}
else if (geomType==MULTILINESTRING_) {
// multilinestring
Expand All @@ -378,9 +376,8 @@ void OsmLuaProcessing::Layer(const string &layerName, bool area) {
if (!CorrectGeometry(mls)) return;

NodeID id = osmMemTiles.store_multi_linestring(mls);
OutputObjectRef oo = osmMemTiles.CreateObject(OutputObjectMultiLinestring(geomType,
layers.layerMap[layerName], id, 0, layerMinZoom));
outputs.push_back(std::make_pair(oo, attributes));
OutputObject oo = OutputObjectMultiLinestring(geomType, layers.layerMap[layerName], id, 0, layerMinZoom);
outputs.push_back(std::make_pair(std::move(oo), attributes));
}
else if (geomType==LINESTRING_) {
// linestring
Expand All @@ -389,9 +386,8 @@ void OsmLuaProcessing::Layer(const string &layerName, bool area) {
if(!CorrectGeometry(ls)) return;

NodeID id = osmMemTiles.store_linestring(ls);
OutputObjectRef oo = osmMemTiles.CreateObject(OutputObjectLinestring(geomType,
layers.layerMap[layerName], id, 0, layerMinZoom));
outputs.push_back(std::make_pair(oo, attributes));
OutputObject oo = OutputObjectLinestring(geomType, layers.layerMap[layerName], id, 0, layerMinZoom);
outputs.push_back(std::make_pair(std::move(oo), attributes));
}
} catch (std::invalid_argument &err) {
cerr << "Error in OutputObject constructor: " << err.what() << endl;
Expand Down Expand Up @@ -425,9 +421,8 @@ void OsmLuaProcessing::LayerAsCentroid(const string &layerName) {
}

NodeID id = osmMemTiles.store_point(geomp);
OutputObjectRef oo = osmMemTiles.CreateObject(OutputObjectPoint(POINT_,
layers.layerMap[layerName], id, 0, layerMinZoom));
outputs.push_back(std::make_pair(oo, attributes));
OutputObject oo = OutputObjectPoint(POINT_, layers.layerMap[layerName], id, 0, layerMinZoom);
outputs.push_back(std::make_pair(std::move(oo), attributes));
}

Point OsmLuaProcessing::calculateCentroid() {
Expand Down Expand Up @@ -466,7 +461,7 @@ void OsmLuaProcessing::AttributeWithMinZoom(const string &key, const string &val
vector_tile::Tile_Value v;
v.set_string_value(val);
outputs.back().second.add(key, v, minzoom);
setVectorLayerMetadata(outputs.back().first->layer, key, 0);
setVectorLayerMetadata(outputs.back().first.layer, key, 0);
}

void OsmLuaProcessing::AttributeNumeric(const string &key, const float val) { AttributeNumericWithMinZoom(key,val,0); }
Expand All @@ -475,7 +470,7 @@ void OsmLuaProcessing::AttributeNumericWithMinZoom(const string &key, const floa
vector_tile::Tile_Value v;
v.set_float_value(val);
outputs.back().second.add(key, v, minzoom);
setVectorLayerMetadata(outputs.back().first->layer, key, 1);
setVectorLayerMetadata(outputs.back().first.layer, key, 1);
}

void OsmLuaProcessing::AttributeBoolean(const string &key, const bool val) { AttributeBooleanWithMinZoom(key,val,0); }
Expand All @@ -484,7 +479,7 @@ void OsmLuaProcessing::AttributeBooleanWithMinZoom(const string &key, const bool
vector_tile::Tile_Value v;
v.set_bool_value(val);
outputs.back().second.add(key, v, minzoom);
setVectorLayerMetadata(outputs.back().first->layer, key, 2);
setVectorLayerMetadata(outputs.back().first.layer, key, 2);
}

template<typename T>
Expand All @@ -497,26 +492,26 @@ static inline T make_valid(double v)
// Set minimum zoom
void OsmLuaProcessing::MinZoom(const double z) {
if (outputs.size()==0) { ProcessingError("Can't set minimum zoom if no Layer set"); return; }
outputs.back().first->setMinZoom(make_valid<unsigned int>(z));
outputs.back().first.setMinZoom(make_valid<unsigned int>(z));
}

// Set z_order
void OsmLuaProcessing::ZOrder(const double z) {
if (outputs.size()==0) { ProcessingError("Can't set z_order if no Layer set"); return; }
#ifdef FLOAT_Z_ORDER
outputs.back().first->setZOrder(make_valid<float>(z));
outputs.back().first.setZOrder(make_valid<float>(z));
#else
outputs.back().first->setZOrder(make_valid<int>(z));
outputs.back().first.setZOrder(make_valid<int>(z));
#endif
}

// Set z_order (variant with scaling)
void OsmLuaProcessing::ZOrderWithScale(const double z, const double scale) {
if (outputs.size()==0) { ProcessingError("Can't set z_order if no Layer set"); return; }
#ifdef FLOAT_Z_ORDER
outputs.back().first->setZOrder(make_valid<float>(z));
outputs.back().first.setZOrder(make_valid<float>(z));
#else
outputs.back().first->setZOrder(make_valid<int>(z/scale*127));
outputs.back().first.setZOrder(make_valid<int>(z/scale*127));
#endif
}

Expand Down Expand Up @@ -577,9 +572,8 @@ void OsmLuaProcessing::setNode(NodeID id, LatpLon node, const tag_map_t &tags) {
if (!this->empty()) {
TileCoordinates index = latpLon2index(node, this->config.baseZoom);

AddAttributesToOutputObjects();
for (auto &output : outputs) {
osmMemTiles.AddObjectToTileIndex(index, output.first);
for (auto &output : OutputsAsOORefs()) {
osmMemTiles.AddObjectToTileIndex(index, output);
}
}
}
Expand Down Expand Up @@ -626,8 +620,7 @@ void OsmLuaProcessing::setWay(WayID wayId, LatpLonVec const &llVec, const tag_ma
}

if (!this->empty()) {
AddAttributesToOutputObjects();
osmMemTiles.AddGeometryToIndex(linestringCached(), outputs);
osmMemTiles.AddGeometryToIndex(linestringCached(), OutputsAsOORefs());
}
}

Expand Down Expand Up @@ -656,13 +649,11 @@ void OsmLuaProcessing::setRelation(int64_t relationId, WayVec const &outerWayVec
}
if (this->empty()) return;

AddAttributesToOutputObjects();

try {
if (isClosed) {
osmMemTiles.AddGeometryToIndex(multiPolygonCached(), outputs);
osmMemTiles.AddGeometryToIndex(multiPolygonCached(), OutputsAsOORefs());
} else {
osmMemTiles.AddGeometryToIndex(multiLinestringCached(), outputs);
osmMemTiles.AddGeometryToIndex(multiLinestringCached(), OutputsAsOORefs());
}
} catch(std::out_of_range &err) {
cout << "In relation " << originalOsmID << ": " << err.what() << endl;
Expand Down
4 changes: 2 additions & 2 deletions src/shp_mem_tiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ OutputObjectRef ShpMemTiles::StoreShapefileGeometry(uint_least8_t layerNum,
oo = CreateObject(OutputObjectLinestring(geomType, layerNum, oid, attrIdx, minzoom));
if (isIndexed) indexedGeometries.push_back(oo);

OutputRefsWithAttributes oolist { std::make_pair(oo, AttributeSet()) };
std::vector<OutputObjectRef> oolist { oo };
AddGeometryToIndex(boost::get<Linestring>(geometry), oolist);

} break;
Expand All @@ -102,7 +102,7 @@ OutputObjectRef ShpMemTiles::StoreShapefileGeometry(uint_least8_t layerNum,
oo = CreateObject(OutputObjectMultiPolygon(geomType, layerNum, oid, attrIdx, minzoom));
if (isIndexed) indexedGeometries.push_back(oo);

OutputRefsWithAttributes oolist { std::make_pair(oo, AttributeSet()) };
std::vector<OutputObjectRef> oolist { oo };
AddGeometryToIndex(boost::get<MultiPolygon>(geometry), oolist);
} break;

Expand Down
22 changes: 11 additions & 11 deletions src/tile_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ OutputObjectsConstItPair GetObjectsAtSubLayer(std::vector<OutputObjectRef> const
// ------------------------------------
// Add geometries to tile/large indices

void TileDataSource::AddGeometryToIndex(Linestring const &geom, OutputRefsWithAttributes const &outputs) {
void TileDataSource::AddGeometryToIndex(Linestring const &geom, std::vector<OutputObjectRef> const &outputs) {
unordered_set<TileCoordinates> tileSet;
try {
insertIntermediateTiles(geom, baseZoom, tileSet);
Expand All @@ -288,11 +288,11 @@ void TileDataSource::AddGeometryToIndex(Linestring const &geom, OutputRefsWithAt
maxTileX = std::max(index.x, maxTileX);
maxTileY = std::max(index.y, maxTileY);
for (auto &output : outputs) {
if (output.first->geomType == POLYGON_) {
if (output->geomType == POLYGON_) {
polygonExists = true;
continue;
}
AddObjectToTileIndex(index, output.first); // not a polygon
AddObjectToTileIndex(index, output); // not a polygon
}
}

Expand All @@ -301,18 +301,18 @@ void TileDataSource::AddGeometryToIndex(Linestring const &geom, OutputRefsWithAt
bool tilesetFilled = false;
uint size = (maxTileX - minTileX + 1) * (maxTileY - minTileY + 1);
for (auto &output : outputs) {
if (output.first->geomType != POLYGON_) continue;
if (output->geomType != POLYGON_) continue;
if (size>= 16) {
// Larger objects - add to rtree
Box box = Box(geom::make<Point>(minTileX, minTileY),
geom::make<Point>(maxTileX, maxTileY));
AddObjectToLargeIndex(box, output.first);
AddObjectToLargeIndex(box, output);
} else {
// Smaller objects - add to each individual tile index
if (!tilesetFilled) { fillCoveredTiles(tileSet); tilesetFilled = true; }
for (auto it = tileSet.begin(); it != tileSet.end(); ++it) {
TileCoordinates index = *it;
AddObjectToTileIndex(index, output.first);
AddObjectToTileIndex(index, output);
}
}
}
Expand All @@ -322,20 +322,20 @@ void TileDataSource::AddGeometryToIndex(Linestring const &geom, OutputRefsWithAt
}
}

void TileDataSource::AddGeometryToIndex(MultiLinestring const &geom, OutputRefsWithAttributes const &outputs) {
void TileDataSource::AddGeometryToIndex(MultiLinestring const &geom, std::vector<OutputObjectRef> const &outputs) {
for (Linestring ls : geom) {
unordered_set<TileCoordinates> tileSet;
insertIntermediateTiles(ls, baseZoom, tileSet);
for (auto it = tileSet.begin(); it != tileSet.end(); ++it) {
TileCoordinates index = *it;
for (auto &output : outputs) {
AddObjectToTileIndex(index, output.first);
AddObjectToTileIndex(index, output);
}
}
}
}

void TileDataSource::AddGeometryToIndex(MultiPolygon const &geom, OutputRefsWithAttributes const &outputs) {
void TileDataSource::AddGeometryToIndex(MultiPolygon const &geom, std::vector<OutputObjectRef> const &outputs) {
unordered_set<TileCoordinates> tileSet;
bool singleOuter = geom.size()==1;
for (Polygon poly : geom) {
Expand Down Expand Up @@ -364,12 +364,12 @@ void TileDataSource::AddGeometryToIndex(MultiPolygon const &geom, OutputRefsWith
// which is suboptimal in shapes like (_) ...... (_) where the outers are significantly disjoint
Box box = Box(geom::make<Point>(minTileX, minTileY),
geom::make<Point>(maxTileX, maxTileY));
AddObjectToLargeIndex(box, output.first);
AddObjectToLargeIndex(box, output);
} else {
// Smaller objects - add to each individual tile index
for (auto it = tileSet.begin(); it != tileSet.end(); ++it) {
TileCoordinates index = *it;
AddObjectToTileIndex(index, output.first);
AddObjectToTileIndex(index, output);
}
}
}
Expand Down

0 comments on commit 2249202

Please sign in to comment.