GEOS  3.3.4
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/BinaryOp.h>
29 #include <geos/geom/Point.h>
30 #include <geos/geom/LineString.h>
31 #include <geos/geom/Polygon.h>
32 #include <geos/geom/util/GeometryExtracter.h>
33 #include <geos/operation/overlay/OverlayOp.h>
34 //#include <geos/operation/overlay/snap/SnapIfNeededOverlayOp.h>
35 
36 #ifdef _MSC_VER
37 #pragma warning(push)
38 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
39 #endif
40 
41 // Forward declarations
42 namespace geos {
43  namespace geom {
44  class GeometryFactory;
45  class Geometry;
46  }
47 }
48 
49 namespace geos {
50 namespace operation { // geos::operation
51 namespace geounion { // geos::operation::geounion
52 
86 class GEOS_DLL UnaryUnionOp
87 {
88 public:
89 
90  template <typename T>
91  static std::auto_ptr<geom::Geometry> Union(const T& geoms)
92  {
93  UnaryUnionOp op(geoms);
94  return op.Union();
95  }
96 
97  template <class T>
98  static std::auto_ptr<geom::Geometry> Union(const T& geoms,
99  geom::GeometryFactory& geomFact)
100  {
101  UnaryUnionOp op(geoms, geomFact);
102  return op.Union();
103  }
104 
105  static std::auto_ptr<geom::Geometry> Union(const geom::Geometry& geom)
106  {
107  UnaryUnionOp op(geom);
108  return op.Union();
109  }
110 
111  template <class T>
112  UnaryUnionOp(const T& geoms, geom::GeometryFactory& geomFactIn)
113  :
114  geomFact(&geomFactIn)
115  {
116  extractGeoms(geoms);
117  }
118 
119  template <class T>
120  UnaryUnionOp(const T& geoms)
121  :
122  geomFact(0)
123  {
124  extractGeoms(geoms);
125  }
126 
127  UnaryUnionOp(const geom::Geometry& geom)
128  :
129  geomFact(geom.getFactory())
130  {
131  extract(geom);
132  }
133 
144  std::auto_ptr<geom::Geometry> Union();
145 
146 private:
147 
148  template <typename T>
149  void extractGeoms(const T& geoms)
150  {
151  for (typename T::const_iterator
152  i=geoms.begin(),
153  e=geoms.end();
154  i!=e;
155  ++i)
156  {
157  const geom::Geometry* geom = *i;
158  extract(*geom);
159  }
160  }
161 
162  void extract(const geom::Geometry& geom)
163  {
164  using namespace geom::util;
165 
166  if ( ! geomFact ) geomFact = geom.getFactory();
167 
168  GeometryExtracter::extract<geom::Polygon>(geom, polygons);
169  GeometryExtracter::extract<geom::LineString>(geom, lines);
170  GeometryExtracter::extract<geom::Point>(geom, points);
171  }
172 
185  std::auto_ptr<geom::Geometry> unionNoOpt(const geom::Geometry& g0)
186  {
188  //using geos::operation::overlay::snap::SnapIfNeededOverlayOp;
189 
190  if ( ! empty.get() ) {
191  empty.reset( geomFact->createEmptyGeometry() );
192  }
193  //return SnapIfNeededOverlayOp::overlayOp(g0, *empty, OverlayOp::opUNION);
194  return BinaryOp(&g0, empty.get(), overlay::overlayOp(OverlayOp::opUNION));
195  }
196 
206  std::auto_ptr<geom::Geometry> unionWithNull(std::auto_ptr<geom::Geometry> g0,
207  std::auto_ptr<geom::Geometry> g1);
208 
209  std::vector<const geom::Polygon*> polygons;
210  std::vector<const geom::LineString*> lines;
211  std::vector<const geom::Point*> points;
212 
213  const geom::GeometryFactory* geomFact;
214 
215  std::auto_ptr<geom::Geometry> empty;
216 };
217 
218 
219 } // namespace geos::operation::union
220 } // namespace geos::operation
221 } // namespace geos
222 
223 #ifdef _MSC_VER
224 #pragma warning(pop)
225 #endif
226 
227 #endif