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
25 changes: 20 additions & 5 deletions src/OSM/features/simplifyOSM.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,26 @@
#include <processor/OSMParser.h>
#include <processor/OSMProcessorBase.h>
#include <processor/bufferWays.h>
#include <processor/filterWays.h>
#include <processor/linkWays.h>
#include <unordered_set>

namespace TSMM::OSM
{
static inline std::unordered_set<std::string> leftWayTags{"motorway", "trunk", "primary", "secondary", "tertiary", "residential"};

bool isWayFiltered(std::shared_ptr<OSMWay> &way)
{
std::string keyString = "highway";
const auto waytags = way->tags();
if (waytags.count(keyString))
{
if (leftWayTags.count(waytags.at(keyString)))
return false;
return true;
}
return true;
}

Comment thread
Mengzhuoxiao marked this conversation as resolved.
class SimplifyOSM
{
Expand All @@ -19,9 +35,8 @@ namespace TSMM::OSM


public:
explicit SimplifyOSM(Conf &conf) : map_(nullptr), conf_(std::move(conf))
explicit SimplifyOSM(Conf &conf) : map_(std::make_shared<OSMMap>()), conf_(std::move(conf))
{
map_ = std::make_shared<OSMMap>();
try
{
osmium::io::Reader reader(conf_.inPath_, osmium::osm_entity_bits::node | osmium::osm_entity_bits::way | osmium::osm_entity_bits::relation);
Expand All @@ -41,14 +56,14 @@ namespace TSMM::OSM
// specific ways to process osmmap
// for example: linkways --> buffer ways --> store the map
std::vector<OSMProcessorBase *> processPipeLine;
OSMProcessorBase *wayFilter = new FilterWays(isWayFiltered);
OSMProcessorBase *linkWays = new LinkWays();
OSMProcessorBase *bufferWays = new BufferWays();
processPipeLine.push_back(wayFilter);
processPipeLine.push_back(linkWays);
processPipeLine.push_back(bufferWays);

for (auto &pipe: processPipeLine)
pipe->process(*map_);

std::for_each(processPipeLine.begin(), processPipeLine.end(), [&](auto &pipe) { pipe->process(*map_); });
map_->save(conf_.outPath_);
}
};
Expand Down
82 changes: 56 additions & 26 deletions src/OSM/map/OSMMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
#define OSMMAP_H

#include <iostream>
#include <map/OSMType.h>
#include <map/OSMNode.h>
#include <map/OSMRelation.h>
#include <map/OSMWay.h>
#include <memory>
#include <osmium/builder/osm_object_builder.hpp>
#include <osmium/io/pbf_output.hpp>
#include <osmium/io/xml_output.hpp>
#include <osmium/memory/buffer.hpp>

#include <unordered_map>

namespace TSMM::OSM
Expand All @@ -16,64 +19,91 @@ namespace TSMM::OSM
class OSMMap
{

std::unordered_map<long long, std::shared_ptr<OSMType::Node>> nodes_;
std::unordered_map<long long, std::shared_ptr<OSMType::Way>> ways_;
std::unordered_map<long long, std::shared_ptr<OSMType::Relation>> relations_;
std::unordered_map<OSMNode::id_t, std::shared_ptr<OSMNode>> nodes_;
std::unordered_map<OSMWay::id_t, std::shared_ptr<OSMWay>> ways_;
std::unordered_map<OSMRelation::id_t, std::shared_ptr<OSMRelation>> relations_;
osmium::memory::Buffer buffer_{10240, osmium::memory::Buffer::auto_grow::yes};

void buildNodes(osmium::memory::Buffer &buffer)
{
for (const auto &node: nodes_)
{
std::for_each(nodes_.begin(), nodes_.end(), [&](const auto &node) {
{
osmium::builder::NodeBuilder builder{buffer};
builder.set_user("tsmm");
osmium::Node &obj = builder.object();

obj.set_id(node.second->id_);
obj.set_uid(node.second->id_);
obj.set_location(osmium::Location{node.second->lon_, node.second->lat_});
obj.set_id(node.second->id());
obj.set_uid(node.second->id());
obj.set_location(osmium::Location{node.second->lon(), node.second->lat()});
}
buffer.commit();
}
});
}

void buildWays(osmium::memory::Buffer &buffer)
{
for (const auto &way: ways_)
{
std::for_each(ways_.begin(), ways_.end(), [&](const auto &way) {
if (way.second->isActive())
{
osmium::builder::WayBuilder builder{buffer};
builder.set_user("tsmm");
osmium::Way &obj = builder.object();

obj.set_id(way.second->id_);
obj.set_uid(way.second->id_);
{
osmium::builder::WayBuilder builder{buffer};
Comment thread
Mengzhuoxiao marked this conversation as resolved.
builder.set_user("tsmm");
osmium::Way &obj = builder.object();

obj.set_id(way.second->id());
obj.set_uid(way.second->id());

{
///< add node ref
osmium::builder::WayNodeListBuilder wayNodeListBuilder{buffer, &builder};
std::for_each(way.second->nodeRefs().begin(), way.second->nodeRefs().end(), [&](const auto &nId) {
osmium::Location node(nodes_[nId]->lon(), nodes_[nId]->lat());
osmium::NodeRef nodeRef{nId, node};
wayNodeListBuilder.add_node_ref(nodeRef);
});
}

{
///< add way tags
osmium::builder::TagListBuilder tl_builder{buffer, &builder};
// add road level tag
std::for_each(way.second->tags().begin(), way.second->tags().end(), [&](const auto &tags) {
tl_builder.add_tag(tags.first, tags.second);
});
}
}
buffer.commit();
}
buffer.commit();
}
});
}


void buildRelations(osmium::memory::Buffer &buffer)
{}

public:
void addNode(long long id, double lat, double lon)
void addNode(OSMWay::id_t id, double lat, double lon)
{
nodes_[id] = std::make_shared<OSMType::Node>(id, lat, lon);
nodes_[id] = std::make_shared<OSMNode>(id, lat, lon);
}

void addWay(long long id)
void addWay(OSMWay::id_t id, std::vector<osmium::object_id_type> &nodeRef, std::unordered_map<std::string, std::string> &tags)
Comment thread
Mengzhuoxiao marked this conversation as resolved.
{
ways_[id] = std::make_shared<OSMType::Way>(id);

ways_[id] = std::make_shared<OSMWay>(id, nodeRef, tags);
}

void addRelation(long long id)
void addRelation(OSMRelation::id_t id)
{
relations_[id] = std::make_shared<OSMType::Relation>(id);
relations_[id] = std::make_shared<OSMRelation>(id);
}


std::unordered_map<OSMWay::id_t, std::shared_ptr<OSMWay>> ways() const
{
return ways_;
}

void save(const std::string &path)
{
std::size_t bufferSize = 10240;
Expand Down
36 changes: 36 additions & 0 deletions src/OSM/map/OSMNode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef OSMNODE_H
#define OSMNODE_H
#include <memory>
#include <unordered_map>
namespace TSMM::OSM
{

class OSMNode
{

public:
typedef std::int64_t id_t;


OSMNode(id_t id, double lat, double lon) : id_(id), lat_(lat), lon_(lon), isActive_(true) {}

void addTag(const std::string &key, const std::string &value)
{
tags_[key] = value;
}

id_t id() const { return id_; }
double lat() const { return lat_; }
double lon() const { return lon_; }


private:
id_t id_;
double lat_, lon_;
bool isActive_;
std::unordered_map<std::string, std::string> tags_;
};

}// namespace TSMM::OSM

#endif//OSMNODE_H
37 changes: 37 additions & 0 deletions src/OSM/map/OSMRelation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef OSMRELATION_H
#define OSMRELATION_H
#include <memory>
#include <random>
#include <unordered_map>
namespace TSMM::OSM
{

class OSMRelation
{
public:
typedef std::int64_t id_t;


explicit OSMRelation(id_t id) : id_(id), isActive_(true) {}
void addMember(const std::string &type, long long refId)
{
members_.emplace_back(type, refId);
}

void addTag(const std::string &key, const std::string &value)
{
tags_[key] = value;
}

id_t id() const { return id_; }

private:
id_t id_;
bool isActive_;
std::vector<std::pair<std::string, long long>> members_;// Type ("node", "way", "relation") and ID
std::unordered_map<std::string, std::string> tags_;
};
}// namespace TSMM::OSM


#endif//OSMRELATION_H
64 changes: 0 additions & 64 deletions src/OSM/map/OSMType.h

This file was deleted.

50 changes: 50 additions & 0 deletions src/OSM/map/OSMWay.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#ifndef OSMWAY_H
#define OSMWAY_H
#include <memory>
#include <osmium/builder/osm_object_builder.hpp>
#include <unordered_map>
namespace TSMM::OSM
{
class OSMWay
{

public:
typedef std::int64_t id_t;

OSMWay(id_t id, std::vector<osmium::object_id_type> &nodeRef, std::unordered_map<std::string, std::string> &tags)
: id_(id), isActive_(true), nodeRefs_(std::move(nodeRef)), tags_(std::move(tags)) {}

void addNode(long long nodeId)
{
nodeRefs_.push_back(nodeId);
}

void addTag(const std::string &key, const std::string &value)
{
tags_[key] = value;
}

id_t id() const { return id_; }

void deactivate() { isActive_ = false; }
bool isActive() const { return isActive_; }

const std::unordered_map<std::string, std::string> &tags() const
{
return tags_;
}

const std::vector<osmium::object_id_type> &nodeRefs() const
{
return nodeRefs_;
}

private:
id_t id_;
bool isActive_;
std::vector<osmium::object_id_type> nodeRefs_;// Stores references to OSMNode IDs
std::unordered_map<std::string, std::string> tags_;
};
}// namespace TSMM::OSM

#endif//OSMWAY_H
12 changes: 11 additions & 1 deletion src/OSM/processor/OSMParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,17 @@ namespace TSMM::OSM

void way(const osmium::Way &way)
{
map_->addWay(way.id());
std::vector<osmium::object_id_type> nodeRef;
std::unordered_map<std::string, std::string> tags;
std::for_each(way.nodes().begin(), way.nodes().end(), [&](const auto &node_ref) {
nodeRef.push_back(node_ref.ref());// Store only node IDs
});

std::for_each(way.tags().begin(), way.tags().end(), [&](const auto &tag) {
tags.insert(std::make_pair(tag.key(), tag.value()));
});

map_->addWay(way.id(), nodeRef, tags);
}

void relation(const osmium::Relation &relation)
Expand Down
Loading