| 1 | /* Copyright (c) 2020, 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/frechet_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 frechet_distance_unittest { |
| 38 | |
| 39 | auto GetCartesianSrs() { |
| 40 | // Cartesian SRS. |
| 41 | return std::unique_ptr<dd::Spatial_reference_system_impl>(); |
| 42 | } |
| 43 | |
| 44 | auto GetGeographicalSrs() { |
| 45 | // EPSG 4326, but with long-lat axes (E-N). |
| 46 | std::unique_ptr<dd::Spatial_reference_system_impl> srs( |
| 47 | dynamic_cast<dd::Spatial_reference_system_impl *>( |
| 48 | dd::create_object<dd::Spatial_reference_system>())); |
| 49 | srs->set_id(4326); |
| 50 | srs->set_name("WGS 84"); |
| 51 | srs->set_created(0UL); |
| 52 | srs->set_last_altered(0UL); |
| 53 | srs->set_organization("EPSG"); |
| 54 | srs->set_organization_coordsys_id(4326); |
| 55 | srs->set_definition( |
| 56 | "GEOGCS[\"WGS 84\",DATUM[\"World Geodetic System " |
| 57 | "1984\",SPHEROID[\"WGS " |
| 58 | "84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]]," |
| 59 | "AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[" |
| 60 | "\"EPSG\",\"8901\"]],UNIT[\"degree\",0.017453292519943278," |
| 61 | "AUTHORITY[\"EPSG\",\"9122\"]],AXIS[\"Lon\",EAST],AXIS[\"Lat\"," |
| 62 | "NORTH],AUTHORITY[\"EPSG\",\"4326\"]]"); |
| 63 | srs->set_description(""); |
| 64 | srs->parse_definition(); |
| 65 | return srs; |
| 66 | } |
| 67 | |
| 68 | auto GetGeographicalSrsDiffFlat() { |
| 69 | // EPSG 4326, but with long-lat axes (E-N) and different flattening. |
| 70 | std::unique_ptr<dd::Spatial_reference_system_impl> srs( |
| 71 | dynamic_cast<dd::Spatial_reference_system_impl *>( |
| 72 | dd::create_object<dd::Spatial_reference_system>())); |
| 73 | srs->set_id(4326); |
| 74 | srs->set_name("WGS 84"); |
| 75 | srs->set_created(0UL); |
| 76 | srs->set_last_altered(0UL); |
| 77 | srs->set_organization("EPSG"); |
| 78 | srs->set_organization_coordsys_id(4326); |
| 79 | srs->set_definition( |
| 80 | "GEOGCS[\"WGS 84\",DATUM[\"World Geodetic System " |
| 81 | "1984\",SPHEROID[\"WGS " |
| 82 | "84\",6378137,290,AUTHORITY[\"EPSG\",\"7030\"]]," |
| 83 | "AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[" |
| 84 | "\"EPSG\",\"8901\"]],UNIT[\"degree\",0.017453292519943278," |
| 85 | "AUTHORITY[\"EPSG\",\"9122\"]],AXIS[\"Lon\",EAST],AXIS[\"Lat\"," |
| 86 | "NORTH],AUTHORITY[\"EPSG\",\"4326\"]]"); |
| 87 | srs->set_description(""); |
| 88 | srs->parse_definition(); |
| 89 | return srs; |
| 90 | } |
| 91 | |
| 92 | template <typename Point, typename Linestring> |
| 93 | auto MakeLinestring(const std::initializer_list<double> &data) { |
| 94 | if (data.size() % 2 != 0) { |
| 95 | throw std::exception(); /* purecov: dead code */ |
| 96 | } |
| 97 | Linestring linestring; |
| 98 | for (auto it = data.begin(); it != data.end(); std::advance(it, 2)) { |
| 99 | linestring.push_back(Point(*it, *(it + 1))); |
| 100 | } |
| 101 | return linestring; |
| 102 | } |
| 103 | |
| 104 | template <typename SRS> |
| 105 | double TestFrechetDistance(const gis::Geometry &g1, const gis::Geometry &g2, |
| 106 | SRS srs, bool *null_value = nullptr) { |
| 107 | double frechet_distance = 0.0; |
| 108 | if (null_value != nullptr) { |
| 109 | bool res = gis::frechet_distance(srs, &g1, &g2, "testcase", |
| 110 | &frechet_distance, null_value); |
| 111 | EXPECT_FALSE(res); |
| 112 | } else { |
| 113 | bool is_null = false; |
| 114 | bool res = gis::frechet_distance(srs, &g1, &g2, "testcase", |
| 115 | &frechet_distance, &is_null); |
| 116 | EXPECT_FALSE(res); |
| 117 | EXPECT_FALSE(is_null); |
| 118 | } |
| 119 | return frechet_distance; |
| 120 | } |
| 121 | |
| 122 | TEST(FrechetDistanceUnitTest, LinestringLinestring) { |
| 123 | auto gl1 = MakeLinestring<gis::Geographic_point, gis::Geographic_linestring>( |
| 124 | {-1., 0.5, -.1, 0.5}); |
| 125 | auto gl2 = MakeLinestring<gis::Geographic_point, gis::Geographic_linestring>( |
| 126 | {-1., 0.5, .1, 0.5}); |
| 127 | auto srs1 = GetGeographicalSrs(); |
| 128 | // 32bit computes distance as 1119900.6074340444 |
| 129 | EXPECT_NEAR(TestFrechetDistance(gl1, gl2, srs1.get()), 1119900.6074340483, |
| 130 | 1e-8); |
| 131 | |
| 132 | auto srs2 = GetGeographicalSrsDiffFlat(); |
| 133 | // 32bit computes distance as 1119925.1618088416 |
| 134 | EXPECT_NEAR(TestFrechetDistance(gl1, gl2, srs2.get()), 1119925.1618088456, |
| 135 | 1e-8); |
| 136 | |
| 137 | auto cl1 = MakeLinestring<gis::Cartesian_point, gis::Cartesian_linestring>( |
| 138 | {-1., 0.5, -.1, 0.5}); |
| 139 | auto cl2 = MakeLinestring<gis::Cartesian_point, gis::Cartesian_linestring>( |
| 140 | {-1., 0.5, .1, 0.5}); |
| 141 | auto srs3 = GetCartesianSrs(); |
| 142 | EXPECT_DOUBLE_EQ(TestFrechetDistance(cl1, cl2, srs3.get()), 0.2); |
| 143 | } |
| 144 | |
| 145 | } // namespace frechet_distance_unittest |