OmniSciDB  bf83d84833
 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  protected:
88  GeoBase(OGRGeometry* geom, const bool owns_geom_obj)
89  : geom_(geom), owns_geom_obj_(owns_geom_obj) {}
90  OGRGeometry* geom_ = nullptr;
92 
93  static int createFromWktString(const std::string& wkt, OGRGeometry** geom);
94  static int createFromWkb(const std::vector<uint8_t>& wkb, OGRGeometry** geom);
95 
96  friend class GeoTypesFactory;
97 };
98 
99 class GeoPoint : public GeoBase {
100  public:
101  GeoPoint(const std::vector<double>& coords);
102  GeoPoint(const std::string& wkt);
103 
104  void getColumns(std::vector<double>& coords) const;
105  GeoType getType() const final { return GeoType::kPOINT; }
106 
107  protected:
108  GeoPoint(OGRGeometry* geom, const bool owns_geom_obj) : GeoBase(geom, owns_geom_obj) {}
109 
110  friend class GeoTypesFactory;
111 };
112 
113 class GeoLineString : public GeoBase {
114  public:
115  GeoLineString(const std::vector<double>& coords);
116  GeoLineString(const std::string& wkt);
117 
118  void getColumns(std::vector<double>& coords, std::vector<double>& bounds) const;
119  GeoType getType() const final { return GeoType::kLINESTRING; }
120 
121  protected:
122  GeoLineString(OGRGeometry* geom, const bool owns_geom_obj)
123  : GeoBase(geom, owns_geom_obj) {}
124 
125  friend class GeoTypesFactory;
126 };
127 
128 class GeoPolygon : public GeoBase {
129  public:
130  GeoPolygon(const std::vector<double>& coords, const std::vector<int32_t>& ring_sizes);
131  GeoPolygon(const std::string& wkt);
132 
133  void getColumns(std::vector<double>& coords,
134  std::vector<int32_t>& ring_sizes,
135  std::vector<double>& bounds) const;
136  GeoType getType() const final { return GeoType::kPOLYGON; }
137 
138  int32_t getNumInteriorRings() const;
139 
140  protected:
141  GeoPolygon(OGRGeometry* geom, const bool owns_geom_obj)
142  : GeoBase(geom, owns_geom_obj) {}
143 
144  friend class GeoTypesFactory;
145 };
146 
147 class GeoMultiPolygon : public GeoBase {
148  public:
149  GeoMultiPolygon(const std::vector<double>& coords,
150  const std::vector<int32_t>& ring_sizes,
151  const std::vector<int32_t>& poly_rings);
152  GeoMultiPolygon(const std::string& wkt);
153 
154  void getColumns(std::vector<double>& coords,
155  std::vector<int32_t>& ring_sizes,
156  std::vector<int32_t>& poly_rings,
157  std::vector<double>& bounds) const;
158  GeoType getType() const final { return GeoType::kMULTIPOLYGON; }
159 
160  protected:
161  GeoMultiPolygon(OGRGeometry* geom, const bool owns_geom_obj)
162  : GeoBase(geom, owns_geom_obj) {}
163 
164  friend class GeoTypesFactory;
165 };
166 
167 class GeoGeometry : public GeoBase {
168  public:
169  GeoGeometry(const std::vector<double>& coords,
170  const std::vector<int32_t>& ring_sizes,
171  const std::vector<int32_t>& poly_rings,
172  const std::vector<int32_t>& geo_kinds){};
173  GeoGeometry(const std::string& wkt){};
174 
175  void getColumns(std::vector<double>& coords,
176  std::vector<int32_t>& ring_sizes,
177  std::vector<int32_t>& poly_rings,
178  std::vector<int32_t>& geo_kinds,
179  std::vector<double>& bounds) const {};
180  GeoType getType() const final { return GeoType::kGEOMETRY; }
181 
182  protected:
183  GeoGeometry(OGRGeometry* geom, const bool owns_geom_obj)
184  : GeoBase(geom, owns_geom_obj) {
185  if (!isEmpty()) {
186  throw GeoTypesError("GeoTypesFactory", "Non-empty GEOMETRY");
187  }
188  }
189 
190  friend class GeoTypesFactory;
191 };
192 
194  public:
195  GeoGeometryCollection(const std::vector<double>& coords,
196  const std::vector<int32_t>& ring_sizes,
197  const std::vector<int32_t>& poly_rings,
198  const std::vector<int32_t>& geo_kinds){};
199  GeoGeometryCollection(const std::string& wkt);
200 
201  void getColumns(std::vector<double>& coords,
202  std::vector<int32_t>& ring_sizes,
203  std::vector<int32_t>& poly_rings,
204  std::vector<int32_t>& geo_kinds,
205  std::vector<double>& bounds) const {};
206  GeoType getType() const final { return GeoType::kGEOMETRYCOLLECTION; }
207 
208  protected:
209  GeoGeometryCollection(OGRGeometry* geom, const bool owns_geom_obj)
210  : GeoBase(geom, owns_geom_obj) {
211  if (!isEmpty()) {
212  throw GeoTypesError("GeoTypesFactory", "Non-empty GEOMETRYCOLLECTION");
213  }
214  }
215 
216  friend class GeoTypesFactory;
217 };
218 
220  public:
221  static OGRGeometry* createOGRGeometry(const std::string& wkt_or_wkb_hex);
222 
223  static std::unique_ptr<GeoBase> createGeoType(const std::string& wkt_or_wkb_hex);
224  static std::unique_ptr<GeoBase> createGeoType(const std::vector<uint8_t>& wkb);
225  static std::unique_ptr<GeoBase> createGeoType(OGRGeometry* geom);
226 
227  static bool getGeoColumns(const std::string& wkt_or_wkb_hex,
228  SQLTypeInfo& ti,
229  std::vector<double>& coords,
230  std::vector<double>& bounds,
231  std::vector<int>& ring_sizes,
232  std::vector<int>& poly_rings,
233  const bool promote_poly_to_mpoly = false);
234 
235  static bool getGeoColumns(const std::vector<uint8_t>& wkb,
236  SQLTypeInfo& ti,
237  std::vector<double>& coords,
238  std::vector<double>& bounds,
239  std::vector<int>& ring_sizes,
240  std::vector<int>& poly_rings,
241  const bool promote_poly_to_mpoly = false);
242 
243  static bool getGeoColumns(OGRGeometry* geom,
244  SQLTypeInfo& ti,
245  std::vector<double>& coords,
246  std::vector<double>& bounds,
247  std::vector<int>& ring_sizes,
248  std::vector<int>& poly_rings,
249  const bool promote_poly_to_mpoly = false);
250 
251  static bool getGeoColumns(const std::vector<std::string>* wkt_or_wkb_hex_column,
252  SQLTypeInfo& ti,
253  std::vector<std::vector<double>>& coords_column,
254  std::vector<std::vector<double>>& bounds_column,
255  std::vector<std::vector<int>>& ring_sizes_column,
256  std::vector<std::vector<int>>& poly_rings_column,
257  const bool promote_poly_to_mpoly = false);
258 
259  static void getNullGeoColumns(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  private:
267  static std::unique_ptr<Geospatial::GeoBase> createGeoTypeImpl(
268  OGRGeometry* geom,
269  const bool owns_geom_obj = true);
270  static void getGeoColumnsImpl(const std::unique_ptr<GeoBase>& geospatial_base,
271  SQLTypeInfo& ti,
272  std::vector<double>& coords,
273  std::vector<double>& bounds,
274  std::vector<int>& ring_sizes,
275  std::vector<int>& poly_rings,
276  const bool promote_poly_to_mpoly = false);
277 };
278 
279 } // namespace Geospatial
OGRGeometry * geom_
Definition: Types.h:90
GeoGeometry(const std::string &wkt)
Definition: Types.h:173
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:889
GeoPoint(const std::vector< double > &coords)
Definition: Types.cpp:536
void getColumns(std::vector< double > &coords) const
Definition: Types.cpp:562
int32_t getNumInteriorRings() const
Definition: Types.cpp:708
GeoLineString(OGRGeometry *geom, const bool owns_geom_obj)
Definition: Types.h:122
GeoMultiPolygon(const std::vector< double > &coords, const std::vector< int32_t > &ring_sizes, const std::vector< int32_t > &poly_rings)
Definition: Types.cpp:714
const OGRGeometry * getOGRGeometry() const
Definition: Types.h:70
bool owns_geom_obj_
Definition: Types.h:91
GeoLineString(const std::vector< double > &coords)
Definition: Types.cpp:580
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:195
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:1064
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:1114
static std::unique_ptr< Geospatial::GeoBase > createGeoTypeImpl(OGRGeometry *geom, const bool owns_geom_obj=true)
Definition: Types.cpp:1043
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
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:180
GeoMultiPolygon(OGRGeometry *geom, const bool owns_geom_obj)
Definition: Types.h:161
GeoType getType() const final
Definition: Types.h:119
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:175
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:169
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:201
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:753
GeoPoint(OGRGeometry *geom, const bool owns_geom_obj)
Definition: Types.h:108
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:907
virtual bool operator==(const GeoBase &other) const
Definition: Types.cpp:200
static OGRGeometry * createOGRGeometry(const std::string &wkt_or_wkb_hex)
Definition: Types.cpp:873
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:602
GeoType getType() const final
Definition: Types.h:136
GeoPolygon(OGRGeometry *geom, const bool owns_geom_obj)
Definition: Types.h:141
bool transform(int32_t srid0, int32_t srid1)
Definition: Types.cpp:383
GeoType getType() const final
Definition: Types.h:158
std::string getWktString() const
Definition: Types.cpp:164
GeoGeometry(OGRGeometry *geom, const bool owns_geom_obj)
Definition: Types.h:183
GeoGeometryCollection(OGRGeometry *geom, const bool owns_geom_obj)
Definition: Types.h:209
GeoBase(OGRGeometry *geom, const bool owns_geom_obj)
Definition: Types.h:88
std::unique_ptr< GeoBase > run(GeoOp op, const GeoBase &other) const
Definition: Types.cpp:406
void getColumns(std::vector< double > &coords, std::vector< int32_t > &ring_sizes, std::vector< double > &bounds) const
Definition: Types.cpp:666
int32_t getBestPlanarSRID() const
Definition: Types.cpp:226
GeoPolygon(const std::vector< double > &coords, const std::vector< int32_t > &ring_sizes)
Definition: Types.cpp:634
GeoType getType() const final
Definition: Types.h:105
virtual GeoType getType() const =0
GeoType getType() const final
Definition: Types.h:206