GEOS  3.13.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 #ifdef _MSC_VER
33 #pragma warning(push)
34 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
35 #endif
36 
37 // Forward declarations
38 namespace geos {
39 namespace geom {
40 class GeometryFactory;
41 class Geometry;
42 }
43 }
44 
45 namespace geos {
46 namespace operation { // geos::operation
47 namespace geounion { // geos::operation::geounion
48 
86 class GEOS_DLL UnaryUnionOp {
87 public:
88 
89  template <typename T>
90  static std::unique_ptr<geom::Geometry>
91  Union(const T& geoms)
92  {
93  UnaryUnionOp op(geoms);
94  return op.Union();
95  }
96 
97  template <class T>
98  static std::unique_ptr<geom::Geometry>
99  Union(const T& geoms,
100  geom::GeometryFactory& geomFact)
101  {
102  UnaryUnionOp op(geoms, geomFact);
103  return op.Union();
104  }
105 
106  static std::unique_ptr<geom::Geometry>
107  Union(const geom::Geometry& geom)
108  {
109  UnaryUnionOp op(geom);
110  return op.Union();
111  }
112 
113  template <class T>
114  UnaryUnionOp(const T& geoms, geom::GeometryFactory& geomFactIn)
115  : geomFact(&geomFactIn)
116  , unionFunction(&defaultUnionFunction)
117  {
118  extractGeoms(geoms);
119  }
120 
121  template <class T>
122  UnaryUnionOp(const T& geoms)
123  : geomFact(nullptr)
124  , unionFunction(&defaultUnionFunction)
125  {
126  extractGeoms(geoms);
127  }
128 
129  UnaryUnionOp(const geom::Geometry& geom)
130  : geomFact(geom.getFactory())
131  , unionFunction(&defaultUnionFunction)
132  {
133  extract(geom);
134  }
135 
136  void setUnionFunction(UnionStrategy* unionFun)
137  {
138  unionFunction = unionFun;
139  }
140 
151  std::unique_ptr<geom::Geometry> Union();
152 
153 private:
154 
155  template <typename T>
156  void
157  extractGeoms(const T& geoms)
158  {
159  for(typename T::const_iterator
160  i = geoms.begin(),
161  e = geoms.end();
162  i != e;
163  ++i) {
164  const geom::Geometry* geom = *i;
165  extract(*geom);
166  }
167  }
168 
169  void
170  extract(const geom::Geometry& geom)
171  {
172  using namespace geom::util;
173 
174  if(! geomFact) {
175  geomFact = geom.getFactory();
176  }
177 
178  GeometryExtracter::extract<geom::Polygon>(geom, polygons);
179  GeometryExtracter::extract<geom::LineString>(geom, lines);
180  GeometryExtracter::extract<geom::Point>(geom, points);
181  }
182 
195  std::unique_ptr<geom::Geometry>
196  unionNoOpt(const geom::Geometry& g0)
197  {
198  if(! empty.get()) {
199  empty = geomFact->createEmptyGeometry();
200  }
201  return unionFunction->Union(&g0, empty.get());
202  }
203 
213  std::unique_ptr<geom::Geometry> unionWithNull(
214  std::unique_ptr<geom::Geometry> g0,
215  std::unique_ptr<geom::Geometry> g1
216  );
217 
218  // Members
219  std::vector<const geom::Polygon*> polygons;
220  std::vector<const geom::LineString*> lines;
221  std::vector<const geom::Point*> points;
222 
223  const geom::GeometryFactory* geomFact;
224  std::unique_ptr<geom::Geometry> empty;
225 
226  UnionStrategy* unionFunction;
227  ClassicUnionStrategy defaultUnionFunction;
228 
229 };
230 
231 
232 } // namespace geos::operation::union
233 } // namespace geos::operation
234 } // namespace geos
235 
236 #ifdef _MSC_VER
237 #pragma warning(pop)
238 #endif
239 
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition: GeometryFactory.h:65
std::unique_ptr< Geometry > createEmptyGeometry() const
Construct the EMPTY Geometry.
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition: Geometry.h:186
const GeometryFactory * getFactory() const
Gets the factory which contains the context in which this geometry was created.
Definition: Geometry.h:216
Unions a collection of Geometry or a single Geometry (which may be a collection) together.
Definition: UnaryUnionOp.h:86
std::unique_ptr< geom::Geometry > Union()
Gets the union of the input geometries.
Definition: UnionStrategy.h:40
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25