| 1 | // Copyright (c) 2021, 2026, Oracle and/or its affiliates. |
| 2 | // |
| 3 | // This program is free software; you can redistribute it and/or modify |
| 4 | // it under the terms of the GNU General Public License, version 2.0, |
| 5 | // as published by the Free Software Foundation. |
| 6 | // |
| 7 | // This program is designed to work with certain software (including |
| 8 | // but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | // as designated in a particular file or component or in included license |
| 10 | // documentation. The authors of MySQL hereby grant you an additional |
| 11 | // permission to link the program and your derivative works with the |
| 12 | // separately licensed software that they have either included with |
| 13 | // the program or referenced in the documentation. |
| 14 | // |
| 15 | // This program is distributed in the hope that it will be useful, |
| 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | // GNU General Public License, version 2.0, for more details. |
| 19 | // |
| 20 | // You should have received a copy of the GNU General Public License |
| 21 | // along with this program; if not, write to the Free Software |
| 22 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 23 | |
| 24 | #include <gtest/gtest.h> |
| 25 | #include <memory> |
| 26 | |
| 27 | #include "sql/gis/buffer.h" // gis::buffer |
| 28 | #include "sql/gis/buffer_strategies.h" // gis::BufferStrategies |
| 29 | #include "sql/gis/geometries.h" // gis::{Coordinate_system, Geometry} |
| 30 | #include "sql/gis/geometries_cs.h" // gis::{geometry types} |
| 31 | #include "unittest/gunit/gis_srs.h" // gis_srs::swapped_epsg4326 |
| 32 | |
| 33 | namespace gis_buffer_unittest { |
| 34 | |
| 35 | // Not testing the internal points of geometries as Boost has its own unittests |
| 36 | |
| 37 | // Explicit testing because (1) not all strategy combinations are valid for all |
| 38 | // geometry types, and (2) the return type may be a Polygon, Multipolygon, or |
| 39 | // Geometrycollection depending on the input. |
| 40 | |
| 41 | // The buffer of a multi-geometry may be merged into a single Polygon if the |
| 42 | // geometries are close enough or buffer distance is large enough. It may |
| 43 | // become an empty Geometrycollection if distance is so negative that buffers |
| 44 | // are shrunk to disappearance. |
| 45 | |
| 46 | template <typename T> |
| 47 | void test_valid_input(const gis::Geometry &g, |
| 48 | const gis::BufferStrategies &strats, const T &expected_g, |
| 49 | const int &expected_size) { |
| 50 | auto m_srs = |
| 51 | (expected_g.coordinate_system() == gis::Coordinate_system::kGeographic) |
| 52 | ? gis_srs::swapped_epsg4326() |
| 53 | : nullptr; |
| 54 | std::unique_ptr<gis::Geometry> result; |
| 55 | bool buffer_error = gis::buffer(m_srs.get(), g, strats, "unittest", &result); |
| 56 | |
| 57 | // If true an exception has been thrown. |
| 58 | ASSERT_FALSE(buffer_error); |
| 59 | // Verify returned geometry is of correct type |
| 60 | ASSERT_EQ(result->type(), expected_g.type()); |
| 61 | // Verify returned geometry has correct size |
| 62 | EXPECT_EQ(down_cast<T *>(result.get())->size(), expected_size); |
| 63 | } |
| 64 | |
| 65 | // Function used to create linearrings for Polygons |
| 66 | gis::Cartesian_linearring linearringFromVector(std::vector<double> data) { |
| 67 | if (data.size() % 2 != 0) { |
| 68 | throw std::exception(); /* purecov: dead code */ |
| 69 | } |
| 70 | gis::Cartesian_linearring lr; |
| 71 | for (size_t i = 0; i + 1 < data.size(); i += 2) { |
| 72 | lr.push_back(gis::Cartesian_point(data[i], data[i + 1])); |
| 73 | } |
| 74 | return lr; |
| 75 | } |
| 76 | |
| 77 | void reset_strategy_combinations(gis::BufferStrategies &s) { |
| 78 | s.combination = 0; |
| 79 | s.join_is_set = false; |
| 80 | s.end_is_set = false; |
| 81 | s.point_is_set = false; |
| 82 | } |
| 83 | |
| 84 | class BufferTest : public ::testing::Test { |
| 85 | public: |
| 86 | gis::BufferStrategies strat; |
| 87 | gis::Cartesian_polygon expected_py; |
| 88 | gis::Cartesian_multipolygon expected_mpy; |
| 89 | gis::Cartesian_geometrycollection expected_gc; |
| 90 | }; |
| 91 | |
| 92 | ////////////////////////////////////////////////////////////////////////////// |
| 93 | |
| 94 | // CARTESIAN GEOMETRIES (all) |
| 95 | |
| 96 | TEST_F(BufferTest, CartesianPoint) { |
| 97 | gis::Cartesian_point c_pt{0, 0}; |
| 98 | // Distance cannot be negative --> Only possible valid return type is Polygon |
| 99 | strat.distance = 3; |
| 100 | test_valid_input<gis::Cartesian_polygon>(c_pt, strat, expected_py, 1); |
| 101 | } |
| 102 | |
| 103 | TEST_F(BufferTest, CartesianLinestring) { |
| 104 | gis::Cartesian_linestring c_ls; |
| 105 | c_ls.push_back(gis::Cartesian_point(0, 0)); |
| 106 | c_ls.push_back(gis::Cartesian_point(1, 1)); |
| 107 | c_ls.push_back(gis::Cartesian_point(2, 0)); |
| 108 | |
| 109 | // Distance cannot be negative --> Only Polygon |
| 110 | strat.distance = 3; |
| 111 | test_valid_input<gis::Cartesian_polygon>(c_ls, strat, expected_py, 1); |
| 112 | } |
| 113 | |
| 114 | TEST_F(BufferTest, CartesianPolygon) { |
| 115 | gis::Cartesian_polygon c_py; |
| 116 | c_py.push_back(linearringFromVector({0, 0, 4, 0, 4, 4, 0, 4, 0, 0})); |
| 117 | |
| 118 | // Distance CAN be negative --> May return Polygon or empty GC |
| 119 | strat.distance = 3; |
| 120 | test_valid_input<gis::Cartesian_polygon>(c_py, strat, expected_py, 1); |
| 121 | strat.distance = -3; |
| 122 | test_valid_input<gis::Cartesian_geometrycollection>(c_py, strat, expected_gc, |
| 123 | 0); |
| 124 | } |
| 125 | |
| 126 | TEST_F(BufferTest, CartesianMultipoint) { |
| 127 | gis::Cartesian_multipoint c_mpt; |
| 128 | c_mpt.push_back(gis::Cartesian_point(0, 0)); |
| 129 | c_mpt.push_back(gis::Cartesian_point(1, 1)); |
| 130 | c_mpt.push_back(gis::Cartesian_point(5, 5)); |
| 131 | |
| 132 | // Distance cannot be negative for multipoint |
| 133 | strat.distance = 3; |
| 134 | test_valid_input<gis::Cartesian_polygon>(c_mpt, strat, expected_py, 1); |
| 135 | strat.distance = 1; |
| 136 | test_valid_input<gis::Cartesian_multipolygon>(c_mpt, strat, expected_mpy, 2); |
| 137 | } |
| 138 | |
| 139 | TEST_F(BufferTest, CartesianMultilinestring) { |
| 140 | gis::Cartesian_linestring ls1; |
| 141 | ls1.push_back(gis::Cartesian_point(0, 0)); |
| 142 | ls1.push_back(gis::Cartesian_point(1, 1)); |
| 143 | ls1.push_back(gis::Cartesian_point(2, 0)); |
| 144 | |
| 145 | gis::Cartesian_linestring ls2; |
| 146 | ls2.push_back(gis::Cartesian_point(0, 4)); |
| 147 | ls2.push_back(gis::Cartesian_point(1, 5)); |
| 148 | ls2.push_back(gis::Cartesian_point(2, 4)); |
| 149 | |
| 150 | gis::Cartesian_multilinestring c_mls; |
| 151 | c_mls.push_back(ls1); |
| 152 | c_mls.push_back(ls2); |
| 153 | |
| 154 | // Distance cannot be negative --> Only Polygon or Multipolygon |
| 155 | strat.distance = 3; |
| 156 | test_valid_input<gis::Cartesian_polygon>(c_mls, strat, expected_py, 1); |
| 157 | strat.distance = 1; |
| 158 | test_valid_input<gis::Cartesian_multipolygon>(c_mls, strat, expected_mpy, 2); |
| 159 | } |
| 160 | |
| 161 | TEST_F(BufferTest, CartesianMultipolygon) { |
| 162 | gis::Cartesian_polygon py1; |
| 163 | py1.push_back(linearringFromVector({0, 0, 4, 0, 4, 4, 0, 4, 0, 0})); |
| 164 | |
| 165 | gis::Cartesian_polygon py2; |
| 166 | py2.push_back(linearringFromVector({8, 0, 9, 0, 9, 1, 8, 1, 8, 0})); |
| 167 | |
| 168 | gis::Cartesian_multipolygon c_mpy; |
| 169 | c_mpy.push_back(py1); |
| 170 | c_mpy.push_back(py2); |
| 171 | |
| 172 | // Distance CAN be negative: May return Polygon, Multipolygon or empty GC |
| 173 | strat.distance = 3; |
| 174 | test_valid_input<gis::Cartesian_polygon>(c_mpy, strat, expected_py, 1); |
| 175 | strat.distance = 1; |
| 176 | test_valid_input<gis::Cartesian_multipolygon>(c_mpy, strat, expected_mpy, 2); |
| 177 | strat.distance = -1; |
| 178 | test_valid_input<gis::Cartesian_polygon>(c_mpy, strat, expected_py, 1); |
| 179 | strat.distance = -3; |
| 180 | test_valid_input<gis::Cartesian_geometrycollection>(c_mpy, strat, expected_gc, |
| 181 | 0); |
| 182 | } |
| 183 | |
| 184 | TEST_F(BufferTest, CartesianGeometrycollectionPos) { |
| 185 | gis::Cartesian_linestring ls1; |
| 186 | ls1.push_back(gis::Cartesian_point(0, 0)); |
| 187 | ls1.push_back(gis::Cartesian_point(1, 1)); |
| 188 | ls1.push_back(gis::Cartesian_point(2, 0)); |
| 189 | |
| 190 | gis::Cartesian_polygon py2; |
| 191 | py2.push_back(linearringFromVector({8, 0, 9, 0, 9, 1, 8, 1, 8, 0})); |
| 192 | |
| 193 | gis::Cartesian_geometrycollection c_gc; |
| 194 | c_gc.push_back(gis::Cartesian_point(0, 2)); |
| 195 | c_gc.push_back(ls1); |
| 196 | c_gc.push_back(py2); |
| 197 | |
| 198 | // Distance CAN be negative --> May return Polygon, Multipolygon or empty GC |
| 199 | strat.distance = 3.1; |
| 200 | test_valid_input<gis::Cartesian_polygon>(c_gc, strat, expected_py, 1); |
| 201 | strat.distance = 1; |
| 202 | test_valid_input<gis::Cartesian_multipolygon>(c_gc, strat, expected_mpy, 2); |
| 203 | } |
| 204 | |
| 205 | TEST_F(BufferTest, CartesianGeometrycollectionNeg) { |
| 206 | gis::Cartesian_polygon py1; |
| 207 | py1.push_back(linearringFromVector({0, 0, 4, 0, 4, 4, 0, 4, 0, 0})); |
| 208 | |
| 209 | gis::Cartesian_polygon py2; |
| 210 | py1.push_back(linearringFromVector({8, 0, 9, 0, 9, 1, 8, 1, 8, 0})); |
| 211 | |
| 212 | gis::Cartesian_polygon py3; |
| 213 | py1.push_back( |
| 214 | linearringFromVector({10, 0, 10.5, 0, 10.5, 0.5, 10, 0.5, 10, 0})); |
| 215 | |
| 216 | gis::Cartesian_multipolygon mpy1; |
| 217 | mpy1.push_back(py1); |
| 218 | mpy1.push_back(py2); |
| 219 | |
| 220 | gis::Cartesian_geometrycollection c_gc; |
| 221 | c_gc.push_back(mpy1); |
| 222 | c_gc.push_back(py3); |
| 223 | |
| 224 | // Distance CAN be negative --> May return Polygon, Multipolygon or empty GC |
| 225 | strat.distance = -0.3; |
| 226 | test_valid_input<gis::Cartesian_multipolygon>(c_gc, strat, expected_mpy, 2); |
| 227 | strat.distance = -0.6; |
| 228 | test_valid_input<gis::Cartesian_polygon>(c_gc, strat, expected_py, 1); |
| 229 | strat.distance = -3; |
| 230 | test_valid_input<gis::Cartesian_geometrycollection>(c_gc, strat, expected_gc, |
| 231 | 0); |
| 232 | } |
| 233 | |
| 234 | ////////////////////////////////////////////////////////////////////////////// |
| 235 | |
| 236 | // GEOGRAPHIC GEOMETRIES (only Point) |
| 237 | |
| 238 | TEST_F(BufferTest, GeographicPoint) { |
| 239 | gis::Geographic_polygon g_py; |
| 240 | strat.distance = 20000; |
| 241 | |
| 242 | gis::Geographic_point g_pt{63.4451715, 10.9052167}; |
| 243 | |
| 244 | // Strategy options cannot be set. |
| 245 | // Distance cannot be negative --> Only possible valid return type is polygon |
| 246 | test_valid_input<gis::Geographic_polygon>(g_pt, strat, g_py, 1); |
| 247 | } |
| 248 | |
| 249 | ////////////////////////////////////////////////////////////////////////////// |
| 250 | |
| 251 | // BUFFER STRATEGIES |
| 252 | |
| 253 | TEST_F(BufferTest, AllStrategies) { |
| 254 | gis::Cartesian_linestring ls1; |
| 255 | ls1.push_back(gis::Cartesian_point(0, 0)); |
| 256 | ls1.push_back(gis::Cartesian_point(1, 1)); |
| 257 | ls1.push_back(gis::Cartesian_point(2, 0)); |
| 258 | |
| 259 | gis::Cartesian_polygon py2; |
| 260 | py2.push_back(linearringFromVector({8, 0, 9, 0, 9, 1, 8, 1, 8, 0})); |
| 261 | |
| 262 | gis::Cartesian_geometrycollection c_gc; |
| 263 | c_gc.push_back(gis::Cartesian_point(0, 2)); |
| 264 | c_gc.push_back(ls1); |
| 265 | c_gc.push_back(py2); |
| 266 | |
| 267 | // Testing all 8 possible strategy combinations: |
| 268 | strat.distance = 0.6; |
| 269 | |
| 270 | // 0 |
| 271 | reset_strategy_combinations(strat); |
| 272 | strat.set_join_round(22); |
| 273 | strat.set_end_round(22); |
| 274 | strat.set_point_circle(22); |
| 275 | test_valid_input<gis::Cartesian_multipolygon>(c_gc, strat, expected_mpy, 3); |
| 276 | |
| 277 | // 1 |
| 278 | reset_strategy_combinations(strat); |
| 279 | strat.set_join_round(22); |
| 280 | strat.set_end_flat(); |
| 281 | strat.set_point_circle(22); |
| 282 | test_valid_input<gis::Cartesian_multipolygon>(c_gc, strat, expected_mpy, 3); |
| 283 | |
| 284 | // 2 |
| 285 | reset_strategy_combinations(strat); |
| 286 | strat.set_join_miter(4.0); |
| 287 | strat.set_end_round(22); |
| 288 | strat.set_point_circle(22); |
| 289 | test_valid_input<gis::Cartesian_multipolygon>(c_gc, strat, expected_mpy, 3); |
| 290 | |
| 291 | // 3 |
| 292 | reset_strategy_combinations(strat); |
| 293 | strat.set_join_miter(4.0); |
| 294 | strat.set_end_flat(); |
| 295 | strat.set_point_circle(22); |
| 296 | test_valid_input<gis::Cartesian_multipolygon>(c_gc, strat, expected_mpy, 3); |
| 297 | |
| 298 | // 4 |
| 299 | reset_strategy_combinations(strat); |
| 300 | strat.set_join_round(22); |
| 301 | strat.set_end_round(22); |
| 302 | strat.set_point_square(); |
| 303 | test_valid_input<gis::Cartesian_multipolygon>(c_gc, strat, expected_mpy, 2); |
| 304 | |
| 305 | // 5 |
| 306 | reset_strategy_combinations(strat); |
| 307 | strat.set_join_round(22); |
| 308 | strat.set_end_flat(); |
| 309 | strat.set_point_square(); |
| 310 | test_valid_input<gis::Cartesian_multipolygon>(c_gc, strat, expected_mpy, 2); |
| 311 | |
| 312 | // 6 |
| 313 | reset_strategy_combinations(strat); |
| 314 | strat.set_join_miter(4.0); |
| 315 | strat.set_end_round(22); |
| 316 | strat.set_point_square(); |
| 317 | test_valid_input<gis::Cartesian_multipolygon>(c_gc, strat, expected_mpy, 2); |
| 318 | |
| 319 | // 7 |
| 320 | reset_strategy_combinations(strat); |
| 321 | strat.set_join_miter(4.0); |
| 322 | strat.set_end_flat(); |
| 323 | strat.set_point_square(); |
| 324 | test_valid_input<gis::Cartesian_multipolygon>(c_gc, strat, expected_mpy, 2); |
| 325 | } |
| 326 | |
| 327 | TEST_F(BufferTest, PointStrategies) { |
| 328 | // Showcasing that different strategies (for equal buffer distance) may yield |
| 329 | // different results wrt. numbers of buffers returned from multi-geometries. |
| 330 | |
| 331 | gis::Cartesian_multipoint c_mpt; |
| 332 | c_mpt.push_back(gis::Cartesian_point(0, 0)); |
| 333 | c_mpt.push_back(gis::Cartesian_point(1, 1)); |
| 334 | |
| 335 | // At 0.70 circle(42) does NOT overlap, while at 0.71 it does |
| 336 | // At 0.50 square() does NOT overlap, while at 0.51 it does |
| 337 | strat.distance = 0.60; |
| 338 | |
| 339 | strat.set_point_circle(42); |
| 340 | test_valid_input<gis::Cartesian_multipolygon>(c_mpt, strat, expected_mpy, 2); |
| 341 | |
| 342 | strat.point_is_set = false; |
| 343 | strat.set_point_square(); |
| 344 | test_valid_input<gis::Cartesian_polygon>(c_mpt, strat, expected_py, 1); |
| 345 | } |
| 346 | |
| 347 | } // namespace gis_buffer_unittest |