GEOS 3.9.1
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#ifndef GEOS_OP_UNION_UNARYUNION_H
20#define GEOS_OP_UNION_UNARYUNION_H
21
22#include <memory>
23#include <vector>
24
25#include <geos/export.h>
26#include <geos/geom/GeometryFactory.h>
27#include <geos/geom/Point.h>
28#include <geos/geom/LineString.h>
29#include <geos/geom/Polygon.h>
30#include <geos/geom/util/GeometryExtracter.h>
31#include <geos/operation/overlay/OverlayOp.h>
32#include <geos/operation/union/CascadedPolygonUnion.h>
33//#include <geos/operation/overlay/snap/SnapIfNeededOverlayOp.h>
34
35#ifdef _MSC_VER
36#pragma warning(push)
37#pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
38#endif
39
40// Forward declarations
41namespace geos {
42namespace geom {
43class GeometryFactory;
44class Geometry;
45}
46}
47
48namespace geos {
49namespace operation { // geos::operation
50namespace geounion { // geos::operation::geounion
51
89class GEOS_DLL UnaryUnionOp {
90public:
91
92 template <typename T>
93 static std::unique_ptr<geom::Geometry>
94 Union(const T& geoms)
95 {
96 UnaryUnionOp op(geoms);
97 return op.Union();
98 }
99
100 template <class T>
101 static std::unique_ptr<geom::Geometry>
102 Union(const T& geoms,
103 geom::GeometryFactory& geomFact)
104 {
105 UnaryUnionOp op(geoms, geomFact);
106 return op.Union();
107 }
108
109 static std::unique_ptr<geom::Geometry>
110 Union(const geom::Geometry& geom)
111 {
112 UnaryUnionOp op(geom);
113 return op.Union();
114 }
115
116 template <class T>
117 UnaryUnionOp(const T& geoms, geom::GeometryFactory& geomFactIn)
118 : geomFact(&geomFactIn)
119 , unionFunction(&defaultUnionFunction)
120 {
121 extractGeoms(geoms);
122 }
123
124 template <class T>
125 UnaryUnionOp(const T& geoms)
126 : geomFact(nullptr)
127 , unionFunction(&defaultUnionFunction)
128 {
129 extractGeoms(geoms);
130 }
131
132 UnaryUnionOp(const geom::Geometry& geom)
133 : geomFact(geom.getFactory())
134 , unionFunction(&defaultUnionFunction)
135 {
136 extract(geom);
137 }
138
139 void setUnionFunction(UnionStrategy* unionFun)
140 {
141 unionFunction = unionFun;
142 }
143
154 std::unique_ptr<geom::Geometry> Union();
155
156private:
157
158 template <typename T>
159 void
160 extractGeoms(const T& geoms)
161 {
162 for(typename T::const_iterator
163 i = geoms.begin(),
164 e = geoms.end();
165 i != e;
166 ++i) {
167 const geom::Geometry* geom = *i;
168 extract(*geom);
169 }
170 }
171
172 void
173 extract(const geom::Geometry& geom)
174 {
175 using namespace geom::util;
176
177 if(! geomFact) {
178 geomFact = geom.getFactory();
179 }
180
181 GeometryExtracter::extract<geom::Polygon>(geom, polygons);
182 GeometryExtracter::extract<geom::LineString>(geom, lines);
183 GeometryExtracter::extract<geom::Point>(geom, points);
184 }
185
198 std::unique_ptr<geom::Geometry>
199 unionNoOpt(const geom::Geometry& g0)
200 {
202 //using geos::operation::overlay::snap::SnapIfNeededOverlayOp;
203
204 if(! empty.get()) {
205 empty = geomFact->createEmptyGeometry();
206 }
207 //return SnapIfNeededOverlayOp::overlayOp(g0, *empty, OverlayOp::opUNION);
208 return unionFunction->Union(&g0, empty.get());
209 }
210
220 std::unique_ptr<geom::Geometry> unionWithNull(
221 std::unique_ptr<geom::Geometry> g0,
222 std::unique_ptr<geom::Geometry> g1
223 );
224
225 // Members
226 std::vector<const geom::Polygon*> polygons;
227 std::vector<const geom::LineString*> lines;
228 std::vector<const geom::Point*> points;
229
230 const geom::GeometryFactory* geomFact;
231 std::unique_ptr<geom::Geometry> empty;
232
233 UnionStrategy* unionFunction;
234 ClassicUnionStrategy defaultUnionFunction;
235
236};
237
238
239} // namespace geos::operation::union
240} // namespace geos::operation
241} // namespace geos
242
243#ifdef _MSC_VER
244#pragma warning(pop)
245#endif
246
247#endif
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition GeometryFactory.h:68
std::unique_ptr< Geometry > createEmptyGeometry() const
Construct the EMPTY Geometry.
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition Geometry.h:188
const GeometryFactory * getFactory() const
Gets the factory which contains the context in which this geometry was created.
Definition Geometry.h:218
Unions a collection of Geometry or a single Geometry (which may be a collection) together.
Definition UnaryUnionOp.h:89
std::unique_ptr< geom::Geometry > Union()
Gets the union of the input geometries.
Definition UnionStrategy.h:40
Computes the geometric overlay of two Geometry.
Definition OverlayOp.h:70
Basic namespace for all GEOS functionalities.
Definition IndexedNestedRingTester.h:26