GEOS 3.14.0dev
UnaryUnionOp.h
1/**********************************************************************
2 *
3 * GEOS - Geometry Engine Open Source
4 * http://geos.osgeo.org
5 *
6 * Copyright (C) 2011 Sandro Santilli <strk@kbt.io>
7 *
8 * This is free software; you can redistribute and/or modify it under
9 * the terms of the GNU Lesser General Public Licence as published
10 * by the Free Software Foundation.
11 * See the COPYING file for more information.
12 *
13 **********************************************************************
14 *
15 * Last port: operation/union/UnaryUnionOp.java r320 (JTS-1.12)
16 *
17 **********************************************************************/
18
19#pragma once
20
21#include <memory>
22#include <vector>
23
24#include <geos/export.h>
25#include <geos/geom/GeometryFactory.h>
26#include <geos/geom/Point.h>
27#include <geos/geom/LineString.h>
28#include <geos/geom/Polygon.h>
29#include <geos/geom/util/GeometryExtracter.h>
30#include <geos/operation/union/CascadedPolygonUnion.h>
31
32#include <geos/util.h>
33
34#ifdef _MSC_VER
35#pragma warning(push)
36#pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
37#endif
38
39// Forward declarations
40namespace geos {
41namespace geom {
42class GeometryFactory;
43class Geometry;
44}
45}
46
47namespace geos {
48namespace operation { // geos::operation
49namespace geounion { // geos::operation::geounion
50
88class GEOS_DLL UnaryUnionOp {
89public:
90
91 template <typename T>
92 static std::unique_ptr<geom::Geometry>
93 Union(const T& geoms)
94 {
95 UnaryUnionOp op(geoms);
96 return op.Union();
97 }
98
99 template <class T>
100 static std::unique_ptr<geom::Geometry>
101 Union(const T& geoms,
102 geom::GeometryFactory& geomFact)
103 {
104 UnaryUnionOp op(geoms, geomFact);
105 return op.Union();
106 }
107
108 static std::unique_ptr<geom::Geometry>
109 Union(const geom::Geometry& geom)
110 {
111 UnaryUnionOp op(geom);
112 return op.Union();
113 }
114
115 template <class T>
116 UnaryUnionOp(const T& geoms, geom::GeometryFactory& geomFactIn)
117 : geomFact(&geomFactIn)
118 , unionFunction(&defaultUnionFunction)
119 {
120 extractGeoms(geoms);
121 }
122
123 template <class T>
124 UnaryUnionOp(const T& geoms)
125 : geomFact(nullptr)
126 , unionFunction(&defaultUnionFunction)
127 {
128 extractGeoms(geoms);
129 }
130
131 UnaryUnionOp(const geom::Geometry& geom)
132 : geomFact(geom.getFactory())
133 , unionFunction(&defaultUnionFunction)
134 {
135 extract(geom);
136 }
137
138 void setUnionFunction(UnionStrategy* unionFun)
139 {
140 unionFunction = unionFun;
141 }
142
153 std::unique_ptr<geom::Geometry> Union();
154
155private:
156
157 template <typename T>
158 void
159 extractGeoms(const T& geoms)
160 {
161 for(typename T::const_iterator
162 i = geoms.begin(),
163 e = geoms.end();
164 i != e;
165 ++i) {
166 const geom::Geometry* geom = *i;
167 extract(*geom);
168 }
169 }
170
171 void
172 extract(const geom::Geometry& geom)
173 {
174 util::ensureNoCurvedComponents(geom);
175
176 using namespace geom::util;
177
178 if(! geomFact) {
179 geomFact = geom.getFactory();
180 }
181
182 GeometryExtracter::extract<geom::Polygon>(geom, polygons);
183 GeometryExtracter::extract<geom::LineString>(geom, lines);
184 GeometryExtracter::extract<geom::Point>(geom, points);
185 }
186
199 std::unique_ptr<geom::Geometry>
200 unionNoOpt(const geom::Geometry& g0)
201 {
202 if(! empty.get()) {
203 empty = geomFact->createEmptyGeometry();
204 }
205 return unionFunction->Union(&g0, empty.get());
206 }
207
217 std::unique_ptr<geom::Geometry> unionWithNull(
218 std::unique_ptr<geom::Geometry> g0,
219 std::unique_ptr<geom::Geometry> g1
220 );
221
222 // Members
223 std::vector<const geom::Polygon*> polygons;
224 std::vector<const geom::LineString*> lines;
225 std::vector<const geom::Point*> points;
226
227 const geom::GeometryFactory* geomFact;
228 std::unique_ptr<geom::Geometry> empty;
229
230 UnionStrategy* unionFunction;
231 ClassicUnionStrategy defaultUnionFunction;
232
233};
234
235
236} // namespace geos::operation::union
237} // namespace geos::operation
238} // namespace geos
239
240#ifdef _MSC_VER
241#pragma warning(pop)
242#endif
243
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition GeometryFactory.h:70
std::unique_ptr< Geometry > createEmptyGeometry(GeometryTypeId type=GEOS_GEOMETRYCOLLECTION, bool hasZ=false, bool hasM=false) const
Construct the EMPTY Geometry.
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition Geometry.h:197
const GeometryFactory * getFactory() const
Gets the factory which contains the context in which this geometry was created.
Definition Geometry.h:227
Unions a collection of Geometry or a single Geometry (which may be a collection) together.
Definition UnaryUnionOp.h:88
std::unique_ptr< geom::Geometry > Union()
Gets the union of the input geometries.
Definition UnionStrategy.h:40
Basic namespace for all GEOS functionalities.
Definition geos.h:39