OmniSciDB  a5dc49c757
 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  const bool validate_with_geos_if_available);
295 
296  static std::unique_ptr<GeoBase> createGeoType(
297  const std::string& wkt_or_wkb_hex,
298  const bool validate_with_geos_if_available);
299  static std::unique_ptr<GeoBase> createGeoType(
300  const WkbView,
301  const bool validate_with_geos_if_available);
302  static std::unique_ptr<GeoBase> createGeoType(
303  OGRGeometry* geom,
304  const bool validate_with_geos_if_available);
305 
306  static bool getGeoColumns(const std::string& wkt_or_wkb_hex,
307  SQLTypeInfo& ti,
308  std::vector<double>& coords,
309  std::vector<double>& bounds,
310  std::vector<int>& ring_sizes,
311  std::vector<int>& poly_rings,
312  const bool validate_with_geos_if_available);
313 
314  static bool getGeoColumns(const std::vector<uint8_t>& wkb,
315  SQLTypeInfo& ti,
316  std::vector<double>& coords,
317  std::vector<double>& bounds,
318  std::vector<int>& ring_sizes,
319  std::vector<int>& poly_rings,
320  const bool validate_with_geos_if_available);
321 
322  static bool getGeoColumns(OGRGeometry* geom,
323  SQLTypeInfo& ti,
324  std::vector<double>& coords,
325  std::vector<double>& bounds,
326  std::vector<int>& ring_sizes,
327  std::vector<int>& poly_rings,
328  const bool validate_with_geos_if_available);
329 
330  static bool getGeoColumns(const std::vector<std::string>* wkt_or_wkb_hex_column,
331  SQLTypeInfo& ti,
332  std::vector<std::vector<double>>& coords_column,
333  std::vector<std::vector<double>>& bounds_column,
334  std::vector<std::vector<int>>& ring_sizes_column,
335  std::vector<std::vector<int>>& poly_rings_column,
336  const bool validate_with_geos_if_available);
337 
338  static void getNullGeoColumns(SQLTypeInfo& ti,
339  std::vector<double>& coords,
340  std::vector<double>& bounds,
341  std::vector<int>& ring_sizes,
342  std::vector<int>& poly_rings);
343 
344  private:
345  static std::unique_ptr<Geospatial::GeoBase> createGeoTypeImpl(
346  OGRGeometry* geom,
347  const bool owns_geom_obj = true);
348  static void getGeoColumnsImpl(const std::unique_ptr<GeoBase>& geospatial_base,
349  SQLTypeInfo& ti,
350  std::vector<double>& coords,
351  std::vector<double>& bounds,
352  std::vector<int>& ring_sizes,
353  std::vector<int>& poly_rings);
354 };
355 
356 } // namespace Geospatial
OGRGeometry * geom_
Definition: Types.h:102
std::unique_ptr< GeoBase > clone() const final
Definition: Types.cpp:646
GeoGeometry(const std::string &wkt)
Definition: Types.h:241
std::unique_ptr< GeoBase > optimized_run(GeoOp op, const GeoBase &other) const
Definition: Types.cpp:440
GeoTypesError(const std::string &type, const int ogr_err)
Definition: Types.h:38
int32_t getNumInteriorRings() const
Definition: Types.cpp:865
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:876
const OGRGeometry * getOGRGeometry() const
Definition: Types.h:80
bool owns_geom_obj_
Definition: Types.h:103
GeoMultiPoint(const std::vector< double > &coords)
Definition: Types.cpp:591
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
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)
Definition: Types.cpp:1342
static std::unique_ptr< Geospatial::GeoBase > createGeoTypeImpl(OGRGeometry *geom, const bool owns_geom_obj=true)
Definition: Types.cpp:1241
bool getWkb(std::vector< uint8_t > &) const
Definition: Types.cpp:187
void getColumns(std::vector< double > &coords) const
Definition: Types.cpp:568
static std::shared_ptr< OGRCoordinateTransformation > getTransformation(int32_t srid0, int32_t srid1)
Definition: Types.cpp:308
std::unique_ptr< GeoBase > clone() const final
Definition: Types.cpp:871
GeoMultiLineString(const std::vector< double > &coords, const std::vector< int32_t > &linestring_sizes)
Definition: Types.cpp:710
GeoTypesError(const std::string &type, const std::string &err)
Definition: Types.h:41
bool isEmpty() const
Definition: Types.cpp:197
virtual ~GeoBase()
Definition: Types.cpp:146
std::unique_ptr< GeoBase > clone() const final
Definition: Types.cpp:705
GeoType getType() const final
Definition: Types.h:248
static int createFromWkbView(OGRGeometry **geom, WkbView const)
Definition: Types.cpp:174
virtual std::unique_ptr< GeoBase > clone() const =0
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)
Definition: Types.cpp:1267
void getColumns(std::vector< double > &coords, std::vector< double > &bounds) const
Definition: Types.cpp:614
GeoType getType() const final
Definition: Types.h:170
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 validate_with_geos_if_available)
Definition: Types.cpp:1121
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:119
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
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:915
std::unique_ptr< GeoBase > clone() const override
Definition: Types.cpp:537
GeoPoint(OGRGeometry *geom, const bool owns_geom_obj)
Definition: Types.h:122
void getColumns(std::vector< double > &coords, std::vector< double > &bounds) const
Definition: Types.cpp:673
virtual bool operator==(const GeoBase &other) const
Definition: Types.cpp:201
std::unique_ptr< GeoBase > clone() const final
Definition: Types.cpp:786
static std::unique_ptr< GeoBase > createGeoType(const std::string &wkt_or_wkb_hex, const bool validate_with_geos_if_available)
Definition: Types.cpp:1085
std::unique_ptr< GeoBase > clone() const final
Definition: Types.cpp:586
static int createFromWktString(const std::string &wkt, OGRGeometry **geom)
Definition: Types.cpp:155
void getColumns(std::vector< double > &coords, std::vector< int32_t > &linestring_sizes, std::vector< double > &bounds) const
Definition: Types.cpp:742
static OGRGeometry * createOGRGeometry(const std::string &wkt_or_wkb_hex, const bool validate_with_geos_if_available)
Definition: Types.cpp:1063
GeoType getType() const final
Definition: Types.h:189
std::unique_ptr< GeoBase > clone() const final
Definition: Types.cpp:969
bool transform(int32_t srid0, int32_t srid1)
Definition: Types.cpp:384
GeoType getType() const final
Definition: Types.h:213
std::string getWktString() const
Definition: Types.cpp:165
GeoPoint(const std::vector< double > &coords)
Definition: Types.cpp:542
GeoBase(OGRGeometry *geom, const bool owns_geom_obj)
Definition: Types.h:100
std::unique_ptr< GeoBase > run(GeoOp op, const GeoBase &other) const
Definition: Types.cpp:407
std::unique_ptr< GeoBase > clone() const final
Definition: Types.cpp:974
size_t size_
Definition: Types.h:33
void getColumns(std::vector< double > &coords, std::vector< int32_t > &ring_sizes, std::vector< double > &bounds) const
Definition: Types.cpp:823
int32_t getBestPlanarSRID() const
Definition: Types.cpp:227
GeoPolygon(const std::vector< double > &coords, const std::vector< int32_t > &ring_sizes)
Definition: Types.cpp:791
GeoType getType() const final
Definition: Types.h:133
GeoType getType() const final
Definition: Types.h:117
virtual GeoType getType() const =0
GeoLineString(const std::vector< double > &coords)
Definition: Types.cpp:651
GeoType getType() const final
Definition: Types.h:276