-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathGeosConverter.cpp
More file actions
284 lines (248 loc) · 9.02 KB
/
GeosConverter.cpp
File metadata and controls
284 lines (248 loc) · 9.02 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
//********************************************************************************************************
//File name: GeosConverter.cpp
//Description:
//********************************************************************************************************
//The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License");
//you may not use this file except in compliance with the License. You may obtain a copy of the License at
//http://www.mozilla.org/MPL/
//Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
//ANY KIND, either express or implied. See the License for the specific language governing rights and
//limitations under the License.
//
//The Original Code is MapWindow Open Source.
//
//The Initial Developer of this version of the Original Code is Daniel P. Ames using portions created by
//Utah State University and the Idaho National Engineering and Environmental Lab that were released as
//public domain in March 2004.
//
//Contributor(s): (Open source contributors should list themselves and their modifications here).
// -------------------------------------------------------------------------------------------------------
#include "StdAfx.h"
#include "GeosConverter.h"
#include "GeosHelper.h"
#include "OgrConverter.h"
// ReSharper disable CppUseAuto
// *********************************************************************
// DoBuffer()
// *********************************************************************
GEOSGeometry* DoBuffer(DOUBLE distance, long nQuadSegments, GEOSGeometry* gsGeom)
{
__try
{
return GeosHelper::Buffer(gsGeom, distance, nQuadSegments);
}
__except (1)
{
return nullptr;
}
}
// *********************************************************************
// GEOSGeom2Shape()
// *********************************************************************
bool GeosConverter::GeomToShapes(GEOSGeom gsGeom, vector<IShape*>* vShapes, bool isM)
{
bool substitute = false;
bool has25D = false;
if (!GeosHelper::IsValid(gsGeom))
{
// during buffer operation shape looses its Z coordinate, but during conversion back to
// shape it should be restored or shape type check won't be passed
has25D = GeosHelper::HasZ(gsGeom);
// would be better to pass units explicitly
// Fixing MWGIS-59: Fixup makes new shape larger:
// GEOSGeometry* gsNew = DoBuffer(m_globalSettings.GetInvalidShapeBufferDistance(umMeters) / 1000.0, 30, gsGeom);
GEOSGeometry* const gsNew = DoBuffer(0, 30, gsGeom);
if (gsNew && GeosHelper::IsValid(gsNew))
{
gsGeom = gsNew;
substitute = true; // it will be deleted below
}
}
bool result = false;
OGRGeometry* const oGeom = GeosHelper::CreateFromGEOS(gsGeom);
if (oGeom)
{
char* const type = GeosHelper::GetGeometryType(gsGeom);
const CString s = type;
GeosHelper::Free(type);
// PM: It seems oForceType is never used:
// auto oForceType = wkbNone;
// if (s == "LinearRing" && oGeom->getGeometryType() != wkbLinearRing)
// oForceType = wkbLinearRing;
result = OgrConverter::GeometryToShapes(oGeom, vShapes, isM, wkbNone, has25D);
OGRGeometryFactory::destroyGeometry(oGeom);
}
if (substitute)
GeosHelper::DestroyGeometry(gsGeom);
return result;
}
// *********************************************************************
// Shape2GEOSGeom()
// *********************************************************************
// Converts MapWinGis shape to GEOS geometry
GEOSGeom GeosConverter::ShapeToGeom(IShape* shp)
{
OGRGeometry* const oGeom = OgrConverter::ShapeToGeometry(shp);
if (oGeom != nullptr)
{
GEOSGeometry* const result = GeosHelper::ExportToGeos(oGeom);
OGRGeometryFactory::destroyGeometry(oGeom);
return result;
}
return nullptr;
}
// *****************************************************
// SimplifyPolygon()
// *****************************************************
// A polygon is expected as input; multi-polygons should be split into parts before treating with this routine
GEOSGeometry* GeosConverter::SimplifyPolygon(const GEOSGeometry *gsGeom, double tolerance)
{
const GEOSGeometry* gsRing = GeosHelper::GetExteriorRing(gsGeom); // no memory is allocated there
// ReSharper disable once CppLocalVariableMayBeConst
GEOSGeom gsPoly = GeosHelper::TopologyPreserveSimplify(gsRing, tolerance); // memory allocation
if (!gsPoly)
return nullptr;
std::vector<GEOSGeom> holes;
for (int n = 0; n < GeosHelper::GetNumInteriorRings(gsGeom); n++)
{
gsRing = GeosHelper::GetInteriorRingN(gsGeom, n); // no memory is allocated there
if (gsRing)
{
GEOSGeom gsOut = GeosHelper::TopologyPreserveSimplify(gsRing, tolerance); // memory allocation
if (gsOut)
{
char* type = GeosHelper::GetGeometryType(gsOut);
const CString s = type;
GeosHelper::Free(type);
if (s == "LinearRing")
holes.push_back(gsOut);
}
}
}
GEOSGeometry *gsNew;
if (!holes.empty())
{
gsNew = GeosHelper::CreatePolygon(gsPoly, &holes[0], holes.size()); // memory allocation (should be released by caller)
}
else
{
gsNew = GeosHelper::CreatePolygon(gsPoly, nullptr, 0);
}
return gsNew;
}
// *****************************************************
// NormalizeSplitResults()
// *****************************************************
void GeosConverter::NormalizeSplitResults(GEOSGeometry* result, GEOSGeometry* subject, ShpfileType shpType, vector<GEOSGeometry*>& results)
{
if (!result) return;
const int numGeoms = GeosHelper::GetNumGeometries(result);
if (numGeoms > 1)
{
if (shpType == SHP_POLYGON)
{
for (int i = 0; i < numGeoms; i++)
{
const GEOSGeometry* polygon = GeosHelper::GetGeometryN(result, i);
GEOSGeometry* intersect = GeosHelper::Intersection(subject, polygon);
if (!intersect)
continue;
double intersectArea;
GeosHelper::Area(intersect, &intersectArea);
GeosHelper::DestroyGeometry(intersect);
double polyArea;
GeosHelper::Area(polygon, &polyArea);
const double areaRatio = intersectArea / polyArea;
if (areaRatio > 0.99 && areaRatio < 1.01) {
GEOSGeometry* clone = GeosHelper::CloneGeometry(polygon);
if (clone) {
results.push_back(clone);
}
}
}
}
else {
for (int i = 0; i < numGeoms; i++) {
const GEOSGeometry* line = GeosHelper::GetGeometryN(result, i);
GEOSGeometry* clone = GeosHelper::CloneGeometry(line);
if (clone) {
results.push_back(clone);
}
}
}
}
}
// ********************************************************************
// MergeGeosGeometries
// ********************************************************************
// Returns GEOS geometry which is result of the union operation for the geometries passed
GEOSGeometry* GeosConverter::MergeGeometries(vector<GEOSGeometry*>& data, ICallback* callback, bool deleteInput /*= true*/, bool displayProgress /*= true*/)
{
if (data.empty())
return nullptr;
USES_CONVERSION;
GEOSGeometry* g1 = nullptr;
GEOSGeometry* g2 = nullptr;
bool stop = false;
int count = 0; // number of union operation performed
long percent = 0;
const int size = data.size();
int depth = 0;
if (size == 1)
{
// no need for calculation
if (deleteInput)
return data[0]; // no need to clone; it will be exactly the same
GEOSGeometry* geomTemp = GeosHelper::CloneGeometry(data[0]);
return geomTemp;
}
while (!stop)
{
stop = true;
for (int i = 0; i < size; i++)
{
if (data[i] != nullptr)
{
if (!g1)
{
g1 = data[i];
data[i] = nullptr;
}
else
{
g2 = data[i];
data[i] = nullptr;
}
if (g2 != nullptr)
{
GEOSGeometry* geom = GeosHelper::Union(g1, g2);
data[i] = geom; // placing the resulting geometry back for further processing
if (deleteInput || depth > 0) // in clipping operation geometries are used several times
// so the intial geometries should be intact (depth == 0)
// in other cases (Buffer, Dissolve) the geometries can be deleted in place
{
GeosHelper::DestroyGeometry(g1);
GeosHelper::DestroyGeometry(g2);
}
g1 = nullptr;
g2 = nullptr;
count++;
stop = false; // in case there is at least one union occurred, we shall run once more
if (displayProgress)
CallbackHelper::Progress(callback, count, size, "Merging shapes...", percent);
}
// it is the last geometry, unpaired one, not the only one, it's the initial and must not be deleted
if (i == size - 1 && !stop && g2 == nullptr && g1 != nullptr && depth == 0 && !deleteInput)
{
// we need to clone it, to be able to apply unified memory management afterwards
// when depth > 0 all interim geometries are deleted, while this one should be preserved
GEOSGeometry* geomTemp = GeosHelper::CloneGeometry(g1);
g1 = geomTemp;
}
}
}
depth++;
}
return g1;
}
// ReSharper restore CppUseAuto