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 
89 class GEOS_DLL UnaryUnionOp {
90 public:
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  :
119  geomFact(&geomFactIn)
120  {
121  extractGeoms(geoms);
122  }
123 
124  template <class T>
125  UnaryUnionOp(const T& geoms)
126  :
127  geomFact(nullptr)
128  {
129  extractGeoms(geoms);
130  }
131 
132  UnaryUnionOp(const geom::Geometry& geom)
133  :
134  geomFact(geom.getFactory())
135  {
136  extract(geom);
137  }
138 
149  std::unique_ptr<geom::Geometry> Union();
150 
151 private:
152 
153  template <typename T>
154  void
155  extractGeoms(const T& geoms)
156  {
157  for(typename T::const_iterator
158  i = geoms.begin(),
159  e = geoms.end();
160  i != e;
161  ++i) {
162  const geom::Geometry* geom = *i;
163  extract(*geom);
164  }
165  }
166 
167  void
168  extract(const geom::Geometry& geom)
169  {
170  using namespace geom::util;
171 
172  if(! geomFact) {
173  geomFact = geom.getFactory();
174  }
175 
176  GeometryExtracter::extract<geom::Polygon>(geom, polygons);
177  GeometryExtracter::extract<geom::LineString>(geom, lines);
178  GeometryExtracter::extract<geom::Point>(geom, points);
179  }
180 
193  std::unique_ptr<geom::Geometry>
194  unionNoOpt(const geom::Geometry& g0)
195  {
197  //using geos::operation::overlay::snap::SnapIfNeededOverlayOp;
198 
199  if(! empty.get()) {
200  empty.reset(geomFact->createEmptyGeometry());
201  }
202  //return SnapIfNeededOverlayOp::overlayOp(g0, *empty, OverlayOp::opUNION);
203  return BinaryOp(&g0, empty.get(), overlay::overlayOp(OverlayOp::opUNION));
204  }
205 
215  std::unique_ptr<geom::Geometry> unionWithNull(std::unique_ptr<geom::Geometry> g0,
216  std::unique_ptr<geom::Geometry> g1);
217 
218  std::vector<const geom::Polygon*> polygons;
219  std::vector<const geom::LineString*> lines;
220  std::vector<const geom::Point*> points;
221 
222  const geom::GeometryFactory* geomFact;
223 
224  std::unique_ptr<geom::Geometry> empty;
225 };
226 
227 
228 } // namespace geos::operation::union
229 } // namespace geos::operation
230 } // namespace geos
231 
232 #ifdef _MSC_VER
233 #pragma warning(pop)
234 #endif
235 
236 #endif
Unions a collection of Geometry or a single Geometry (which may be a collection) together.
Definition: UnaryUnionOp.h:89
Computes the geometric overlay of two Geometry.
Definition: OverlayOp.h:70
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
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition: GeometryFactory.h:66
OverlayOp::overlayOp Adapter for use with geom::BinaryOp.
Definition: OverlayOp.h:406
Basic namespace for all GEOS functionalities.
Definition: IndexedNestedRingTester.h:25
Geometry * createEmptyGeometry() const
Construct the EMPTY Geometry.