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