OmniSciDB  6686921089
 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 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  kEQUALS = 7
69  };
70  virtual GeoType getType() const = 0;
71  const OGRGeometry* getOGRGeometry() const { return geom_; }
72 
73  int32_t getBestPlanarSRID() const;
74  bool transform(int32_t srid0, int32_t srid1);
75  bool transform(SQLTypeInfo& ti);
76  static std::shared_ptr<OGRCoordinateTransformation> getTransformation(int32_t srid0,
77  int32_t srid1);
78 
79  virtual bool operator==(const GeoBase& other) const;
80 
81  bool isEmpty() const;
82 
83  std::unique_ptr<GeoBase> run(GeoOp op, const GeoBase& other) const;
84  std::unique_ptr<GeoBase> optimized_run(GeoOp op, const GeoBase& other) const;
85  std::unique_ptr<GeoBase> run(GeoOp op, double param) const;
86  bool run(GeoOp op) const;
87 
88  virtual std::unique_ptr<GeoBase> clone() const = 0;
89 
90  protected:
91  GeoBase(OGRGeometry* geom, const bool owns_geom_obj)
92  : geom_(geom), owns_geom_obj_(owns_geom_obj) {}
93  OGRGeometry* geom_ = nullptr;
95 
96  static int createFromWktString(const std::string& wkt, OGRGeometry** geom);
97  static int createFromWkb(const std::vector<uint8_t>& wkb, OGRGeometry** geom);
98 
99  friend class GeoTypesFactory;
100 };
101 
102 class GeoPoint : public GeoBase {
103  public:
104  GeoPoint(const std::vector<double>& coords);
105  GeoPoint(const std::string& wkt);
106 
107  void getColumns(std::vector<double>& coords) const;
108  GeoType getType() const final { return GeoType::kPOINT; }
109 
110  std::unique_ptr<GeoBase> clone() const override;
111 
112  protected:
113  GeoPoint(OGRGeometry* geom, const bool owns_geom_obj) : GeoBase(geom, owns_geom_obj) {}
114 
115  friend class GeoTypesFactory;
116 };
117 
118 class GeoLineString : public GeoBase {
119  public:
120  GeoLineString(const std::vector<double>& coords);
121  GeoLineString(const std::string& wkt);
122 
123  void getColumns(std::vector<double>& coords, std::vector<double>& bounds) const;
125 
126  std::unique_ptr<GeoBase> clone() const final;
127 
128  protected:
129  GeoLineString(OGRGeometry* geom, const bool owns_geom_obj)
130  : GeoBase(geom, owns_geom_obj) {}
131 
132  friend class GeoTypesFactory;
133 };
134 
135 class GeoPolygon : public GeoBase {
136  public:
137  GeoPolygon(const std::vector<double>& coords, const std::vector<int32_t>& ring_sizes);
138  GeoPolygon(const std::string& wkt);
139 
140  void getColumns(std::vector<double>& coords,
141  std::vector<int32_t>& ring_sizes,
142  std::vector<double>& bounds) const;
144 
145  int32_t getNumInteriorRings() const;
146 
147  std::unique_ptr<GeoBase> clone() const final;
148 
149  protected:
150  GeoPolygon(OGRGeometry* geom, const bool owns_geom_obj)
151  : GeoBase(geom, owns_geom_obj) {}
152 
153  friend class GeoTypesFactory;
154 };
155 
156 class GeoMultiPolygon : public GeoBase {
157  public:
158  GeoMultiPolygon(const std::vector<double>& coords,
159  const std::vector<int32_t>& ring_sizes,
160  const std::vector<int32_t>& poly_rings);
161  GeoMultiPolygon(const std::string& wkt);
162 
163  void getColumns(std::vector<double>& coords,
164  std::vector<int32_t>& ring_sizes,
165  std::vector<int32_t>& poly_rings,
166  std::vector<double>& bounds) const;
168 
169  std::unique_ptr<GeoBase> clone() const final;
170 
171  protected:
172  GeoMultiPolygon(OGRGeometry* geom, const bool owns_geom_obj)
173  : GeoBase(geom, owns_geom_obj) {}
174 
175  friend class GeoTypesFactory;
176 };
177 
178 class GeoGeometry : public GeoBase {
179  public:
180  GeoGeometry(const std::vector<double>& coords,
181  const std::vector<int32_t>& ring_sizes,
182  const std::vector<int32_t>& poly_rings,
183  const std::vector<int32_t>& geo_kinds){};
184  GeoGeometry(const std::string& wkt){};
185 
186  void getColumns(std::vector<double>& coords,
187  std::vector<int32_t>& ring_sizes,
188  std::vector<int32_t>& poly_rings,
189  std::vector<int32_t>& geo_kinds,
190  std::vector<double>& bounds) const {};
192 
193  std::unique_ptr<GeoBase> clone() const final;
194 
195  protected:
196  GeoGeometry(OGRGeometry* geom, const bool owns_geom_obj)
197  : GeoBase(geom, owns_geom_obj) {
198  if (!isEmpty()) {
199  throw GeoTypesError("GeoTypesFactory", "Non-empty GEOMETRY");
200  }
201  }
202 
203  friend class GeoTypesFactory;
204 };
205 
207  public:
208  GeoGeometryCollection(const std::vector<double>& coords,
209  const std::vector<int32_t>& ring_sizes,
210  const std::vector<int32_t>& poly_rings,
211  const std::vector<int32_t>& geo_kinds){};
212  GeoGeometryCollection(const std::string& wkt);
213 
214  void getColumns(std::vector<double>& coords,
215  std::vector<int32_t>& ring_sizes,
216  std::vector<int32_t>& poly_rings,
217  std::vector<int32_t>& geo_kinds,
218  std::vector<double>& bounds) const {};
220 
221  std::unique_ptr<GeoBase> clone() const final;
222 
223  protected:
224  GeoGeometryCollection(OGRGeometry* geom, const bool owns_geom_obj)
225  : GeoBase(geom, owns_geom_obj) {
226  if (!isEmpty()) {
227  throw GeoTypesError("GeoTypesFactory", "Non-empty GEOMETRYCOLLECTION");
228  }
229  }
230 
231  friend class GeoTypesFactory;
232 };
233 
235  public:
236  static OGRGeometry* createOGRGeometry(const std::string& wkt_or_wkb_hex);
237 
238  static std::unique_ptr<GeoBase> createGeoType(const std::string& wkt_or_wkb_hex);
239  static std::unique_ptr<GeoBase> createGeoType(const std::vector<uint8_t>& wkb);
240  static std::unique_ptr<GeoBase> createGeoType(OGRGeometry* geom);
241 
242  static bool getGeoColumns(const std::string& wkt_or_wkb_hex,
243  SQLTypeInfo& ti,
244  std::vector<double>& coords,
245  std::vector<double>& bounds,
246  std::vector<int>& ring_sizes,
247  std::vector<int>& poly_rings,
248  const bool promote_poly_to_mpoly = false);
249 
250  static bool getGeoColumns(const std::vector<uint8_t>& wkb,
251  SQLTypeInfo& ti,
252  std::vector<double>& coords,
253  std::vector<double>& bounds,
254  std::vector<int>& ring_sizes,
255  std::vector<int>& poly_rings,
256  const bool promote_poly_to_mpoly = false);
257 
258  static bool getGeoColumns(OGRGeometry* geom,
259  SQLTypeInfo& ti,
260  std::vector<double>& coords,
261  std::vector<double>& bounds,
262  std::vector<int>& ring_sizes,
263  std::vector<int>& poly_rings,
264  const bool promote_poly_to_mpoly = false);
265 
266  static bool getGeoColumns(const std::vector<std::string>* wkt_or_wkb_hex_column,
267  SQLTypeInfo& ti,
268  std::vector<std::vector<double>>& coords_column,
269  std::vector<std::vector<double>>& bounds_column,
270  std::vector<std::vector<int>>& ring_sizes_column,
271  std::vector<std::vector<int>>& poly_rings_column,
272  const bool promote_poly_to_mpoly = false);
273 
274  static void getNullGeoColumns(SQLTypeInfo& ti,
275  std::vector<double>& coords,
276  std::vector<double>& bounds,
277  std::vector<int>& ring_sizes,
278  std::vector<int>& poly_rings,
279  const bool promote_poly_to_mpoly = false);
280 
281  private:
282  static std::unique_ptr<Geospatial::GeoBase> createGeoTypeImpl(
283  OGRGeometry* geom,
284  const bool owns_geom_obj = true);
285  static void getGeoColumnsImpl(const std::unique_ptr<GeoBase>& geospatial_base,
286  SQLTypeInfo& ti,
287  std::vector<double>& coords,
288  std::vector<double>& bounds,
289  std::vector<int>& ring_sizes,
290  std::vector<int>& poly_rings,
291  const bool promote_poly_to_mpoly = false);
292 };
293 
294 } // namespace Geospatial
OGRGeometry * geom_
Definition: Types.h:93
GeoGeometry(const std::string &wkt)
Definition: Types.h:184
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:71
bool owns_geom_obj_
Definition: Types.h:94
#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:208
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:191
virtual std::unique_ptr< GeoBase > clone() const =0
GeoType getType() const final
Definition: Types.h:124
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:186
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:180
bool g_enable_smem_group_by true
static int createFromWkb(const std::vector< uint8_t > &wkb, OGRGeometry **geom)
Definition: Types.cpp:173
std::unique_ptr< GeoBase > clone() const override
Definition: Types.cpp:536
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:214
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
GeoPoint(OGRGeometry *geom, const bool owns_geom_obj)
Definition: Types.h:113
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:143
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:167
std::string getWktString() const
Definition: Types.cpp:164
GeoBase(OGRGeometry *geom, const bool owns_geom_obj)
Definition: Types.h:91
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:108
virtual GeoType getType() const =0
GeoType getType() const final
Definition: Types.h:219