File: gis_wkb_parser-t.cc

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (631 lines) | stat: -rw-r--r-- 26,859 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/*
  Copyright (c) 2017, 2025, Oracle and/or its affiliates.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License, version 2.0,
  as published by the Free Software Foundation.

  This program is designed to work with certain software (including
  but not limited to OpenSSL) that is licensed under separate terms,
  as designated in a particular file or component or in included license
  documentation.  The authors of MySQL hereby grant you an additional
  permission to link the program and your derivative works with the
  separately licensed software that they have either included with
  the program or referenced in the documentation.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License, version 2.0, for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
*/

#include "my_config.h"

#include <gtest/gtest.h>
#include <memory>  // unique_ptr
#include <string>

#include "sql/dd/dd.h"
#include "sql/dd/impl/types/spatial_reference_system_impl.h"
#include "sql/dd/properties.h"
#include "sql/dd/types/spatial_reference_system.h"
#include "sql/gis/geometries.h"
#include "sql/gis/wkb.h"

namespace wkb_parser__unittest {

class WkbParserTest : public ::testing::Test {
 protected:
  dd::Spatial_reference_system_impl *m_cartesian_srs;
  dd::Spatial_reference_system_impl *m_lat_long_srs;
  dd::Spatial_reference_system_impl *m_long_lat_srs;
  dd::Spatial_reference_system_impl *m_tweaked_geo_srs;

  void SetUp() override {
    m_cartesian_srs = dynamic_cast<dd::Spatial_reference_system_impl *>(
        dd::create_object<dd::Spatial_reference_system>());
    m_cartesian_srs->set_id(3857);
    m_cartesian_srs->set_name("WGS 84 / Pseudo-Mercator");
    m_cartesian_srs->set_created(0UL);
    m_cartesian_srs->set_last_altered(0UL);
    m_cartesian_srs->set_organization("EPSG");
    m_cartesian_srs->set_organization_coordsys_id(3857);
    m_cartesian_srs->set_definition(
        "PROJCS[\"WGS 84 / Pseudo-Mercator\",GEOGCS[\"WGS 84\",DATUM[\"World "
        "Geodetic System 1984\",SPHEROID[\"WGS "
        "84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY["
        "\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\","
        "\"8901\"]],UNIT[\"degree\",0.017453292519943278,AUTHORITY[\"EPSG\","
        "\"9122\"]],AXIS[\"Lat\",NORTH],AXIS[\"Lon\",EAST],AUTHORITY[\"EPSG\","
        "\"4326\"]],PROJECTION[\"Popular Visualisation Pseudo "
        "Mercator\",AUTHORITY[\"EPSG\",\"1024\"]],PARAMETER[\"Latitude of "
        "natural "
        "origin\",0,AUTHORITY[\"EPSG\",\"8801\"]],PARAMETER[\"Longitude of "
        "natural origin\",0,AUTHORITY[\"EPSG\",\"8802\"]],PARAMETER[\"False "
        "easting\",0,AUTHORITY[\"EPSG\",\"8806\"]],PARAMETER[\"False "
        "northing\",0,AUTHORITY[\"EPSG\",\"8807\"]],UNIT[\"metre\",1,AUTHORITY["
        "\"EPSG\",\"9001\"]],AXIS[\"X\",EAST],AXIS[\"Y\",NORTH],AUTHORITY["
        "\"EPSG\",\"3857\"]]");
    m_cartesian_srs->set_description("");
    m_cartesian_srs->parse_definition();

    // Standard EPSG 4326, lat-long (N-E), Greenwich Meridian
    m_lat_long_srs = dynamic_cast<dd::Spatial_reference_system_impl *>(
        dd::create_object<dd::Spatial_reference_system>());
    m_lat_long_srs->set_id(4326);
    m_lat_long_srs->set_name("WGS 84");
    m_lat_long_srs->set_created(0UL);
    m_lat_long_srs->set_last_altered(0UL);
    m_lat_long_srs->set_organization("EPSG");
    m_lat_long_srs->set_organization_coordsys_id(4326);
    m_lat_long_srs->set_definition(
        "GEOGCS[\"WGS 84\",DATUM[\"World Geodetic System 1984\",SPHEROID[\"WGS "
        "84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY["
        "\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\","
        "\"8901\"]],UNIT[\"degree\",0.017453292519943278,AUTHORITY[\"EPSG\","
        "\"9122\"]],AXIS[\"Lat\",NORTH],AXIS[\"Lon\",EAST],AUTHORITY[\"EPSG\","
        "\"4326\"]]");
    m_lat_long_srs->set_description("");
    m_lat_long_srs->parse_definition();

    // Axis-swapped EPSG 4326, long-lat (E-N), Greenwich Meridian
    m_long_lat_srs = dynamic_cast<dd::Spatial_reference_system_impl *>(
        dd::create_object<dd::Spatial_reference_system>());
    m_long_lat_srs->set_id(4326);
    m_long_lat_srs->set_name("Long-Lat WGS 84");
    m_long_lat_srs->set_created(0UL);
    m_long_lat_srs->set_last_altered(0UL);
    m_long_lat_srs->set_organization("EPSG");
    m_long_lat_srs->set_organization_coordsys_id(4326);
    m_long_lat_srs->set_definition(
        "GEOGCS[\"Long-Lat WGS 84\",DATUM[\"World Geodetic System "
        "1984\",SPHEROID[\"WGS "
        "84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY["
        "\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\","
        "\"8901\"]],UNIT[\"degree\",0.017453292519943278,AUTHORITY[\"EPSG\","
        "\"9122\"]],AXIS[\"Lon\",EAST],AXIS[\"Lat\",NORTH],AUTHORITY[\"EPSG\","
        "\"4326\"]]");
    m_long_lat_srs->set_description("");
    m_long_lat_srs->parse_definition();

    // Lat-long (S-W) in grad, Meridian 10 grad East of Greenwich
    m_tweaked_geo_srs = dynamic_cast<dd::Spatial_reference_system_impl *>(
        dd::create_object<dd::Spatial_reference_system>());
    m_tweaked_geo_srs->set_id(1000000);
    m_tweaked_geo_srs->set_name("Tweaked");
    m_tweaked_geo_srs->set_created(0UL);
    m_tweaked_geo_srs->set_last_altered(0UL);
    m_tweaked_geo_srs->set_organization("EPSG");
    m_tweaked_geo_srs->set_organization_coordsys_id(1000000);
    m_tweaked_geo_srs->set_definition(
        "GEOGCS[\"Tweaked\",DATUM[\"World Geodetic System "
        "1984\",SPHEROID[\"WGS "
        "84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY["
        "\"EPSG\",\"6326\"]],PRIMEM[\"10 "
        "E\",10,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"grad\",0."
        "01570796326794895,AUTHORITY[\"EPSG\",\"9122\"]],AXIS[\"Lat\",SOUTH],"
        "AXIS[\"Lon\",WEST],AUTHORITY[\"EPSG\",\"4326\"]]");
    m_tweaked_geo_srs->set_description("");
    m_tweaked_geo_srs->parse_definition();
  }

