-
Notifications
You must be signed in to change notification settings - Fork 24
/
prepair.cpp
254 lines (234 loc) · 9.7 KB
/
prepair.cpp
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
// prepair
//
// Copyright © 2009-2022,
// Ken Arroyo Ohori [email protected]
// Hugo Ledoux [email protected]
// Martijn Meijers [email protected]
// All rights reserved.
//
// 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 3 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 for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <iostream>
#include <fstream>
#include <boost/program_options.hpp>
#include <ogrsf_frmts.h>
#include "Polygon_repair.h"
int main(int argc, const char *argv[]) {
boost::program_options::options_description input_options("input options");
input_options.add_options()
("wkt,w", boost::program_options::value<std::string>()->value_name("'POLYGON(...)'"), "Read WKT passed directly as a parameter")
("wktfile,t", boost::program_options::value<std::string>()->value_name("PATH"), "Read text file containing one WKT per line")
("ogrin,i", boost::program_options::value<std::string>()->value_name("path"), "Read data source using OGR")
("help,h", "View all options");
boost::program_options::options_description output_options("output options");
output_options.add_options()
("ogrout,o", boost::program_options::value<std::string>()->value_name("path"), "Output to a file using OGR");
boost::program_options::options_description all_options;
all_options.add(input_options).add(output_options);
boost::program_options::variables_map vm;
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, all_options), vm);
boost::program_options::notify(vm);
GDALDataset *in_dataset = NULL;
std::ifstream in_file;
OGRGeometry *in_geometry = NULL;
// Help or invalid args
if (argc < 2 || vm.count("help")) {
std::cout << "usage: prepair input_option [output_option]" << std::endl;
std::cout << "if no output option is provided, prepair will output WKT directly" << std::endl << std::endl;
std::cout << input_options << std::endl;
std::cout << output_options << std::endl;
return 0;
}
// Input
if (vm.count("wkt")) {
std::string wkt = vm["wkt"].as<std::string>();
OGRErr error = OGRGeometryFactory::createFromWkt(wkt.c_str(), NULL, &in_geometry);
if (error != OGRERR_NONE) {
switch (error) {
case OGRERR_UNSUPPORTED_GEOMETRY_TYPE:
std::cerr << "Error: unsupported geometry" << std::endl;
break;
case OGRERR_NOT_ENOUGH_DATA:
std::cerr << "Error: degenerate input" << std::endl;
break;
case OGRERR_CORRUPT_DATA:
std::cerr << "Error: corrupted input" << std::endl;
break;
default:
std::cerr << "Error" << std::endl;
break;
} return 1;
} if (in_geometry->IsEmpty()) {
std::cerr << "Error: empty geometry" << std::endl;
return 1;
} if ((in_geometry->getGeometryType() != wkbPolygon) &&
(in_geometry->getGeometryType() != wkbPolygon25D) &&
(in_geometry->getGeometryType() != wkbMultiPolygon) &&
(in_geometry->getGeometryType() != wkbMultiPolygon25D)) {
std::cerr << "Error: geometry must be Polygon or MultiPolygon (in 2D or 3D)" << std::endl;
return 1;
}
}
else if (vm.count("wktfile")) {
in_file.open(vm["wktfile"].as<std::string>().c_str(), std::ios::in);
if (!in_file.is_open()) {
std::cerr << "Error: couldn't open file" << std::endl;
return 1;
}
}
else if (vm.count("ogrin")) {
GDALAllRegister();
in_dataset = (GDALDataset *)GDALOpenEx(vm["ogrin"].as<std::string>().c_str(), GDAL_OF_VECTOR, NULL, NULL, NULL);
if (in_dataset == NULL) {
std::cerr << "Error: couldn't open file" << std::endl;
return 1;
}
}
// Output
if (vm.count("ogrout")) {
GDALAllRegister();
std::filesystem::path extension = std::filesystem::path(vm["ogrout"].as<std::string>()).extension();
std::string out_driver_name;
if (extension.compare(".csv") == 0) out_driver_name = "CSV";
else if (extension.compare(".dxf") == 0) out_driver_name = "DXF";
else if (extension.compare(".gdb") == 0) out_driver_name = "FileGDB";
else if (extension.compare(".json") == 0) out_driver_name = "GeoJSON";
else if (extension.compare(".geojson") == 0) out_driver_name = "GeoJSON";
else if (extension.compare(".gml") == 0) out_driver_name = "GML";
else if (extension.compare(".gpkg") == 0) out_driver_name = "GPKG";
else if (extension.compare(".kml") == 0) out_driver_name = "KML";
else if (extension.compare(".shp") == 0) out_driver_name = "ESRI Shapefile";
else {
std::cout << "Error: unknown output format" << std::endl;
return 1;
} GDALDriver *out_driver = GetGDALDriverManager()->GetDriverByName(out_driver_name.c_str());
if (out_driver == NULL) {
std::cerr << "Error: OGR driver not found" << std::endl;
return 1;
}
if (std::filesystem::exists(std::filesystem::path(vm["ogrout"].as<std::string>()))) {
std::cout << "Overwriting " << out_driver_name << " file " << vm["ogrout"].as<std::string>() << "..." << std::endl;
if (out_driver->Delete(vm["ogrout"].as<std::string>().c_str())!= OGRERR_NONE) {
std::cerr << "Error: couldn't overwrite file" << std::endl;
return 1;
}
} else std::cout << "Writing " << out_driver_name << " file " << vm["ogrout"].as<std::string>() << "..." << std::endl;
GDALDataset *out_dataset = out_driver->Create(vm["ogrout"].as<std::string>().c_str(), 0, 0, 0, GDT_Unknown, NULL);
if (out_dataset == NULL) {
std::cout << "Error: couldn't create file" << std::endl;
return 1;
}
if (vm.count("wkt")) {
OGRLayer *out_layer = out_dataset->CreateLayer("polygons");
if (out_layer == NULL) {
std::cout << "Error: couldn't create layer." << std::endl;
return 1;
} OGRFeature *out_feature = OGRFeature::CreateFeature(out_layer->GetLayerDefn());
Polygon_repair pr;
pr.geometry = in_geometry;
pr.repair();
OGRGeometry *out_geometry = pr.geometry;
out_feature->SetGeometry(out_geometry);
if (out_layer->CreateFeature(out_feature) != OGRERR_NONE) {
std::cout << "Error: couldn't create feature." << std::endl;
return 1;
}
}
else if (vm.count("wktfile")) {
OGRLayer *out_layer = out_dataset->CreateLayer("polygons");
if (out_layer == NULL) {
std::cout << "Error: couldn't create layer." << std::endl;
return 1;
} std::string line;
while (std::getline(in_file, line)) {
OGRErr error = OGRGeometryFactory::createFromWkt(line.c_str(), NULL, &in_geometry);
if (error != OGRERR_NONE) {
OGRFeature *out_feature = OGRFeature::CreateFeature(out_layer->GetLayerDefn());
Polygon_repair pr;
pr.geometry = in_geometry;
pr.repair();
OGRGeometry *out_geometry = pr.geometry;
out_feature->SetGeometry(out_geometry);
if (out_layer->CreateFeature(out_feature) != OGRERR_NONE) {
std::cout << "Error: couldn't create feature." << std::endl;
return 1;
}
}
}
}
else if (vm.count("ogrin")) {
for (OGRLayer *layer: in_dataset->GetLayers()) {
OGRLayer *out_layer = out_dataset->CreateLayer(layer->GetName());
if (out_layer == NULL) {
std::cout << "Error: couldn't create layer." << std::endl;
return 1;
} for (OGRFeatureUniquePtr &feature: *layer) {
OGRFeature *out_feature = feature->Clone();
Polygon_repair pr;
pr.geometry = feature->GetGeometryRef();
pr.repair();
OGRGeometry *out_geometry = pr.geometry;
out_feature->SetGeometry(out_geometry);
if (out_layer->CreateFeature(out_feature) != OGRERR_NONE) {
std::cout << "Error: couldn't create feature." << std::endl;
return 1;
}
}
}
}
GDALClose(out_dataset);
}
else {
if (vm.count("wkt")) {
Polygon_repair pr;
pr.geometry = in_geometry;
pr.repair();
OGRGeometry *out_geometry = pr.geometry;
char *output_wkt;
out_geometry->exportToWkt(&output_wkt);
std::cout << output_wkt << std::endl;
delete output_wkt;
}
else if (vm.count("wktfile")) {
std::string line;
while (std::getline(in_file, line)) {
OGRErr error = OGRGeometryFactory::createFromWkt(line.c_str(), NULL, &in_geometry);
if (error != OGRERR_NONE) {
Polygon_repair pr;
pr.geometry = in_geometry;
pr.repair();
OGRGeometry *out_geometry = pr.geometry;
char *output_wkt;
out_geometry->exportToWkt(&output_wkt);
std::cout << output_wkt << std::endl;
delete output_wkt;
} else std::cout << std::endl;
}
}
else if (vm.count("ogrin")) {
for (OGRLayer *layer: in_dataset->GetLayers()) {
for (OGRFeatureUniquePtr &feature: *layer) {
Polygon_repair pr;
pr.geometry = feature->GetGeometryRef();
pr.repair();
OGRGeometry *out_geometry = pr.geometry;
char *output_wkt;
out_geometry->exportToWkt(&output_wkt);
std::cout << output_wkt << std::endl;
delete output_wkt;
}
}
}
}
return 0;
}