File: OSMDocument.h

package info (click to toggle)
osm2pgrouting 3.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,036 kB
  • sloc: cpp: 3,754; python: 3,105; xml: 504; sh: 61; sql: 54; ansic: 46; makefile: 18
file content (161 lines) | stat: -rw-r--r-- 4,904 bytes parent folder | download
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/***************************************************************************
 *   Copyright (C) 2016 by pgRouting developers                            *
 *   project@pgrouting.org                                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License t &or more details.                        *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#ifndef SRC_OSMDOCUMENT_H_
#define SRC_OSMDOCUMENT_H_

#include <iostream>
#include <map>
#include <vector>
#include "utilities/utilities.h"
#include "configuration/configuration.h"
#include "utilities/prog_options.h"
#include "database/Export2DB.h"

namespace osm2pgr {


class Node;
class Way;
class Relation;

/**
    An osm-document.
*/
class OSMDocument {
 public:
    typedef std::vector<Node> Nodes;
    typedef std::vector<Way> Ways;
    typedef std::vector<Relation> Relations;

    //! Constructor
    OSMDocument(
            const Configuration& config,
            const po::variables_map &vm,
            const Export2DB &db_conn,
            size_t lines);

    inline size_t lines() const {return m_lines;}

    //! Do the configuration has the @b tag ?
    inline bool config_has_tag(const Tag &tag) const {
        return m_rConfig.has_tag(tag);
    }

    inline double priority(const Tag &tag) const {
        return m_rConfig.priority(tag);
    }

    inline double maxspeed(const Tag &tag) const {
        return m_rConfig.maxspeed(tag);
    }

    const Nodes& nodes() const {return m_nodes;}
    const Ways& ways() const {return m_ways;}
    const Relations& relations() const {return m_relations;}

    void AddNode(const Node &n);
    void AddWay(const Way &w);
    void AddRelation(const Relation &r);
    void endOfFile();

    //! find node by using an ID
    bool has_node(int64_t nodeRefId) const;
    Node* FindNode(int64_t nodeRefId);

    bool has_way(int64_t way_id) const;
    Way* FindWay(int64_t way_id);


    void add_node(Way &way, const char **atts);

    /**
     * add the configuration tag used for the speeds
     */
    void add_config(Element *osm_element, const Tag &tag) const;

    inline uint16_t nodeErrs() const {return m_nodeErrs;}

 private:
    template <typename T>
        bool
        do_export_osm(const T &container) {
            return m_vm.count("addnodes") && (container.size() % m_chunk_size) == 0;
        }


    void wait_child() const;

    template <typename T>
        void
        osm_table_export(const T &osm_items, const std::string &table) const {
            if (osm_items.empty()) return;

            if (m_vm.count("addnodes")) {
#if 0
                auto pid = fork();
                if (pid < 0) {
                    std::cerr << "Failed to fork" << endl;
                    exit(1);
                }
                if (pid > 0) return;
#endif
            }
            auto residue = osm_items.size() % m_chunk_size;
            auto start = residue? osm_items.size() - residue : osm_items.size() - m_chunk_size;
            T export_items = T(osm_items.begin() + static_cast<int64_t>(start), osm_items.end());

            m_db_conn.export_osm(export_items, table);

            if (m_vm.count("addnodes")) {
#if 0
                /*
                 * finish the child process
                 */
                _exit(0);
#endif
            }
        }

   void export_pois() const;


 private:
    // ! parsed nodes TODO change to sorted vector
    Nodes m_nodes;
    //! parsed ways
    Ways m_ways;
    //! parsed relations
    Relations  m_relations;
    bool       m_relPending;
    bool       m_waysPending;

    const Configuration& m_rConfig;
    po::variables_map m_vm;
    const Export2DB &m_db_conn;

    size_t m_chunk_size;
    uint16_t m_nodeErrs;
    size_t m_lines;
};

}  // end namespace osm2pgr
#endif  // SRC_OSMDOCUMENT_H_