  void TearDown() override {
    delete m_cartesian_srs;
    delete m_lat_long_srs;
    delete m_long_lat_srs;
    delete m_tweaked_geo_srs;
  }

  WkbParserTest() = default;

 private:
  WkbParserTest(WkbParserTest const &) = delete;
  WkbParserTest &operator=(WkbParserTest const &) = delete;
};

TEST_F(WkbParserTest, Invalid) {
  std::unique_ptr<gis::Geometry> g;
  // Incorrect byte_order
  //              |bo||WKB type      ||x                             ||y |
  std::string bo(
      "\x03\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0\x3f\x00\x00\x00\x00\x00"
      "\x00\x00\x40",
      21);
  g = gis::parse_wkb(nullptr, m_cartesian_srs, bo.c_str(), bo.length());
  EXPECT_FALSE(g);

  // Invalid WKB type
  //                        |bo||WKB type      ||x
  //                        ||y                             |
  std::string invalid_type(
      "\x01\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0\x3f\x00\x00\x00\x00\x00"
      "\x00\x00\x40",
      21);
  g = gis::parse_wkb(nullptr, m_cartesian_srs, invalid_type.c_str(),
                     invalid_type.length());
  EXPECT_FALSE(g);

  // Too short WKB type
  //                      |bo||WKB type      |
  std::string short_type("\x01\xff\x00\x00", 4);
  g = gis::parse_wkb(nullptr, m_cartesian_srs, short_type.c_str(),
                     short_type.length());
  EXPECT_FALSE(g);
}

TEST_F(WkbParserTest, Point) {
  std::unique_ptr<gis::Geometry> g;

  // Little-endian POINT(1 2)
  //              |bo||WKB type      ||x                             ||y |
  std::string le(
      "\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0\x3f\x00\x00\x00\x00\x00"
      "\x00\x00\x40",
      21);
  // Big-endian POINT(1 2)
  //              |bo||WKB type      ||x                             ||y |
  std::string be(
      "\x00\x00\x00\x00\x01\x3f\xf0\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00\x00"
      "\x00\x00\x00",
      21);

  // Cartesian
  g = gis::parse_wkb(nullptr, m_cartesian_srs, le.c_str(), le.length());
  EXPECT_EQ(g->type(), gis::Geometry_type::kPoint);
  EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).x(), 1.0);
  EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).y(), 2.0);

  g = gis::parse_wkb(nullptr, m_cartesian_srs, be.c_str(), be.length());
  EXPECT_EQ(g->type(), gis::Geometry_type::kPoint);
  EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).x(), 1.0);
  EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).y(), 2.0);

  // Lat-long
  g = gis::parse_wkb(nullptr, m_lat_long_srs, le.c_str(), le.length());
  EXPECT_EQ(g->type(), gis::Geometry_type::kPoint);
  EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).x(),
                   0.034906585039886556);
  EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).y(),
                   0.017453292519943278);

  g = gis::parse_wkb(nullptr, m_lat_long_srs, be.c_str(), be.length());
  EXPECT_EQ(g->type(), gis::Geometry_type::kPoint);
  EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).x(),
                   0.034906585039886556);
  EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).y(),
                   0.017453292519943278);

  // Long-lat
  g = gis::parse_wkb(nullptr, m_long_lat_srs, le.c_str(), le.length());
  EXPECT_EQ(g->type(), gis::Geometry_type::kPoint);
  EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).x(),
                   0.017453292519943278);
  EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).y(),
                   0.034906585039886556);

  g = gis::parse_wkb(nullptr, m_long_lat_srs, be.c_str(), be.length());
  EXPECT_EQ(g->type(), gis::Geometry_type::kPoint);
  EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).x(),
                   0.017453292519943278);
  EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).y(),
                   0.034906585039886556);

  // Lat-long, grad, meridian 10 grad East of Greenwich
  g = gis::parse_wkb(nullptr, m_tweaked_geo_srs, le.c_str(), le.length());
  EXPECT_EQ(g->type(), gis::Geometry_type::kPoint);
  EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).x(), 0.1256637061435916);
  EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).y(),
                   -0.01570796326794895);

  g = gis::parse_wkb(nullptr, m_tweaked_geo_srs, be.c_str(), be.length());
  EXPECT_EQ(g->type(), gis::Geometry_type::kPoint);
  EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).x(), 0.1256637061435916);
  EXPECT_DOUBLE_EQ(static_cast<gis::Point &>(*g.get()).y(),
                   -0.01570796326794895);

  // One byte shorter than it should be
  //                     |bo||WKB type      ||x                             ||y
  //                     |
  std::string too_short(
      "\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0\x3f\x00\x00\x00\x00\x00"
      "\x00\x00",
      20);
  // One byte longer than WKB data
  //                    |bo||WKB type      ||x                             ||y |
  std::string too_long(
      "\x00\x00\x00\x00\x01\x3f\xf0\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00\x00"
      "\x00\x00\x00\x00",
      22);

  g = gis::parse_wkb(nullptr, m_cartesian_srs, too_short.c_str(),
                     too_short.length());
  EXPECT_FALSE(g);

  g = gis::parse_wkb(nullptr, m_cartesian_srs, too_long.c_str(),
                     too_long.length());
  EXPECT_FALSE(g);
}

