Skip to content

Commit 5dc668b

Browse files
authored
WKBReader: Support curved geometry types (#1106)
1 parent 2aa9820 commit 5dc668b

File tree

6 files changed

+351
-116
lines changed

6 files changed

+351
-116
lines changed

include/geos/geom/Geometry.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ namespace geos { // geos
7070
namespace geom { // geos::geom
7171

7272
/// Geometry types
73-
enum GeometryTypeId {
73+
enum GeometryTypeId : int {
7474
/// a point
7575
GEOS_POINT,
7676
/// a linestring

include/geos/geom/GeometryTypeName.h

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**********************************************************************
2+
*
3+
* GEOS - Geometry Engine Open Source
4+
* http://geos.osgeo.org
5+
*
6+
* Copyright (C) 2024 ISciences, LLC
7+
*
8+
* This is free software; you can redistribute and/or modify it under
9+
* the terms of the GNU Lesser General Public Licence as published
10+
* by the Free Software Foundation.
11+
* See the COPYING file for more information.
12+
*
13+
**********************************************************************/
14+
15+
#pragma once
16+
17+
namespace geos {
18+
namespace geom {
19+
20+
class Curve;
21+
class CurvePolygon;
22+
class GeometryCollection;
23+
class LineString;
24+
class LinearRing;
25+
class MultiCurve;
26+
class MultiLineString;
27+
class MultiPoint;
28+
class MultiPolygon;
29+
class MultiSurface;
30+
class Point;
31+
class Polygon;
32+
class SimpleCurve;
33+
class Surface;
34+
35+
// These structures allow templates to have compile-time access to a type's human-readable name.
36+
template<typename T>
37+
struct GeometryTypeName {};
38+
39+
template<>
40+
struct GeometryTypeName<geom::Curve> {
41+
static constexpr const char* name = "Curve";
42+
};
43+
44+
template<>
45+
struct GeometryTypeName<geom::CurvePolygon> {
46+
static constexpr const char* name = "CurvePolygon";
47+
};
48+
49+
template<>
50+
struct GeometryTypeName<geom::GeometryCollection> {
51+
static constexpr const char* name = "GeometryCollection";
52+
};
53+
54+
template<>
55+
struct GeometryTypeName<geom::LineString> {
56+
static constexpr const char* name = "LineString";
57+
};
58+
59+
template<>
60+
struct GeometryTypeName<geom::LinearRing> {
61+
static constexpr const char* name = "LinearRing";
62+
};
63+
64+
template<>
65+
struct GeometryTypeName<geom::MultiCurve> {
66+
static constexpr const char* name = "MultiCurve";
67+
};
68+
69+
template<>
70+
struct GeometryTypeName<geom::MultiLineString> {
71+
static constexpr const char* name = "MultiLineString";
72+
};
73+
74+
template<>
75+
struct GeometryTypeName<geom::MultiPoint> {
76+
static constexpr const char* name = "MultiPoint";
77+
};
78+
79+
template<>
80+
struct GeometryTypeName<geom::MultiPolygon> {
81+
static constexpr const char* name = "MultiPolygon";
82+
};
83+
84+
template<>
85+
struct GeometryTypeName<geom::MultiSurface> {
86+
static constexpr const char* name = "MultiSurface";
87+
};
88+
89+
template<>
90+
struct GeometryTypeName<geom::Point> {
91+
static constexpr const char* name = "Point";
92+
};
93+
94+
template<>
95+
struct GeometryTypeName<geom::Polygon> {
96+
static constexpr const char* name = "Polygon";
97+
};
98+
99+
template<>
100+
struct GeometryTypeName<geom::SimpleCurve> {
101+
static constexpr const char* name = "SimpleCurve";
102+
};
103+
104+
template<>
105+
struct GeometryTypeName<geom::Surface> {
106+
static constexpr const char* name = "Surface";
107+
};
108+
109+
}
110+
}

include/geos/io/WKBReader.h

+30-4
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,15 @@
2020
#pragma once
2121

2222
#include <geos/export.h>
23-
23+
#include <geos/geom/Geometry.h>
24+
#include <geos/geom/GeometryTypeName.h>
2425
#include <geos/io/ByteOrderDataInStream.h> // for composition
2526

2627
#include <iosfwd> // ostream, istream
2728
#include <memory>
2829
// #include <vector>
2930
#include <array>
3031

31-
#define BAD_GEOM_TYPE_MSG "Bad geometry type encountered in"
32-
3332
#ifdef _MSC_VER
3433
#pragma warning(push)
3534
#pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
@@ -41,17 +40,24 @@ namespace geom {
4140

4241
class GeometryFactory;
4342
class Coordinate;
43+
class CircularString;
44+
class CompoundCurve;
45+
class CurvePolygon;
4446
class Geometry;
47+
enum GeometryTypeId : int;
4548
class GeometryCollection;
4649
class Point;
4750
class LineString;
4851
class LinearRing;
4952
class Polygon;
53+
class MultiCurve;
5054
class MultiPoint;
5155
class MultiLineString;
5256
class MultiPolygon;
57+
class MultiSurface;
5358
class PrecisionModel;
5459
class CoordinateSequence;
60+
class SimpleCurve;
5561

5662
} // namespace geom
5763
} // namespace geos
@@ -148,22 +154,42 @@ class GEOS_DLL WKBReader {
148154

149155
std::unique_ptr<geom::LinearRing> readLinearRing();
150156

157+
std::unique_ptr<geom::CircularString> readCircularString();
158+
159+
std::unique_ptr<geom::CompoundCurve> readCompoundCurve();
160+
151161
std::unique_ptr<geom::Polygon> readPolygon();
152162

163+
std::unique_ptr<geom::CurvePolygon> readCurvePolygon();
164+
153165
std::unique_ptr<geom::MultiPoint> readMultiPoint();
154166

155167
std::unique_ptr<geom::MultiLineString> readMultiLineString();
156168

169+
std::unique_ptr<geom::MultiCurve> readMultiCurve();
170+
157171
std::unique_ptr<geom::MultiPolygon> readMultiPolygon();
158172

173+
std::unique_ptr<geom::MultiSurface> readMultiSurface();
174+
159175
std::unique_ptr<geom::GeometryCollection> readGeometryCollection();
160176

161177
std::unique_ptr<geom::CoordinateSequence> readCoordinateSequence(unsigned int); // throws IOException
162178

163-
void minMemSize(int geomType, uint64_t size);
179+
void minMemSize(geom::GeometryTypeId geomType, uint64_t size) const;
164180

165181
void readCoordinate(); // throws IOException
166182

183+
template<typename T>
184+
std::unique_ptr<T> readChild()
185+
{
186+
auto g = readGeometry();
187+
if (dynamic_cast<const T*>(g.get())) {
188+
return std::unique_ptr<T>(static_cast<T*>(g.release()));
189+
}
190+
throw io::ParseException(std::string("Expected ") + geom::GeometryTypeName<T>::name + " but got " + g->getGeometryType());
191+
}
192+
167193
// Declare type as noncopyable
168194
WKBReader(const WKBReader& other) = delete;
169195
WKBReader& operator=(const WKBReader& rhs) = delete;

0 commit comments

Comments
 (0)