| 1 | |
| 2 | /* |
| 3 | Copyright (c) 2020, 2026, Oracle and/or its affiliates. |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License, version 2.0, |
| 7 | as published by the Free Software Foundation. |
| 8 | |
| 9 | This program is designed to work with certain software (including |
| 10 | but not limited to OpenSSL) that is licensed under separate terms, |
| 11 | as designated in a particular file or component or in included license |
| 12 | documentation. The authors of MySQL hereby grant you an additional |
| 13 | permission to link the program and your derivative works with the |
| 14 | separately licensed software that they have either included with |
| 15 | the program or referenced in the documentation. |
| 16 | |
| 17 | This program is distributed in the hope that it will be useful, |
| 18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | GNU General Public License, version 2.0, for more details. |
| 21 | |
| 22 | You should have received a copy of the GNU General Public License |
| 23 | along with this program; if not, write to the Free Software |
| 24 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 | */ |
| 26 | #include <gtest/gtest.h> |
| 27 | #include "sql/dd/dd.h" |
| 28 | #include "sql/dd/impl/types/spatial_reference_system_impl.h" |
| 29 | #include "sql/dd/types/spatial_reference_system.h" |
| 30 | #include "sql/gis/geometries.h" |
| 31 | #include "sql/gis/geometries_cs.h" |
| 32 | #include "sql/gis/mbr_utils.h" |
| 33 | |
| 34 | namespace mbr_utils_unittest { |
| 35 | gis::Geographic_linearring linearringFromVector(std::vector<double> data) { |
| 36 | if (data.size() % 2 != 0) { |
| 37 | throw std::exception(); /* purecov: dead code */ |
| 38 | } |
| 39 | gis::Geographic_linearring lr; |
| 40 | for (size_t i = 0; i + 1 < data.size(); i += 2) { |
| 41 | lr.push_back(gis::Geographic_point(data[i], data[i + 1])); |
| 42 | } |
| 43 | return lr; |
| 44 | } |
| 45 | |
| 46 | gis::Geographic_polygon polygonFromVector(std::vector<double> data) { |
| 47 | gis::Geographic_polygon py; |
| 48 | py.push_back(linearringFromVector(data)); |
| 49 | return py; |
| 50 | } |
| 51 | |
| 52 | std::unique_ptr<dd::Spatial_reference_system_impl> get_srs() { |
| 53 | // EPSG 4326, but with long-lat axes (E-N). |
| 54 | std::unique_ptr<dd::Spatial_reference_system_impl> m_srs( |
| 55 | dynamic_cast<dd::Spatial_reference_system_impl *>( |
| 56 | dd::create_object<dd::Spatial_reference_system>())); |
| 57 | m_srs->set_id(4326); |
| 58 | m_srs->set_name("WGS 84"); |
| 59 | m_srs->set_created(0UL); |
| 60 | m_srs->set_last_altered(0UL); |
| 61 | m_srs->set_organization("EPSG"); |
| 62 | m_srs->set_organization_coordsys_id(4326); |
| 63 | m_srs->set_definition( |
| 64 | "GEOGCS[\"WGS 84\",DATUM[\"World Geodetic System " |
| 65 | "1984\",SPHEROID[\"WGS " |
| 66 | "84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]]," |
| 67 | "AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[" |
| 68 | "\"EPSG\",\"8901\"]],UNIT[\"degree\",0.017453292519943278," |
| 69 | "AUTHORITY[\"EPSG\",\"9122\"]],AXIS[\"Lon\",EAST],AXIS[\"Lat\"," |
| 70 | "NORTH],AUTHORITY[\"EPSG\",\"4326\"]]"); |
| 71 | m_srs->set_description(""); |
| 72 | m_srs->parse_definition(); |
| 73 | return m_srs; |
| 74 | } |
| 75 | |
| 76 | TEST(MBR_Utils_test, test) { |
| 77 | auto polygon = polygonFromVector({0.26377745335935482, 0.84009247535954357, |
| 78 | 0.26387320519564444, 0.84008767903244352, |
| 79 | 0.26387320679785603, 0.84008767099694759, |
| 80 | 0.26377745335935482, 0.84009247535954357}); |
| 81 | gis::Geographic_box box; |
| 82 | std::unique_ptr<dd::Spatial_reference_system> srs = get_srs(); |
| 83 | gis::box_envelope(&polygon, srs.get(), &box); |
| 84 | EXPECT_GT(box.max_corner().x(), 0.26); |
| 85 | EXPECT_GT(box.max_corner().y(), 0.84); |
| 86 | EXPECT_LT(box.max_corner().x(), 0.27); |
| 87 | EXPECT_LT(box.max_corner().y(), 0.85); |
| 88 | } |
| 89 | } // namespace mbr_utils_unittest |