Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- #include <vector>
- #include <string>
- #include <future>
- #include <thread>
- #include <mutex>
- #include <condition_variable>
- #include <functional>
- #include <osmium/io/any_input.hpp>
- #include <osmium/handler.hpp>
- #include <osmium/visitor.hpp>
- class WayData {
- public:
- osmium::object_id_type id;
- std::vector<std::pair<double, double>> nodes; // Store lon, lat pairs
- std::string name;
- WayData(const osmium::Way& way) {
- id = way.id();
- for (const auto& node : way.nodes()) {
- if (node.location().valid()) {
- nodes.emplace_back(node.lon(), node.lat());
- }
- }
- const char* name_tag = way.tags()["name"];
- if (name_tag) {
- name = name_tag;
- }
- }
- };
- std::string process_way(const WayData& wayData) {
- std::string feature = "{ \"type\": \"Feature\", \"properties\": { \"id\": \"" + std::to_string(wayData.id) + "\", ";
- if (!wayData.name.empty()) {
- feature += "\"name\": \"" + wayData.name + "\", ";
- }
- feature += "\"geometry\": { \"type\": \"LineString\", \"coordinates\": [";
- for (const auto& node : wayData.nodes) {
- feature += "[" + std::to_string(node.first) + ", " + std::to_string(node.second) + "],";
- }
- if (feature.back() == ',') {
- feature.pop_back();
- }
- feature += "] } }";
- return feature;
- }
- class ThreadPool {
- public:
- ThreadPool(size_t threads) : stop(false) {
- for(size_t i = 0; i < threads; ++i)
- workers.emplace_back([this] {
- while(true) {
- std::function<void()> task;
- {
- std::unique_lock<std::mutex> lock(this->queue_mutex);
- this->condition.wait(lock, [this]{ return this->stop || !this->tasks.empty(); });
- if(this->stop && this->tasks.empty())
- return;
- task = std::move(this->tasks.front());
- this->tasks.pop();
- }
- task();
- }
- });
- }
- ~ThreadPool() {
- {
- std::unique_lock<std::mutex> lock(queue_mutex);
- stop = true;
- }
- condition.notify_all();
- for(std::thread &worker: workers)
- worker.join();
- }
- template<class F, class... Args>
- auto enqueue(F&& f, Args&&... args)
- -> std::future<typename std::result_of<F(Args...)>::type> {
- using return_type = typename std::result_of<F(Args...)>::type;
- auto task = std::make_shared< std::packaged_task<return_type()> >(
- std::bind(std::forward<F>(f), std::forward<Args>(args)...)
- );
- std::future<return_type> res = task->get_future();
- {
- std::unique_lock<std::mutex> lock(queue_mutex);
- if(stop)
- throw std::runtime_error("enqueue on stopped ThreadPool");
- tasks.emplace([task](){ (*task)(); });
- }
- condition.notify_one();
- return res;
- }
- private:
- std::vector< std::thread > workers;
- std::queue< std::function<void()> > tasks;
- std::mutex queue_mutex;
- std::condition_variable condition;
- bool stop;
- };
- int main(int argc, char* argv[]) {
- int num_threads = std::thread::hardware_concurrency();
- std::string input_file_name;
- for (int i = 1; i < argc; ++i) {
- std::string arg = argv[i];
- if (arg == "-j" && i + 1 < argc) {
- num_threads = std::stoi(argv[++i]);
- } else {
- input_file_name = argv[i];
- }
- }
- if (input_file_name.empty()) {
- std::cerr << "Usage: " << argv[0] << " [-j num_threads] OSM_FILE.osm.pbf" << std::endl;
- return 1;
- }
- osmium::io::File input_file(input_file_name);
- osmium::io::Reader reader(input_file);
- ThreadPool pool(num_threads);
- std::vector<std::future<std::string>> results;
- while (osmium::memory::Buffer buffer = reader.read()) {
- for (const auto& way : buffer.select<osmium::Way>()) {
- WayData wayData(way);
- results.emplace_back(pool.enqueue(process_way, wayData));
- }
- }
- std::vector<std::string> features;
- for (auto& result : results) {
- std::string feature = result.get();
- if (!feature.empty()) {
- features.push_back(feature);
- }
- }
- std::ofstream file("output.geojson");
- file << "{ \"type\": \"FeatureCollection\", \"features\": [";
- for (const auto& feature : features) {
- file << feature << ",";
- }
- if (!features.empty()) {
- file.seekp(-1, std::ios_base::end);
- }
- file << "] }";
- file.close();
- reader.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement