Repository navigation
Expand file tree
/
Copy pathutil.cpp
More file actions
371 lines (335 loc) · 10.9 KB
/
Copy pathutil.cpp
File metadata and controls
371 lines (335 loc) · 10.9 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include <charconv>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include "util.h"
#include <cstring>
#include <cctype>
#include "cppo5m/io.h"
using namespace std;
int ReadFileContents(const char *filename, int binaryMode, std::string &contentOut)
{
contentOut = "";
std::ios_base::openmode mode = (std::ios_base::openmode)0;
if(binaryMode)
mode ^= std::ios::binary;
std::ifstream file(filename, mode);
if(!file) return 0;
file.seekg(0,std::ios::end);
std::streampos length = file.tellg();
file.seekg(0,std::ios::beg);
contentOut.resize(length);
file.read(&contentOut[0],length);
return 1;
}
//http://stackoverflow.com/a/236803/4288232
void split2(const string &s, char delim, vector<string> &elems) {
stringstream ss;
ss.str(s);
string item;
while (getline(ss, item, delim)) {
elems.push_back(item);
}
}
vector<string> split(const string &s, char delim) {
vector<string> elems;
split2(s, delim, elems);
return elems;
}
// Inspired by PHP's implode and Python's join
string mergestr(const std::vector<string> &parts, const std::string &glue)
{
stringstream ss;
for(size_t i=0; i<parts.size(); i++)
{
if(i != 0)
ss << glue;
ss << parts[i];
}
return ss.str();
}
// A setting can be given in the environment instead of the config file, or to
// replace what the file says: the setting's name in capitals after PGMAP_, so
// PGMAP_DBHOST for dbhost and PGMAP_DUMP_PATH for dump_path. These two
// variables begin the same way and are not settings.
static const char *CONFIG_ENV_PREFIX = "PGMAP_";
static bool IsConfigEnvName(const std::string &name)
{
return name.compare(0, strlen(CONFIG_ENV_PREFIX), CONFIG_ENV_PREFIX) == 0 &&
name.size() > strlen(CONFIG_ENV_PREFIX) && name != "PGMAP_CONFIG" && name != "PGMAP_CFLAGS";
}
static std::string ConfigEnvName(const std::string &setting)
{
string name = CONFIG_ENV_PREFIX;
for(char ch : setting)
name.push_back((char)toupper((unsigned char)ch));
return name;
}
extern char **environ;
bool ReadSettingsFile(const std::string &settingsPath, std::map<std::string, std::string> &configOut)
{
string configContent;
bool found = ReadFileContents(settingsPath.c_str(), false, configContent);
if(found)
{
std::vector<std::string> lines = split(configContent, '\n');
for(size_t i=0; i < lines.size(); i++)
{
const std::string &line = lines[i];
std::vector<std::string> parts = split(line, ':');
if (parts.size() < 2) continue;
std::vector<std::string> partsSlice(++parts.begin(), parts.end());
string value = mergestr(partsSlice, ":");
configOut[parts[0]] = value;
}
}
// Settings given in the environment replace those of the file, and can
// stand in for a file that is not there
bool fromEnvironment = false;
for(char **entry = environ; entry != nullptr && *entry != nullptr; entry++)
{
string text(*entry);
size_t equals = text.find('=');
if(equals == string::npos) continue;
string name = text.substr(0, equals);
if(!IsConfigEnvName(name)) continue;
string setting;
for(char ch : name.substr(strlen(CONFIG_ENV_PREFIX)))
setting.push_back((char)tolower((unsigned char)ch));
configOut[setting] = text.substr(equals + 1);
fromEnvironment = true;
}
if(!found && !fromEnvironment)
cerr << "Warning: config file \"" << settingsPath << "\" could not be found" << endl;
return found || fromEnvironment;
}
void WriteSettingsFile(const std::string &settingsPath, const std::map<std::string, std::string> &config)
{
std::ofstream outfile;
outfile.open (settingsPath);
for(auto it=config.begin(); it!=config.end(); it++)
{
outfile << it->first << ":" << it->second << endl;
}
outfile.close();
}
//https://stackoverflow.com/a/15372760/4288232
void StrReplaceAll( string &s, const string &search, const string &replace ) {
for( size_t pos = 0; ; pos += replace.length() ) {
// Locate the substring to replace
pos = s.find( search, pos );
if( pos == string::npos ) break;
// Replace by erasing and inserting
s.erase( pos, search.length() );
s.insert( pos, replace );
}
}
// Connection strings should have escaped quotes
// https://www.postgresql.org/docs/9.6/static/libpq-connect.html
std::string EscapeQuotes(std::string str)
{
StrReplaceAll(str, "\"", "\\\"");
StrReplaceAll(str, "'", "\\'");
return str;
}
std::string GetConfigValue(const std::string &name,
const std::string &defaultValue)
{
// The environment comes first: see ReadSettingsFile
const char *given = getenv(ConfigEnvName(name).c_str());
if(given != nullptr)
return given;
const char *named = getenv("PGMAP_CONFIG");
string settingsPath = (named != nullptr && named[0] != '\0') ? named : "config.cfg";
string configContent;
if(!ReadFileContents(settingsPath.c_str(), false, configContent))
return defaultValue;
std::vector<std::string> lines = split(configContent, '\n');
bool found = false;
string value;
for(size_t i=0; i < lines.size(); i++)
{
// The same reading of a line as ReadSettingsFile: a name, a colon and
// the value, which may itself contain colons. A later line replaces
// an earlier one of the same name.
size_t colon = lines[i].find(':');
if(colon == string::npos || lines[i].substr(0, colon) != name) continue;
value = lines[i].substr(colon + 1);
found = true;
}
if(!found)
return defaultValue;
const char *blank = " \t\r";
size_t first = value.find_first_not_of(blank);
if(first == string::npos)
return "";
return value.substr(first, value.find_last_not_of(blank) - first + 1);
}
std::string GeneratePgConnectionString(std::map<std::string, std::string> config)
{
std::stringstream ss;
ss << "dbname=";
ss << "'" << EscapeQuotes(config["dbname"]) << "'";
ss << " user=";
ss << "'" << EscapeQuotes(config["dbuser"]) << "'";
ss << " password=";
ss << "'" << EscapeQuotes(config["dbpass"]) << "'";
ss << " hostaddr=";
ss << "'" << EscapeQuotes(config["dbhost"]) << "'";
// The port is optional in the settings file. It is checked here because,
// unlike the other values, it goes into the connection string unquoted.
string port = config["dbport"];
size_t first = port.find_first_not_of(" \t\r");
port = first == string::npos ? "" : port.substr(first, port.find_last_not_of(" \t\r") - first + 1);
if(port.empty())
port = "5432";
if(port.size() > 5 || port.find_first_not_of("0123456789") != string::npos ||
atoi(port.c_str()) < 1 || atoi(port.c_str()) > 65535)
throw invalid_argument("dbport in the settings file must be a port number from 1 to 65535");
ss << " port=";
ss << port;
return ss.str();
}
const char *OsmInputFileNames()
{
return ".osm, .o5m, .pbf or .json, optionally followed by .gz";
}
void LoadOsmFromFile(const std::string &filename, shared_ptr<class IDataStreamHandler> output)
{
//Check the name before opening anything, so a typing mistake is reported as one
OsmFormat format;
try
{
format = FormatFromFilename(filename);
}
catch(const invalid_argument &)
{
throw invalid_argument(string("Input file name must end in ") + OsmInputFileNames() + ": " + filename);
}
std::filebuf file;
if(!file.open(filename, std::ios::in | std::ios::binary))
throw runtime_error("Cannot open input file " + filename);
cout << "Reading from input " << filename << endl;
std::unique_ptr<DecodeGzip> gzip;
std::streambuf *stream = &file;
if(filename.size() > 3 && filename.compare(filename.size() - 3, 3, ".gz") == 0)
{
gzip.reset(new DecodeGzip(file));
stream = gzip.get();
}
//The decoder sends Finish to the handler when it reaches the end
std::unique_ptr<OsmDecoder> decoder = MakeDecoder(format, *stream, *output);
decoder->Decode();
}
// From https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
double long2tilex(double lon, int z)
{
return (lon + 180.0) / 360.0 * pow(2.0, z);
}
double lat2tiley(double lat, int z)
{
return (1.0 - log( tan(lat * M_PI/180.0) + 1.0 / cos(lat * M_PI/180.0)) / M_PI) / 2.0 * pow(2.0, z);
}
double tilex2long(int x, int z)
{
return x / pow(2.0, z) * 360.0 - 180;
}
double tiley2lat(int y, int z)
{
double n = M_PI - 2.0 * M_PI * y / pow(2.0, z);
return 180.0 / M_PI * atan(0.5 * (exp(n) - exp(-n)));
}
void FindOuterBbox(const std::vector<std::vector<double> > &bboxesIn, std::vector<double> &bboxOut)
{
for(size_t i=0; i<bboxesIn.size(); i++)
{
const std::vector<double> &bi = bboxesIn[i];
if(bi.size() != 4) continue;
if(bboxOut.size() == 0)
{
bboxOut = bi;
}
else
{
if(bi[0] < bboxOut[0]) bboxOut[0] = bi[0];
if(bi[1] < bboxOut[1]) bboxOut[1] = bi[1];
if(bi[2] > bboxOut[2]) bboxOut[2] = bi[2];
if(bi[3] > bboxOut[3]) bboxOut[3] = bi[3];
}
}
}
void FindOuterBboxes(const std::vector<std::vector<double> > &bboxesIn,
std::vector<std::vector<double> > &bboxesOut)
{
bboxesOut.clear();
std::vector<const std::vector<double> *> boxes;
for(const std::vector<double> &box : bboxesIn)
if(box.size() == 4)
boxes.push_back(&box);
if(boxes.empty())
return;
// The stretches of longitude covered, west to east, overlaps merged
std::sort(boxes.begin(), boxes.end(), [](const std::vector<double> *a, const std::vector<double> *b) {
return (*a)[0] < (*b)[0]; });
std::vector<std::pair<double, double> > covered;
for(const std::vector<double> *box : boxes)
{
if(!covered.empty() && (*box)[0] <= covered.back().second)
covered.back().second = std::max(covered.back().second, (*box)[2]);
else
covered.push_back({(*box)[0], (*box)[2]});
}
// The widest gap between them. The gap across the antimeridian wins a
// tie, so that one box is kept when two would be no smaller.
double widest = (covered.front().first + 360.0) - covered.back().second;
size_t after = covered.size();
for(size_t i = 0; i + 1 < covered.size(); i++)
{
double gap = covered[i + 1].first - covered[i].second;
if(gap > widest)
{
widest = gap;
after = i;
}
}
if(after == covered.size())
{
// The stretch does not cross the antimeridian: one box
std::vector<double> outer;
FindOuterBbox(bboxesIn, outer);
bboxesOut.push_back(outer);
return;
}
// It does: the boxes west of the gap, and those east of it
double split = covered[after].second;
std::vector<std::vector<double> > west, east;
for(const std::vector<double> *box : boxes)
((*box)[2] <= split ? west : east).push_back(*box);
for(const std::vector<std::vector<double> > *side : {&west, &east})
{
std::vector<double> outer;
FindOuterBbox(*side, outer);
bboxesOut.push_back(outer);
}
}
std::string Took(std::chrono::steady_clock::time_point started)
{
double seconds = std::chrono::duration<double>(std::chrono::steady_clock::now() - started).count();
char text[64];
if(seconds < 60)
snprintf(text, sizeof(text), "%.1fs", seconds);
else if(seconds < 3600)
snprintf(text, sizeof(text), "%dm %02ds", (int)seconds / 60, (int)seconds % 60);
else
snprintf(text, sizeof(text), "%dh %02dm %02ds", (int)seconds / 3600, ((int)seconds / 60) % 60, (int)seconds % 60);
return text;
}
std::string ShortestDouble(double value)
{
char text[32];
auto end = std::to_chars(text, text + sizeof(text), value).ptr;
return std::string(text, end);
}