forked from lduchesne/cmatopo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpg.cpp
More file actions
285 lines (227 loc) · 6.67 KB
/
pg.cpp
File metadata and controls
285 lines (227 loc) · 6.67 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include <pg.h>
#include <string>
#include <cassert>
#include <sstream>
#include <iostream>
#include <boost/algorithm/string/join.hpp>
#include <zones.h>
using namespace cma;
using namespace std;
using namespace boost::algorithm;
namespace cma {
extern GEOSContextHandle_t hdl;
}
namespace cma {
PG::PG(const string& connect_str)
{
_conn = PQconnectdb(connect_str.c_str());
}
PG::~PG()
{
if (_conn) {
PQfinish(_conn);
}
}
bool PG::connected() const
{
return _conn != NULL && PQstatus(_conn) == CONNECTION_OK;
}
PGresult* PG::query(const string& sql, bool single_row_mode)
{
cout << sql << endl;
if (!single_row_mode) {
return PQexec(_conn, sql.c_str());
}
int qid = PQsendQuery(_conn, sql.c_str());
if (PQsetSingleRowMode(_conn) != 1) {
cerr << "Warning: could not set single-row mode for query: " << sql << endl;
}
return next_result();
}
PGresult* PG::next_result()
{
return PQgetResult(_conn);
}
bool PG::success(PGresult* res) const
{
if (!res) {
return false;
}
int status = PQresultStatus(res);
return (status == PGRES_TUPLES_OK || status == PGRES_COMMAND_OK);
}
int PG::get_line_count()
{
ostringstream oss;
oss << "SELECT count(1) FROM way;";
PGresult* res = query(oss.str().c_str());
if (!success(res)) {
PQclear(res);
return -1;
}
int count = atoi(PQgetvalue(res, 0, 0));
PQclear(res);
return count;
}
GEOSGeometry* PG::get_line(int id)
{
ostringstream oss;
oss << "SELECT line2d_m FROM way WHERE id=" << id;
PGresult* res = query(oss.str().c_str());
if (!success(res)) {
PQclear(res);
return nullptr;
}
char* line2d;
GEOSWKBReader* wkb_reader = GEOSWKBReader_create_r(hdl);
line2d = PQgetvalue(res, 0, 0);
GEOSGeometry* line = GEOSWKBReader_readHEX_r(hdl, wkb_reader, (const unsigned char*)line2d, PQgetlength(res, 0, 0));
assert (line != NULL);
GEOSWKBReader_destroy_r(hdl, wkb_reader);
return line;
}
bool PG::get_lines(
const GEOSGeometry* geom,
linesV& lines,
bool within,
int limit)
{
string q = _build_query(geom, within, limit);
PGresult* res = query(q.c_str());
if (!success(res)) {
PQclear(res);
return false;
}
char* id;
char* line2d;
GEOSWKBReader* wkb_reader = GEOSWKBReader_create_r(hdl);
for (int i = 0; i < PQntuples(res); i++) {
id = PQgetvalue(res, i, 0);
line2d = PQgetvalue(res, i, 1);
GEOSGeometry* line = GEOSWKBReader_readHEX_r(hdl, wkb_reader, (const unsigned char*)line2d, PQgetlength(res, i, 1));
if (line != NULL) {
// assert (line != NULL);
lines.push_back(make_pair(atoi(id), line));
}
else {
cerr << "Line id " << id << " could not be converted to a GEOSGeometry..." << endl;
}
}
GEOSWKBReader_destroy_r(hdl, wkb_reader);
PQclear(res);
return true;
}
bool PG::get_common_lines(
const OGREnvelope& env1,
const OGREnvelope& env2,
linesV& lines,
int limit)
{
GEOSGeometry* g1 = OGREnvelope2GEOSGeom(env1);
GEOSGeometry* g2 = OGREnvelope2GEOSGeom(env2);
string env1_str = build_pg_geom(g1);
string env2_str = build_pg_geom(g2);
OGREnvelope m = env1;
m.Merge(env2);
GEOSGeometry* g3 = OGREnvelope2GEOSGeom(m);
string m_str = build_pg_geom(g3);
ostringstream oss;
oss << "SELECT id, line2d_m FROM way WHERE "
<< m_str << " && line2d_m AND ST_Contains(" << m_str << ", line2d_m) AND "
<< env1_str << " && line2d_m AND NOT ST_Contains(" << env1_str << ", line2d_m) AND "
<< env2_str << " && line2d_m AND NOT ST_Contains(" << env2_str << ", line2d_m)";
PGresult* res = query(oss.str().c_str());
if (!success(res)) {
PQclear(res);
return false;
}
char* id;
char* line2d;
GEOSWKBReader* wkb_reader = GEOSWKBReader_create_r(hdl);
for (int i = 0; i < PQntuples(res); i++) {
id = PQgetvalue(res, i, 0);
line2d = PQgetvalue(res, i, 1);
GEOSGeometry* line = GEOSWKBReader_readHEX_r(hdl, wkb_reader, (const unsigned char*)line2d, PQgetlength(res, i, 1));
assert (line != NULL);
lines.push_back(make_pair(atoi(id), line));
}
GEOSWKBReader_destroy_r(hdl, wkb_reader);
GEOSGeom_destroy_r(hdl, g1);
GEOSGeom_destroy_r(hdl, g2);
GEOSGeom_destroy_r(hdl, g3);
}
bool PG::get_line_ids(
const GEOSGeometry* envelope,
set<int>& line_ids,
bool within,
int limit)
{
string q = _build_query(envelope, within, limit);
PGresult* res = query(q.c_str());
if (!success(res)) {
PQclear(res);
return false;
}
char* id;
for (int i = 0; i < PQntuples(res); i++) {
id = PQgetvalue(res, i, 0);
assert (id != NULL);
line_ids.insert(atoi(id));
}
PQclear(res);
return true;
}
bool PG::get_lines(const std::set<int> lineIds, linesV& lines)
{
vector<string> ids;
transform(lineIds.begin(), lineIds.end(), back_inserter(ids), [](int a) {
return to_string(a);
});
ostringstream oss;
oss << "SELECT id, line2d_m FROM way WHERE id IN (" << join(ids, ",") << ")";
PGresult* res = query(oss.str().c_str());
if (!success(res)) {
PQclear(res);
return false;
}
char* id;
char* line2d;
GEOSWKBReader* wkb_reader = GEOSWKBReader_create_r(hdl);
for (int i = 0; i < PQntuples(res); i++) {
id = PQgetvalue(res, i, 0);
line2d = PQgetvalue(res, i, 1);
GEOSGeometry* line = GEOSWKBReader_readHEX_r(hdl, wkb_reader, (const unsigned char*)line2d, PQgetlength(res, i, 1));
assert (line != NULL);
lines.push_back(make_pair(atoi(id), line));
}
GEOSWKBReader_destroy_r(hdl, wkb_reader);
PQclear(res);
return true;
}
string PG::_build_query(
const GEOSGeometry* geom,
bool within,
int limit)
{
string geom_str = build_pg_geom(geom);
ostringstream oss;
oss << "SELECT id, line2d_m FROM way WHERE ";
if (!within) oss << " NOT ";
oss << " ST_Contains(" << geom_str << ", line2d_m) ";
if (!within) oss << " AND ST_Intersects(" << geom_str << ", line2d_m) ";
oss << " ORDER BY id";
if (limit > 0) oss << " LIMIT " << limit;
cout << oss.str() << endl;
return oss.str();
}
string PG::build_pg_geom(const GEOSGeometry* geom) const
{
GEOSWKTWriter* wkt_writer = GEOSWKTWriter_create_r(hdl);
char* hex = GEOSWKTWriter_write_r(hdl, wkt_writer, geom);
ostringstream oss_geom;
oss_geom << " ST_SetSRID('" << hex << "'::geometry, 3395) ";
GEOSFree_r(hdl, hex);
GEOSWKTWriter_destroy_r(hdl, wkt_writer);
return oss_geom.str();
}
} // namespace cma