File: Export2DB.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 (159 lines) | stat: -rw-r--r-- 5,161 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
/***************************************************************************
 *   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_EXPORT2DB_H_
#define SRC_EXPORT2DB_H_

#include <pqxx/pqxx>
#include <libpq-fe.h>
#include <map>
#include <vector>
#include <string>

#include "osm_elements/Node.h"
#include "osm_elements/Way.h"
#include "osm_elements/Relation.h"
#include "configuration/configuration.h"
#include "utilities/prog_options.h"
#include "database/table_management.h"

namespace osm2pgr {

/**
 * This class connects to a postgresql database. For using this class,
 * you also need to install postgis and pgrouting
 */

class Export2DB {
 public:
     typedef std::vector<Node> Nodes;
     typedef std::vector<Way> Ways;
     typedef std::vector<Relation> Relations;

     /**
      * Constructor
      * @param vm variable map holding the configuration
      * @param db_conn conection string
      *
      */
     explicit Export2DB(const po::variables_map &vm, const std::string &db_conn);

     /**
      * Destructor
      * closes the connection to the database
      */
     ~Export2DB();

#if 1
     //! connects to database
     int connect();
#endif

     bool has_extension(const std::string &name) const;
#ifndef NDBEUG
     bool install_postGIS() const;
#endif

     //! creates needed tables and geometries
     void createTables() const;


     /** @brief export values to osm_* table
      *
      * T must have:
      *     T.values
      *
      * @param[in] items  vector of values to be inserted into
      * @param[in] table
      */
     template <typename T>
         void export_osm (
                 std::vector<T> &items,
                 const std::string &table) const {
             auto osm_table = m_tables.get_table(table);
             std::vector<std::string> values(items.size(), "");

             size_t i(0);
             for (auto it = items.begin(); it != items.end(); ++it, ++i) {
                 auto item = *it;
                 values[i] = tab_separated(item.values(osm_table.columns(), true));
             }

             export_osm(values, osm_table);
         }

     void export_configuration(
             const std::map<std::string, Tag_key>& items) const;

     void exportWays(
             const Ways &ways,
             const Configuration &config) const;

     void dropTables() const;
     void createFKeys() const;
     void process_pois() const;
     bool exists(const std::string &table) const;

 private:

     void export_osm(
             const std::vector<std::string> &values,
             const Table &table) const;

     void process_section(const std::string &ways_columns, pqxx::work &Xaction) const;

     void fill_adjacent_edges(
             const std::string &table,
             const std::string &vertices_tab,
             pqxx::work &Xaction) const;

     void fill_vertices_table(
             const std::string &table,
             const std::string &vertices_tab,
             pqxx::work &Xaction) const;

     void fill_source_target(
             const std::string &table,
             const std::string &vertices_tab,
             pqxx::work &Xaction) const;

     int64_t get_val(const std::string sql) const;
     void execute(const std::string sql) const;

     Table configuration() const {return m_tables.configuration();}
     Table vertices() const {return m_tables.vertices();}
     Table ways() const {return m_tables.ways();}
     Table pois() const {return m_tables.pois();}
     Table osm_ways() const {return m_tables.osm_ways();}
     Table osm_nodes() const {return m_tables.osm_nodes();}
     Table osm_relations() const {return m_tables.osm_relations();}

 private:
     po::variables_map m_vm;

     std::string conninf;

     Tables m_tables;

};
}  // namespace osm2pgr

#endif  // SRC_EXPORT2DB_H_