[generator] Refactor: osm-file processing decomposition

This commit is contained in:
Anatoly Serdtcev 2019-10-28 18:31:05 +03:00 committed by LaGrunge
parent bc76c3212c
commit 28af652ff7
2 changed files with 51 additions and 28 deletions

View file

@ -15,6 +15,13 @@
#include <utility>
#include <vector>
struct NodeElement
{
uint64_t m_nodeOsmId;
double m_lat;
double m_lon;
};
struct WayElement
{
std::vector<uint64_t> nodes;

View file

@ -50,24 +50,61 @@ uint64_t SourceReader::Read(char * buffer, uint64_t bufferSize)
}
// Functions ---------------------------------------------------------------------------------------
void BuildIntermediateNode(OsmElement const & element, NodeElement & node)
{
auto position = MercatorBounds::FromLatLon(element.m_lat, element.m_lon);
node = {element.m_id, position.y, position.x};
}
bool BuildIntermediateWay(OsmElement const & element, WayElement & way)
{
way.m_wayOsmId = element.m_id;
way.nodes = element.Nodes();
return way.IsValid();
}
bool BuildIntermediateRelation(OsmElement const & element, RelationElement & relation)
{
for (auto const & member : element.Members())
{
switch (member.m_type) {
case OsmElement::EntityType::Node:
relation.nodes.emplace_back(member.m_ref, string(member.m_role));
break;
case OsmElement::EntityType::Way:
relation.ways.emplace_back(member.m_ref, string(member.m_role));
break;
case OsmElement::EntityType::Relation:
// we just ignore type == "relation"
break;
default:
break;
}
}
for (auto const & tag : element.Tags())
relation.tags.emplace(tag.m_key, tag.m_value);
return relation.IsValid();
}
void AddElementToCache(cache::IntermediateDataWriter & cache, OsmElement & element)
{
switch (element.m_type)
{
case OsmElement::EntityType::Node:
{
auto const pt = MercatorBounds::FromLatLon(element.m_lat, element.m_lon);
cache.AddNode(element.m_id, pt.y, pt.x);
NodeElement node;
BuildIntermediateNode(element, node);
cache.AddNode(node.m_nodeOsmId, node.m_lat, node.m_lon);
break;
}
case OsmElement::EntityType::Way:
{
// Store way.
WayElement way(element.m_id);
for (uint64_t nd : element.Nodes())
way.nodes.push_back(nd);
if (way.IsValid())
if (BuildIntermediateWay(element, way))
cache.AddWay(element.m_id, way);
break;
}
@ -75,29 +112,8 @@ void AddElementToCache(cache::IntermediateDataWriter & cache, OsmElement & eleme
{
// store relation
RelationElement relation;
for (auto const & member : element.Members())
{
switch (member.m_type) {
case OsmElement::EntityType::Node:
relation.nodes.emplace_back(member.m_ref, string(member.m_role));
break;
case OsmElement::EntityType::Way:
relation.ways.emplace_back(member.m_ref, string(member.m_role));
break;
case OsmElement::EntityType::Relation:
// we just ignore type == "relation"
break;
default:
break;
}
}
for (auto const & tag : element.Tags())
relation.tags.emplace(tag.m_key, tag.m_value);
if (relation.IsValid())
if (BuildIntermediateRelation(element, relation))
cache.AddRelation(element.m_id, relation);
break;
}
default: