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