/src/geos/src/geom/util/GeometryCombiner.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /********************************************************************** |
2 | | * |
3 | | * GEOS - Geometry Engine Open Source |
4 | | * http://geos.osgeo.org |
5 | | * |
6 | | * Copyright (C) 2006-2011 Refractions Research Inc. |
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: geom/util/GeometryCombiner.java r320 (JTS-1.12) |
16 | | * |
17 | | **********************************************************************/ |
18 | | |
19 | | #include <geos/geom/util/GeometryCombiner.h> |
20 | | #include <geos/geom/Geometry.h> |
21 | | #include <geos/geom/GeometryFactory.h> |
22 | | |
23 | | namespace geos { |
24 | | namespace geom { // geos.geom |
25 | | namespace util { // geos.geom.util |
26 | | |
27 | | std::unique_ptr<Geometry> |
28 | | GeometryCombiner::combine(std::vector<std::unique_ptr<Geometry>>&& geoms) |
29 | 0 | { |
30 | 0 | GeometryCombiner combiner(std::move(geoms)); |
31 | 0 | return combiner.combine(); |
32 | 0 | } |
33 | | |
34 | | std::unique_ptr<Geometry> |
35 | | GeometryCombiner::combine(std::vector<const Geometry*> const& geoms) |
36 | 0 | { |
37 | 0 | GeometryCombiner combiner(geoms); |
38 | 0 | return combiner.combine(); |
39 | 0 | } |
40 | | |
41 | | std::unique_ptr<Geometry> |
42 | | GeometryCombiner::combine(const Geometry* g0, const Geometry* g1) |
43 | 0 | { |
44 | 0 | std::vector<const Geometry*> geoms; |
45 | 0 | geoms.push_back(g0); |
46 | 0 | geoms.push_back(g1); |
47 | |
|
48 | 0 | GeometryCombiner combiner(geoms); |
49 | 0 | return combiner.combine(); |
50 | 0 | } |
51 | | |
52 | | std::unique_ptr<Geometry> |
53 | | GeometryCombiner::combine(std::unique_ptr<Geometry> && g0, |
54 | | std::unique_ptr<Geometry> && g1) |
55 | 0 | { |
56 | 0 | std::vector<std::unique_ptr<Geometry>> geoms(2); |
57 | 0 | geoms[0] = std::move(g0); |
58 | 0 | geoms[1] = std::move(g1); |
59 | 0 | GeometryCombiner combiner(std::move(geoms)); |
60 | 0 | return combiner.combine(); |
61 | 0 | } |
62 | | |
63 | | std::unique_ptr<Geometry> |
64 | | GeometryCombiner::combine(std::unique_ptr<Geometry> && g0, |
65 | | std::unique_ptr<Geometry> && g1, |
66 | | std::unique_ptr<Geometry> && g2) |
67 | 0 | { |
68 | 0 | std::vector<std::unique_ptr<Geometry>> geoms(3); |
69 | 0 | geoms[0] = std::move(g0); |
70 | 0 | geoms[1] = std::move(g1); |
71 | 0 | geoms[2] = std::move(g2); |
72 | 0 | GeometryCombiner combiner(std::move(geoms)); |
73 | 0 | return combiner.combine(); |
74 | 0 | } |
75 | | |
76 | | std::unique_ptr<Geometry> |
77 | | GeometryCombiner::combine(const Geometry* g0, const Geometry* g1, |
78 | | const Geometry* g2) |
79 | 0 | { |
80 | 0 | std::vector<const Geometry*> geoms; |
81 | 0 | geoms.push_back(g0); |
82 | 0 | geoms.push_back(g1); |
83 | 0 | geoms.push_back(g2); |
84 | |
|
85 | 0 | GeometryCombiner combiner(geoms); |
86 | 0 | return combiner.combine(); |
87 | 0 | } |
88 | | |
89 | 0 | GeometryCombiner::GeometryCombiner(std::vector<const Geometry*> const& geoms) : skipEmpty(false) |
90 | 0 | { |
91 | 0 | for(const auto& geom : geoms) { |
92 | 0 | for (std::size_t i = 0; i < geom->getNumGeometries(); i++) { |
93 | 0 | auto part = geom->getGeometryN(i); |
94 | 0 | inputGeoms.push_back(part->clone()); |
95 | 0 | } |
96 | 0 | } |
97 | 0 | } |
98 | | |
99 | 0 | GeometryCombiner::GeometryCombiner(std::vector<std::unique_ptr<Geometry>> && geoms) : skipEmpty(false) |
100 | 0 | { |
101 | 0 | for(auto& geom : geoms) { |
102 | 0 | auto coll = dynamic_cast<GeometryCollection *>(geom.get()); |
103 | 0 | if (coll) { |
104 | 0 | for (auto &part : coll->releaseGeometries()) { |
105 | 0 | inputGeoms.push_back(std::move(part)); |
106 | 0 | } |
107 | 0 | } else { |
108 | 0 | inputGeoms.push_back(std::move(geom)); |
109 | 0 | } |
110 | 0 | } |
111 | 0 | } |
112 | | |
113 | | std::unique_ptr<Geometry> |
114 | | GeometryCombiner::combine() |
115 | 0 | { |
116 | 0 | auto geomFactory = inputGeoms.empty() ? GeometryFactory::getDefaultInstance() : inputGeoms.front()->getFactory(); |
117 | |
|
118 | 0 | if (skipEmpty) { |
119 | 0 | inputGeoms.erase(std::remove_if(inputGeoms.begin(), inputGeoms.end(), [](std::unique_ptr<Geometry> & g) { |
120 | 0 | return g->isEmpty(); |
121 | 0 | }), inputGeoms.end()); |
122 | 0 | } |
123 | | |
124 | | // return the "simplest possible" geometry |
125 | 0 | return geomFactory->buildGeometry(std::move(inputGeoms)); |
126 | 0 | } |
127 | | |
128 | 0 | void GeometryCombiner::setSkipEmpty(bool b) { |
129 | 0 | skipEmpty = b; |
130 | 0 | } |
131 | | |
132 | | } // namespace geos.geom.util |
133 | | } // namespace geos.geom |
134 | | } // namespace geos |
135 | | |