File: check_wkt_handler.hpp

package info (click to toggle)
libosmium 2.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,564 kB
  • sloc: cpp: 53,570; sh: 148; makefile: 19
file content (88 lines) | stat: -rw-r--r-- 2,702 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#ifndef CHECK_WKT_HANDLER_HPP
#define CHECK_WKT_HANDLER_HPP

#include <cassert>
#include <fstream>
#include <map>
#include <sstream>
#include <string>

#include <osmium/handler.hpp>
#include <osmium/osm.hpp>
#include <osmium/osm/types.hpp>

class CheckWKTHandler : public osmium::handler::Handler {

    std::map<osmium::object_id_type, std::string> m_geometries;
    osmium::geom::WKTFactory<> m_factory;

    void read_wkt_file(const std::string& filename) {
        std::ifstream in{filename, std::ifstream::in};
        if (in) {
            osmium::object_id_type id;
            std::string line;
            while (std::getline(in, line)) {
                const std::size_t pos = line.find_first_of(' ');

                if (pos == std::string::npos) {
                    std::cerr << filename << " not formatted correctly\n";
                    std::exit(1);
                }

                std::string id_str = line.substr(0, pos);
                std::istringstream iss{id_str};
                iss >> id;

                if (m_geometries.find(id) != m_geometries.end()) {
                     std::cerr << filename + " contains id " << id << "twice\n";
                     std::exit(1);
                }

                m_geometries[id] = line.substr(pos+1);
            }
        }
    }

public:

    CheckWKTHandler(const std::string& dirname, int test_id) :
        osmium::handler::Handler() {

        std::string filename = dirname + "/" + std::to_string(test_id / 100) + "/" + std::to_string(test_id) + "/";
        read_wkt_file(filename + "nodes.wkt");
        read_wkt_file(filename + "ways.wkt");
    }

    ~CheckWKTHandler() {
        if (!m_geometries.empty()) {
            for (const auto& geom : m_geometries) {
                std::cerr << "geometry id " << geom.first << " not in data.osm.\n";
            }
            std::exit(1);
        }
    }

    void node(const osmium::Node& node) {
        const std::string wkt = m_geometries[node.id()];
        assert(wkt != "" && "Missing geometry for node in nodes.wkt");

        const std::string this_wkt = m_factory.create_point(node.location());
        assert(wkt == this_wkt && "wkt geometries don't match");
        m_geometries.erase(node.id());
    }

    void way(const osmium::Way& way) {
        const std::string wkt = m_geometries[way.id()];
        assert(wkt != "" && "Missing geometry for way in ways.wkt");

        if (wkt != "NULL") {
            const std::string this_wkt = m_factory.create_linestring(way);
            assert(wkt == this_wkt && "wkt geometries don't match");
        }
        m_geometries.erase(way.id());
    }

}; // CheckWKTHandler


#endif // CHECK_WKT_HANDLER_HPP