Debian Codegraph @

unittest/gunit/gis_wkb_writer-t.cc

raw symbols SCIP
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 /// @file
25 ///
26 /// This file test gis::write_geometry and the gis::Wkb_visitor class.
27
28 #include "my_config.h"
29
30 #include <gtest/gtest.h>
31 #include <memory> // unique_ptr
32 #include <string>
33
34 #include "sql/dd/dd.h"
35 #include "sql/dd/impl/types/spatial_reference_system_impl.h"
36 #include "sql/dd/properties.h"
37 #include "sql/dd/types/spatial_reference_system.h"
38 #include "sql/gis/geometries.h"
39 #include "sql/gis/geometries_cs.h"
40 #include "sql/gis/wkb.h"
41
42 namespace wkb_writer_unittest {
43
44 class WkbWriterTest : public ::testing::Test {
45 protected:
46 dd::Spatial_reference_system_impl *m_cartesian_srs;
47 dd::Spatial_reference_system_impl *m_radian_srs;
48 dd::Spatial_reference_system_impl *m_grad_srs;
49
50 void SetUp() override {
51 m_cartesian_srs = dynamic_cast<dd::Spatial_reference_system_impl *>(
52 dd::create_object<dd::Spatial_reference_system>());
53 m_cartesian_srs->set_id(3857);
54 m_cartesian_srs->set_name("WGS 84 / Pseudo-Mercator");
55 m_cartesian_srs->set_created(0UL);
56 m_cartesian_srs->set_last_altered(0UL);
57 m_cartesian_srs->set_organization("EPSG");
58 m_cartesian_srs->set_organization_coordsys_id(3857);
59 m_cartesian_srs->set_definition(
60 "PROJCS[\"WGS 84 / Pseudo-Mercator\",GEOGCS[\"WGS 84\",DATUM[\"World "
61 "Geodetic System 1984\",SPHEROID[\"WGS "
62 "84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY["
63 "\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\","
64 "\"8901\"]],UNIT[\"degree\",0.017453292519943278,AUTHORITY[\"EPSG\","
65 "\"9122\"]],AXIS[\"Lat\",NORTH],AXIS[\"Lon\",EAST],AUTHORITY[\"EPSG\","
66 "\"4326\"]],PROJECTION[\"Popular Visualisation Pseudo "
67 "Mercator\",AUTHORITY[\"EPSG\",\"1024\"]],PARAMETER[\"Latitude of "
68 "natural "
69 "origin\",0,AUTHORITY[\"EPSG\",\"8801\"]],PARAMETER[\"Longitude of "
70 "natural origin\",0,AUTHORITY[\"EPSG\",\"8802\"]],PARAMETER[\"False "
71 "easting\",0,AUTHORITY[\"EPSG\",\"8806\"]],PARAMETER[\"False "
72 "northing\",0,AUTHORITY[\"EPSG\",\"8807\"]],UNIT[\"metre\",1,AUTHORITY["
73 "\"EPSG\",\"9001\"]],AXIS[\"X\",EAST],AXIS[\"Y\",NORTH],AUTHORITY["
74 "\"EPSG\",\"3857\"]]");
75 m_cartesian_srs->set_description("");
76 m_cartesian_srs->parse_definition();
77
78 // EPSG 4326, lat-long (N-E), Greenwich Meridian, but in radians
79 m_radian_srs = dynamic_cast<dd::Spatial_reference_system_impl *>(
80 dd::create_object<dd::Spatial_reference_system>());
81 m_radian_srs->set_id(1000000);
82 m_radian_srs->set_name("WGS 84");
83 m_radian_srs->set_created(0UL);
84 m_radian_srs->set_last_altered(0UL);
85 m_radian_srs->set_organization("EPSG");
86 m_radian_srs->set_organization_coordsys_id(4326);
87 m_radian_srs->set_definition(
88 "GEOGCS[\"WGS 84\",DATUM[\"World Geodetic System 1984\",SPHEROID[\"WGS "
89 "84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY["
90 "\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\","
91 "\"8901\"]],UNIT[\"radian\",1.0],AXIS[\"Lat\",NORTH],AXIS[\"Lon\",EAST]"
92 ",AUTHORITY[\"EPSG\",\"4326\"]]");
93 m_radian_srs->set_description("");
94 m_radian_srs->parse_definition();
95
96 // Lat-long (S-W) in grad, Meridian 10 grad East of Greenwich
97 m_grad_srs = dynamic_cast<dd::Spatial_reference_system_impl *>(
98 dd::create_object<dd::Spatial_reference_system>());
99 m_grad_srs->set_id(1000001);
100 m_grad_srs->set_name("Tweaked");
101 m_grad_srs->set_created(0UL);
102 m_grad_srs->set_last_altered(0UL);
103 m_grad_srs->set_organization("EPSG");
104 m_grad_srs->set_organization_coordsys_id(1000000);
105 m_grad_srs->set_definition(
106 "GEOGCS[\"Tweaked\",DATUM[\"World Geodetic System "
107 "1984\",SPHEROID[\"WGS "
108 "84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY["
109 "\"EPSG\",\"6326\"]],PRIMEM[\"10 "
110 "E\",10,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"grad\",0."
111 "01570796326794895,AUTHORITY[\"EPSG\",\"9122\"]],AXIS[\"Lat\",SOUTH],"
112 "AXIS[\"Lon\",WEST],AUTHORITY[\"EPSG\",\"4326\"]]");
113 m_grad_srs->set_description("");
114 m_grad_srs->parse_definition();
115 }
116
117 void TearDown() override {
118 delete m_cartesian_srs;
119 delete m_radian_srs;
120 delete m_grad_srs;
121 }
122
123 WkbWriterTest() = default;
124
125 private:
126 WkbWriterTest(WkbWriterTest const &) = delete;
127 WkbWriterTest &operator=(WkbWriterTest const &) = delete;
128 };
129
130 TEST_F(WkbWriterTest, Point) {
131 gis::Cartesian_point cpt(1, 2);
132 gis::Geographic_point gpt(1, 2);
133
134 // Little-endian POINT(1 2)
135 std::string point(
136 "\x00\x00\x00\x00" // SRID
137 "\x01" // Byte order
138 "\x01\x00\x00\x00" // WKB type
139 "\x00\x00\x00\x00\x00\x00\xf0\x3f" // X
140 "\x00\x00\x00\x00\x00\x00\x00\x40", // Y
141 25);
142
143 // Cartesian
144 String srid0_point;
145 gis::write_geometry(nullptr, cpt, &srid0_point);
146 EXPECT_EQ(point.size(), srid0_point.length());
147 EXPECT_FALSE(memcmp(point.c_str(), srid0_point.c_ptr(), point.size()));
148
149 String cartesian_point;
150 gis::write_geometry(m_cartesian_srs, cpt, &cartesian_point);
151 EXPECT_EQ(point.size(), cartesian_point.length());
152 EXPECT_FALSE(memcmp("\x11\x0f\x00\x00", cartesian_point.c_ptr(), 4));
153 EXPECT_FALSE(
154 memcmp(point.c_str() + 4, cartesian_point.c_ptr() + 4, point.size() - 4));
155
156 // Radians
157 String radian_point;
158 gis::write_geometry(m_radian_srs, gpt, &radian_point);
159 EXPECT_EQ(point.size(), radian_point.length());
160 EXPECT_FALSE(memcmp("\x40\x42\x0f\x00", radian_point.c_ptr(), 4));
161 EXPECT_FALSE(
162 memcmp(point.c_str() + 4, radian_point.c_ptr() + 4, point.size() - 4));
163
164 // Lat-long (S-W), grad, meridian 10 grad East of Greenwich. Point is 1 grad
165 // West (=9 grad East of Greenwich), 2 grad South.
166 gis::Geographic_point grad_pt(0.14137166941154056, -0.031415926535897899);
167 String grad_point;
168 gis::write_geometry(m_grad_srs, grad_pt, &grad_point);
169 EXPECT_EQ(point.size(), grad_point.length());
170 EXPECT_FALSE(memcmp("\x41\x42\x0f\x00", grad_point.c_ptr(), 4));
171 EXPECT_FALSE(
172 memcmp(point.c_str() + 4, grad_point.c_ptr() + 4, point.size() - 4));
173 }
174
175 TEST_F(WkbWriterTest, Linestring) {
176 gis::Cartesian_linestring cls;
177 cls.push_back(gis::Cartesian_point(0, 0));
178 cls.push_back(gis::Cartesian_point(0, 0));
179 gis::Geographic_linestring gls;
180 gls.push_back(gis::Geographic_point(0, 0));
181 gls.push_back(gis::Geographic_point(0, 0));
182
183 // Linestring with two points
184 std::string linestring(
185 "\x00\x00\x00\x00" // SRID
186 "\x01" // Byte order
187 "\x02\x00\x00\x00" // WKB type
188 "\x02\x00\x00\x00" // numPoints
189 "\x00\x00\x00\x00\x00\x00\x00\x00" // X
190 "\x00\x00\x00\x00\x00\x00\x00\x00" // Y
191 "\x00\x00\x00\x00\x00\x00\x00\x00" // X
192 "\x00\x00\x00\x00\x00\x00\x00\x00", // Y
193 45);
194
195 // Cartesian
196 String srid0_linestring;
197 gis::write_geometry(nullptr, cls, &srid0_linestring);
198 EXPECT_EQ(linestring.size(), srid0_linestring.length());
199 EXPECT_FALSE(
200 memcmp(linestring.c_str(), srid0_linestring.c_ptr(), linestring.size()));
201
202 String cartesian_linestring;
203 gis::write_geometry(m_cartesian_srs, cls, &cartesian_linestring);
204 EXPECT_EQ(linestring.size(), cartesian_linestring.length());
205 EXPECT_FALSE(memcmp("\x11\x0f\x00\x00", cartesian_linestring.c_ptr(), 4));
206 EXPECT_FALSE(memcmp(linestring.c_str() + 4, cartesian_linestring.c_ptr() + 4,
207 linestring.size() - 4));
208
209 // Radians
210 String radian_linestring;
211 gis::write_geometry(m_radian_srs, gls, &radian_linestring);
212 EXPECT_EQ(linestring.size(), radian_linestring.length());
213 EXPECT_FALSE(memcmp("\x40\x42\x0f\x00", radian_linestring.c_ptr(), 4));
214 EXPECT_FALSE(memcmp(linestring.c_str() + 4, radian_linestring.c_ptr() + 4,
215 linestring.size() - 4));
216 }
217
218 TEST_F(WkbWriterTest, Polygon) {
219 gis::Cartesian_polygon cpy;
220 gis::Cartesian_linearring cexterior;
221 cexterior.push_back(gis::Cartesian_point(0, 0));
222 cexterior.push_back(gis::Cartesian_point(0, 0));
223 cexterior.push_back(gis::Cartesian_point(0, 0));
224 cexterior.push_back(gis::Cartesian_point(0, 0));
225 cpy.push_back(cexterior);
226 gis::Cartesian_linearring cinterior;
227 cinterior.push_back(gis::Cartesian_point(0, 0));
228 cinterior.push_back(gis::Cartesian_point(0, 0));
229 cinterior.push_back(gis::Cartesian_point(0, 0));
230 cinterior.push_back(gis::Cartesian_point(0, 0));
231 cpy.push_back(cinterior);
232
233 gis::Geographic_polygon gpy;
234 gis::Geographic_linearring gexterior;
235 gexterior.push_back(gis::Geographic_point(0, 0));
236 gexterior.push_back(gis::Geographic_point(0, 0));
237 gexterior.push_back(gis::Geographic_point(0, 0));
238 gexterior.push_back(gis::Geographic_point(0, 0));
239 gpy.push_back(gexterior);
240 gis::Geographic_linearring ginterior;
241 ginterior.push_back(gis::Geographic_point(0, 0));
242 ginterior.push_back(gis::Geographic_point(0, 0));
243 ginterior.push_back(gis::Geographic_point(0, 0));
244 ginterior.push_back(gis::Geographic_point(0, 0));
245 gpy.push_back(ginterior);
246
247 // Polygon with two rings, each with four points
248 std::string polygon(
249 "\x00\x00\x00\x00" // SRID
250 "\x01" // Byte order
251 "\x03\x00\x00\x00" // WKB type
252 "\x02\x00\x00\x00" // numRings
253 "\x04\x00\x00\x00" // numPoints
254 "\x00\x00\x00\x00\x00\x00\x00\x00" // X
255 "\x00\x00\x00\x00\x00\x00\x00\x00" // Y
256 "\x00\x00\x00\x00\x00\x00\x00\x00" // X
257 "\x00\x00\x00\x00\x00\x00\x00\x00" // Y
258 "\x00\x00\x00\x00\x00\x00\x00\x00" // X
259 "\x00\x00\x00\x00\x00\x00\x00\x00" // Y
260 "\x00\x00\x00\x00\x00\x00\x00\x00" // X
261 "\x00\x00\x00\x00\x00\x00\x00\x00" // Y
262 "\x04\x00\x00\x00" // numPoints
263 "\x00\x00\x00\x00\x00\x00\x00\x00" // X
264 "\x00\x00\x00\x00\x00\x00\x00\x00" // Y
265 "\x00\x00\x00\x00\x00\x00\x00\x00" // X
266 "\x00\x00\x00\x00\x00\x00\x00\x00" // Y
267 "\x00\x00\x00\x00\x00\x00\x00\x00" // X
268 "\x00\x00\x00\x00\x00\x00\x00\x00" // Y
269 "\x00\x00\x00\x00\x00\x00\x00\x00" // X
270 "\x00\x00\x00\x00\x00\x00\x00\x00", // Y
271 149);
272
273 // Cartesian
274 String srid0_polygon;
275 gis::write_geometry(nullptr, cpy, &srid0_polygon);
276 EXPECT_EQ(polygon.size(), srid0_polygon.length());
277 EXPECT_FALSE(memcmp(polygon.c_str(), srid0_polygon.c_ptr(), polygon.size()));
278
279 String cartesian_polygon;
280 gis::write_geometry(m_cartesian_srs, cpy, &cartesian_polygon);
281 EXPECT_EQ(polygon.size(), cartesian_polygon.length());
282 EXPECT_FALSE(memcmp("\x11\x0f\x00\x00", cartesian_polygon.c_ptr(), 4));
283 EXPECT_FALSE(memcmp(polygon.c_str() + 4, cartesian_polygon.c_ptr() + 4,
284 polygon.size() - 4));
285
286 // Radians
287 String radian_polygon;
288 gis::write_geometry(m_radian_srs, gpy, &radian_polygon);
289 EXPECT_EQ(polygon.size(), radian_polygon.length());
290 EXPECT_FALSE(memcmp("\x40\x42\x0f\x00", radian_polygon.c_ptr(), 4));
291 EXPECT_FALSE(memcmp(polygon.c_str() + 4, radian_polygon.c_ptr() + 4,
292 polygon.size() - 4));
293 }
294
295 TEST_F(WkbWriterTest, Geometrycollection) {
296 gis::Cartesian_geometrycollection cgc;
297 cgc.push_back(gis::Cartesian_point(1, 2));
298 gis::Geographic_geometrycollection ggc;
299 ggc.push_back(gis::Geographic_point(1, 2));
300
301 // Little-endian GEOMETRYCOLLECTION(POINT(1 2));
302 std::string geometrycollection(
303 "\x00\x00\x00\x00" // SRID
304 "\x01" // Byte order
305 "\x07\x00\x00\x00" // WKB type
306 "\x01\x00\x00\x00" // numGeometries
307 "\x01" // Byte order
308 "\x01\x00\x00\x00" // WKB type
309 "\x00\x00\x00\x00\x00\x00\xf0\x3f" // X
310 "\x00\x00\x00\x00\x00\x00\x00\x40", // Y
311 34);
312
313 // Cartesian
314 String srid0_geometrycollection;
315 gis::write_geometry(nullptr, cgc, &srid0_geometrycollection);
316 EXPECT_EQ(geometrycollection.size(), srid0_geometrycollection.length());
317 EXPECT_FALSE(memcmp(geometrycollection.c_str(),
318 srid0_geometrycollection.c_ptr(),
319 geometrycollection.size()));
320
321 String cartesian_geometrycollection;
322 gis::write_geometry(m_cartesian_srs, cgc, &cartesian_geometrycollection);
323 EXPECT_EQ(geometrycollection.size(), cartesian_geometrycollection.length());
324 EXPECT_FALSE(
325 memcmp("\x11\x0f\x00\x00", cartesian_geometrycollection.c_ptr(), 4));
326 EXPECT_FALSE(memcmp(geometrycollection.c_str() + 4,
327 cartesian_geometrycollection.c_ptr() + 4,
328 geometrycollection.size() - 4));
329
330 // Radians
331 String radian_geometrycollection;
332 gis::write_geometry(m_radian_srs, ggc, &radian_geometrycollection);
333 EXPECT_EQ(geometrycollection.size(), radian_geometrycollection.length());
334 EXPECT_FALSE(
335 memcmp("\x40\x42\x0f\x00", radian_geometrycollection.c_ptr(), 4));
336 EXPECT_FALSE(memcmp(geometrycollection.c_str() + 4,
337 radian_geometrycollection.c_ptr() + 4,
338 geometrycollection.size() - 4));
339 }
340
341 } // namespace wkb_writer_unittest