OmniSciDB  a987f07e93
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Types.h
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <memory>
20 #include <stdexcept>
21 #include <string>
22 #include <vector>
23 
24 #include "Shared/sqltypes.h"
25 
26 class OGRGeometry;
27 class OGRCoordinateTransformation;
28 
29 namespace Geospatial {
30 
31 struct WkbView {
32  uint8_t const* ptr_;
33  size_t size_;
34 };
35 
36 class GeoTypesError : public std::runtime_error {
37  public:
38  explicit GeoTypesError(const std::string& type, const int ogr_err)
39  : std::runtime_error("Geo" + type +
40  " Error: " + GeoTypesError::OGRErrorToStr(ogr_err)) {}
41  explicit GeoTypesError(const std::string& type, const std::string& err)
42  : std::runtime_error("Geo" + type + " Error: " + err) {}
43 
44  private:
45  static std::string OGRErrorToStr(const int ogr_err);
46 };
47 
48 class GeoTypesFactory;
49 
50 class GeoBase {
51  public:
53  virtual ~GeoBase();
54 
55  std::string getWktString() const;
56  bool getWkb(std::vector<uint8_t>&) const;
57  enum class GeoType {
58  kPOINT,
60  kPOLYGON,
62  kGEOMETRY,
66  };
67  enum class GeoOp {
68  kPROJECTION = 0,
69  kINTERSECTION = 1,
70  kDIFFERENCE = 2,
71  kUNION = 3,
72  kBUFFER = 4,
73  kISVALID = 5,
74  kISEMPTY = 6,
75  kEQUALS = 7,
76  kCONCAVEHULL = 8,
77  kCONVEXHULL = 9
78  };
79  virtual GeoType getType() const = 0;
80  const OGRGeometry* getOGRGeometry() const { return geom_; }
81 
82  int32_t getBestPlanarSRID() const;
83  bool transform(int32_t srid0, int32_t srid1);
84  bool transform(SQLTypeInfo& ti);
85  static std::shared_ptr<OGRCoordinateTransformation> getTransformation(int32_t srid0,
86  int32_t srid1);
87 
88  virtual bool operator==(const GeoBase& other) const;
89 
90  bool isEmpty() const;
91 
92  std::unique_ptr<GeoBase> run(GeoOp op, const GeoBase& other) const;
93  std::unique_ptr<GeoBase> optimized_run(GeoOp op, const GeoBase& other) const;
94  std::unique_ptr<GeoBase> run(GeoOp op, double param) const;
95  bool run(GeoOp op) const;
96 
97  virtual std::unique_ptr<GeoBase> clone() const = 0;
98 
99  protected:
100  GeoBase(OGRGeometry* geom, const bool owns_geom_obj)
101  : geom_(geom), owns_geom_obj_(owns_geom_obj) {}
102  OGRGeometry* geom_ = nullptr;
104 
105  static int createFromWktString(const std::string& wkt, OGRGeometry** geom);
106  static int createFromWkbView(OGRGeometry** geom, WkbView const);
107 
108  friend class GeoTypesFactory;
109 };
110 
111 class GeoPoint : public GeoBase {
112  public:
113  GeoPoint(const std::vector<double>& coords);
114  GeoPoint(const std::string& wkt);
115 
116  void getColumns(std::vector<double>& coords) const;
117  GeoType getType() const final { return GeoType::kPOINT; }
118 
119  std::unique_ptr<GeoBase> clone() const override;
120 
121  protected:
122  GeoPoint(OGRGeometry* geom, const bool owns_geom_obj) : GeoBase(geom, owns_geom_obj) {}
123 
124  friend class GeoTypesFactory;
125 };
126 
127 class GeoMultiPoint : public GeoBase {
128  public:
129  GeoMultiPoint(const std::vector<double>& coords);
130  GeoMultiPoint(const std::string& wkt);
131 
132  void getColumns(std::vector<double>& coords, std::vector<double>& bounds) const;
133  GeoType getType() const final { return GeoType::kMULTIPOINT; }
134 
135  std::unique_ptr<GeoBase> clone() const final;
136 
137  protected:
138  GeoMultiPoint(OGRGeometry* geom, const bool owns_geom_obj)
139  : GeoBase(geom, owns_geom_obj) {}
140 
141  friend class GeoTypesFactory;
142 };
143 
144 class GeoLineString : public GeoBase {
145  public:
146  GeoLineString(const std::vector<double>& coords);
147  GeoLineString(const std::string& wkt);
148 
149  void getColumns(std::vector<double>& coords, std::vector<double>& bounds) const;
150  GeoType getType() const final { return GeoType::kLINESTRING; }
151 
152  std::unique_ptr<GeoBase> clone() const final;
153 
154  protected:
155  GeoLineString(OGRGeometry* geom, const bool owns_geom_obj)
156  : GeoBase(geom, owns_geom_obj) {}
157 
158  friend class GeoTypesFactory;
159 };
160 
161 class GeoMultiLineString : public GeoBase {
162  public:
163  GeoMultiLineString(const std::vector<double>& coords,
164  const std::vector<int32_t>& linestring_sizes);
165  GeoMultiLineString(const std::string& wkt);
166 
167  void getColumns(std::vector<double>& coords,
168  std::vector<int32_t>& linestring_sizes,
169  std::vector<double>& bounds) const;
170  GeoType getType() const final { return GeoType::kMULTILINESTRING; }
171 
172  std::unique_ptr<GeoBase> clone() const final;
173 
174  protected:
175  GeoMultiLineString(OGRGeometry* geom, const bool owns_geom_obj)
176  : GeoBase(geom, owns_geom_obj) {}
177 
178  friend class GeoTypesFactory;
179 };
180 
181 class GeoPolygon : public GeoBase {
182  public:
183  GeoPolygon(const std::vector<double>& coords, const std::vector<int32_t>& ring_sizes);
184  GeoPolygon(const std::string& wkt);
185 
186  void getColumns(std::vector<double>& coords,
187  std::vector<int32_t>& ring_sizes,
188  std::vector<double>& bounds) const;
189  GeoType getType() const final { return GeoType::kPOLYGON; }
190 
191  int32_t getNumInteriorRings() const;
192 
193  std::unique_ptr<GeoBase> clone() const final;
194 
195  protected:
196  GeoPolygon(OGRGeometry* geom, const bool owns_geom_obj)
197  : GeoBase(geom, owns_geom_obj) {}
198 
199  friend class GeoTypesFactory;
200 };
201 
202 class GeoMultiPolygon : public GeoBase {
203  public:
204  GeoMultiPolygon(const std::vector<double>& coords,
205  const std::vector<int32_t>& ring_sizes,
206  const std::vector<int32_t>& poly_rings);
207  GeoMultiPolygon(const std::string& wkt);
208 
209  void getColumns(std::vector<double>& coords,
210  std::vector<int32_t>& ring_sizes,
211  std::vector<int32_t>& poly_rings,
212  std::vector<double>& bounds) const;
213  GeoType getType() const final { return GeoType::kMULTIPOLYGON; }
214 
215  std::unique_ptr<GeoBase> clone() const final;
216 
217  protected:
218  GeoMultiPolygon(OGRGeometry* geom, const bool owns_geom_obj)
219  : GeoBase(geom, owns_geom_obj) {}
220 
221  friend class GeoTypesFactory;
222 };
223 
224 // TODO: with addition of MULTILINESTING and generic GEOMETRY, GEOMETRYCOLLECTION types
225 // need to rename poly-specific ring_sizes and poly_rings arrays and columns to meta
226 // names. First meta layer above the coords: meta1 array will contain component sizes
227 // - for MULTILINESTRING it will hold linestring sizes
228 // - for POLYGON and MULTIPOLYGON it will hold ring sizes
229 // Second meta layer above the coords: meta2 array will contain larger component sizes
230 // - for MULTIPOLYGON it will hold poly sizes in terms of rings (ring counts)
231 // Thrid meta layer above the coords: meta3 array will contain geometry kinds
232 // - for GEOMETRY it will hold the single geometry kind
233 // - for GEOMETRYCOLLECTION it will hold geometry kinds included in the collection
234 
235 class GeoGeometry : public GeoBase {
236  public:
237  GeoGeometry(const std::vector<double>& coords,
238  const std::vector<int32_t>& ring_sizes,
239  const std::vector<int32_t>& poly_rings,
240  const std::vector<int32_t>& geo_kinds){};
241  GeoGeometry(const std::string& wkt){};
242 
243  void getColumns(std::vector<double>& coords,
244  std::vector<int32_t>& ring_sizes,
245  std::vector<int32_t>& poly_rings,
246  std::vector<int32_t>& geo_kinds,
247  std::vector<double>& bounds) const {};
248  GeoType getType() const final { return GeoType::kGEOMETRY; }
249 
250  std::unique_ptr<GeoBase> clone() const final;
251 
252  protected:
253  GeoGeometry(OGRGeometry* geom, const bool owns_geom_obj)
254  : GeoBase(geom, owns_geom_obj) {
255  if (!isEmpty()) {
256  throw GeoTypesError("GeoTypesFactory", "Non-empty GEOMETRY");
257  }
258  }
259 
260  friend class GeoTypesFactory;
261 };
262 
264  public:
265  GeoGeometryCollection(const std::vector<double>& coords,
266  const std::vector<int32_t>& ring_sizes,
267  const std::vector<int32_t>& poly_rings,
268  const std::vector<int32_t>& geo_kinds){};
269  GeoGeometryCollection(const std::string& wkt);
270 
271  void getColumns(std::vector<double>& coords,
272  std::vector<int32_t>& ring_sizes,
273  std::vector<int32_t>& poly_rings,
274  std::vector<int32_t>& geo_kinds,
275  std::vector<double>& bounds) const {};
276  GeoType getType() const final { return GeoType::kGEOMETRYCOLLECTION; }
277 
278  std::unique_ptr<GeoBase> clone() const final;
279 
280  protected:
281  GeoGeometryCollection(OGRGeometry* geom, const bool owns_geom_obj)
282  : GeoBase(geom, owns_geom_obj) {
283  if (!isEmpty()) {
284  throw GeoTypesError("GeoTypesFactory", "Non-empty GEOMETRYCOLLECTION");
285  }
286  }
287 
288  friend class GeoTypesFactory;
289 };
290 
292  public:
293  static OGRGeometry* createOGRGeometry(const std::string& wkt_or_wkb_hex);
294 
295  static std::unique_ptr<GeoBase> createGeoType(const std::string& wkt_or_wkb_hex);
296  static std::unique_ptr<GeoBase> createGeoType(const WkbView);
297  static std::unique_ptr<GeoBase> createGeoType(OGRGeometry* geom);
298 
299  static bool getGeoColumns(const std::string& wkt_or_wkb_hex,
300  SQLTypeInfo& ti,
301  std::vector<double>& coords,
302  std::vector<double>& bounds,
303  std::vector<int>& ring_sizes,
304  std::vector<int>& poly_rings,
305  const bool promote_poly_to_mpoly = false);
306 
307  static bool getGeoColumns(const std::vector<uint8_t>& wkb,
308  SQLTypeInfo& ti,
309  std::vector<double>& coords,
310  std::vector<double>& bounds,
311  std::vector<int>& ring_sizes,
312  std::vector<int>& poly_rings,
313  const bool promote_poly_to_mpoly = false);
314 
315  static bool getGeoColumns(OGRGeometry* geom,
316  SQLTypeInfo& ti,
317  std::vector<double>& coords,
318  std::vector<double>& bounds,
319  std::vector<int>& ring_sizes,
320  std::vector<int>& poly_rings,
321  const bool promote_poly_to_mpoly = false);
322 
323  static bool getGeoColumns(const std::vector<std::string>* wkt_or_wkb_hex_column,
324  SQLTypeInfo& ti,
325  std::vector<std::vector<double>>& coords_column,
326  std::vector<std::vector<double>>& bounds_column,
327  std::vector<std::vector<int>>& ring_sizes_column,
328  std::vector<std::vector<int>>& poly_rings_column,
329  const bool promote_poly_to_mpoly = false);
330 
331  static void getNullGeoColumns(SQLTypeInfo& ti,
332  std::vector<double>& coords,
333  std::vector<double>& bounds,
334  std::vector<int>& ring_sizes,
335  std::vector<int>& poly_rings,
336  const bool promote_poly_to_mpoly = false);
337 
338  private:
339  static std::unique_ptr<Geospatial::GeoBase> createGeoTypeImpl(
340  OGRGeometry* geom,
341  const bool owns_geom_obj = true);
342  static void getGeoColumnsImpl(const std::unique_ptr<GeoBase>& geospatial_base,
343  SQLTypeInfo& ti,
344  std::vector<double>& coords,
345  std::vector<double>& bounds,
346  std::vector<int>& ring_sizes,
347  std::vector<int>& poly_rings,
348  const bool promote_poly_to_mpoly = false);
349 };
350 
351 } // namespace Geospatial
OGRGeometry * geom_
Definition: Types.h:102
GeoGeometry(const std::string &wkt)
Definition: Types.h:241
std::unique_ptr< GeoBase > optimized_run(GeoOp op, const GeoBase &other) const
Definition: Types.cpp:439
GeoTypesError(const std::string &type, const int ogr_err)
Definition: Types.h:38
static std::unique_ptr< GeoBase > createGeoType(const std::string &wkt_or_wkb_hex)
Definition: Types.cpp:1061
GeoPoint(const std::vector< double > &coords)
Definition: Types.cpp:541
void getColumns(std::vector< double > &coords, std::vector< double > &bounds) const
Definition: Types.cpp:613
void getColumns(std::vector< double > &coords) const
Definition: Types.cpp:567
int32_t getNumInteriorRings() const
Definition: Types.cpp:864
std::unique_ptr< GeoBase > clone() const final
Definition: Types.cpp:585
uint8_t const * ptr_
Definition: Types.h:32
GeoMultiPolygon(const std::vector< double > &coords, const std::vector< int32_t > &ring_sizes, const std::vector< int32_t > &poly_rings)
Definition: Types.cpp:875
const OGRGeometry * getOGRGeometry() const
Definition: Types.h:80
bool owns_geom_obj_
Definition: Types.h:103
GeoLineString(const std::vector< double > &coords)
Definition: Types.cpp:650
GeoMultiLineString(const std::vector< double > &coords, const std::vector< int32_t > &linestring_sizes)
Definition: Types.cpp:709
GeoGeometryCollection(const std::vector< double > &coords, const std::vector< int32_t > &ring_sizes, const std::vector< int32_t > &poly_rings, const std::vector< int32_t > &geo_kinds)
Definition: Types.h:265
static void getGeoColumnsImpl(const std::unique_ptr< GeoBase > &geospatial_base, SQLTypeInfo &ti, std::vector< double > &coords, std::vector< double > &bounds, std::vector< int > &ring_sizes, std::vector< int > &poly_rings, const bool promote_poly_to_mpoly=false)
Definition: Types.cpp:1243
Constants for Builtin SQL Types supported by HEAVY.AI.
static void getNullGeoColumns(SQLTypeInfo &ti, std::vector< double > &coords, std::vector< double > &bounds, std::vector< int > &ring_sizes, std::vector< int > &poly_rings, const bool promote_poly_to_mpoly=false)
Definition: Types.cpp:1309
static std::unique_ptr< Geospatial::GeoBase > createGeoTypeImpl(OGRGeometry *geom, const bool owns_geom_obj=true)
Definition: Types.cpp:1217
bool getWkb(std::vector< uint8_t > &) const
Definition: Types.cpp:186
static std::shared_ptr< OGRCoordinateTransformation > getTransformation(int32_t srid0, int32_t srid1)
Definition: Types.cpp:307
std::unique_ptr< GeoBase > clone() const final
Definition: Types.cpp:870
GeoTypesError(const std::string &type, const std::string &err)
Definition: Types.h:41
bool isEmpty() const
Definition: Types.cpp:196
virtual ~GeoBase()
Definition: Types.cpp:145
GeoType getType() const final
Definition: Types.h:248
static int createFromWkbView(OGRGeometry **geom, WkbView const)
Definition: Types.cpp:173
virtual std::unique_ptr< GeoBase > clone() const =0
GeoType getType() const final
Definition: Types.h:170
GeoType getType() const final
Definition: Types.h:150
void getColumns(std::vector< double > &coords, std::vector< int32_t > &ring_sizes, std::vector< int32_t > &poly_rings, std::vector< int32_t > &geo_kinds, std::vector< double > &bounds) const
Definition: Types.h:243
static std::string OGRErrorToStr(const int ogr_err)
Definition: Types.cpp:118
GeoGeometry(const std::vector< double > &coords, const std::vector< int32_t > &ring_sizes, const std::vector< int32_t > &poly_rings, const std::vector< int32_t > &geo_kinds)
Definition: Types.h:237
bool g_enable_smem_group_by true
std::unique_ptr< GeoBase > clone() const override
Definition: Types.cpp:536
void getColumns(std::vector< double > &coords, std::vector< int32_t > &linestring_sizes, std::vector< double > &bounds) const
Definition: Types.cpp:741
void getColumns(std::vector< double > &coords, std::vector< int32_t > &ring_sizes, std::vector< int32_t > &poly_rings, std::vector< int32_t > &geo_kinds, std::vector< double > &bounds) const
Definition: Types.h:271
void getColumns(std::vector< double > &coords, std::vector< int32_t > &ring_sizes, std::vector< int32_t > &poly_rings, std::vector< double > &bounds) const
Definition: Types.cpp:914
GeoPoint(OGRGeometry *geom, const bool owns_geom_obj)
Definition: Types.h:122
static bool getGeoColumns(const std::string &wkt_or_wkb_hex, SQLTypeInfo &ti, std::vector< double > &coords, std::vector< double > &bounds, std::vector< int > &ring_sizes, std::vector< int > &poly_rings, const bool promote_poly_to_mpoly=false)
Definition: Types.cpp:1079
virtual bool operator==(const GeoBase &other) const
Definition: Types.cpp:200
static OGRGeometry * createOGRGeometry(const std::string &wkt_or_wkb_hex)
Definition: Types.cpp:1044
std::unique_ptr< GeoBase > clone() const final
Definition: Types.cpp:785
static int createFromWktString(const std::string &wkt, OGRGeometry **geom)
Definition: Types.cpp:154
void getColumns(std::vector< double > &coords, std::vector< double > &bounds) const
Definition: Types.cpp:672
GeoType getType() const final
Definition: Types.h:189
std::unique_ptr< GeoBase > clone() const final
Definition: Types.cpp:968
GeoMultiPoint(const std::vector< double > &coords)
Definition: Types.cpp:590
bool transform(int32_t srid0, int32_t srid1)
Definition: Types.cpp:383
GeoType getType() const final
Definition: Types.h:213
std::string getWktString() const
Definition: Types.cpp:164
GeoBase(OGRGeometry *geom, const bool owns_geom_obj)
Definition: Types.h:100
std::unique_ptr< GeoBase > clone() const final
Definition: Types.cpp:645
std::unique_ptr< GeoBase > run(GeoOp op, const GeoBase &other) const
Definition: Types.cpp:406
std::unique_ptr< GeoBase > clone() const final
Definition: Types.cpp:973
size_t size_
Definition: Types.h:33
std::unique_ptr< GeoBase > clone() const final
Definition: Types.cpp:704
void getColumns(std::vector< double > &coords, std::vector< int32_t > &ring_sizes, std::vector< double > &bounds) const
Definition: Types.cpp:822
int32_t getBestPlanarSRID() const
Definition: Types.cpp:226
GeoPolygon(const std::vector< double > &coords, const std::vector< int32_t > &ring_sizes)
Definition: Types.cpp:790
GeoType getType() const final
Definition: Types.h:133
GeoType getType() const final
Definition: Types.h:117
virtual GeoType getType() const =0
GeoType getType() const final
Definition: Types.h:276