Have UnaryUnion use BinaryOp for the simple case
[geos.git] / include / geos / operation / union / UnaryUnionOp.h
blob9abbc2227701019b19bb602a7e31f4921090edc7
1 /**********************************************************************
2 * $Id$
4 * GEOS - Geometry Engine Open Source
5 * http://geos.refractions.net
7 * Copyright (C) 2011 Sandro Santilli <strk@keybit.net>
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.
14 **********************************************************************
16 * Last port: operation/union/UnaryUnionOp.java r320 (JTS-1.12)
18 **********************************************************************/
20 #ifndef GEOS_OP_UNION_UNARYUNION_H
21 #define GEOS_OP_UNION_UNARYUNION_H
23 #include <memory>
24 #include <vector>
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>
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
41 // Forward declarations
42 namespace geos {
43 namespace geom {
44 class GeometryFactory;
45 class Geometry;
49 namespace geos {
50 namespace operation { // geos::operation
51 namespace geounion { // geos::operation::geounion
53 /**
54 * Unions a collection of Geometry or a single Geometry
55 * (which may be a collection) together.
56 * By using this special-purpose operation over a collection of
57 * geometries it is possible to take advantage of various optimizations
58 * to improve performance.
59 * Heterogeneous {@link GeometryCollection}s are fully supported.
61 * The result obeys the following contract:
63 * - Unioning a set of overlapping {@link Polygons}s has the effect of
64 * merging the areas (i.e. the same effect as
65 * iteratively unioning all individual polygons together).
66 * - Unioning a set of {@link LineString}s has the effect of
67 * <b>fully noding</b> and <b>dissolving</b> the input linework.
68 * In this context "fully noded" means that there will be a node or
69 * endpoint in the output for every endpoint or line segment crossing
70 * in the input.
71 * "Dissolved" means that any duplicate (e.g. coincident) line segments
72 * or portions of line segments will be reduced to a single line segment
73 * in the output. * This is consistent with the semantics of the
74 * {@link Geometry#union(Geometry)} operation.
75 * If <b>merged</b> linework is required, the {@link LineMerger} class
76 * can be used.
77 * - Unioning a set of {@link Points}s has the effect of merging
78 * al identical points (producing a set with no duplicates).
80 * <tt>UnaryUnion</tt> always operates on the individual components of
81 * MultiGeometries.
82 * So it is possible to use it to "clean" invalid self-intersecting
83 * MultiPolygons (although the polygon components must all still be
84 * individually valid.)
86 class GEOS_DLL UnaryUnionOp
88 public:
90 template <typename T>
91 static std::auto_ptr<geom::Geometry> Union(const T& geoms)
93 UnaryUnionOp op(geoms);
94 return op.Union();
97 template <class T>
98 static std::auto_ptr<geom::Geometry> Union(const T& geoms,
99 geom::GeometryFactory& geomFact)
101 UnaryUnionOp op(geoms, geomFact);
102 return op.Union();
105 static std::auto_ptr<geom::Geometry> Union(const geom::Geometry& geom)
107 UnaryUnionOp op(geom);
108 return op.Union();
111 template <class T>
112 UnaryUnionOp(const T& geoms, geom::GeometryFactory& geomFactIn)
114 geomFact(&geomFactIn)
116 extractGeoms(geoms);
119 template <class T>
120 UnaryUnionOp(const T& geoms)
122 geomFact(0)
124 extractGeoms(geoms);
127 UnaryUnionOp(const geom::Geometry& geom)
129 geomFact(geom.getFactory())
131 extract(geom);
135 * \brief
136 * Gets the union of the input geometries.
138 * If no input geometries were provided, a POINT EMPTY is returned.
140 * @return a Geometry containing the union
141 * @return an empty GEOMETRYCOLLECTION if no geometries were provided
142 * in the input
144 std::auto_ptr<geom::Geometry> Union();
146 private:
148 template <typename T>
149 void extractGeoms(const T& geoms)
151 for (typename T::const_iterator
152 i=geoms.begin(),
153 e=geoms.end();
154 i!=e;
155 ++i)
157 const geom::Geometry* geom = *i;
158 extract(*geom);
162 void extract(const geom::Geometry& geom)
164 using namespace geom::util;
166 if ( ! geomFact ) geomFact = geom.getFactory();
168 GeometryExtracter::extract<geom::Polygon>(geom, polygons);
169 GeometryExtracter::extract<geom::LineString>(geom, lines);
170 GeometryExtracter::extract<geom::Point>(geom, points);
174 * Computes a unary union with no extra optimization,
175 * and no short-circuiting.
176 * Due to the way the overlay operations
177 * are implemented, this is still efficient in the case of linear
178 * and puntal geometries.
179 * Uses robust version of overlay operation
180 * to ensure identical behaviour to the <tt>union(Geometry)</tt> operation.
182 * @param g0 a geometry
183 * @return the union of the input geometry
185 std::auto_ptr<geom::Geometry> unionNoOpt(const geom::Geometry& g0)
187 using geos::operation::overlay::OverlayOp;
188 //using geos::operation::overlay::snap::SnapIfNeededOverlayOp;
190 if ( ! empty.get() ) {
191 empty.reset( geomFact->createEmptyGeometry() );
193 //return SnapIfNeededOverlayOp::overlayOp(g0, *empty, OverlayOp::opUNION);
194 return BinaryOp(&g0, empty.get(), overlay::overlayOp(OverlayOp::opUNION));
198 * Computes the union of two geometries,
199 * either of both of which may be null.
201 * @param g0 a Geometry (ownership transferred)
202 * @param g1 a Geometry (ownership transferred)
203 * @return the union of the input(s)
204 * @return null if both inputs are null
206 std::auto_ptr<geom::Geometry> unionWithNull(std::auto_ptr<geom::Geometry> g0,
207 std::auto_ptr<geom::Geometry> g1);
209 std::vector<const geom::Polygon*> polygons;
210 std::vector<const geom::LineString*> lines;
211 std::vector<const geom::Point*> points;
213 const geom::GeometryFactory* geomFact;
215 std::auto_ptr<geom::Geometry> empty;
219 } // namespace geos::operation::union
220 } // namespace geos::operation
221 } // namespace geos
223 #ifdef _MSC_VER
224 #pragma warning(pop)
225 #endif
227 #endif