GEOS  3.3.3
UnaryUnionOp.h
1 /**********************************************************************
2  * $Id$
3  *
4  * GEOS - Geometry Engine Open Source
5  * http://geos.refractions.net
6  *
7  * Copyright (C) 2011 Sandro Santilli <strk@keybit.net>
8  *
9  * This is free software; you can redistribute and/or modify it under
10  * the terms of the GNU Lesser General Public Licence as published
11  * by the Free Software Foundation.
12  * See the COPYING file for more information.
13  *
14  **********************************************************************
15  *
16  * Last port: operation/union/UnaryUnionOp.java r320 (JTS-1.12)
17  *
18  **********************************************************************/
19 
20 #ifndef GEOS_OP_UNION_UNARYUNION_H
21 #define GEOS_OP_UNION_UNARYUNION_H
22 
23 #include <memory>
24 #include <vector>
25 
26 #include <geos/export.h>
27 #include <geos/geom/GeometryFactory.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/snap/SnapIfNeededOverlayOp.h>
33 
34 #ifdef _MSC_VER
35 #pragma warning(push)
36 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
37 #endif
38 
39 // Forward declarations
40 namespace geos {
41  namespace geom {
42  class GeometryFactory;
43  class Geometry;
44  }
45 }
46 
47 namespace geos {
48 namespace operation { // geos::operation
49 namespace geounion { // geos::operation::geounion
50 
84 class GEOS_DLL UnaryUnionOp
85 {
86 public:
87 
88  template <typename T>
89  static std::auto_ptr<geom::Geometry> Union(const T& geoms)
90  {
91  UnaryUnionOp op(geoms);
92  return op.Union();
93  }
94 
95  template <class T>
96  static std::auto_ptr<geom::Geometry> Union(const T& geoms,
97  geom::GeometryFactory& geomFact)
98  {
99  UnaryUnionOp op(geoms, geomFact);
100  return op.Union();
101  }
102 
103  static std::auto_ptr<geom::Geometry> Union(const geom::Geometry& geom)
104  {
105  UnaryUnionOp op(geom);
106  return op.Union();
107  }
108 
109  template <class T>
110  UnaryUnionOp(const T& geoms, geom::GeometryFactory& geomFactIn)
111  :
112  geomFact(&geomFactIn)
113  {
114  extractGeoms(geoms);
115  }
116 
117  template <class T>
118  UnaryUnionOp(const T& geoms)
119  :
120  geomFact(0)
121  {
122  extractGeoms(geoms);
123  }
124 
125  UnaryUnionOp(const geom::Geometry& geom)
126  :
127  geomFact(geom.getFactory())
128  {
129  extract(geom);
130  }
131 
142  std::auto_ptr<geom::Geometry> Union();
143 
144 private:
145 
146  template <typename T>
147  void extractGeoms(const T& geoms)
148  {
149  for (typename T::const_iterator
150  i=geoms.begin(),
151  e=geoms.end();
152  i!=e;
153  ++i)
154  {
155  const geom::Geometry* geom = *i;
156  extract(*geom);
157  }
158  }
159 
160  void extract(const geom::Geometry& geom)
161  {
162  using namespace geom::util;
163 
164  if ( ! geomFact ) geomFact = geom.getFactory();
165 
166  GeometryExtracter::extract<geom::Polygon>(geom, polygons);
167  GeometryExtracter::extract<geom::LineString>(geom, lines);
168  GeometryExtracter::extract<geom::Point>(geom, points);
169  }
170 
183  std::auto_ptr<geom::Geometry> unionNoOpt(const geom::Geometry& g0)
184  {
187 
188  if ( ! empty.get() ) {
189  empty.reset( geomFact->createEmptyGeometry() );
190  }
191  return SnapIfNeededOverlayOp::overlayOp(g0, *empty, OverlayOp::opUNION);
192  }
193 
203  std::auto_ptr<geom::Geometry> unionWithNull(std::auto_ptr<geom::Geometry> g0,
204  std::auto_ptr<geom::Geometry> g1);
205 
206  std::vector<const geom::Polygon*> polygons;
207  std::vector<const geom::LineString*> lines;
208  std::vector<const geom::Point*> points;
209 
210  const geom::GeometryFactory* geomFact;
211 
212  std::auto_ptr<geom::Geometry> empty;
213 };
214 
215 
216 } // namespace geos::operation::union
217 } // namespace geos::operation
218 } // namespace geos
219 
220 #ifdef _MSC_VER
221 #pragma warning(pop)
222 #endif
223 
224 #endif