OmniSciDB  2e3a973ef4
Types.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, 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 
28 namespace Geospatial {
29 
30 class GeoTypesError : public std::runtime_error {
31  public:
32  explicit GeoTypesError(const std::string& type, const int ogr_err)
33  : std::runtime_error("Geo" + type +
34  " Error: " + GeoTypesError::OGRErrorToStr(ogr_err)) {}
35  explicit GeoTypesError(const std::string& type, const std::string& err)
36  : std::runtime_error("Geo" + type + " Error: " + err) {}
37 
38  private:
39  static std::string OGRErrorToStr(const int ogr_err);
40 };
41 
42 class GeoTypesFactory;
43 
44 class GeoBase {
45  public:
46  GeoBase() : owns_geom_obj_(true) {}
47  virtual ~GeoBase();
48 
49  std::string getWktString() const;
50  bool getWkb(std::vector<uint8_t>&) const;
51  enum class GeoType {
52  kPOINT,
54  kPOLYGON,
56  kGEOMETRY,
57  kGEOMETRYCOLLECTION
58  };
59  enum class GeoOp {
60  kPROJECTION = 0,
61  kINTERSECTION = 1,
62  kDIFFERENCE = 2,
63  kUNION = 3,
64  kBUFFER = 4,
65  kISVALID = 5,
66  kISEMPTY = 6
67  };
68  virtual GeoType getType() const = 0;
69  const OGRGeometry* getOGRGeometry() const { return geom_; }
70 
71  virtual bool operator==(const GeoBase& other) const;
72 
73  bool isEmpty() const;
74 
75  std::unique_ptr<GeoBase> run(GeoOp op, const GeoBase& other) const;
76  std::unique_ptr<GeoBase> optimized_run(GeoOp op, const GeoBase& other) const;
77  std::unique_ptr<GeoBase> run(GeoOp op, double param) const;
78  bool run(GeoOp op) const;
79 
80  protected:
81  GeoBase(OGRGeometry* geom, const bool owns_geom_obj)
82  : geom_(geom), owns_geom_obj_(owns_geom_obj) {}
83  OGRGeometry* geom_ = nullptr;
85 
86  static int createFromWktString(const std::string& wkt, OGRGeometry** geom);
87  static int createFromWkb(const std::vector<uint8_t>& wkb, OGRGeometry** geom);
88 
89  friend class GeoTypesFactory;
90 };
91 
92 class GeoPoint : public GeoBase {
93  public:
94  GeoPoint(const std::vector<double>& coords);
95  GeoPoint(const std::string& wkt);
96 
97  void getColumns(std::vector<double>& coords) const;
98  GeoType getType() const final { return GeoType::kPOINT; }
99 
100  protected:
101  GeoPoint(OGRGeometry* geom, const bool owns_geom_obj) : GeoBase(geom, owns_geom_obj) {}
102 
103  friend class GeoTypesFactory;
104 };
105 
106 class GeoLineString : public GeoBase {
107  public:
108  GeoLineString(const std::vector<double>& coords);
109  GeoLineString(const std::string& wkt);
110 
111  void getColumns(std::vector<double>& coords, std::vector<double>& bounds) const;
112  GeoType getType() const final { return GeoType::kLINESTRING; }
113 
114  protected:
115  GeoLineString(OGRGeometry* geom, const bool owns_geom_obj)
116  : GeoBase(geom, owns_geom_obj) {}
117 
118  friend class GeoTypesFactory;
119 };
120 
121 class GeoPolygon : public GeoBase {
122  public:
123  GeoPolygon(const std::vector<double>& coords, const std::vector<int32_t>& ring_sizes);
124  GeoPolygon(const std::string& wkt);
125 
126  void getColumns(std::vector<double>& coords,
127  std::vector<int32_t>& ring_sizes,
128  std::vector<double>& bounds) const;
129  GeoType getType() const final { return GeoType::kPOLYGON; }
130 
131  int32_t getNumInteriorRings() const;
132 
133  protected:
134  GeoPolygon(OGRGeometry* geom, const bool owns_geom_obj)
135  : GeoBase(geom, owns_geom_obj) {}
136 
137  friend class GeoTypesFactory;
138 };
139 
140 class GeoMultiPolygon : public GeoBase {
141  public:
142  GeoMultiPolygon(const std::vector<double>& coords,
143  const std::vector<int32_t>& ring_sizes,
144  const std::vector<int32_t>& poly_rings);
145  GeoMultiPolygon(const std::string& wkt);
146 
147  void getColumns(std::vector<double>& coords,
148  std::vector<int32_t>& ring_sizes,
149  std::vector<int32_t>& poly_rings,
150  std::vector<double>& bounds) const;
151  GeoType getType() const final { return GeoType::kMULTIPOLYGON; }
152 
153  protected:
154  GeoMultiPolygon(OGRGeometry* geom, const bool owns_geom_obj)
155  : GeoBase(geom, owns_geom_obj) {}
156 
157  friend class GeoTypesFactory;
158 };
159 
160 class GeoGeometry : public GeoBase {
161  public:
162  GeoGeometry(const std::vector<double>& coords,
163  const std::vector<int32_t>& ring_sizes,
164  const std::vector<int32_t>& poly_rings,
165  const std::vector<int32_t>& geo_kinds){};
166  GeoGeometry(const std::string& wkt){};
167 
168  void getColumns(std::vector<double>& coords,
169  std::vector<int32_t>& ring_sizes,
170  std::vector<int32_t>& poly_rings,
171  std::vector<int32_t>& geo_kinds,
172  std::vector<double>& bounds) const {};
173  GeoType getType() const final { return GeoType::kGEOMETRY; }
174 
175  protected:
176  GeoGeometry(OGRGeometry* geom, const bool owns_geom_obj)
177  : GeoBase(geom, owns_geom_obj) {
178  if (!isEmpty()) {
179  throw GeoTypesError("GeoTypesFactory", "Non-empty GEOMETRY");
180  }
181  }
182 
183  friend class GeoTypesFactory;
184 };
185 
187  public:
188  GeoGeometryCollection(const std::vector<double>& coords,
189  const std::vector<int32_t>& ring_sizes,
190  const std::vector<int32_t>& poly_rings,
191  const std::vector<int32_t>& geo_kinds){};
192  GeoGeometryCollection(const std::string& wkt);
193 
194  void getColumns(std::vector<double>& coords,
195  std::vector<int32_t>& ring_sizes,
196  std::vector<int32_t>& poly_rings,
197  std::vector<int32_t>& geo_kinds,
198  std::vector<double>& bounds) const {};
199  GeoType getType() const final { return GeoType::kGEOMETRYCOLLECTION; }
200 
201  protected:
202  GeoGeometryCollection(OGRGeometry* geom, const bool owns_geom_obj)
203  : GeoBase(geom, owns_geom_obj) {
204  if (!isEmpty()) {
205  throw GeoTypesError("GeoTypesFactory", "Non-empty GEOMETRYCOLLECTION");
206  }
207  }
208 
209  friend class GeoTypesFactory;
210 };
211 
213  public:
214  static OGRGeometry* createOGRGeometry(const std::string& wkt_or_wkb_hex);
215 
216  static std::unique_ptr<GeoBase> createGeoType(const std::string& wkt_or_wkb_hex);
217  static std::unique_ptr<GeoBase> createGeoType(const std::vector<uint8_t>& wkb);
218  static std::unique_ptr<GeoBase> createGeoType(OGRGeometry* geom);
219 
220  static bool getGeoColumns(const std::string& wkt_or_wkb_hex,
221  SQLTypeInfo& ti,
222  std::vector<double>& coords,
223  std::vector<double>& bounds,
224  std::vector<int>& ring_sizes,
225  std::vector<int>& poly_rings,
226  const bool promote_poly_to_mpoly = false);
227 
228  static bool getGeoColumns(const std::vector<uint8_t>& wkb,
229  SQLTypeInfo& ti,
230  std::vector<double>& coords,
231  std::vector<double>& bounds,
232  std::vector<int>& ring_sizes,
233  std::vector<int>& poly_rings,
234  const bool promote_poly_to_mpoly = false);
235 
236  static bool getGeoColumns(OGRGeometry* geom,
237  SQLTypeInfo& ti,
238  std::vector<double>& coords,
239  std::vector<double>& bounds,
240  std::vector<int>& ring_sizes,
241  std::vector<int>& poly_rings,
242  const bool promote_poly_to_mpoly = false);
243 
244  static bool getGeoColumns(const std::vector<std::string>* wkt_or_wkb_hex_column,
245  SQLTypeInfo& ti,
246  std::vector<std::vector<double>>& coords_column,
247  std::vector<std::vector<double>>& bounds_column,
248  std::vector<std::vector<int>>& ring_sizes_column,
249  std::vector<std::vector<int>>& poly_rings_column,
250  const bool promote_poly_to_mpoly = false);
251 
252  static void getNullGeoColumns(SQLTypeInfo& ti,
253  std::vector<double>& coords,
254  std::vector<double>& bounds,
255  std::vector<int>& ring_sizes,
256  std::vector<int>& poly_rings,
257  const bool promote_poly_to_mpoly = false);
258 
259  private:
260  static std::unique_ptr<Geospatial::GeoBase> createGeoTypeImpl(
261  OGRGeometry* geom,
262  const bool owns_geom_obj = true);
263  static void getGeoColumnsImpl(const std::unique_ptr<GeoBase>& geospatial_base,
264  SQLTypeInfo& ti,
265  std::vector<double>& coords,
266  std::vector<double>& bounds,
267  std::vector<int>& ring_sizes,
268  std::vector<int>& poly_rings,
269  const bool promote_poly_to_mpoly = false);
270 };
271 
272 } // namespace Geospatial
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:194
GeoGeometry(const std::string &wkt)
Definition: Types.h:166
GeoTypesError(const std::string &type, const int ogr_err)
Definition: Types.h:32
GeoLineString(OGRGeometry *geom, const bool owns_geom_obj)
Definition: Types.h:115
bool owns_geom_obj_
Definition: Types.h:84
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:188
Constants for Builtin SQL Types supported by OmniSci.
GeoTypesError(const std::string &type, const std::string &err)
Definition: Types.h:35
GeoType getType() const final
Definition: Types.h:173
GeoMultiPolygon(OGRGeometry *geom, const bool owns_geom_obj)
Definition: Types.h:154
GeoType getType() const final
Definition: Types.h:112
static std::string OGRErrorToStr(const int ogr_err)
Definition: Types.cpp:112
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:162
GeoPoint(OGRGeometry *geom, const bool owns_geom_obj)
Definition: Types.h:101
bool operator==(const SlotSize &lhs, const SlotSize &rhs)
GeoType getType() const final
Definition: Types.h:129
GeoPolygon(OGRGeometry *geom, const bool owns_geom_obj)
Definition: Types.h:134
GeoType getType() const final
Definition: Types.h:151
GeoGeometry(OGRGeometry *geom, const bool owns_geom_obj)
Definition: Types.h:176
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:168
GeoGeometryCollection(OGRGeometry *geom, const bool owns_geom_obj)
Definition: Types.h:202
GeoBase(OGRGeometry *geom, const bool owns_geom_obj)
Definition: Types.h:81
static bool run
const OGRGeometry * getOGRGeometry() const
Definition: Types.h:69
GeoType getType() const final
Definition: Types.h:98
GeoType getType() const final
Definition: Types.h:199