| 1 | /* Copyright (c) 2018, 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 <initializer_list> |
| 25 | |
| 26 | #include <gtest/gtest.h> |
| 27 | #include "mysqld_error.h" |
| 28 | #include "sql/dd/dd.h" |
| 29 | #include "sql/dd/impl/types/spatial_reference_system_impl.h" |
| 30 | #include "sql/dd/types/spatial_reference_system.h" |
| 31 | #include "sql/gis/distance.h" |
| 32 | #include "sql/gis/geometries.h" |
| 33 | #include "sql/gis/geometries_cs.h" |
| 34 | #include "sql/gis/st_units_of_measure.h" |
| 35 | #include "unittest/gunit/test_utils.h" |
| 36 | |
| 37 | namespace distance_unittest { |
| 38 | |
| 39 | class DistanceUnitTest : public ::testing::Test { |
| 40 | public: |
| 41 | my_testing::Server_initializer initializer; |
| 42 | |
| 43 | DistanceUnitTest() = default; |
| 44 | void SetUp() override { initializer.SetUp(); } |
| 45 | void TearDown() override { initializer.TearDown(); } |
| 46 | THD *thd() { return initializer.thd(); } |
| 47 | ~DistanceUnitTest() override = default; |
| 48 | }; |
| 49 | TEST_F(DistanceUnitTest, unordered_map) { |
| 50 | auto units = gis::units(); |
| 51 | auto find_res = units.find("metre"); |
| 52 | EXPECT_TRUE(find_res->first == "metre"); |
| 53 | find_res = units.find("metrE"); |
| 54 | EXPECT_TRUE(find_res->first == "metre"); |
| 55 | find_res = units.find("metrE"); |
| 56 | EXPECT_FALSE(find_res == units.end()); |
| 57 | find_res = units.find("metEr"); |
| 58 | EXPECT_TRUE(find_res == units.end()); |
| 59 | find_res = units.find("Clarke's foot"); |
| 60 | EXPECT_FALSE(find_res == units.end()); |
| 61 | } |
| 62 | TEST_F(DistanceUnitTest, get_conversion_factor) { |
| 63 | double conversion_factor = 0; |
| 64 | EXPECT_FALSE(gis::get_conversion_factor("metre", &conversion_factor)); |
| 65 | EXPECT_FALSE(gis::get_conversion_factor("METRE", &conversion_factor)); |
| 66 | EXPECT_FALSE(gis::get_conversion_factor("British foot (Sears 1922)", |
| 67 | &conversion_factor)); |
| 68 | EXPECT_FALSE(gis::get_conversion_factor("claRke'S LInk", &conversion_factor)); |
| 69 | } |
| 70 | TEST_F(DistanceUnitTest, er_unit_not_found) { |
| 71 | initializer.set_expected_error(ER_UNIT_NOT_FOUND); |
| 72 | double conversion_factor = 0; |
| 73 | EXPECT_TRUE(gis::get_conversion_factor("MITRE", &conversion_factor)); |
| 74 | } |
| 75 | |
| 76 | std::unique_ptr<dd::Spatial_reference_system_impl> GetGeographicalSrs() { |
| 77 | // EPSG 4326, but with long-lat axes (E-N). |
| 78 | std::unique_ptr<dd::Spatial_reference_system_impl> srs( |
| 79 | dynamic_cast<dd::Spatial_reference_system_impl *>( |
| 80 | dd::create_object<dd::Spatial_reference_system>())); |
| 81 | srs->set_id(4326); |
| 82 | srs->set_name("WGS 84"); |
| 83 | srs->set_created(0UL); |
| 84 | srs->set_last_altered(0UL); |
| 85 | srs->set_organization("EPSG"); |
| 86 | srs->set_organization_coordsys_id(4326); |
| 87 | srs->set_definition( |
| 88 | "GEOGCS[\"WGS 84\",DATUM[\"World Geodetic System " |
| 89 | "1984\",SPHEROID[\"WGS " |
| 90 | "84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]]," |
| 91 | "AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[" |
| 92 | "\"EPSG\",\"8901\"]],UNIT[\"degree\",0.017453292519943278," |
| 93 | "AUTHORITY[\"EPSG\",\"9122\"]],AXIS[\"Lon\",EAST],AXIS[\"Lat\"," |
| 94 | "NORTH],AUTHORITY[\"EPSG\",\"4326\"]]"); |
| 95 | srs->set_description(""); |
| 96 | srs->parse_definition(); |
| 97 | return srs; |
| 98 | } |
| 99 | |
| 100 | gis::Geographic_linestring MakeLinestring( |
| 101 | const std::initializer_list<double> &data) { |
| 102 | if (data.size() % 2 != 0) { |
| 103 | throw std::exception(); /* purecov: dead code */ |
| 104 | } |
| 105 | typename gis::Geographic_linestring linestring; |
| 106 | for (std::initializer_list<double>::const_iterator it = data.begin(); |
| 107 | it != data.end(); std::advance(it, 2)) { |
| 108 | linestring.push_back(gis::Geographic_point(*it, *(it + 1))); |
| 109 | } |
| 110 | return linestring; |
| 111 | } |
| 112 | |
| 113 | gis::Geographic_linearring MakeLinearring( |
| 114 | const std::initializer_list<double> &data) { |
| 115 | if (data.size() % 2 != 0) { |
| 116 | throw std::exception(); /* purecov: dead code */ |
| 117 | } |
| 118 | gis::Geographic_linearring linear_ring; |
| 119 | for (std::initializer_list<double>::const_iterator it = data.begin(); |
| 120 | it != data.end(); std::advance(it, 2)) { |
| 121 | linear_ring.push_back(gis::Geographic_point(*it, *(it + 1))); |
| 122 | } |
| 123 | return linear_ring; |
| 124 | } |
| 125 | gis::Geographic_polygon MakePolygon( |
| 126 | const std::initializer_list<std::initializer_list<double>> &data) { |
| 127 | gis::Geographic_polygon polygon; |
| 128 | for (auto ring : data) { |
| 129 | polygon.push_back(MakeLinearring(ring)); |
| 130 | } |
| 131 | return polygon; |
| 132 | } |
| 133 | gis::Geographic_multipoint MakeMultipoint( |
| 134 | const std::initializer_list<std::initializer_list<double>> &data) { |
| 135 | gis::Geographic_multipoint multipoint; |
| 136 | for (auto point : data) { |
| 137 | EXPECT_EQ(point.size(), 2); |
| 138 | multipoint.push_back( |
| 139 | gis::Geographic_point(*point.begin(), *(point.begin() + 1))); |
| 140 | } |
| 141 | return multipoint; |
| 142 | } |
| 143 | |
| 144 | gis::Geographic_multilinestring MakeMultilinestring( |
| 145 | const std::initializer_list<std::initializer_list<double>> &data) { |
| 146 | gis ::Geographic_multilinestring multilinestring; |
| 147 | for (const auto &linestring : data) { |
| 148 | multilinestring.push_back(MakeLinestring(linestring)); |
| 149 | } |
| 150 | return multilinestring; |
| 151 | } |
| 152 | |
| 153 | gis::Geographic_multipolygon MakeMultipolygon( |
| 154 | const std::initializer_list< |
| 155 | std::initializer_list<std::initializer_list<double>>> &data) { |
| 156 | gis::Geographic_multipolygon multipolygon; |
| 157 | for (auto polygon : data) { |
| 158 | multipolygon.push_back(MakePolygon(polygon)); |
| 159 | } |
| 160 | return multipolygon; |
| 161 | } |
| 162 | |
| 163 | double TestDistanceGeographic(const gis::Geometry &g1, const gis::Geometry &g2, |
| 164 | bool *null_value = nullptr) { |
| 165 | auto srs = GetGeographicalSrs(); |
| 166 | double distance = 0.0; |
| 167 | if (null_value != nullptr) { |
| 168 | bool res = gis::distance(srs.get(), &g1, &g2, &distance, null_value); |
| 169 | EXPECT_FALSE(res); |
| 170 | } else { |
| 171 | bool is_null = false; |
| 172 | bool res = gis::distance(srs.get(), &g1, &g2, &distance, &is_null); |
| 173 | EXPECT_FALSE(res); |
| 174 | EXPECT_FALSE(is_null); |
| 175 | } |
| 176 | return distance; |
| 177 | } |
| 178 | |
| 179 | TEST(DistanceTest, Geographic_pp) { |
| 180 | EXPECT_GT(TestDistanceGeographic(gis::Geographic_point{0., 0.}, |
| 181 | gis::Geographic_point{1. * M_PI / 360, 0.}), |
| 182 | 55659.0); |
| 183 | EXPECT_LT(TestDistanceGeographic(gis::Geographic_point{0., 0.}, |
| 184 | gis::Geographic_point{1. * M_PI / 360, 0.}), |
| 185 | 55660.0); |
| 186 | } |
| 187 | |
| 188 | TEST(DistanceTest, GeographicLinestringPoint) { |
| 189 | gis::Geographic_linestring geographic_linestring = |
| 190 | MakeLinestring({-0.2, 0.05, 0.2, 0.05}); |
| 191 | gis::Geographic_point geographic_point1{0, 0}; |
| 192 | gis::Geographic_point geographic_point2{0, 0.10}; |
| 193 | EXPECT_GT(TestDistanceGeographic(geographic_point1, geographic_linestring), |
| 194 | TestDistanceGeographic(geographic_point2, geographic_linestring)); |
| 195 | EXPECT_GT(TestDistanceGeographic(geographic_linestring, geographic_point1), |
| 196 | TestDistanceGeographic(geographic_linestring, geographic_point2)); |
| 197 | } |
| 198 | |
| 199 | TEST(DistanceTest, Geographic_lsls) { |
| 200 | EXPECT_LT( |
| 201 | TestDistanceGeographic(MakeLinestring({-1., 0., 1, 0}), |
| 202 | MakeLinestring({-1., 0.08, 1, 0.004, 1, 0.08})), |
| 203 | TestDistanceGeographic(MakeLinestring({-1., 0., 1, 0}), |
| 204 | MakeLinestring({-1., 0.08, 1, 0.08}))); |
| 205 | } |
| 206 | |
| 207 | TEST(DistanceTest, GeographicPolygonPoint) { |
| 208 | gis::Geographic_point geographic_point1{0.1, 0.1}; |
| 209 | gis::Geographic_polygon geographic_polygon = |
| 210 | MakePolygon({{0, 0, 1, 0, 0, 1, 0, 0}}); |
| 211 | EXPECT_DOUBLE_EQ( |
| 212 | TestDistanceGeographic(geographic_polygon, geographic_point1), 0); |
| 213 | geographic_polygon = |
| 214 | MakePolygon({{0, 0, 1, 0, 0, 1, 0, 0}, |
| 215 | {0.05, 0.05, 0.05, 0.95, 0.95, 0.05, 0.05, 0.05}}); |
| 216 | EXPECT_GT(TestDistanceGeographic(geographic_polygon, geographic_point1), 0); |
| 217 | EXPECT_DOUBLE_EQ( |
| 218 | TestDistanceGeographic(geographic_polygon, geographic_point1), |
| 219 | TestDistanceGeographic(geographic_point1, geographic_polygon)); |
| 220 | } |
| 221 | |
| 222 | TEST(DistanceTest, GeographicPolygonLinestring) { |
| 223 | gis::Geographic_polygon geographic_polygon = |
| 224 | MakePolygon({{0, 0, 1, 0, 0, 1, 0, 0}}); |
| 225 | gis::Geographic_linestring geographic_linestring1 = |
| 226 | MakeLinestring({-1., 0.5, -.1, 0.5}); |
| 227 | gis::Geographic_linestring geographic_linestring2 = |
| 228 | MakeLinestring({-1., 0.5, .1, 0.5}); |
| 229 | EXPECT_GT(TestDistanceGeographic(geographic_polygon, geographic_linestring1), |
| 230 | 0); |
| 231 | EXPECT_DOUBLE_EQ( |
| 232 | TestDistanceGeographic(geographic_linestring2, geographic_polygon), 0); |
| 233 | } |
| 234 | |
| 235 | TEST(DistanceTest, GeographicPolygonPolygon) { |
| 236 | gis::Geographic_polygon geographic_polygon1 = |
| 237 | MakePolygon({{0, 0, 1, 0, 0, 1, 0, 0}}); |
| 238 | gis::Geographic_polygon geographic_polygon2 = |
| 239 | MakePolygon({{-1, 0, -.1, 0, -1, 1, -1, 0}}); |
| 240 | gis::Geographic_polygon geographic_polygon3 = |
| 241 | MakePolygon({{-1, 0, .1, 0, -1, 1, -1, 0}}); |
| 242 | EXPECT_GT(TestDistanceGeographic(geographic_polygon1, geographic_polygon2), |
| 243 | 0); |
| 244 | EXPECT_DOUBLE_EQ( |
| 245 | TestDistanceGeographic(geographic_polygon1, geographic_polygon3), 0); |
| 246 | } |
| 247 | |
| 248 | TEST(DistanceTest, Geographic_Multipoint_Point) { |
| 249 | { |
| 250 | gis::Geographic_multipoint geographic_multipoint; |
| 251 | gis::Geographic_multipoint geographic_point; |
| 252 | geographic_multipoint.push_back(gis::Geographic_point({-1, 0})); |
| 253 | geographic_multipoint.push_back(gis::Geographic_point({1, 0})); |
| 254 | gis::Geographic_point geographic_point1{0.0, 0.1}; |
| 255 | gis::Geographic_point geographic_point2{1.0, 0.0}; |
| 256 | EXPECT_GT(TestDistanceGeographic(geographic_multipoint, geographic_point1), |
| 257 | 0); |
| 258 | EXPECT_DOUBLE_EQ( |
| 259 | TestDistanceGeographic(geographic_multipoint, geographic_point2), 0); |
| 260 | } |
| 261 | { |
| 262 | gis::Geographic_point geographic_point{0, 0}; |
| 263 | bool is_null = false; |
| 264 | TestDistanceGeographic(gis::Geographic_multipoint(), geographic_point, |
| 265 | &is_null); |
| 266 | EXPECT_TRUE(is_null); |
| 267 | is_null = false; |
| 268 | TestDistanceGeographic(geographic_point, gis::Geographic_multipoint(), |
| 269 | &is_null); |
| 270 | EXPECT_TRUE(is_null); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | TEST(DistanceTest, Geographic_Multipoint_Linestring) { |
| 275 | gis::Geographic_linestring geographic_linestring = |
| 276 | MakeLinestring({-1, 0, 1, 0}); |
| 277 | gis::Geographic_multipoint geographic_multipoint1 = |
| 278 | MakeMultipoint({{-1, 0}, {1, 0}}); |
| 279 | gis::Geographic_point geographic_point1{0.0, 0.1}; |
| 280 | EXPECT_LT( |
| 281 | TestDistanceGeographic(geographic_linestring, geographic_multipoint1), |
| 282 | TestDistanceGeographic(geographic_multipoint1, geographic_point1)); |
| 283 | EXPECT_DOUBLE_EQ( |
| 284 | TestDistanceGeographic(geographic_linestring, geographic_point1), |
| 285 | TestDistanceGeographic(geographic_point1, geographic_linestring)); |
| 286 | } |
| 287 | |
| 288 | TEST(DistanceTest, Geographic_Multipoint_Polygon) { |
| 289 | gis::Geographic_multipoint geographic_multipoint1; |
| 290 | geographic_multipoint1.push_back(gis::Geographic_point({-1.1, 0})); |
| 291 | geographic_multipoint1.push_back(gis::Geographic_point({1.1, 0})); |
| 292 | gis::Geographic_polygon geographic_polygon = |
| 293 | MakePolygon({{0, 0, 1, 0, 0, 1, 0, 0}}); |
| 294 | EXPECT_GT(TestDistanceGeographic(geographic_multipoint1, geographic_polygon), |
| 295 | 0); |
| 296 | EXPECT_DOUBLE_EQ( |
| 297 | TestDistanceGeographic(geographic_multipoint1, geographic_polygon), |
| 298 | TestDistanceGeographic(geographic_polygon, geographic_multipoint1)); |
| 299 | } |
| 300 | |
| 301 | TEST(DistanceTest, Geographic_Multipoint_Multipoint) { |
| 302 | gis::Geographic_multipoint geographic_multipoint1; |
| 303 | geographic_multipoint1.push_back(gis::Geographic_point({-1, 0})); |
| 304 | geographic_multipoint1.push_back(gis::Geographic_point({1, 0})); |
| 305 | gis::Geographic_multipoint geographic_multipoint2; |
| 306 | geographic_multipoint2.push_back(gis::Geographic_point({0, -1})); |
| 307 | geographic_multipoint2.push_back(gis::Geographic_point({0, 1})); |
| 308 | EXPECT_GT( |
| 309 | TestDistanceGeographic(geographic_multipoint1, geographic_multipoint2), |
| 310 | 0); |
| 311 | EXPECT_DOUBLE_EQ( |
| 312 | TestDistanceGeographic(geographic_multipoint1, geographic_multipoint2), |
| 313 | TestDistanceGeographic(geographic_multipoint2, geographic_multipoint1)); |
| 314 | } |
| 315 | |
| 316 | TEST(DistanceTest, Geographic_Multilinestring_point) { |
| 317 | { |
| 318 | gis::Geographic_multilinestring geographic_multilinestring1; |
| 319 | geographic_multilinestring1.push_back(MakeLinestring({-1, 0, 1, 0})); |
| 320 | gis::Geographic_point geographic_point1(0, 1); |
| 321 | EXPECT_GT( |
| 322 | TestDistanceGeographic(geographic_multilinestring1, geographic_point1), |
| 323 | 0); |
| 324 | } |
| 325 | { |
| 326 | gis::Geographic_point geographic_point{0, 0}; |
| 327 | bool is_null = false; |
| 328 | TestDistanceGeographic(gis::Geographic_multilinestring(), geographic_point, |
| 329 | &is_null); |
| 330 | EXPECT_TRUE(is_null); |
| 331 | is_null = false; |
| 332 | TestDistanceGeographic(geographic_point, gis::Geographic_multilinestring(), |
| 333 | &is_null); |
| 334 | EXPECT_TRUE(is_null); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | TEST(DistanceTest, Geographic_Multilinestring_Linestring) { |
| 339 | gis::Geographic_multilinestring geographic_multilinestring1; |
| 340 | geographic_multilinestring1.push_back(MakeLinestring({-1, 0, 1, 0})); |
| 341 | gis::Geographic_linestring geographic_linestringtr1 = |
| 342 | MakeLinestring({-0, -1, 0, 1}); |
| 343 | EXPECT_DOUBLE_EQ(TestDistanceGeographic(geographic_multilinestring1, |
| 344 | geographic_linestringtr1), |
| 345 | 0); |
| 346 | EXPECT_DOUBLE_EQ(TestDistanceGeographic(geographic_linestringtr1, |
| 347 | geographic_multilinestring1), |
| 348 | 0); |
| 349 | } |
| 350 | |
| 351 | TEST(DistanceTest, Geographic_Multilinestring_Polygon) { |
| 352 | gis::Geographic_multilinestring geographic_multilinestring1; |
| 353 | geographic_multilinestring1.push_back(MakeLinestring({-1, 0.5, 1, 0.5})); |
| 354 | gis::Geographic_polygon geographic_polygon1 = |
| 355 | MakePolygon({{0, 0, 1, 0, 0, 1, 0, 0}}); |
| 356 | EXPECT_DOUBLE_EQ( |
| 357 | TestDistanceGeographic(geographic_multilinestring1, geographic_polygon1), |
| 358 | 0); |
| 359 | EXPECT_DOUBLE_EQ( |
| 360 | TestDistanceGeographic(geographic_polygon1, geographic_multilinestring1), |
| 361 | 0); |
| 362 | } |
| 363 | |
| 364 | TEST(DistanceTest, Geographic_Multilinestring_Multipoint) { |
| 365 | gis::Geographic_multilinestring geographic_multilinestring1; |
| 366 | geographic_multilinestring1.push_back(MakeLinestring({-1, 0, 1, 0})); |
| 367 | gis::Geographic_multipoint geographic_multipoint1; |
| 368 | geographic_multipoint1.push_back(gis::Geographic_point(0, 1)); |
| 369 | EXPECT_GT(TestDistanceGeographic(geographic_multilinestring1, |
| 370 | geographic_multipoint1), |
| 371 | 0); |
| 372 | EXPECT_GT(TestDistanceGeographic(geographic_multipoint1, |
| 373 | geographic_multilinestring1), |
| 374 | 0); |
| 375 | } |
| 376 | |
| 377 | TEST(DistanceTest, Geographic_Multilinestring_Multilinestring) { |
| 378 | gis::Geographic_multilinestring geographic_multilinestring1; |
| 379 | geographic_multilinestring1.push_back(MakeLinestring({-1, 0, 1, 0})); |
| 380 | gis::Geographic_multilinestring geographic_multilinestring2; |
| 381 | geographic_multilinestring2.push_back(MakeLinestring({-0, -1, 0, 1})); |
| 382 | EXPECT_DOUBLE_EQ(TestDistanceGeographic(geographic_multilinestring1, |
| 383 | geographic_multilinestring2), |
| 384 | 0); |
| 385 | } |
| 386 | |
| 387 | TEST(DistanceTest, GeographicMultipolygon_Point) { |
| 388 | { |
| 389 | gis::Geographic_multipolygon geographic_multipolygon; |
| 390 | gis::Geographic_polygon geographic_polygon; |
| 391 | gis::Geographic_linearring linearring = |
| 392 | MakeLinearring({0, 0, 1, 0, 0, 1, 0, 0}); |
| 393 | geographic_polygon.push_back(linearring); |
| 394 | geographic_multipolygon.push_back(geographic_polygon); |
| 395 | gis::Geographic_point geographic_point1{0.1, 0.1}; |
| 396 | EXPECT_DOUBLE_EQ( |
| 397 | TestDistanceGeographic(geographic_multipolygon, geographic_point1), 0); |
| 398 | EXPECT_DOUBLE_EQ( |
| 399 | TestDistanceGeographic(geographic_point1, geographic_multipolygon), 0); |
| 400 | } |
| 401 | { |
| 402 | gis::Geographic_multipolygon geographic_multipolygon; |
| 403 | gis::Geographic_polygon geographic_polygon; |
| 404 | gis::Geographic_linearring linearring = |
| 405 | MakeLinearring({0, 0, 1, 0, 0, 1, 0, 0}); |
| 406 | geographic_polygon.push_back(linearring); |
| 407 | linearring = |
| 408 | MakeLinearring({0.05, 0.05, 0.05, 0.95, 0.95, 0.05, 0.05, 0.05}); |
| 409 | geographic_polygon.push_back(linearring); |
| 410 | geographic_multipolygon.push_back(geographic_polygon); |
| 411 | gis::Geographic_point geographic_point1{0.1, 0.1}; |
| 412 | EXPECT_GT( |
| 413 | TestDistanceGeographic(geographic_multipolygon, geographic_point1), 0); |
| 414 | } |
| 415 | { |
| 416 | gis::Geographic_multipolygon geographic_multipolygon; |
| 417 | gis::Geographic_polygon geographic_polygon1 = |
| 418 | MakePolygon({{0, 0, 1, 0, 0, 1, 0, 0}, |
| 419 | {0.05, 0.05, 0.05, 0.95, 0.95, 0.05, 0.05, 0.05}}); |
| 420 | gis::Geographic_polygon geographic_polygon2 = |
| 421 | MakePolygon({{0.5, 0, 1.5, 0, .5, 1, .5, 0}, |
| 422 | {.55, 0.05, 0.55, 0.95, 0.55, 0.05, 0.55, 0.05}}); |
| 423 | geographic_multipolygon.push_back(geographic_polygon2); |
| 424 | gis::Geographic_point geographic_point1{0.2, 0.2}; |
| 425 | gis::Geographic_point geographic_point2{0.525, 0.2}; |
| 426 | EXPECT_GT( |
| 427 | TestDistanceGeographic(geographic_multipolygon, geographic_point1), 0); |
| 428 | EXPECT_DOUBLE_EQ( |
| 429 | TestDistanceGeographic(geographic_multipolygon, geographic_point2), 0); |
| 430 | } |
| 431 | { |
| 432 | gis::Geographic_point geographic_point{0, 0}; |
| 433 | bool is_null = false; |
| 434 | TestDistanceGeographic(gis::Geographic_multipolygon(), geographic_point, |
| 435 | &is_null); |
| 436 | EXPECT_TRUE(is_null); |
| 437 | is_null = false; |
| 438 | TestDistanceGeographic(geographic_point, gis::Geographic_multipolygon(), |
| 439 | &is_null); |
| 440 | EXPECT_TRUE(is_null); |
| 441 | } |
| 442 | } |
| 443 | TEST(DistanceTest, Geographic_Multipolygon_Linestring) { |
| 444 | gis::Geographic_polygon geographic_polygon = |
| 445 | MakePolygon({{0, 0, 1, 0, 0, 1, 0, 0}}); |
| 446 | gis::Geographic_multipolygon geographic_multipolygon; |
| 447 | geographic_multipolygon.push_back(geographic_polygon); |
| 448 | gis::Geographic_linestring geographic_linestringtr1 = |
| 449 | MakeLinestring({-1., 0.5, -.1, 0.5}); |
| 450 | EXPECT_GT( |
| 451 | TestDistanceGeographic(geographic_multipolygon, geographic_linestringtr1), |
| 452 | 0); |
| 453 | EXPECT_GT( |
| 454 | TestDistanceGeographic(geographic_linestringtr1, geographic_multipolygon), |
| 455 | 0); |
| 456 | } |
| 457 | |
| 458 | TEST(DistanceTest, Geographic_Multipolygon_polygon) { |
| 459 | gis::Geographic_polygon geographic_polygon1 = |
| 460 | MakePolygon({{0, 0, 1, 0, 0, 1, 0, 0}}); |
| 461 | gis::Geographic_polygon geographic_polygon2 = |
| 462 | MakePolygon({{-1, 0, -.1, 0, -1, 1, -1, 0}}); |
| 463 | gis::Geographic_multipolygon geographic_multipolygon1; |
| 464 | geographic_multipolygon1.push_back(geographic_polygon1); |
| 465 | EXPECT_GT( |
| 466 | TestDistanceGeographic(geographic_multipolygon1, geographic_polygon2), 0); |
| 467 | EXPECT_DOUBLE_EQ( |
| 468 | TestDistanceGeographic(geographic_multipolygon1, geographic_polygon2), |
| 469 | TestDistanceGeographic(geographic_polygon2, geographic_multipolygon1)); |
| 470 | } |
| 471 | |
| 472 | TEST(DistanceTest, Geographic_Multipolygon_MultiPoint) { |
| 473 | gis::Geographic_multipolygon geographic_multipolygon; |
| 474 | gis::Geographic_polygon geographic_polygon1 = |
| 475 | MakePolygon({{0, 0, 1, 0, 0, 1, 0, 0}, |
| 476 | {0.05, 0.05, 0.05, 0.95, 0.95, 0.05, 0.05, 0.05}}); |
| 477 | geographic_multipolygon.push_back(geographic_polygon1); |
| 478 | gis::Geographic_polygon geographic_polygon2 = |
| 479 | MakePolygon({{0.5, 0, 1.5, 0, .5, 1, .5, 0}, |
| 480 | {.55, 0.05, 0.55, 0.95, 0.55, 0.05, 0.55, 0.05}}); |
| 481 | geographic_multipolygon.push_back(geographic_polygon2); |
| 482 | gis::Geographic_point geographic_point1{0.2, 0.2}; |
| 483 | gis::Geographic_point geographic_point2{0.525, 0.2}; |
| 484 | gis::Geographic_multipoint geographic_multipoint; |
| 485 | geographic_multipoint.push_back(geographic_point1); |
| 486 | geographic_multipoint.push_back(geographic_point2); |
| 487 | EXPECT_DOUBLE_EQ( |
| 488 | TestDistanceGeographic(geographic_multipolygon, geographic_multipoint), |
| 489 | 0); |
| 490 | EXPECT_DOUBLE_EQ( |
| 491 | TestDistanceGeographic(geographic_multipoint, geographic_multipolygon), |
| 492 | 0); |
| 493 | } |
| 494 | |
| 495 | TEST(DistanceTest, Geographic_Multipolygon_Multilinestring) { |
| 496 | { |
| 497 | gis::Geographic_polygon geographic_polygon = |
| 498 | MakePolygon({{0, 0, 1, 0, 0, 1, 0, 0}}); |
| 499 | gis::Geographic_multipolygon geographic_multipolygon; |
| 500 | geographic_multipolygon.push_back(geographic_polygon); |
| 501 | gis::Geographic_linestring geographic_linestring1 = |
| 502 | MakeLinestring({-1., 0.5, -.1, 0.5}); |
| 503 | gis::Geographic_multilinestring geographic_multilinestring1; |
| 504 | geographic_multilinestring1.push_back(geographic_linestring1); |
| 505 | EXPECT_GT(TestDistanceGeographic(geographic_multipolygon, |
| 506 | geographic_multilinestring1), |
| 507 | 0); |
| 508 | EXPECT_DOUBLE_EQ(TestDistanceGeographic(geographic_multilinestring1, |
| 509 | geographic_multipolygon), |
| 510 | TestDistanceGeographic(geographic_multipolygon, |
| 511 | geographic_multilinestring1)); |
| 512 | } |
| 513 | { |
| 514 | bool is_null; |
| 515 | TestDistanceGeographic(gis::Geographic_multipolygon(), |
| 516 | gis::Geographic_multilinestring(), &is_null); |
| 517 | EXPECT_TRUE(is_null); |
| 518 | } |
| 519 | { |
| 520 | gis::Geographic_multipolygon geographic_multipolygon = |
| 521 | MakeMultipolygon({{{0, 0, 1, 0, 0, 1, 0, 0}}}); |
| 522 | bool is_null; |
| 523 | TestDistanceGeographic(geographic_multipolygon, |
| 524 | gis::Geographic_multilinestring(), &is_null); |
| 525 | EXPECT_TRUE(is_null); |
| 526 | } |
| 527 | { |
| 528 | gis::Geographic_multilinestring geographic_multilinestring; |
| 529 | geographic_multilinestring.push_back(MakeLinestring({-1., 0.5, -.1, 0.5})); |
| 530 | bool is_null; |
| 531 | TestDistanceGeographic(gis::Geographic_multipolygon(), |
| 532 | geographic_multilinestring, &is_null); |
| 533 | EXPECT_TRUE(is_null); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | TEST(DistanceTest, Geographic_Multipolygon_Multipolygon) { |
| 538 | gis::Geographic_polygon geographic_polygon1 = |
| 539 | MakePolygon({{0, 0, 1, 0, 0, 1, 0, 0}}); |
| 540 | gis::Geographic_polygon geographic_polygon2 = |
| 541 | MakePolygon({{-1, 0, -.1, 0, -1, 1, -1, 0}}); |
| 542 | gis::Geographic_multipolygon geographic_multipolygon1, |
| 543 | geographic_multipolygon2; |
| 544 | geographic_multipolygon1.push_back(geographic_polygon1); |
| 545 | geographic_multipolygon2.push_back(geographic_polygon2); |
| 546 | EXPECT_GT(TestDistanceGeographic(geographic_multipolygon1, |
| 547 | geographic_multipolygon2), |
| 548 | 0); |
| 549 | EXPECT_DOUBLE_EQ(TestDistanceGeographic(geographic_multipolygon1, |
| 550 | geographic_multipolygon2), |
| 551 | TestDistanceGeographic(geographic_multipolygon2, |
| 552 | geographic_multipolygon1)); |
| 553 | } |
| 554 | |
| 555 | TEST(DistanceTest, Geographic_GeometryCollection) { |
| 556 | { |
| 557 | gis::Geographic_geometrycollection geographic_geometrycollection; |
| 558 | geographic_geometrycollection.push_back( |
| 559 | MakeLinestring({-1., 0.5, -.1, 0.5})); |
| 560 | gis::Geographic_point geographic_point{0, 0}; |
| 561 | EXPECT_GT( |
| 562 | TestDistanceGeographic(geographic_geometrycollection, geographic_point), |
| 563 | 0); |
| 564 | } |
| 565 | { |
| 566 | gis::Geographic_geometrycollection geographic_geometrycollection; |
| 567 | geographic_geometrycollection.push_back( |
| 568 | MakeLinestring({-1., 0.5, -.1, 0.5})); |
| 569 | EXPECT_GT(TestDistanceGeographic(gis::Geographic_point{0, 0}, |
| 570 | geographic_geometrycollection), |
| 571 | 0); |
| 572 | EXPECT_GT(TestDistanceGeographic(MakeLinestring({0, 0, 0.05, 0}), |
| 573 | geographic_geometrycollection), |
| 574 | 0); |
| 575 | EXPECT_GT( |
| 576 | TestDistanceGeographic(MakePolygon({{0, 0, 0.5, 0, 0, 0.25, 0, 0}}), |
| 577 | geographic_geometrycollection), |
| 578 | 0); |
| 579 | EXPECT_GT(TestDistanceGeographic( |
| 580 | MakeMultipoint({{0, 0}, {0.5, 0}, {0, 0.25}, {0, 0}}), |
| 581 | geographic_geometrycollection), |
| 582 | 0); |
| 583 | EXPECT_GT(TestDistanceGeographic(MakeMultilinestring({{0, 0, 0.05, 0}}), |
| 584 | geographic_geometrycollection), |
| 585 | 0); |
| 586 | EXPECT_GT(TestDistanceGeographic( |
| 587 | MakeMultipolygon({{{0, 0, 0.5, 0, 0, 0.25, 0, 0}}}), |
| 588 | geographic_geometrycollection), |
| 589 | 0); |
| 590 | } |
| 591 | } |
| 592 | TEST(DistanceTest, Bug29545865) { |
| 593 | EXPECT_GT( |
| 594 | TestDistanceGeographic( |
| 595 | gis::Geographic_point{3.32696 / 180 * M_PI, -6.29345 / 180 * M_PI}, |
| 596 | MakeLinestring({-16.42203 / 180 * M_PI, -7.52882 / 180 * M_PI, |
| 597 | 4.89998 / 180 * M_PI, -6.15568 / 180 * M_PI})), |
| 598 | 470.0); |
| 599 | EXPECT_LT( |
| 600 | TestDistanceGeographic( |
| 601 | gis::Geographic_point{3.32696 / 180 * M_PI, -6.29345 / 180 * M_PI}, |
| 602 | MakeLinestring({-16.42203 / 180 * M_PI, -7.52882 / 180 * M_PI, |
| 603 | 4.89998 / 180 * M_PI, -6.15568 / 180 * M_PI})), |
| 604 | 530.0); |
| 605 | |
| 606 | EXPECT_GT( |
| 607 | TestDistanceGeographic( |
| 608 | gis::Geographic_point{-7.35561 / 180 * M_PI, 7.2137 / 180 * M_PI}, |
| 609 | MakeLinestring({8.65279 / 180 * M_PI, -2.71668 / 180 * M_PI, |
| 610 | -7.13372 / 180 * M_PI, 8.35583 / 180 * M_PI, |
| 611 | -9.09998 / 180 * M_PI, -1.22625 / 180 * M_PI})), |
| 612 | 1500); |
| 613 | EXPECT_LT( |
| 614 | TestDistanceGeographic( |
| 615 | gis::Geographic_point{-7.35561 / 180 * M_PI, 7.2137 / 180 * M_PI}, |
| 616 | MakeLinestring({8.65279 / 180 * M_PI, -2.71668 / 180 * M_PI, |
| 617 | -7.13372 / 180 * M_PI, 8.35583 / 180 * M_PI, |
| 618 | -9.09998 / 180 * M_PI, -1.22625 / 180 * M_PI})), |
| 619 | 2000); |
| 620 | } |
| 621 | |
| 622 | } // namespace distance_unittest |