TEST_F(WkbParserTest, Linestring) {
  std::unique_ptr<gis::Geometry> g;

  // Linestring with no points
  //                    |bo||WKB type      ||numPoints     |
  std::string empty_ls("\x01\x02\x00\x00\x00\x00\x00\x00\x00", 9);
  g = gis::parse_wkb(nullptr, m_cartesian_srs, empty_ls.c_str(),
                     empty_ls.length());
  EXPECT_FALSE(g);

  // Linestring with one point
  //                  |bo||WKB type      ||numPoints     ||x
  //                  ||y                             |
  std::string one_ls(
      "\x01\x02\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00",
      25);
  g = gis::parse_wkb(nullptr, m_cartesian_srs, one_ls.c_str(), one_ls.length());
  EXPECT_FALSE(g);

  // Linestring with two points
  //                  |bo||WKB type      ||numPoints     ||x
  //                  ||y                             ||x
  //                  ||y                             |
  std::string two_ls(
      "\x01\x02\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00",
      41);
  g = gis::parse_wkb(nullptr, m_cartesian_srs, two_ls.c_str(), two_ls.length());
  EXPECT_EQ(g->type(), gis::Geometry_type::kLinestring);
  EXPECT_FALSE(static_cast<gis::Linestring *>(g.get())->empty());
  EXPECT_EQ(static_cast<gis::Linestring *>(g.get())->size(), 2UL);

  //                    |x                             ||y |
  std::string pt_0_0(
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16);
  std::string pt_10_0(
      "\x00\x00\x00\x00\x00\x00\x24\x40\x00\x00\x00\x00\x00\x00\x00\x00", 16);
  std::string pt_10_10(
      "\x00\x00\x00\x00\x00\x00\x24\x40\x00\x00\x00\x00\x00\x00\x24\x40", 16);
  std::string pt_0_10(
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x24\x40", 16);
  //              |bo||WKB type      ||numPoints     |
  std::string ls("\x01\x02\x00\x00\x00\x04\x00\x00\x00", 9);
  ls.append(pt_0_0);
  ls.append(pt_10_0);
  ls.append(pt_10_10);
  ls.append(pt_10_0);

  g = gis::parse_wkb(nullptr, m_cartesian_srs, ls.c_str(), ls.length());
  EXPECT_EQ(g->type(), gis::Geometry_type::kLinestring);
  EXPECT_FALSE(static_cast<gis::Linestring *>(g.get())->empty());
  EXPECT_EQ(static_cast<gis::Linestring *>(g.get())->size(), 4UL);
}

TEST_F(WkbParserTest, Polygon) {
  std::unique_ptr<gis::Geometry> g;

  // Polygon with no rings
  //                    |bo||WKB type      ||numRings      |
  std::string empty_py("\x01\x03\x00\x00\x00\x00\x00\x00\x00", 9);
  g = gis::parse_wkb(nullptr, m_cartesian_srs, empty_py.c_str(),
                     empty_py.length());
  EXPECT_FALSE(g);

  // Polygon with exterior ring with no points
  //                   |bo||WKB type      ||numRings      ||numPoints     |
  std::string zero_py("\x01\x03\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00",
                      13);
  g = gis::parse_wkb(nullptr, m_cartesian_srs, zero_py.c_str(),
                     zero_py.length());
  EXPECT_FALSE(g);

  // Polygon with exterior ring with one point
  //                  |bo||WKB type      ||numRings      ||numPoints     ||x
  //                  ||y                             |
  std::string one_py(
      "\x01\x03\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
      29);
  g = gis::parse_wkb(nullptr, m_cartesian_srs, one_py.c_str(), one_py.length());
  EXPECT_FALSE(g);

  // Polygon with exterior ring with two points
  //                  |bo||WKB type      ||numRings      ||numPoints     ||x
  //                  ||y                             ||x
  //                  ||y                             |
  std::string two_py(
      "\x01\x03\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00",
      45);
  g = gis::parse_wkb(nullptr, m_cartesian_srs, two_py.c_str(), two_py.length());
  EXPECT_FALSE(g);

  // Polygon with exterior ring with three points
  //                    |bo||WKB type      ||numRings      ||numPoints     ||x
  //                    ||y                             ||x
  //                    ||y                             ||x
  //                    ||y                             |
  std::string three_py(
      "\x01\x03\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      "\x00\x00\x00\x00\x00\x00\x00",
      61);
  g = gis::parse_wkb(nullptr, m_cartesian_srs, three_py.c_str(),
                     three_py.length());
  EXPECT_FALSE(g);

  //                    |x                             ||y |
  std::string pt_0_0(
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16);
  std::string pt_10_0(
      "\x00\x00\x00\x00\x00\x00\x24\x40\x00\x00\x00\x00\x00\x00\x00\x00", 16);
  std::string pt_10_10(
      "\x00\x00\x00\x00\x00\x00\x24\x40\x00\x00\x00\x00\x00\x00\x24\x40", 16);
  std::string pt_0_10(
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x24\x40", 16);
  std::string exterior("\x05\x00\x00\x00", 4);
  exterior.append(pt_0_0);
  exterior.append(pt_10_0);
  exterior.append(pt_10_10);
  exterior.append(pt_0_10);
  exterior.append(pt_0_0);
  //                  |x                             ||y |
  std::string pt_2_2(
      "\x00\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00\x00\x00\x00\x00\x40", 16);
  std::string pt_4_2(
      "\x00\x00\x00\x00\x00\x00\x10\x40\x00\x00\x00\x00\x00\x00\x00\x40", 16);
  std::string pt_4_4(
      "\x00\x00\x00\x00\x00\x00\x10\x40\x00\x00\x00\x00\x00\x00\x10\x40", 16);
  std::string pt_2_4(
      "\x00\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00\x00\x00\x00\x10\x40", 16);
  //                     |numPoints     |
  std::string interior1("\x05\x00\x00\x00", 4);
  interior1.append(pt_2_2);
  interior1.append(pt_2_4);
  interior1.append(pt_4_4);
  interior1.append(pt_4_2);
  interior1.append(pt_2_2);
  //                  |x                             ||y |
  std::string pt_6_6(
      "\x00\x00\x00\x00\x00\x00\x18\x40\x00\x00\x00\x00\x00\x00\x18\x40", 16);
  std::string pt_6_8(
      "\x00\x00\x00\x00\x00\x00\x18\x40\x00\x00\x00\x00\x00\x00\x20\x40", 16);
  std::string pt_8_8(
      "\x00\x00\x00\x00\x00\x00\x20\x40\x00\x00\x00\x00\x00\x00\x20\x40", 16);
  //                     |numPoints     |
  std::string interior2("\x04\x00\x00\x00", 4);
  interior2.append(pt_6_6);
  interior2.append(pt_6_8);
  interior2.append(pt_8_8);
  interior2.append(pt_6_6);
  // Little-endian POLYGON((0 0, 10 0, 10 10, 0 10, 0 0), (2 2, 2 4, 4 4, 4 2),
  // (6 6, 6 8, 8 8, 6 6))
  //              |bo||WKB type      ||numRings      |
  std::string py("\x01\x03\x00\x00\x00\x03\x00\x00\x00", 9);
  py.append(exterior);
  py.append(interior1);
  py.append(interior2);

  g = gis::parse_wkb(nullptr, m_cartesian_srs, py.c_str(), py.length());
  EXPECT_EQ(g->type(), gis::Geometry_type::kPolygon);
  EXPECT_EQ(static_cast<gis::Polygon *>(g.get())->size(), 3UL);
  EXPECT_FALSE(static_cast<gis::Polygon *>(g.get())->empty());
  EXPECT_EQ(static_cast<gis::Polygon *>(g.get())->interior_ring(1).size(), 4UL);
}

TEST_F(WkbParserTest, Multipoint) {
  std::unique_ptr<gis::Geometry> g;

  // Little-endian POINT(1 2)
  //              |bo||WKB type      ||x                             ||y |
  std::string le(
      "\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0\x3f\x00\x00\x00\x00\x00"
      "\x00\x00\x40",
      21);
  // Big-endian POINT(1 2)
  //              |bo||WKB type      ||x                             ||y |
  std::string be(
      "\x00\x00\x00\x00\x01\x3f\xf0\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00\x00"
      "\x00\x00\x00",
      21);
  // Mixed-endian MULTIPOINT((1 2), (1 2))
  //               |bo||WKB type      ||numPoints     |
  std::string mpt("\x01\x04\x00\x00\x00\x02\x00\x00\x00", 9);
  mpt.append(le);
  mpt.append(be);

  g = gis::parse_wkb(nullptr, m_cartesian_srs, mpt.c_str(), mpt.length());
  EXPECT_EQ(g->type(), gis::Geometry_type::kMultipoint);
  EXPECT_FALSE(static_cast<gis::Multipoint *>(g.get())->empty());
  EXPECT_EQ(static_cast<gis::Multipoint *>(g.get())->size(), 2UL);

  // Multipoint with no points
  //                     |bo||WKB type      ||numPoints     |
  std::string empty_mpt("\x01\x04\x00\x00\x00\x00\x00\x00\x00", 9);
  g = gis::parse_wkb(nullptr, m_cartesian_srs, empty_mpt.c_str(),
                     empty_mpt.length());
  EXPECT_FALSE(g);
}

TEST_F(WkbParserTest, Multilinestring) {
  std::unique_ptr<gis::Geometry> g;

  //                    |x                             ||y |
  std::string pt_0_0(
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16);
  std::string pt_10_0(
      "\x00\x00\x00\x00\x00\x00\x24\x40\x00\x00\x00\x00\x00\x00\x00\x00", 16);
  std::string pt_10_10(
      "\x00\x00\x00\x00\x00\x00\x24\x40\x00\x00\x00\x00\x00\x00\x24\x40", 16);
  std::string pt_0_10(
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x24\x40", 16);
  //              |bo||WKB type      ||numPoints     |
  std::string ls("\x01\x02\x00\x00\x00\x04\x00\x00\x00", 9);
  ls.append(pt_0_0);
  ls.append(pt_10_0);
  ls.append(pt_10_10);
  ls.append(pt_10_0);
  // Mixed-endian MULTILINESTRING((0 0, 10 0, 10 10, 0 10))
  //               |bo||WKB type      ||numLinestrings|
  std::string mls("\x00\x00\x00\x00\x05\x00\x00\x00\x01", 9);
  mls.append(ls);

  g = gis::parse_wkb(nullptr, m_cartesian_srs, mls.c_str(), mls.length());
  EXPECT_EQ(g->type(), gis::Geometry_type::kMultilinestring);
  EXPECT_FALSE(static_cast<gis::Multilinestring *>(g.get())->empty());
  EXPECT_EQ(static_cast<gis::Multilinestring *>(g.get())->size(), 1UL);

  // Multilinestrings with no linestrings
  //                     |bo||WKB type      ||numLinestrings|
  std::string empty_mls("\x01\x05\x00\x00\x00\x00\x00\x00\x00", 9);
  g = gis::parse_wkb(nullptr, m_cartesian_srs, empty_mls.c_str(),
                     empty_mls.length());
  EXPECT_FALSE(g);
}

TEST_F(WkbParserTest, Multipolygon) {
  std::unique_ptr<gis::Geometry> g;

  //                    |x                             ||y |
  std::string pt_0_0(
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16);
  std::string pt_10_0(
      "\x00\x00\x00\x00\x00\x00\x24\x40\x00\x00\x00\x00\x00\x00\x00\x00", 16);
  std::string pt_10_10(
      "\x00\x00\x00\x00\x00\x00\x24\x40\x00\x00\x00\x00\x00\x00\x24\x40", 16);
  std::string pt_0_10(
      "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x24\x40", 16);
  std::string exterior("\x05\x00\x00\x00", 4);
  exterior.append(pt_0_0);
  exterior.append(pt_10_0);
  exterior.append(pt_10_10);
  exterior.append(pt_0_10);
  exterior.append(pt_0_0);
  //                  |x                             ||y |
  std::string pt_2_2(
      "\x00\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00\x00\x00\x00\x00\x40", 16);
  std::string pt_4_2(
      "\x00\x00\x00\x00\x00\x00\x10\x40\x00\x00\x00\x00\x00\x00\x00\x40", 16);
  std::string pt_4_4(
      "\x00\x00\x00\x00\x00\x00\x10\x40\x00\x00\x00\x00\x00\x00\x10\x40", 16);
  std::string pt_2_4(
      "\x00\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00\x00\x00\x00\x10\x40", 16);
  //                     |numPoints     |
  std::string interior1("\x05\x00\x00\x00", 4);
  interior1.append(pt_2_2);
  interior1.append(pt_2_4);
  interior1.append(pt_4_4);
  interior1.append(pt_4_2);
  interior1.append(pt_2_2);
  //                  |x                             ||y |
  std::string pt_6_6(
      "\x00\x00\x00\x00\x00\x00\x18\x40\x00\x00\x00\x00\x00\x00\x18\x40", 16);
  std::string pt_6_8(
      "\x00\x00\x00\x00\x00\x00\x18\x40\x00\x00\x00\x00\x00\x00\x20\x40", 16);
  std::string pt_8_8(
      "\x00\x00\x00\x00\x00\x00\x20\x40\x00\x00\x00\x00\x00\x00\x20\x40", 16);
  //                     |numPoints     |
  std::string interior2("\x04\x00\x00\x00", 4);
  interior2.append(pt_6_6);
  interior2.append(pt_6_8);
  interior2.append(pt_8_8);
  interior2.append(pt_6_6);
  //              |bo||WKB type      ||numRings      |
  std::string py("\x01\x03\x00\x00\x00\x03\x00\x00\x00", 9);
  py.append(exterior);
  py.append(interior1);
  py.append(interior2);
  // Mixed-endian MULTIPOLYGON(((0 0, 10 0, 10 10, 0 10, 0 0),
  // (2 2, 2 4, 4 4, 4 2), (6 6, 6 8, 8 8, 6 6)))
  //               |bo||WKB type      ||numPolygons   |
  std::string mpy("\x00\x00\x00\x00\x06\x00\x00\x00\x01", 9);
  mpy.append(py);

  g = gis::parse_wkb(nullptr, m_cartesian_srs, mpy.c_str(), mpy.length());
  EXPECT_EQ(g->type(), gis::Geometry_type::kMultipolygon);
  EXPECT_FALSE(static_cast<gis::Multipolygon *>(g.get())->empty());
  EXPECT_EQ(static_cast<gis::Multipolygon *>(g.get())->size(), 1UL);

  // Multipolygon with no polygons
  //                     |bo||WKB type      ||numPolygons   |
  std::string empty_mpy("\x01\x06\x00\x00\x00\x00\x00\x00\x00", 9);
  g = gis::parse_wkb(nullptr, m_cartesian_srs, empty_mpy.c_str(),
                     empty_mpy.length());
  EXPECT_FALSE(g);
}

TEST_F(WkbParserTest, Geometrycollection) {
  std::unique_ptr<gis::Geometry> g;

  // Little-endian POINT(1 2)
  //              |bo||WKB type      ||x                             ||y |
  std::string le(
      "\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0\x3f\x00\x00\x00\x00\x00"
      "\x00\x00\x40",
      21);
  // Big-endian POINT(1 2)
  //              |bo||WKB type      ||x                             ||y |
  std::string be(
      "\x00\x00\x00\x00\x01\x3f\xf0\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00\x00"
      "\x00\x00\x00",
      21);
  // Mixed-endian MULTIPOINT((1 2), (1 2))
  //              |bo||WKB type      ||numPoints     |
  std::string gc("\x01\x07\x00\x00\x00\x02\x00\x00\x00", 9);
  gc.append(le);
  gc.append(be);

  g = gis::parse_wkb(nullptr, m_cartesian_srs, gc.c_str(), gc.length());
  EXPECT_EQ(g->type(), gis::Geometry_type::kGeometrycollection);
  EXPECT_FALSE(static_cast<gis::Geometrycollection *>(g.get())->empty());
  EXPECT_EQ(static_cast<gis::Geometrycollection *>(g.get())->size(), 2UL);

  // Empty geometrycollection
  //                    |bo||WKB type      ||numGeometries |
  std::string empty_gc("\x01\x07\x00\x00\x00\x00\x00\x00\x00", 9);
  g = gis::parse_wkb(nullptr, m_cartesian_srs, empty_gc.c_str(),
                     empty_gc.length());
  EXPECT_TRUE(g);

  // Geometry collection with empty geometrycollection
  //                      |bo||WKB type      ||numGeometries ||bo||WKB type
  //                      ||numGeometries |
  std::string gc_empty_gc(
      "\x01\x07\x00\x00\x00\x01\x00\x00\x00\x01\x07\x00\x00\x00\x00\x00\x00"
      "\x00",
      18);
  g = gis::parse_wkb(nullptr, m_cartesian_srs, gc_empty_gc.c_str(),
                     gc_empty_gc.length());
  EXPECT_TRUE(g);
  EXPECT_FALSE(static_cast<gis::Geometrycollection *>(g.get())->empty());
  EXPECT_TRUE(static_cast<gis::Geometrycollection *>(g.get())->is_empty());
}

}  // namespace wkb_parser__unittest