Debian Codegraph @

unittest/gunit/gis_wkb_parser-t.cc

raw symbols SCIP
1 /*
2 Copyright (c) 2017, 2026, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License, version 2.0, for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 #include "my_config.h"
27
28 #include <gtest/gtest.h>
29 #include <memory> // unique_ptr
30 #include <string>
31
32 #include "sql/dd/dd.h"
33 #include "sql/dd/impl/types/spatial_reference_system_impl.h"
34 #include "sql/dd/properties.h"
35 #include "sql/dd/types/spatial_reference_system.h"
36 #include "sql/gis/geometries.h"
37 #include "sql/gis/wkb.h"
38
39 namespace wkb_parser__unittest {
40
41 class WkbParserTest : public ::testing::Test {
42 protected:
43 dd::Spatial_reference_system_impl *m_cartesian_srs;
44 dd::Spatial_reference_system_impl *m_lat_long_srs;
45 dd::Spatial_reference_system_impl *m_long_lat_srs;
46 dd::Spatial_reference_system_impl *m_tweaked_geo_srs;
47
48 void SetUp() override {
49 m_cartesian_srs = dynamic_cast<dd::Spatial_reference_system_impl *>(
50 dd::create_object<dd::Spatial_reference_system>());
51 m_cartesian_srs->set_id(3857);
52 m_cartesian_srs->set_name("WGS 84 / Pseudo-Mercator");
53 m_cartesian_srs->set_created(0UL);
54 m_cartesian_srs->set_last_altered(0UL);
55 m_cartesian_srs->set_organization("EPSG");
56 m_cartesian_srs->set_organization_coordsys_id(3857);
57 m_cartesian_srs->set_definition(
58 "PROJCS[\"WGS 84 / Pseudo-Mercator\",GEOGCS[\"WGS 84\",DATUM[\"World "
59 "Geodetic System 1984\",SPHEROID[\"WGS "
60 "84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY["
61 "\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\","
62 "\"8901\"]],UNIT[\"degree\",0.017453292519943278,AUTHORITY[\"EPSG\","
63 "\"9122\"]],AXIS[\"Lat\",NORTH],AXIS[\"Lon\",EAST],AUTHORITY[\"EPSG\","
64 "\"4326\"]],PROJECTION[\"Popular Visualisation Pseudo "
65 "Mercator\",AUTHORITY[\"EPSG\",\"1024\"]],PARAMETER[\"Latitude of "
66 "natural "
67 "origin\",0,AUTHORITY[\"EPSG\",\"8801\"]],PARAMETER[\"Longitude of "
68 "natural origin\",0,AUTHORITY[\"EPSG\",\"8802\"]],PARAMETER[\"False "
69 "easting\",0,AUTHORITY[\"EPSG\",\"8806\"]],PARAMETER[\"False "
70 "northing\",0,AUTHORITY[\"EPSG\",\"8807\"]],UNIT[\"metre\",1,AUTHORITY["
71 "\"EPSG\",\"9001\"]],AXIS[\"X\",EAST],AXIS[\"Y\",NORTH],AUTHORITY["
72 "\"EPSG\",\"3857\"]]");
73 m_cartesian_srs->set_description("");
74 m_cartesian_srs->parse_definition();
75
76 // Standard EPSG 4326, lat-long (N-E), Greenwich Meridian
77 m_lat_long_srs = dynamic_cast<dd::Spatial_reference_system_impl *>(
78 dd::create_object<dd::Spatial_reference_system>());
79 m_lat_long_srs->set_id(4326);
80 m_lat_long_srs->set_name("WGS 84");
81 m_lat_long_srs->set_created(0UL);
82 m_lat_long_srs->set_last_altered(0UL);
83 m_lat_long_srs->set_organization("EPSG");
84 m_lat_long_srs->set_organization_coordsys_id(4326);
85 m_lat_long_srs->set_definition(
86 "GEOGCS[\"WGS 84\",DATUM[\"World Geodetic System 1984\",SPHEROID[\"WGS "
87 "84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY["
88 "\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\","
89 "\"8901\"]],UNIT[\"degree\",0.017453292519943278,AUTHORITY[\"EPSG\","
90 "\"9122\"]],AXIS[\"Lat\",NORTH],AXIS[\"Lon\",EAST],AUTHORITY[\"EPSG\","
91 "\"4326\"]]");
92 m_lat_long_srs->set_description("");
93 m_lat_long_srs->parse_definition();
94
95 // Axis-swapped EPSG 4326, long-lat (E-N), Greenwich Meridian
96 m_long_lat_srs = dynamic_cast<dd::Spatial_reference_system_impl *>(
97 dd::create_object<dd::Spatial_reference_system>());
98 m_long_lat_srs->set_id(4326);
99 m_long_lat_srs->set_name("Long-Lat WGS 84");
100 m_long_lat_srs->set_created(0UL);
101 m_long_lat_srs->set_last_altered(0UL);
102 m_long_lat_srs->set_organization("EPSG");
103 m_long_lat_srs->set_organization_coordsys_id(4326);
104 m_long_lat_srs->set_definition(
105 "GEOGCS[\"Long-Lat WGS 84\",DATUM[\"World Geodetic System "
106 "1984\",SPHEROID[\"WGS "
107 "84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY["
108 "\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\","
109 "\"8901\"]],UNIT[\"degree\",0.017453292519943278,AUTHORITY[\"EPSG\","
110 "\"9122\"]],AXIS[\"Lon\",EAST],AXIS[\"Lat\",NORTH],AUTHORITY[\"EPSG\","
111 "\"4326\"]]");
112 m_long_lat_srs->set_description("");
113 m_long_lat_srs->parse_definition();
114
115 // Lat-long (S-W) in grad, Meridian 10 grad East of Greenwich
116 m_tweaked_geo_srs = dynamic_cast<dd::Spatial_reference_system_impl *>(
117 dd::create_object<dd::Spatial_reference_system>());
118 m_tweaked_geo_srs->set_id(1000000);
119 m_tweaked_geo_srs->set_name("Tweaked");
120 m_tweaked_geo_srs->set_created(0UL);
121 m_tweaked_geo_srs->set_last_altered(0UL);
122 m_tweaked_geo_srs->set_organization("EPSG");
123 m_tweaked_geo_srs->set_organization_coordsys_id(1000000);
124 m_tweaked_geo_srs->set_definition(
125 "GEOGCS[\"Tweaked\",DATUM[\"World Geodetic System "
126 "1984\",SPHEROID[\"WGS "
127 "84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY["
128 "\"EPSG\",\"6326\"]],PRIMEM[\"10 "
129 "E\",10,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"grad\",0."
130 "01570796326794895,AUTHORITY[\"EPSG\",\"9122\"]],AXIS[\"Lat\",SOUTH],"
131 "AXIS[\"Lon\",WEST],AUTHORITY[\"EPSG\",\"4326\"]]");
132 m_tweaked_geo_srs->set_description("");
133 m_tweaked_geo_srs->parse_definition();
134 }
135
136 void TearDown() override {
137 delete m_cartesian_srs;
138 delete m_lat_long_srs;
139 delete m_long_lat_srs;
140 delete m_tweaked_geo_srs;
141 }
142
143 WkbParserTest() = default;
144
145 private:
146 WkbParserTest(WkbParserTest const &) = delete;
147 WkbParserTest &operator=(WkbParserTest const &) = delete;
148 };
149
150 TEST_F(WkbParserTest, Invalid) {
151 std::unique_ptr<gis::Geometry> g;
152 // Incorrect byte_order
153 // |bo||WKB type ||x ||y |
154 std::string bo(
155 "\x03\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0\x3f\x00\x00\x00\x00\x00"
156 "\x00\x00\x40",
157 21);
158 g = gis::parse_wkb(nullptr, m_cartesian_srs, bo.c_str(), bo.length());
159 EXPECT_FALSE(g);
160
161 // Invalid WKB type
162 // |bo||WKB type ||x
163 // ||y |
164 std::string invalid_type(
165 "\x01\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0\x3f\x00\x00\x00\x00\x00"
166 "\x00\x00\x40",
167 21);
168 g = gis::parse_wkb(nullptr, m_cartesian_srs, invalid_type.c_str(),
169 invalid_type.length());
170 EXPECT_FALSE(g);
171
172 // Too short WKB type
173 // |bo||WKB type |
174 std::string short_type("\x01\xff\x00\x00", 4);
175 g = gis::parse_wkb(nullptr, m_cartesian_srs, short_type.c_str(),
176 short_type.length());
177 EXPECT_FALSE(g);
178 }
179
180 TEST_F(WkbParserTest, Point) {
181 std::unique_ptr<gis::Geometry> g;
182
183 // Little-endian POINT(1 2)
184 // |bo||WKB type ||x ||y |
185 std::string le(
186 "\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0\x3f\x00\x00\x00\x00\x00"
187 "\x00\x00\x40",
188 21);
189 // Big-endian POINT(1 2)
190 // |bo||WKB type ||x ||y |
191 std::string be(
192 "\x00\x00\x00\x00\x01\x3f\xf0\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00\x00"
193 "\x00\x00\x00",
194 21);
195
196 // Cartesian
197 g = gis::parse_wkb(nullptr, m_cartesian_srs, le.c_str(), le.length());
198 EXPECT_EQ(g->type(), gis::Geometry_type::kPoint);
199 EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).x(), 1.0);
200 EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).y(), 2.0);
201
202 g = gis::parse_wkb(nullptr, m_cartesian_srs, be.c_str(), be.length());
203 EXPECT_EQ(g->type(), gis::Geometry_type::kPoint);
204 EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).x(), 1.0);
205 EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).y(), 2.0);
206
207 // Lat-long
208 g = gis::parse_wkb(nullptr, m_lat_long_srs, le.c_str(), le.length());
209 EXPECT_EQ(g->type(), gis::Geometry_type::kPoint);
210 EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).x(),
211 0.034906585039886556);
212 EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).y(),
213 0.017453292519943278);
214
215 g = gis::parse_wkb(nullptr, m_lat_long_srs, be.c_str(), be.length());
216 EXPECT_EQ(g->type(), gis::Geometry_type::kPoint);
217 EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).x(),
218 0.034906585039886556);
219 EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).y(),
220 0.017453292519943278);
221
222 // Long-lat
223 g = gis::parse_wkb(nullptr, m_long_lat_srs, le.c_str(), le.length());
224 EXPECT_EQ(g->type(), gis::Geometry_type::kPoint);
225 EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).x(),
226 0.017453292519943278);
227 EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).y(),
228 0.034906585039886556);
229
230 g = gis::parse_wkb(nullptr, m_long_lat_srs, be.c_str(), be.length());
231 EXPECT_EQ(g->type(), gis::Geometry_type::kPoint);
232 EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).x(),
233 0.017453292519943278);
234 EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).y(),
235 0.034906585039886556);
236
237 // Lat-long, grad, meridian 10 grad East of Greenwich
238 g = gis::parse_wkb(nullptr, m_tweaked_geo_srs, le.c_str(), le.length());
239 EXPECT_EQ(g->type(), gis::Geometry_type::kPoint);
240 EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).x(), 0.1256637061435916);
241 EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).y(),
242 -0.01570796326794895);
243
244 g = gis::parse_wkb(nullptr, m_tweaked_geo_srs, be.c_str(), be.length());
245 EXPECT_EQ(g->type(), gis::Geometry_type::kPoint);
246 EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).x(), 0.1256637061435916);
247 EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).y(),
248 -0.01570796326794895);
249
250 // One byte shorter than it should be
251 // |bo||WKB type ||x ||y
252 // |
253 std::string too_short(
254 "\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0\x3f\x00\x00\x00\x00\x00"
255 "\x00\x00",
256 20);
257 // One byte longer than WKB data
258 // |bo||WKB type ||x ||y |
259 std::string too_long(
260 "\x00\x00\x00\x00\x01\x3f\xf0\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00\x00"
261 "\x00\x00\x00\x00",
262 22);
263
264 g = gis::parse_wkb(nullptr, m_cartesian_srs, too_short.c_str(),
265 too_short.length());
266 EXPECT_FALSE(g);
267
268 g = gis::parse_wkb(nullptr, m_cartesian_srs, too_long.c_str(),
269 too_long.length());
270 EXPECT_FALSE(g);
271 }
272
273 TEST_F(WkbParserTest, Linestring) {
274 std::unique_ptr<gis::Geometry> g;
275
276 // Linestring with no points
277 // |bo||WKB type ||numPoints |
278 std::string empty_ls("\x01\x02\x00\x00\x00\x00\x00\x00\x00", 9);
279 g = gis::parse_wkb(nullptr, m_cartesian_srs, empty_ls.c_str(),
280 empty_ls.length());
281 EXPECT_FALSE(g);
282
283 // Linestring with one point
284 // |bo||WKB type ||numPoints ||x
285 // ||y |
286 std::string one_ls(
287 "\x01\x02\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
288 "\x00\x00\x00\x00\x00\x00\x00",
289 25);
290 g = gis::parse_wkb(nullptr, m_cartesian_srs, one_ls.c_str(), one_ls.length());
291 EXPECT_FALSE(g);
292
293 // Linestring with two points
294 // |bo||WKB type ||numPoints ||x
295 // ||y ||x
296 // ||y |
297 std::string two_ls(
298 "\x01\x02\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
299 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
300 "\x00\x00\x00\x00\x00",
301 41);
302 g = gis::parse_wkb(nullptr, m_cartesian_srs, two_ls.c_str(), two_ls.length());
303 EXPECT_EQ(g->type(), gis::Geometry_type::kLinestring);
304 EXPECT_FALSE(static_cast<gis::Linestring *>(g.get())->empty());
305 EXPECT_EQ(static_cast<gis::Linestring *>(g.get())->size(), 2UL);
306
307 // |x ||y |
308 std::string pt_0_0(
309 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16);
310 std::string pt_10_0(
311 "\x00\x00\x00\x00\x00\x00\x24\x40\x00\x00\x00\x00\x00\x00\x00\x00", 16);
312 std::string pt_10_10(
313 "\x00\x00\x00\x00\x00\x00\x24\x40\x00\x00\x00\x00\x00\x00\x24\x40", 16);
314 std::string pt_0_10(
315 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x24\x40", 16);
316 // |bo||WKB type ||numPoints |
317 std::string ls("\x01\x02\x00\x00\x00\x04\x00\x00\x00", 9);
318 ls.append(pt_0_0);
319 ls.append(pt_10_0);
320 ls.append(pt_10_10);
321 ls.append(pt_10_0);
322
323 g = gis::parse_wkb(nullptr, m_cartesian_srs, ls.c_str(), ls.length());
324 EXPECT_EQ(g->type(), gis::Geometry_type::kLinestring);
325 EXPECT_FALSE(static_cast<gis::Linestring *>(g.get())->empty());
326 EXPECT_EQ(static_cast<gis::Linestring *>(g.get())->size(), 4UL);
327 }
328
329 TEST_F(WkbParserTest, Polygon) {
330 std::unique_ptr<gis::Geometry> g;
331
332 // Polygon with no rings
333 // |bo||WKB type ||numRings |
334 std::string empty_py("\x01\x03\x00\x00\x00\x00\x00\x00\x00", 9);
335 g = gis::parse_wkb(nullptr, m_cartesian_srs, empty_py.c_str(),
336 empty_py.length());
337 EXPECT_FALSE(g);
338
339 // Polygon with exterior ring with no points
340 // |bo||WKB type ||numRings ||numPoints |
341 std::string zero_py("\x01\x03\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00",
342 13);
343 g = gis::parse_wkb(nullptr, m_cartesian_srs, zero_py.c_str(),
344 zero_py.length());
345 EXPECT_FALSE(g);
346
347 // Polygon with exterior ring with one point
348 // |bo||WKB type ||numRings ||numPoints ||x
349 // ||y |
350 std::string one_py(
351 "\x01\x03\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00"
352 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
353 29);
354 g = gis::parse_wkb(nullptr, m_cartesian_srs, one_py.c_str(), one_py.length());
355 EXPECT_FALSE(g);
356
357 // Polygon with exterior ring with two points
358 // |bo||WKB type ||numRings ||numPoints ||x
359 // ||y ||x
360 // ||y |
361 std::string two_py(
362 "\x01\x03\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00"
363 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
364 "\x00\x00\x00\x00\x00\x00\x00\x00\x00",
365 45);
366 g = gis::parse_wkb(nullptr, m_cartesian_srs, two_py.c_str(), two_py.length());
367 EXPECT_FALSE(g);
368
369 // Polygon with exterior ring with three points
370 // |bo||WKB type ||numRings ||numPoints ||x
371 // ||y ||x
372 // ||y ||x
373 // ||y |
374 std::string three_py(
375 "\x01\x03\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00"
376 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
377 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
378 "\x00\x00\x00\x00\x00\x00\x00",
379 61);
380 g = gis::parse_wkb(nullptr, m_cartesian_srs, three_py.c_str(),
381 three_py.length());
382 EXPECT_FALSE(g);
383
384 // |x ||y |
385 std::string pt_0_0(
386 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16);
387 std::string pt_10_0(
388 "\x00\x00\x00\x00\x00\x00\x24\x40\x00\x00\x00\x00\x00\x00\x00\x00", 16);
389 std::string pt_10_10(
390 "\x00\x00\x00\x00\x00\x00\x24\x40\x00\x00\x00\x00\x00\x00\x24\x40", 16);
391 std::string pt_0_10(
392 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x24\x40", 16);
393 std::string exterior("\x05\x00\x00\x00", 4);
394 exterior.append(pt_0_0);
395 exterior.append(pt_10_0);
396 exterior.append(pt_10_10);
397 exterior.append(pt_0_10);
398 exterior.append(pt_0_0);
399 // |x ||y |
400 std::string pt_2_2(
401 "\x00\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00\x00\x00\x00\x00\x40", 16);
402 std::string pt_4_2(
403 "\x00\x00\x00\x00\x00\x00\x10\x40\x00\x00\x00\x00\x00\x00\x00\x40", 16);
404 std::string pt_4_4(
405 "\x00\x00\x00\x00\x00\x00\x10\x40\x00\x00\x00\x00\x00\x00\x10\x40", 16);
406 std::string pt_2_4(
407 "\x00\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00\x00\x00\x00\x10\x40", 16);
408 // |numPoints |
409 std::string interior1("\x05\x00\x00\x00", 4);
410 interior1.append(pt_2_2);
411 interior1.append(pt_2_4);
412 interior1.append(pt_4_4);
413 interior1.append(pt_4_2);
414 interior1.append(pt_2_2);
415 // |x ||y |
416 std::string pt_6_6(
417 "\x00\x00\x00\x00\x00\x00\x18\x40\x00\x00\x00\x00\x00\x00\x18\x40", 16);
418 std::string pt_6_8(
419 "\x00\x00\x00\x00\x00\x00\x18\x40\x00\x00\x00\x00\x00\x00\x20\x40", 16);
420 std::string pt_8_8(
421 "\x00\x00\x00\x00\x00\x00\x20\x40\x00\x00\x00\x00\x00\x00\x20\x40", 16);
422 // |numPoints |
423 std::string interior2("\x04\x00\x00\x00", 4);
424 interior2.append(pt_6_6);
425 interior2.append(pt_6_8);
426 interior2.append(pt_8_8);
427 interior2.append(pt_6_6);
428 // Little-endian POLYGON((0 0, 10 0, 10 10, 0 10, 0 0), (2 2, 2 4, 4 4, 4 2),
429 // (6 6, 6 8, 8 8, 6 6))
430 // |bo||WKB type ||numRings |
431 std::string py("\x01\x03\x00\x00\x00\x03\x00\x00\x00", 9);
432 py.append(exterior);
433 py.append(interior1);
434 py.append(interior2);
435
436 g = gis::parse_wkb(nullptr, m_cartesian_srs, py.c_str(), py.length());
437 EXPECT_EQ(g->type(), gis::Geometry_type::kPolygon);
438 EXPECT_EQ(static_cast<gis::Polygon *>(g.get())->size(), 3UL);
439 EXPECT_FALSE(static_cast<gis::Polygon *>(g.get())->empty());
440 EXPECT_EQ(static_cast<gis::Polygon *>(g.get())->interior_ring(1).size(), 4UL);
441 }
442
443 TEST_F(WkbParserTest, Multipoint) {
444 std::unique_ptr<gis::Geometry> g;
445
446 // Little-endian POINT(1 2)
447 // |bo||WKB type ||x ||y |
448 std::string le(
449 "\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0\x3f\x00\x00\x00\x00\x00"
450 "\x00\x00\x40",
451 21);
452 // Big-endian POINT(1 2)
453 // |bo||WKB type ||x ||y |
454 std::string be(
455 "\x00\x00\x00\x00\x01\x3f\xf0\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00\x00"
456 "\x00\x00\x00",
457 21);
458 // Mixed-endian MULTIPOINT((1 2), (1 2))
459 // |bo||WKB type ||numPoints |
460 std::string mpt("\x01\x04\x00\x00\x00\x02\x00\x00\x00", 9);
461 mpt.append(le);
462 mpt.append(be);
463
464 g = gis::parse_wkb(nullptr, m_cartesian_srs, mpt.c_str(), mpt.length());
465 EXPECT_EQ(g->type(), gis::Geometry_type::kMultipoint);
466 EXPECT_FALSE(static_cast<gis::Multipoint *>(g.get())->empty());
467 EXPECT_EQ(static_cast<gis::Multipoint *>(g.get())->size(), 2UL);
468
469 // Multipoint with no points
470 // |bo||WKB type ||numPoints |
471 std::string empty_mpt("\x01\x04\x00\x00\x00\x00\x00\x00\x00", 9);
472 g = gis::parse_wkb(nullptr, m_cartesian_srs, empty_mpt.c_str(),
473 empty_mpt.length());
474 EXPECT_FALSE(g);
475 }
476
477 TEST_F(WkbParserTest, Multilinestring) {
478 std::unique_ptr<gis::Geometry> g;
479
480 // |x ||y |
481 std::string pt_0_0(
482 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16);
483 std::string pt_10_0(
484 "\x00\x00\x00\x00\x00\x00\x24\x40\x00\x00\x00\x00\x00\x00\x00\x00", 16);
485 std::string pt_10_10(
486 "\x00\x00\x00\x00\x00\x00\x24\x40\x00\x00\x00\x00\x00\x00\x24\x40", 16);
487 std::string pt_0_10(
488 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x24\x40", 16);
489 // |bo||WKB type ||numPoints |
490 std::string ls("\x01\x02\x00\x00\x00\x04\x00\x00\x00", 9);
491 ls.append(pt_0_0);
492 ls.append(pt_10_0);
493 ls.append(pt_10_10);
494 ls.append(pt_10_0);
495 // Mixed-endian MULTILINESTRING((0 0, 10 0, 10 10, 0 10))
496 // |bo||WKB type ||numLinestrings|
497 std::string mls("\x00\x00\x00\x00\x05\x00\x00\x00\x01", 9);
498 mls.append(ls);
499
500 g = gis::parse_wkb(nullptr, m_cartesian_srs, mls.c_str(), mls.length());
501 EXPECT_EQ(g->type(), gis::Geometry_type::kMultilinestring);
502 EXPECT_FALSE(static_cast<gis::Multilinestring *>(g.get())->empty());
503 EXPECT_EQ(static_cast<gis::Multilinestring *>(g.get())->size(), 1UL);
504
505 // Multilinestrings with no linestrings
506 // |bo||WKB type ||numLinestrings|
507 std::string empty_mls("\x01\x05\x00\x00\x00\x00\x00\x00\x00", 9);
508 g = gis::parse_wkb(nullptr, m_cartesian_srs, empty_mls.c_str(),
509 empty_mls.length());
510 EXPECT_FALSE(g);
511 }
512
513 TEST_F(WkbParserTest, Multipolygon) {
514 std::unique_ptr<gis::Geometry> g;
515
516 // |x ||y |
517 std::string pt_0_0(
518 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16);
519 std::string pt_10_0(
520 "\x00\x00\x00\x00\x00\x00\x24\x40\x00\x00\x00\x00\x00\x00\x00\x00", 16);
521 std::string pt_10_10(
522 "\x00\x00\x00\x00\x00\x00\x24\x40\x00\x00\x00\x00\x00\x00\x24\x40", 16);
523 std::string pt_0_10(
524 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x24\x40", 16);
525 std::string exterior("\x05\x00\x00\x00", 4);
526 exterior.append(pt_0_0);
527 exterior.append(pt_10_0);
528 exterior.append(pt_10_10);
529 exterior.append(pt_0_10);
530 exterior.append(pt_0_0);
531 // |x ||y |
532 std::string pt_2_2(
533 "\x00\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00\x00\x00\x00\x00\x40", 16);
534 std::string pt_4_2(
535 "\x00\x00\x00\x00\x00\x00\x10\x40\x00\x00\x00\x00\x00\x00\x00\x40", 16);
536 std::string pt_4_4(
537 "\x00\x00\x00\x00\x00\x00\x10\x40\x00\x00\x00\x00\x00\x00\x10\x40", 16);
538 std::string pt_2_4(
539 "\x00\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00\x00\x00\x00\x10\x40", 16);
540 // |numPoints |
541 std::string interior1("\x05\x00\x00\x00", 4);
542 interior1.append(pt_2_2);
543 interior1.append(pt_2_4);
544 interior1.append(pt_4_4);
545 interior1.append(pt_4_2);
546 interior1.append(pt_2_2);
547 // |x ||y |
548 std::string pt_6_6(
549 "\x00\x00\x00\x00\x00\x00\x18\x40\x00\x00\x00\x00\x00\x00\x18\x40", 16);
550 std::string pt_6_8(
551 "\x00\x00\x00\x00\x00\x00\x18\x40\x00\x00\x00\x00\x00\x00\x20\x40", 16);
552 std::string pt_8_8(
553 "\x00\x00\x00\x00\x00\x00\x20\x40\x00\x00\x00\x00\x00\x00\x20\x40", 16);
554 // |numPoints |
555 std::string interior2("\x04\x00\x00\x00", 4);
556 interior2.append(pt_6_6);
557 interior2.append(pt_6_8);
558 interior2.append(pt_8_8);
559 interior2.append(pt_6_6);
560 // |bo||WKB type ||numRings |
561 std::string py("\x01\x03\x00\x00\x00\x03\x00\x00\x00", 9);
562 py.append(exterior);
563 py.append(interior1);
564 py.append(interior2);
565 // Mixed-endian MULTIPOLYGON(((0 0, 10 0, 10 10, 0 10, 0 0),
566 // (2 2, 2 4, 4 4, 4 2), (6 6, 6 8, 8 8, 6 6)))
567 // |bo||WKB type ||numPolygons |
568 std::string mpy("\x00\x00\x00\x00\x06\x00\x00\x00\x01", 9);
569 mpy.append(py);
570
571 g = gis::parse_wkb(nullptr, m_cartesian_srs, mpy.c_str(), mpy.length());
572 EXPECT_EQ(g->type(), gis::Geometry_type::kMultipolygon);
573 EXPECT_FALSE(static_cast<gis::Multipolygon *>(g.get())->empty());
574 EXPECT_EQ(static_cast<gis::Multipolygon *>(g.get())->size(), 1UL);
575
576 // Multipolygon with no polygons
577 // |bo||WKB type ||numPolygons |
578 std::string empty_mpy("\x01\x06\x00\x00\x00\x00\x00\x00\x00", 9);
579 g = gis::parse_wkb(nullptr, m_cartesian_srs, empty_mpy.c_str(),
580 empty_mpy.length());
581 EXPECT_FALSE(g);
582 }
583
584 TEST_F(WkbParserTest, Geometrycollection) {
585 std::unique_ptr<gis::Geometry> g;
586
587 // Little-endian POINT(1 2)
588 // |bo||WKB type ||x ||y |
589 std::string le(
590 "\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0\x3f\x00\x00\x00\x00\x00"
591 "\x00\x00\x40",
592 21);
593 // Big-endian POINT(1 2)
594 // |bo||WKB type ||x ||y |
595 std::string be(
596 "\x00\x00\x00\x00\x01\x3f\xf0\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00\x00"
597 "\x00\x00\x00",
598 21);
599 // Mixed-endian MULTIPOINT((1 2), (1 2))
600 // |bo||WKB type ||numPoints |
601 std::string gc("\x01\x07\x00\x00\x00\x02\x00\x00\x00", 9);
602 gc.append(le);
603 gc.append(be);
604
605 g = gis::parse_wkb(nullptr, m_cartesian_srs, gc.c_str(), gc.length());
606 EXPECT_EQ(g->type(), gis::Geometry_type::kGeometrycollection);
607 EXPECT_FALSE(static_cast<gis::Geometrycollection *>(g.get())->empty());
608 EXPECT_EQ(static_cast<gis::Geometrycollection *>(g.get())->size(), 2UL);
609
610 // Empty geometrycollection
611 // |bo||WKB type ||numGeometries |
612 std::string empty_gc("\x01\x07\x00\x00\x00\x00\x00\x00\x00", 9);
613 g = gis::parse_wkb(nullptr, m_cartesian_srs, empty_gc.c_str(),
614 empty_gc.length());
615 EXPECT_TRUE(g);
616
617 // Geometry collection with empty geometrycollection
618 // |bo||WKB type ||numGeometries ||bo||WKB type
619 // ||numGeometries |
620 std::string gc_empty_gc(
621 "\x01\x07\x00\x00\x00\x01\x00\x00\x00\x01\x07\x00\x00\x00\x00\x00\x00"
622 "\x00",
623 18);
624 g = gis::parse_wkb(nullptr, m_cartesian_srs, gc_empty_gc.c_str(),
625 gc_empty_gc.length());
626 EXPECT_TRUE(g);
627 EXPECT_FALSE(static_cast<gis::Geometrycollection *>(g.get())->empty());
628 EXPECT_TRUE(static_cast<gis::Geometrycollection *>(g.get())->is_empty());
629 }
630
631 } // namespace wkb_parser__unittest