GEOS  3.8.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 #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/BinaryOp.h>
28 #include <geos/geom/Point.h>
29 #include <geos/geom/LineString.h>
30 #include <geos/geom/Polygon.h>
31 #include <geos/geom/util/GeometryExtracter.h>
32 #include <geos/operation/overlay/OverlayOp.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
41 namespace geos {
42 namespace geom {
43 class GeometryFactory;
44 class Geometry;
45 }
46 }
47 
48 namespace geos {
49 namespace operation { // geos::operation
50 namespace geounion { // geos::operation::geounion
51 
85 class GEOS_DLL UnaryUnionOp {
86 public:
87 
88  template <typename T>
89  static std::unique_ptr<geom::Geometry>
90  Union(const T& geoms)
91  {
92  UnaryUnionOp op(geoms);
93  return op.Union();
94  }
95 
96  template <class T>
97  static std::unique_ptr<geom::Geometry>
98  Union(const T& geoms,
99  geom::GeometryFactory& geomFact)
100  {
101  UnaryUnionOp op(geoms, geomFact);
102  return op.Union();
103  }
104 
105  static std::unique_ptr<geom::Geometry>
106  Union(const geom::Geometry& geom)
107  {
108  UnaryUnionOp op(geom);
109  return op.Union();
110  }
111 
112  template <class T>
113  UnaryUnionOp(const T& geoms, geom::GeometryFactory& geomFactIn)
114  :
115  geomFact(&geomFactIn)
116  {
117  extractGeoms(geoms);
118  }
119 
120  template <class T>
121  UnaryUnionOp(const T& geoms)
122  :
123  geomFact(nullptr)
124  {
125  extractGeoms(geoms);
126  }
127 
128  UnaryUnionOp(const geom::Geometry& geom)
129  :
130  geomFact(geom.getFactory())
131  {
132  extract(geom);
133  }
134 
145  std::unique_ptr<geom::Geometry> Union();
146 
147 private:
148 
149  template <typename T>
150  void
151  extractGeoms(const T& geoms)
152  {
153  for(typename T::const_iterator
154  i = geoms.begin(),
155  e = geoms.end();
156  i != e;
157  ++i) {
158  const geom::Geometry* geom = *i;
159  extract(*geom);
160  }
161  }
162 
163  void
164  extract(const geom::Geometry& geom)
165  {
166  using namespace geom::util;
167 
168  if(! geomFact) {
169  geomFact = geom.getFactory();
170  }
171 
172  GeometryExtracter::extract<geom::Polygon>(geom, polygons);
173  GeometryExtracter::extract<geom::LineString>(geom, lines);
174  GeometryExtracter::extract<geom::Point>(geom, points);
175  }
176 
189  std::unique_ptr<geom::Geometry>
190  unionNoOpt(const geom::Geometry& g0)
191  {
193  //using geos::operation::overlay::snap::SnapIfNeededOverlayOp;
194 
195  if(! empty.get()) {
196  empty.reset(geomFact->createEmptyGeometry());
197  }
198  //return SnapIfNeededOverlayOp::overlayOp(g0, *empty, OverlayOp::opUNION);
199  return BinaryOp(&g0, empty.get(), overlay::overlayOp(OverlayOp::opUNION));
200  }
201 
211  std::unique_ptr<geom::Geometry> unionWithNull(std::unique_ptr<geom::Geometry> g0,
212  std::unique_ptr<geom::Geometry> g1);
213 
214  std::vector<const geom::Polygon*> polygons;
215  std::vector<const geom::LineString*> lines;
216  std::vector<const geom::Point*> points;
217 
218  const geom::GeometryFactory* geomFact;
219 
220  std::unique_ptr<geom::Geometry> empty;
221 };
222 
223 
224 } // namespace geos::operation::union
225 } // namespace geos::operation
226 } // namespace geos
227 
228 #ifdef _MSC_VER
229 #pragma warning(pop)
230 #endif
231 
232 #endif
Definition: UnaryUnionOp.h:85
Computes the geometric overlay of two Geometry.
Definition: OverlayOp.h:69
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition: Geometry.h:187
const GeometryFactory * getFactory() const
Gets the factory which contains the context in which this geometry was created.
Definition: Geometry.h:217
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition: GeometryFactory.h:67
OverlayOp::overlayOp Adapter for use with geom::BinaryOp.
Definition: OverlayOp.h:405
Basic namespace for all GEOS functionalities.
Definition: IndexedNestedRingTester.h:25
Geometry * createEmptyGeometry() const
Construct the EMPTY Geometry.