chore: fixes and code cleanup

This commit is contained in:
Vlad Vesa 2020-08-19 09:17:34 +03:00
parent c96bd24cca
commit f1aa563d41
3 changed files with 1955 additions and 1933 deletions

View file

@ -1,12 +1,12 @@
// Copyright 2018, University of Freiburg,
// Chair of Algorithms and Data Structures.
// Authors: Patrick Brosi <brosi@informatik.uni-freiburg.de>
#include <climits>
#include <pwd.h>
#include <csignal>
#include <cstdio>
#include <sys/stat.h>
#include <unistd.h>
#include <climits>
#include <csignal>
#include <cstdio>
#include <fstream>
#include <map>
#include <string>
@ -163,14 +163,12 @@ int main(int argc, char** argv) {
LOG(INFO) << "Writing filtered XML to " << cfg.writeOsm << " ...";
BBoxIdx box(BOX_PADDING);
for (size_t i = 0; i < cfg.feedPaths.size(); i++) {
ShapeBuilder::getGtfsBox(&gtfs[i], cmdCfgMots, cfg.shapeTripId, true,
&box);
ShapeBuilder::getGtfsBox(&gtfs[i], cmdCfgMots, cfg.shapeTripId, true, &box);
}
OsmBuilder osmBuilder;
std::vector<pfaedle::osm::OsmReadOpts> opts;
for (const auto& o : motCfgReader.getConfigs()) {
if (std::find_first_of(o.mots.begin(), o.mots.end(), cmdCfgMots.begin(),
cmdCfgMots.end()) != o.mots.end()) {
if (std::find_first_of(o.mots.begin(), o.mots.end(), cmdCfgMots.begin(), cmdCfgMots.end()) != o.mots.end()) {
opts.push_back(o.osmBuildOpts);
}
}
@ -185,14 +183,12 @@ int main(int argc, char** argv) {
} else if (cfg.writeOverpass) {
BBoxIdx box(BOX_PADDING);
for (size_t i = 0; i < cfg.feedPaths.size(); i++) {
ShapeBuilder::getGtfsBox(&gtfs[i], cmdCfgMots, cfg.shapeTripId, true,
&box);
ShapeBuilder::getGtfsBox(&gtfs[i], cmdCfgMots, cfg.shapeTripId, true, &box);
}
OsmBuilder osmBuilder;
std::vector<pfaedle::osm::OsmReadOpts> opts;
for (const auto& o : motCfgReader.getConfigs()) {
if (std::find_first_of(o.mots.begin(), o.mots.end(), cmdCfgMots.begin(),
cmdCfgMots.end()) != o.mots.end()) {
if (std::find_first_of(o.mots.begin(), o.mots.end(), cmdCfgMots.begin(), cmdCfgMots.end()) != o.mots.end()) {
opts.push_back(o.osmBuildOpts);
}
}
@ -251,8 +247,7 @@ int main(int argc, char** argv) {
}
}
ShapeBuilder shapeBuilder(&gtfs[0], &evalFeed, cmdCfgMots, motCfg, &ecoll,
&graph, &fStops, &restr, cfg);
ShapeBuilder shapeBuilder(&gtfs[0], &evalFeed, cmdCfgMots, motCfg, &ecoll, &graph, &fStops, &restr, cfg);
if (cfg.writeGraph) {
LOG(INFO) << "Outputting graph.json...";

View file

@ -2,7 +2,10 @@
// Chair of Algorithms and Data Structures.
// Authors: Patrick Brosi <brosi@informatik.uni-freiburg.de>
#include <cfloat>
#include <osmium/io/any_input.hpp>
#include <osmium/geom/haversine.hpp>
#include <osmium/visitor.hpp>
#include <algorithm>
#include <exception>
#include <iostream>
@ -13,12 +16,6 @@
#include <utility>
#include <vector>
#include <osmium/io/any_input.hpp>
#include <osmium/util/file.hpp>
#include <osmium/geom/haversine.hpp>
#include <osmium/visitor.hpp>
#include <osmium/index/map/flex_mem.hpp>
#include "pfaedle/Def.h"
#include "pfaedle/osm/BBoxIdx.h"
#include "pfaedle/osm/Osm.h"
@ -55,51 +52,51 @@ using pfaedle::osm::BlockSearch;
using ad::cppgtfs::gtfs::Stop;
class NodeHandler : public osmium::handler::Handler {
const pfaedle::osm::OsmFilter& filter;
const pfaedle::osm::BBoxIdx& bbox;
pfaedle::osm::OsmIdSet& bboxNodes;
pfaedle::osm::OsmIdSet& noHupNodes;
const pfaedle::osm::OsmFilter& _filter;
const pfaedle::osm::BBoxIdx& _bbox;
pfaedle::osm::OsmIdSet& _bboxNodes;
pfaedle::osm::OsmIdSet& _noHupNodes;
public:
NodeHandler(const pfaedle::osm::OsmFilter& filter,
const pfaedle::osm::BBoxIdx& bbox,
pfaedle::osm::OsmIdSet& bboxNodes,
pfaedle::osm::OsmIdSet& noHupNodes) :
filter(filter),
bbox(bbox),
bboxNodes(bboxNodes),
noHupNodes(noHupNodes)
{
}
_filter(filter),
_bbox(bbox),
_bboxNodes(bboxNodes),
_noHupNodes(noHupNodes)
{}
void node(const osmium::Node& node) {
bool ignored = false;
for (const auto& tag : node.tags()) {
if (filter.nohup(tag.key(), tag.value())) {
noHupNodes.add(node.id());
if (_filter.nohup(tag.key(), tag.value())) {
_noHupNodes.add(node.id());
ignored = true;
break;
}
}
if(!ignored) {
Point<double> point(node.location().lon(), node.location().lat());
if (bbox.contains(point)) {
bboxNodes.add(node.id());
}
}
}
if (!ignored) {
Point<double> point{node.location().lon(), node.location().lat()};
if (_bbox.contains(point)) {
_bboxNodes.add(node.id());
}
}
}
};
class RelationHandler: public osmium::handler::Handler {
const pfaedle::osm::OsmFilter& filter;
const pfaedle::osm::BBoxIdx& bbox;
const pfaedle::osm::AttrKeySet& keepAttrs;
pfaedle::osm::RelLst& rels;
pfaedle::osm::RelMap& nodeRels;
pfaedle::osm::RelMap& wayRels;
pfaedle::osm::Restrictions& restrictions;
const pfaedle::osm::OsmFilter& _filter;
const pfaedle::osm::BBoxIdx& _bbox;
const pfaedle::osm::AttrKeySet& _keepAttrs;
pfaedle::osm::RelLst& _rels;
pfaedle::osm::RelMap& _nodeRels;
pfaedle::osm::RelMap& _wayRels;
pfaedle::osm::Restrictions& _restrictions;
public:
RelationHandler(const pfaedle::osm::OsmFilter& filter,
const pfaedle::osm::BBoxIdx& bbox,
@ -108,61 +105,68 @@ public:
pfaedle::osm::RelMap& nodeRels,
pfaedle::osm::RelMap& wayRels,
pfaedle::osm::Restrictions& restrictions) :
filter(filter),
bbox(bbox),
keepAttrs(keepAttrs),
rels(rels),
nodeRels(nodeRels),
wayRels(wayRels),
restrictions(restrictions){
}
_filter(filter),
_bbox(bbox),
_keepAttrs(keepAttrs),
_rels(rels),
_nodeRels(nodeRels),
_wayRels(wayRels),
_restrictions(restrictions)
{}
void relation(const osmium::Relation &relation) {
OsmRel rel;
rel.id = relation.id();
if(rel.id == 0) return;
if (rel.id == 0) {
return;
}
for (const auto& tag : relation.tags()) {
if (keepAttrs.count(tag.key())) {
if (_keepAttrs.count(tag.key())) {
rel.attrs[tag.key()] = tag.value();
}
}
for (const auto& member : relation.members()) {
using osmium::item_type;
auto& obj = member.get_object();
if (member.type() == osmium::item_type::node) {
if (member.type() == item_type::node) {
rel.nodes.push_back(obj.id());
rel.nodeRoles.emplace_back(member.role());
} else if (member.type() == osmium::item_type::way) {
} else if (member.type() == item_type::way) {
rel.ways.push_back(obj.id());
rel.wayRoles.emplace_back(member.role());
}
}
for (auto id : rel.nodes) {
nodeRels[id].push_back(rels.rels.size() - 1);
}
for (auto id : rel.ways) {
wayRels[id].push_back(rels.rels.size() - 1);
_nodeRels[id].push_back(_rels.rels.size() - 1);
}
uint64_t keepFlags = filter.keep(rel.attrs, pfaedle::osm::OsmFilter::REL);
uint64_t dropFlags = filter.drop(rel.attrs, pfaedle::osm::OsmFilter::REL);
for (auto id : rel.ways) {
_wayRels[id].push_back(_rels.rels.size() - 1);
}
uint64_t keepFlags = _filter.keep(rel.attrs, pfaedle::osm::OsmFilter::REL);
uint64_t dropFlags = _filter.drop(rel.attrs, pfaedle::osm::OsmFilter::REL);
if (rel.id && !rel.attrs.empty() && keepFlags && !dropFlags) {
rel.keepFlags = keepFlags;
rel.dropFlags = dropFlags;
}
rels.rels.push_back(rel.attrs);
_rels.rels.push_back(rel.attrs);
if (rel.keepFlags & pfaedle::osm::REL_NO_DOWN) {
rels.flat.insert(rels.rels.size() - 1);
_rels.flat.insert(_rels.rels.size() - 1);
}
// more processing
{
if (!rel.attrs.count("type")) return;
if (rel.attrs.find("type")->second != "restriction") return;
bool pos = filter.posRestr(rel.attrs);
bool neg = filter.negRestr(rel.attrs);
bool pos = _filter.posRestr(rel.attrs);
bool neg = _filter.negRestr(rel.attrs);
if (!pos && !neg) return;
@ -190,9 +194,9 @@ public:
if (from && to && via) {
if (pos) {
restrictions.pos[via].emplace_back(from, to);
_restrictions.pos[via].emplace_back(from, to);
} else if (neg) {
restrictions.neg[via].emplace_back(from, to);
_restrictions.neg[via].emplace_back(from, to);
}
}
}
@ -200,26 +204,27 @@ public:
};
class WayHandler: public osmium::handler::Handler {
Graph &g;
const pfaedle::osm::RelLst &rels;
const pfaedle::osm::RelMap &wayRels;
const pfaedle::osm::OsmFilter &filter;
const pfaedle::osm::OsmIdSet &bBoxNodes;
pfaedle::osm::NIdMap &nodes;
pfaedle::osm::NIdMultMap &multiNodes;
const pfaedle::osm::OsmIdSet &noHupNodes;
const pfaedle::osm::AttrKeySet &keepAttrs;
const pfaedle::osm::Restrictions &rawRests;
pfaedle::osm::Restrictor &restor;
const pfaedle::osm::FlatRels &fl;
pfaedle::osm::EdgTracks &eTracks;
const pfaedle::osm::OsmReadOpts &opts;
pfaedle::trgraph::Graph& _graph;
const pfaedle::osm::RelLst& _rels;
const pfaedle::osm::RelMap& _wayRels;
const pfaedle::osm::OsmFilter& _filter;
const pfaedle::osm::OsmIdSet& _bBoxNodes;
pfaedle::osm::NIdMap& _nodes;
pfaedle::osm::NIdMultMap& _multiNodes;
const pfaedle::osm::OsmIdSet& _noHupNodes;
const pfaedle::osm::AttrKeySet& _keepAttrs;
const pfaedle::osm::Restrictions& _restrictions;
pfaedle::osm::Restrictor& _restrictor;
const pfaedle::osm::FlatRels& _fl;
pfaedle::osm::EdgTracks& _eTracks;
const pfaedle::osm::OsmReadOpts& _opts;
std::map<TransitEdgeLine, TransitEdgeLine *> _lines;
std::map<size_t, TransitEdgeLine *> _relLines;
public:
WayHandler(Graph &g,
WayHandler(pfaedle::trgraph::Graph &g,
const pfaedle::osm::RelLst &rels,
const pfaedle::osm::RelMap &wayRels,
const pfaedle::osm::OsmFilter &filter,
@ -233,22 +238,21 @@ public:
const pfaedle::osm::FlatRels &fl,
pfaedle::osm::EdgTracks &eTracks,
const pfaedle::osm::OsmReadOpts &opts) :
g(g),
rels(rels),
wayRels(wayRels),
filter(filter),
bBoxNodes(bBoxNodes),
nodes(nodes),
multiNodes(multiNodes),
noHupNodes(noHupNodes),
keepAttrs(keepAttrs),
rawRests(rawRests),
restor(restor),
fl(fl),
eTracks(eTracks),
opts(opts) {
}
_graph(g),
_rels(rels),
_wayRels(wayRels),
_filter(filter),
_bBoxNodes(bBoxNodes),
_nodes(nodes),
_multiNodes(multiNodes),
_noHupNodes(noHupNodes),
_keepAttrs(keepAttrs),
_restrictions(rawRests),
_restrictor(restor),
_fl(fl),
_eTracks(eTracks),
_opts(opts)
{}
void way(const osmium::Way &way) {
OsmWay w;
@ -257,17 +261,17 @@ public:
w.nodes.emplace_back(node.ref());
}
for (const auto &tag : way.tags()) {
if (keepAttrs.count(tag.key())) {
if (_keepAttrs.count(tag.key())) {
w.attrs[tag.key()] = tag.value();
}
}
bool valid = false;
if (w.id && w.nodes.size() > 1 &&
(relKeep(w.id, wayRels, fl) || filter.keep(w.attrs, pfaedle::osm::OsmFilter::WAY)) &&
!filter.drop(w.attrs, pfaedle::osm::OsmFilter::WAY)) {
(relKeep(w.id, _wayRels, _fl) || _filter.keep(w.attrs, pfaedle::osm::OsmFilter::WAY)) &&
!_filter.drop(w.attrs, pfaedle::osm::OsmFilter::WAY)) {
for (auto nid : w.nodes) {
if (bBoxNodes.has(nid)) {
if (_bBoxNodes.has(nid)) {
valid = true;
break;
}
@ -277,44 +281,48 @@ public:
if (valid) {
Node *last = nullptr;
std::vector<TransitEdgeLine *> lines;
if (wayRels.count(w.id)) {
lines = getLines(wayRels.find(w.id)->second, rels, opts);
if (_wayRels.count(w.id)) {
lines = getLines(_wayRels.find(w.id)->second, _rels, _opts);
}
std::string track =
getAttrByFirstMatch(opts.edgePlatformRules, w.id, w.attrs, wayRels,
rels, opts.trackNormzer);
getAttrByFirstMatch(_opts.edgePlatformRules, w.id, w.attrs, _wayRels, _rels, _opts.trackNormzer);
uint64_t lastnid = 0;
for (auto nid : w.nodes) {
Node *n = nullptr;
if (noHupNodes.has(nid)) {
n = g.addNd();
multiNodes[nid].insert(n);
} else if (!nodes.count(nid)) {
if (!bBoxNodes.has(nid)) {
if (_noHupNodes.has(nid)) {
n = _graph.addNd();
_multiNodes[nid].insert(n);
} else if (!_nodes.count(nid)) {
if (!_bBoxNodes.has(nid)) {
continue;
}
n = g.addNd();
nodes[nid] = n;
n = _graph.addNd();
_nodes[nid] = n;
} else {
n = nodes[nid];
n = _nodes[nid];
}
if (last) {
auto e = g.addEdg(last, n, EdgePL());
auto e = _graph.addEdg(last, n, EdgePL());
if (!e)
continue;
processRestr(nid, w.id, rawRests, e, n, &restor);
processRestr(lastnid, w.id, rawRests, e, last, &restor);
processRestrictor(nid, w.id, _restrictions, e, n, &_restrictor);
processRestrictor(lastnid, w.id, _restrictions, e, last, &_restrictor);
e->pl().addLines(lines);
e->pl().setLvl(filter.level(w.attrs));
e->pl().setLvl(_filter.level(w.attrs));
if (!track.empty()) {
eTracks[e] = track;
_eTracks[e] = track;
}
if (filter.oneway(w.attrs)) e->pl().setOneWay(1);
if (filter.onewayrev(w.attrs)) e->pl().setOneWay(2);
if (_filter.oneway(w.attrs))
e->pl().setOneWay(1);
if (_filter.onewayrev(w.attrs))
e->pl().setOneWay(2);
}
lastnid = nid;
last = n;
@ -322,38 +330,36 @@ public:
}
}
static bool relKeep(uint64_t id, const pfaedle::osm::RelMap &rels,
const pfaedle::osm::FlatRels &fl) {
static bool relKeep(uint64_t id, const pfaedle::osm::RelMap &rels, const pfaedle::osm::FlatRels &fl) {
auto it = rels.find(id);
if (it == rels.end()) return false;
for (auto relId : it->second) {
// as soon as any of this entities relations is not flat, return true
if (!fl.count(relId)) return true;
}
if (it == rels.end())
return false;
return std::any_of(it->second.begin(), it->second.end(), [fl](decltype(*it->second.begin()) relId){
return !fl.count(relId);
});
}
std::vector<TransitEdgeLine *> getLines(
const std::vector<size_t> &edgeRels, const pfaedle::osm::RelLst &rels,
const pfaedle::osm::OsmReadOpts &ops) {
std::vector<TransitEdgeLine *> getLines(const std::vector<size_t> &edgeRels,
const pfaedle::osm::RelLst &rels,
const pfaedle::osm::OsmReadOpts &ops)
{
std::vector<TransitEdgeLine *> ret;
for (size_t relId : edgeRels) {
TransitEdgeLine *elp = nullptr;
TransitEdgeLine* transitEdgeLinePointer = nullptr;
if (_relLines.count(relId)) {
elp = _relLines[relId];
transitEdgeLinePointer = _relLines[relId];
} else {
TransitEdgeLine el;
TransitEdgeLine transitEdgeLine;
bool found = false;
for (const auto &r : ops.relLinerules.sNameRule) {
for (const auto &relAttr : rels.rels[relId]) {
if (relAttr.first == r) {
el.shortName = ops.lineNormzer(pfxml::file::decode(relAttr.second));
if (!el.shortName.empty()) found = true;
transitEdgeLine.shortName = ops.lineNormzer(pfxml::file::decode(relAttr.second));
if (!transitEdgeLine.shortName.empty()) found = true;
}
}
if (found) break;
@ -363,8 +369,8 @@ public:
for (const auto &r : ops.relLinerules.fromNameRule) {
for (const auto &relAttr : rels.rels[relId]) {
if (relAttr.first == r) {
el.fromStr = ops.statNormzer(pfxml::file::decode(relAttr.second));
if (!el.fromStr.empty()) found = true;
transitEdgeLine.fromStr = ops.statNormzer(pfxml::file::decode(relAttr.second));
if (!transitEdgeLine.fromStr.empty()) found = true;
}
}
if (found) break;
@ -374,32 +380,33 @@ public:
for (const auto &r : ops.relLinerules.toNameRule) {
for (const auto &relAttr : rels.rels[relId]) {
if (relAttr.first == r) {
el.toStr = ops.statNormzer(pfxml::file::decode(relAttr.second));
if (!el.toStr.empty()) found = true;
transitEdgeLine.toStr = ops.statNormzer(pfxml::file::decode(relAttr.second));
if (!transitEdgeLine.toStr.empty()) found = true;
}
}
if (found) break;
}
if (!el.shortName.size() && !el.fromStr.size() && !el.toStr.size())
if (transitEdgeLine.shortName.empty() && transitEdgeLine.fromStr.empty() &&
transitEdgeLine.toStr.empty())
continue;
if (_lines.count(el)) {
elp = _lines[el];
_relLines[relId] = elp;
if (_lines.count(transitEdgeLine)) {
transitEdgeLinePointer = _lines[transitEdgeLine];
_relLines[relId] = transitEdgeLinePointer;
} else {
elp = new TransitEdgeLine(el);
_lines[el] = elp;
_relLines[relId] = elp;
transitEdgeLinePointer = new TransitEdgeLine(transitEdgeLine);
_lines[transitEdgeLine] = transitEdgeLinePointer;
_relLines[relId] = transitEdgeLinePointer;
}
}
ret.push_back(elp);
ret.push_back(transitEdgeLinePointer);
}
return ret;
}
static void processRestr(uint64_t nid, uint64_t wid,
static void processRestrictor(uint64_t nid, uint64_t wid,
const pfaedle::osm::Restrictions &rawRests,
Edge *e,
Node *n,
@ -467,7 +474,7 @@ public:
};
class NodeHandler2: public osmium::handler::Handler {
Graph &g;
pfaedle::trgraph::Graph& _graph;
const pfaedle::osm::RelLst& rels;
const pfaedle::osm::RelMap& nodeRels;
const pfaedle::osm::OsmFilter& filter;
@ -478,6 +485,7 @@ class NodeHandler2: public osmium::handler::Handler {
const pfaedle::osm::AttrKeySet& keepAttrs;
const pfaedle::osm::FlatRels& fl;
const pfaedle::osm::OsmReadOpts& opts;
public:
NodeHandler2(Graph &g,
const pfaedle::osm::RelLst &rels,
@ -490,7 +498,7 @@ public:
const pfaedle::osm::AttrKeySet &keepAttrs,
const pfaedle::osm::FlatRels &fl,
const pfaedle::osm::OsmReadOpts &opts) :
g(g),
_graph(g),
rels(rels),
nodeRels(nodeRels),
filter(filter),
@ -500,20 +508,19 @@ public:
orphanStations(orphanStations),
keepAttrs(keepAttrs),
fl(fl),
opts(opts) {
}
opts(opts)
{}
void node(const osmium::Node &node) {
OsmNode nd;
for(const auto& tag: node.tags())
{
nd.id = node.id();
nd.lat = node.location().lat();
nd.lng = node.location().lon();
for (const auto &tag : node.tags()) {
if (keepAttrs.count(tag.key()))
nd.attrs[tag.key()] = tag.value();
}
nd.lat = node.location().lat();
nd.lng = node.location().lon();
nd.id = node.id();
bool valid = false;
if (nd.id &&
@ -524,28 +531,32 @@ public:
!filter.drop(nd.attrs, pfaedle::osm::OsmFilter::NODE))) {
valid = true;
}
pfaedle::osm::StAttrGroups attrGroups;
if(valid)
{
if (valid) {
Node *n = nullptr;
auto pos = util::geo::latLngToWebMerc<PFAEDLE_PRECISION>(nd.lat, nd.lng);
if (nodes.count(nd.id)) {
n = (nodes)[nd.id];
n = nodes[nd.id];
n->pl().setGeom(pos);
if (filter.station(nd.attrs)) {
auto si = getStatInfo(n, nd.id, pos, nd.attrs, &attrGroups, nodeRels,
rels, opts);
if (!si.isNull()) n->pl().setSI(si);
auto si = getStatInfo(n, nd.id, pos, nd.attrs, &attrGroups, nodeRels, rels, opts);
if (!si.isNull())
n->pl().setSI(si);
} else if (filter.blocker(nd.attrs)) {
n->pl().setBlocker();
}
} else if ((multNodes).count(nd.id)) {
for (auto* n : (multNodes)[nd.id]) {
} else if (multNodes.count(nd.id)) {
for (auto *n : multNodes[nd.id]) {
n->pl().setGeom(pos);
if (filter.station(nd.attrs)) {
auto si = getStatInfo(n, nd.id, pos, nd.attrs, &attrGroups, nodeRels,
rels, opts);
if (!si.isNull()) n->pl().setSI(si);
auto si = getStatInfo(n, nd.id, pos, nd.attrs, &attrGroups, nodeRels, rels, opts);
if (!si.isNull())
n->pl().setSI(si);
} else if (filter.blocker(nd.attrs)) {
n->pl().setBlocker();
}
@ -553,10 +564,12 @@ public:
} else {
// these are nodes without any connected edges
if (filter.station(nd.attrs)) {
auto tmp = g.addNd(NodePL(pos));
auto si = getStatInfo(tmp, nd.id, pos, nd.attrs, &attrGroups, nodeRels,
rels, opts);
if (!si.isNull()) tmp->pl().setSI(si);
auto tmp = _graph.addNd(NodePL(pos));
auto si = getStatInfo(tmp, nd.id, pos, nd.attrs, &attrGroups, nodeRels, rels, opts);
if (!si.isNull())
tmp->pl().setSI(si);
if (tmp->pl().getSI()) {
tmp->pl().getSI()->setIsFromOsm(false);
orphanStations.insert(tmp);
@ -567,44 +580,46 @@ public:
}
bool relKeep(uint64_t id, const pfaedle::osm::RelMap& rels, const pfaedle::osm::FlatRels& fl) const {
static bool relKeep(uint64_t id, const pfaedle::osm::RelMap &rels, const pfaedle::osm::FlatRels &fl) {
auto it = rels.find(id);
if (it == rels.end()) return false;
if (it == rels.end())
return false;
for (uint64_t relId : it->second) {
// as soon as any of this entities relations is not flat, return true
if (!fl.count(relId)) return true;
if (!fl.count(relId))
return true;
}
return false;
}
Nullable<StatInfo> getStatInfo(Node* node, uint64_t nid,
const POINT& pos, const pfaedle::osm::AttrMap& m,
static Nullable<StatInfo> getStatInfo(Node *node, uint64_t nid,
const POINT &pos,
const pfaedle::osm::AttrMap &m,
pfaedle::osm::StAttrGroups *groups,
const pfaedle::osm::RelMap &nodeRels,
const pfaedle::osm::RelLst &rels,
const pfaedle::osm::OsmReadOpts& ops) const {
std::string platform;
std::vector<std::string> names;
const pfaedle::osm::OsmReadOpts &ops) {
std::vector<std::string> names =
getAttrMatchRanked(ops.statAttrRules.nameRule, nid, m, nodeRels, rels, ops.statNormzer);
std::string platform =
getAttrByFirstMatch(ops.statAttrRules.platformRule, nid, m, nodeRels, rels, ops.trackNormzer);
names = getAttrMatchRanked(ops.statAttrRules.nameRule, nid, m, nodeRels, rels,
ops.statNormzer);
platform = getAttrByFirstMatch(ops.statAttrRules.platformRule, nid, m,
nodeRels, rels, ops.trackNormzer);
if (!names.size()) return Nullable<StatInfo>();
if (names.empty()) {
return Nullable<StatInfo>();
}
auto ret = StatInfo(names[0], platform, true);
#ifdef PFAEDLE_STATION_IDS
ret.setId(getAttrByFirstMatch(ops.statAttrRules.idRule, nid, m, nodeRels,
rels, ops.idNormzer));
ret.setId(getAttrByFirstMatch(ops.statAttrRules.idRule, nid, m, nodeRels, rels, ops.idNormzer));
#endif
for (size_t i = 1; i < names.size(); i++) ret.addAltName(names[i]);
for (size_t i = 1; i < names.size(); i++) {
ret.addAltName(names[i]);
}
bool groupFound = false;
for (const auto &rule : ops.statGroupNAttrRules) {
@ -632,10 +647,10 @@ public:
std::string ruleVal = getAttr(rule.attr, nid, m, nodeRels, rels);
if (!ruleVal.empty()) {
// add new group
auto* g = new StatGroup();
if (node) g->addNode(node);
ret.setGroup(g);
(*groups)[rule.attr.attr][ruleVal].push_back(g);
auto *group = new StatGroup();
if (node) group->addNode(node);
ret.setGroup(group);
(*groups)[rule.attr.attr][ruleVal].push_back(group);
break;
}
}
@ -644,36 +659,45 @@ public:
return ret;
}
std::string getAttrByFirstMatch(const pfaedle::osm::DeepAttrLst& rule, uint64_t id,
static std::string getAttrByFirstMatch(const pfaedle::osm::DeepAttrLst &rule,
uint64_t id,
const pfaedle::osm::AttrMap &attrs,
const pfaedle::osm::RelMap &entRels,
const pfaedle::osm::RelLst &rels,
const Normalizer& norm) const {
const Normalizer &norm) {
std::string ret;
for (const auto &s : rule) {
ret = norm(pfxml::file::decode(getAttr(s, id, attrs, entRels, rels)));
if (!ret.empty()) return ret;
if (!ret.empty()) {
break;
}
}
return ret;
}
std::vector<std::string> getAttrMatchRanked(
const pfaedle::osm::DeepAttrLst& rule, uint64_t id, const pfaedle::osm::AttrMap& attrs,
const pfaedle::osm::RelMap& entRels, const pfaedle::osm::RelLst& rels, const Normalizer& norm) const {
static std::vector<std::string> getAttrMatchRanked(const pfaedle::osm::DeepAttrLst &rule,
uint64_t id,
const pfaedle::osm::AttrMap &attrs,
const pfaedle::osm::RelMap &entRels,
const pfaedle::osm::RelLst &rels,
const Normalizer &norm) {
std::vector<std::string> ret;
for (const auto &s : rule) {
std::string tmp =
norm(pfxml::file::decode(getAttr(s, id, attrs, entRels, rels)));
if (!tmp.empty()) ret.push_back(tmp);
std::string tmp = norm(pfxml::file::decode(getAttr(s, id, attrs, entRels, rels)));
if (!tmp.empty()) {
ret.push_back(tmp);
}
}
return ret;
}
std::string getAttr(const pfaedle::osm::DeepAttrRule& s, uint64_t id,
const pfaedle::osm::AttrMap& attrs, const pfaedle::osm::RelMap& entRels,
const pfaedle::osm::RelLst& rels) const {
static std::string getAttr(const pfaedle::osm::DeepAttrRule &s,
uint64_t id,
const pfaedle::osm::AttrMap &attrs,
const pfaedle::osm::RelMap &entRels,
const pfaedle::osm::RelLst &rels) {
if (s.relRule.kv.first.empty()) {
if (attrs.find(s.attr) != attrs.end()) {
return attrs.find(s.attr)->second;
@ -692,6 +716,7 @@ public:
return "";
}
};
// _____________________________________________________________________________
bool EqSearch::operator()(const Node* cand, const StatInfo* si) const {
if (orphanSnap && cand->pl().getSI() &&
@ -729,19 +754,21 @@ void OsmBuilder::read(const std::string& path, const OsmReadOpts& opts,
OsmFilter filter(opts);
osmium::io::Reader reader{path, osmium::osm_entity_bits::node | osmium::osm_entity_bits::way |osmium::osm_entity_bits::relation};
osmium::io::Reader reader {path,
osmium::osm_entity_bits::node |
osmium::osm_entity_bits::way |
osmium::osm_entity_bits::relation};
osmium::io::Reader reader_nodes{path,
osmium::osm_entity_bits::node};
NodeHandler nodeHandler(filter, bbox, bboxNodes, noHupNodes);
RelationHandler relationHandler(filter, bbox, attrKeys[2], intmRels, nodeRels, wayRels, rawRests);
WayHandler wayHandler(*g, intmRels, wayRels, filter, bboxNodes, nodes, multNodes,
noHupNodes, attrKeys[1], rawRests, *res,
intmRels.flat, eTracks, opts);
WayHandler wayHandler(*g, intmRels, wayRels, filter, bboxNodes, nodes, multNodes, noHupNodes,
attrKeys[1], rawRests, *res, intmRels.flat, eTracks, opts);
osmium::apply(reader, nodeHandler, relationHandler, wayHandler);
osmium::io::Reader reader_nodes{path, osmium::osm_entity_bits::node};
NodeHandler2 nodeHandler2(*g, intmRels, nodeRels, filter, bboxNodes, nodes,
multNodes, orphanStations, attrKeys[0], intmRels.flat, opts);
NodeHandler2 nodeHandler2(*g, intmRels, nodeRels, filter, bboxNodes, nodes, multNodes, orphanStations,
attrKeys[0], intmRels.flat, opts);
osmium::apply(reader_nodes, nodeHandler2);
// we do four passes of the file here to be as memory creedy as possible:

View file

@ -16,7 +16,7 @@ namespace osm {
class OsmFilter {
public:
enum Type : uint64_t { NODE = 16, WAY = 8, REL = 4, ALL = 0 };
OsmFilter() {}
OsmFilter() = default;
OsmFilter(const MultAttrMap& keep, const MultAttrMap& drop);
explicit OsmFilter(const OsmReadOpts& o);
uint64_t keep(const AttrMap& attrs, Type t) const;