Repository navigation
Expand file tree
/
Copy pathextract.cpp
More file actions
186 lines (170 loc) · 5.72 KB
/
Copy pathextract.cpp
File metadata and controls
186 lines (170 loc) · 5.72 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
#include <fstream>
#include <iostream>
#include "util.h"
#include "osmfile.h"
#include "cppGzip/EncodeGzip.h"
#include "cppo5m/model.h"
#include "cppo5m/osmxml.h"
#include "pgmap.h"
#include <boost/program_options.hpp>
namespace po = boost::program_options;
int main(int argc, char **argv)
{
auto started = std::chrono::steady_clock::now();
// Declare the supported options.
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("wkt", po::value<string>(), "WKT polygon file name")
("bbox", po::value<string>(), "bbox shape (e.g -1.078,50.788,-1.074,50.790)")
("buffer", po::value<double>(), "Grow the --wkt polygon outwards by this many metres first")
("save-db", "Save the extract to the database instead of a file, within --bbox or the --wkt polygon")
("name", po::value<string>()->default_value(""), "Optional database extract name")
("out", po::value<string>(), (string("Output file name, ending in ") + OsmFileWriter::SupportedNames()).c_str())
("no-update-info", "Leave out of the file header what keeping it up to date needs: the edit IDs it is current to, "
"its area (--bbox or the --wkt boundary) and the server to update from (public_url in config.cfg).")
("edit-ids", "Kept for scripts that give it: the edit IDs are written unless --no-update-info is given")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help")) {
cout << desc << "\n";
return 1;
}
string wkt;
if (vm.count("wkt"))
{
string wktFina = vm["wkt"].as<string>();
ReadFileContents(wktFina.c_str(), false, wkt);
}
vector<double> bbox;
if (vm.count("bbox"))
{
vector<string> bboxvals = split(vm["bbox"].as<string>(), ',');
for(size_t i=0; i < bboxvals.size(); i++)
{
try {
size_t consumed = 0;
double value = stod(bboxvals[i], &consumed);
if(consumed != bboxvals[i].size()) throw invalid_argument("trailing characters");
bbox.push_back(value);
} catch(const exception &) {
cerr << "Invalid bbox coordinate" << endl;
return 2;
}
}
if(bbox.size() != 4)
{
cerr << "Bbox must have 4 numbers" << endl;
exit(-1);
}
}
if(bbox.size() == 0 && wkt.size() == 0)
{
cerr << "Bbox or WKT filename must be specified" << endl;
exit(-2);
}
double buffer = vm.count("buffer") ? vm["buffer"].as<double>() : 0;
if(vm.count("buffer") && wkt.empty())
{
cerr << "--buffer needs --wkt" << endl;
return 2;
}
if(vm.count("save-db"))
{
if((bbox.size() == 4) == (wkt.size() > 0) || vm.count("out") || vm.count("edit-ids") || vm.count("no-update-info"))
{
cerr << "--save-db requires one of --bbox and --wkt, and cannot be combined with --out, --edit-ids or "
"--no-update-info" << endl;
return 2;
}
try
{
map<string,string> config;
ReadSettingsFile("config.cfg", config);
PgMap map(GeneratePgConnectionString(config), config["dbtableprefix"],
config["dbtablemodifyprefix"], config["dbtablemodifyprefix"], config["dbtabletestprefix"]);
auto transaction = map.GetTransaction("ACCESS SHARE");
if(vm.count("buffer")) wkt = transaction->BufferExtractBoundary(wkt, buffer);
int64_t id = wkt.size() > 0 ? transaction->SaveExtractWkt(wkt, vm["name"].as<string>()) :
transaction->SaveExtract(bbox, vm["name"].as<string>());
transaction->Commit();
cout << "Saved database extract " << id << " in " << Took(started) << endl;
return 0;
}
catch(const exception &error)
{
cerr << "Database extract failed after " << Took(started) << ": " << error.what() << endl;
return 1;
}
}
string outFina = "extract.o5m.gz";
if (vm.count("out"))
{
outFina = vm["out"].as<string>();
}
try
{
OsmFileWriter::FormatOf(outFina); // An unusable name is refused before any work
}
catch(const invalid_argument &error)
{
cerr << error.what() << endl;
return 2;
}
bool updateInfo = !vm.count("no-update-info");
cout << "Reading settings from config.cfg" << endl;
std::map<string, string> config;
ReadSettingsFile("config.cfg", config);
string cstr = GeneratePgConnectionString(config);
class PgMap pgMap(cstr, config["dbtableprefix"], config["dbtablemodifyprefix"], config["dbtablemodifyprefix"], config["dbtabletestprefix"]);
if (pgMap.Ready()) {
cout << "Opened database successfully" << endl;
} else {
cout << "Can't open database" << endl;
return 1;
}
try
{
std::shared_ptr<class PgTransaction> transaction = pgMap.GetTransaction("ACCESS SHARE");
if(vm.count("buffer")) wkt = transaction->BufferExtractBoundary(wkt, buffer);
// The edit IDs come from the same snapshot as the map query below.
TagMap rootAttribs;
if(updateInfo)
{
rootAttribs = transaction->GetLatestEditIdAttribs();
if(bbox.size() == 4)
rootAttribs["bbox"] = ShortestDouble(bbox[0]) + "," + ShortestDouble(bbox[1]) + "," +
ShortestDouble(bbox[2]) + "," + ShortestDouble(bbox[3]);
else
rootAttribs["boundary"] = transaction->BufferExtractBoundary(wkt, 0); // As stored: checked and tidied
if(!config["public_url"].empty())
rootAttribs["update_url"] = config["public_url"];
}
OsmFileWriter writer(outFina, rootAttribs);
shared_ptr<IDataStreamHandler> enc = writer.Encoder();
std::shared_ptr<class PgMapQuery> mapQuery = transaction->GetQueryMgr();
int ret = 0;
if(bbox.size() > 0)
ret = mapQuery->Start(bbox, time(nullptr), enc);
else
ret = mapQuery->Start(wkt, time(nullptr), enc);
while(ret == 0)
{
ret = mapQuery->Continue();
}
if(ret < 0)
throw runtime_error("Map query failed");
mapQuery.reset();
enc.reset();
writer.Close();
}
catch(const exception &error)
{
cerr << "Extract failed after " << Took(started) << ": " << error.what() << endl;
return 1;
}
cout << "Wrote " << outFina << " in " << Took(started) << endl;
return 0;
}