1 /**********************************************************************
3 * GEOS - Geometry Engine Open Source
4 * http://geos.osgeo.org
6 * Copyright (C) 2006-2011 Refractions Research Inc.
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.
13 **********************************************************************
15 * Last port: geom/util/GeometryCombiner.java r320 (JTS-1.12)
17 **********************************************************************/
19 #include <geos/geom/util/GeometryCombiner.h>
20 #include <geos/geom/Geometry.h>
21 #include <geos/geom/GeometryFactory.h>
22 #include <geos/geom/GeometryCollection.h>
25 namespace geom
{ // geos.geom
26 namespace util
{ // geos.geom.util
28 Geometry
* GeometryCombiner::combine(std::vector
<Geometry
*> const& geoms
)
30 GeometryCombiner
combiner(geoms
);
31 return combiner
.combine();
34 Geometry
* GeometryCombiner::combine(const Geometry
* g0
, const Geometry
* g1
)
36 std::vector
<Geometry
*> geoms
;
37 geoms
.push_back(const_cast<Geometry
*>(g0
));
38 geoms
.push_back(const_cast<Geometry
*>(g1
));
40 GeometryCombiner
combiner(geoms
);
41 return combiner
.combine();
44 Geometry
* GeometryCombiner::combine(const Geometry
* g0
, const Geometry
* g1
,
47 std::vector
<Geometry
*> geoms
;
48 geoms
.push_back(const_cast<Geometry
*>(g0
));
49 geoms
.push_back(const_cast<Geometry
*>(g1
));
50 geoms
.push_back(const_cast<Geometry
*>(g2
));
52 GeometryCombiner
combiner(geoms
);
53 return combiner
.combine();
56 GeometryCombiner::GeometryCombiner(std::vector
<Geometry
*> const& geoms
)
57 : geomFactory(extractFactory(geoms
)), skipEmpty(false), inputGeoms(geoms
)
61 GeometryFactory
const*
62 GeometryCombiner::extractFactory(std::vector
<Geometry
*> const& geoms
)
64 return geoms
.empty() ? NULL
: geoms
.front()->getFactory();
67 Geometry
* GeometryCombiner::combine()
69 std::vector
<Geometry
*> elems
;
71 std::vector
<Geometry
*>::const_iterator end
= inputGeoms
.end();
72 for (std::vector
<Geometry
*>::const_iterator i
= inputGeoms
.begin();
75 extractElements(*i
, elems
);
79 if (geomFactory
!= NULL
) {
80 return geomFactory
->createGeometryCollection(NULL
);
85 // return the "simplest possible" geometry
86 return geomFactory
->buildGeometry(elems
);
90 GeometryCombiner::extractElements(Geometry
* geom
, std::vector
<Geometry
*>& elems
)
95 for (std::size_t i
= 0; i
< geom
->getNumGeometries(); ++i
) {
96 Geometry
* elemGeom
= const_cast<Geometry
*>(geom
->getGeometryN(i
));
97 if (skipEmpty
&& elemGeom
->isEmpty())
99 elems
.push_back(elemGeom
);
103 } // namespace geos.geom.util
104 } // namespace geos.geom