forked from tudelft3d/prepair
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepair.cpp
214 lines (176 loc) · 6.82 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
/*
Copyright (c) 2009-2014,
Ken Arroyo Ohori [email protected]
Hugo Ledoux [email protected]
Martijn Meijers [email protected]
All rights reserved.
This file is part of prepair: 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.
Licensees holding a valid commercial license may use this file in
accordance with the commercial license agreement provided with
the software.
This file 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.
*/
#include "PolygonRepair.h"
double minArea = 0;
double isrTolerance = 0;
bool shpOut = false;
bool computeRobustness = false;
bool pointSet = false;
bool timeResults = false;
void usage();
bool savetoshp(OGRMultiPolygon* multiPolygon);
int main (int argc, const char * argv[]) {
time_t startTime = time(NULL);
if (argc < 2 || strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
usage();
return(0);
}
OGRGeometry *geometry;
for (int argNum = 1; argNum < argc; ++argNum) {
//-- whether to compute the robustness or not
if (strcmp(argv[argNum], "--robustness") == 0) {
computeRobustness = true;
}
//-- whether to use the point set topology paradigm or not
//-- if not, odd-even paradigm is used by default
else if (strcmp(argv[argNum], "--setdiff") == 0) {
pointSet = true;
}
//-- mininum area to keep in output
else if (strcmp(argv[argNum], "--minarea") == 0) {
minArea = atof(argv[argNum+1]);
++argNum;
}
//-- ISR snapping tolerance
else if (strcmp(argv[argNum], "--isr") == 0) {
isrTolerance = atof(argv[argNum+1]);
++argNum;
// TODO : scale dataset if tolerance < 1.0 because of CGAL bug
}
//-- output a shapefile (out.shp) instead of a WKT
else if (strcmp(argv[argNum], "--shpOut") == 0) {
shpOut = true;
}
//-- time the results
else if (strcmp(argv[argNum], "--time") == 0) {
timeResults = true;
}
//-- reading from WKT passed directly
else if (strcmp(argv[argNum], "--wkt") == 0) {
unsigned int bufferSize = 100000000;
char *inputWKT = (char *)malloc(bufferSize*sizeof(char *));
strcpy(inputWKT, argv[argNum+1]);
++argNum;
OGRGeometryFactory::createFromWkt(&inputWKT, NULL, &geometry);
if (geometry == NULL) {
std::cout << "Error: WKT is not valid" << std::endl;
return 1;
}
}
//-- reading from WKT stored in first line of a text file
else if (strcmp(argv[argNum], "-f") == 0) {
unsigned int bufferSize = 100000000;
char *inputWKT = (char *)malloc(bufferSize*sizeof(char *));
if (argNum + 1 <= argc - 1 && argv[argNum+1][0] != '-') {
std::ifstream infile(argv[argNum+1], std::ifstream::in);
infile.getline(inputWKT, bufferSize);
++argNum;
} else {
std::cerr << "Error: Missing input file name." << std::endl;
return 1;
}
OGRGeometryFactory::createFromWkt(&inputWKT, NULL, &geometry);
if (geometry == NULL) {
std::cout << "Error: WKT is not valid" << std::endl;
return 1;
}
}
//-- reading from a ogr dataset (most supported: shp, geojson, gml, etc)
else if (strcmp(argv[argNum], "--ogr") == 0) {
OGRRegisterAll();
OGRDataSource *dataSource = OGRSFDriverRegistrar::Open(argv[argNum+1], false);
++argNum;
if (dataSource == NULL) {
std::cerr << "Error: Could not open file." << std::endl;
return false;
}
OGRLayer *dataLayer = dataSource->GetLayer(0); //-- get first layer
dataLayer->ResetReading();
//-- Reads all features in this layer
OGRFeature *feature;
//-- get first feature
if (dataLayer->GetFeatureCount(true) > 1)
std::cout << "Reading only the first feature in the shapefile." << std::endl;
feature = dataLayer->GetNextFeature();
if (feature->GetGeometryRef()->getGeometryType() == wkbPolygon) {
geometry = static_cast<OGRPolygon *>(feature->GetGeometryRef());feature->GetGeometryRef();
}
else {
std::cout << "First feature ain't a POLYGON." << std::endl;
return(0);
}
}
else {
usage();
return(0);
}
}
if (!timeResults) startTime = 0;
PolygonRepair prepair;
//-- compute robustness
if (computeRobustness == true)
std::cout << "Robustness of input polygon: " << sqrt(prepair.computeRobustness(geometry)) <<std::endl;
OGRGeometry *snappedGeometry;
if (isrTolerance != 0) {
snappedGeometry = prepair.isr(geometry, isrTolerance);
} else {
snappedGeometry = geometry;
}
OGRMultiPolygon *outPolygons;
if (pointSet) {
outPolygons = prepair.repairPointSet(snappedGeometry, startTime);
} else {
outPolygons = prepair.repairOddEven(snappedGeometry, startTime);
}
if (minArea > 0) {
prepair.removeSmallPolygons(outPolygons, minArea);
}
//-- output well known text
if (shpOut) {
prepair.saveToShp(outPolygons, "out.shp");
}
else {
char *outputWKT;
outPolygons->exportToWkt(&outputWKT);
std::cout << outputWKT << std::endl;
}
//-- compute robustness
if (computeRobustness == true)
std::cout << "Robustness of output polygon: " << sqrt(prepair.computeRobustness()) <<std::endl;
//-- time results
if (timeResults) {
time_t totalTime = time(NULL)-startTime;
std::cout << "Done! Process finished in " << totalTime/60 << " minutes " << totalTime%60 << " seconds." << std::endl;
}
return 0;
}
void usage() {
std::cout << "=== prepair Help ===\n" << std::endl;
std::cout << "Usage: prepair --wkt 'POLYGON((0 0, 0 10, 10 0, 10 10, 0 0))'" << std::endl;
std::cout << "OR" << std::endl;
std::cout << "Usage: prepair -f infile.txt (infile.txt must contain one WKT on the 1st line)" << std::endl;
std::cout << "OR" << std::endl;
std::cout << "Usage: prepair --ogr infile.shp (first polygon of infile.shp is processed)" << std::endl;
std::cout << "================================================================================" << std::endl;
std::cout << "Additional options:" << std::endl;
std::cout << "--robustness Compute the robustness of the input/output" << std::endl;
std::cout << "--setdiff Uses the point set topology paradigm (default: odd-even paradigm)" << std::endl;
std::cout << "--minarea AREA Only output polygons larger than AREA" << std::endl;
std::cout << "--isr GRIDSIZE Snap round the input before repairing" << std::endl;
std::cout << "--time Benchmark the different stages of the process" << std::endl;
}