OmniSciDB  8a228a1076
geo_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 #ifndef GEOSPATIAL_TYPES_H_
18 #define GEOSPATIAL_TYPES_H_
19 
20 #include <memory>
21 #include <stdexcept>
22 #include <string>
23 #include <vector>
24 
25 #include "sqltypes.h"
26 
27 class OGRGeometry;
28 
29 namespace Geo_namespace {
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:
47  GeoBase() : owns_geom_obj_(true) {}
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,
58  kGEOMETRYCOLLECTION
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  virtual bool operator==(const GeoBase& other) const;
73 
74  bool isEmpty() const;
75 
76  std::unique_ptr<GeoBase> run(GeoOp op, const GeoBase& other) const;
77  std::unique_ptr<GeoBase> optimized_run(GeoOp op, const GeoBase& other) const;
78  std::unique_ptr<GeoBase> run(GeoOp op, double param) const;
79  bool run(GeoOp op) const;
80 
81  protected:
82  GeoBase(OGRGeometry* geom, const bool owns_geom_obj)
83  : geom_(geom), owns_geom_obj_(owns_geom_obj) {}
84  OGRGeometry* geom_ = nullptr;
86 
87  static int createFromWktString(const std::string& wkt, OGRGeometry** geom);
88  static int createFromWkb(const std::vector<uint8_t>& wkb, OGRGeometry** geom);
89 
90  friend class GeoTypesFactory;
91 };
92 
93 class GeoPoint : public GeoBase {
94  public:
95  GeoPoint(const std::vector<double>& coords);
96  GeoPoint(const std::string& wkt);
97 
98  void getColumns(std::vector<double>& coords) const;
99  GeoType getType() const final { return GeoType::kPOINT; }
100 
101  protected:
102  GeoPoint(OGRGeometry* geom, const bool owns_geom_obj) : GeoBase(geom, owns_geom_obj) {}
103 
104  friend class GeoTypesFactory;
105 };
106 
107 class GeoLineString : public GeoBase {
108  public:
109  GeoLineString(const std::vector<double>& coords);
110  GeoLineString(const std::string& wkt);
111 
112  void getColumns(std::vector<double>& coords, std::vector<double>& bounds) const;
113  GeoType getType() const final { return GeoType::kLINESTRING; }
114 
115  protected:
116  GeoLineString(OGRGeometry* geom, const bool owns_geom_obj)
117  : GeoBase(geom, owns_geom_obj) {}
118 
119  friend class GeoTypesFactory;
120 };
121 
122 class GeoPolygon : public GeoBase {
123  public:
124  GeoPolygon(const std::vector<double>& coords, const std::vector<int32_t>& ring_sizes);
125  GeoPolygon(const std::string& wkt);
126 
127  void getColumns(std::vector<double>& coords,
128  std::vector<int32_t>& ring_sizes,
129  std::vector<double>& bounds) const;
130  GeoType getType() const final { return GeoType::kPOLYGON; }
131 
132  int32_t getNumInteriorRings() const;
133 
134  protected:
135  GeoPolygon(OGRGeometry* geom, const bool owns_geom_obj)
136  : GeoBase(geom, owns_geom_obj) {}
137 
138  friend class GeoTypesFactory;
139 };
140 
141 class GeoMultiPolygon : public GeoBase {
142  public:
143  GeoMultiPolygon(const std::vector<double>& coords,
144  const std::vector<int32_t>& ring_sizes,
145  const std::vector<int32_t>& poly_rings);
146  GeoMultiPolygon(const std::string& wkt);
147 
148  void getColumns(std::vector<double>& coords,
149  std::vector<int32_t>& ring_sizes,
150  std::vector<int32_t>& poly_rings,
151  std::vector<double>& bounds) const;
152  GeoType getType() const final { return GeoType::kMULTIPOLYGON; }
153 
154  protected:
155  GeoMultiPolygon(OGRGeometry* geom, const bool owns_geom_obj)
156  : GeoBase(geom, owns_geom_obj) {}
157 
158  friend class GeoTypesFactory;
159 };
160 
161 class GeoGeometry : public GeoBase {
162  public:
163  GeoGeometry(const std::vector<double>& coords,
164  const std::vector<int32_t>& ring_sizes,
165  const std::vector<int32_t>& poly_rings,
166  const std::vector<int32_t>& geo_kinds){};
167  GeoGeometry(const std::string& wkt){};
168 
169  void getColumns(std::vector<double>& coords,
170  std::vector<int32_t>& ring_sizes,
171  std::vector<int32_t>& poly_rings,
172  std::vector<int32_t>& geo_kinds,
173  std::vector<double>& bounds) const {};
174  GeoType getType() const final { return GeoType::kGEOMETRY; }
175 
176  protected:
177  GeoGeometry(OGRGeometry* geom, const bool owns_geom_obj)
178  : GeoBase(geom, owns_geom_obj) {
179  if (!isEmpty()) {
180  throw GeoTypesError("GeoTypesFactory", "Non-empty GEOMETRY");
181  }
182  }
183 
184  friend class GeoTypesFactory;
185 };
186 
188  public:
189  GeoGeometryCollection(const std::vector<double>& coords,
190  const std::vector<int32_t>& ring_sizes,
191  const std::vector<int32_t>& poly_rings,
192  const std::vector<int32_t>& geo_kinds){};
193  GeoGeometryCollection(const std::string& wkt);
194 
195  void getColumns(std::vector<double>& coords,
196  std::vector<int32_t>& ring_sizes,
197  std::vector<int32_t>& poly_rings,
198  std::vector<int32_t>& geo_kinds,
199  std::vector<double>& bounds) const {};
200  GeoType getType() const final { return GeoType::kGEOMETRYCOLLECTION; }
201 
202  protected:
203  GeoGeometryCollection(OGRGeometry* geom, const bool owns_geom_obj)
204  : GeoBase(geom, owns_geom_obj) {
205  if (!isEmpty()) {
206  throw GeoTypesError("GeoTypesFactory", "Non-empty GEOMETRYCOLLECTION");
207  }
208  }
209 
210  friend class GeoTypesFactory;
211 };
212 
214  public:
215  static OGRGeometry* createOGRGeometry(const std::string& wkt_or_wkb_hex);
216 
217  static std::unique_ptr<GeoBase> createGeoType(const std::string& wkt_or_wkb_hex);
218  static std::unique_ptr<GeoBase> createGeoType(const std::vector<uint8_t>& wkb);
219  static std::unique_ptr<GeoBase> createGeoType(OGRGeometry* geom);
220 
221  static bool getGeoColumns(const std::string& wkt_or_wkb_hex,
222  SQLTypeInfo& ti,
223  std::vector<double>& coords,
224  std::vector<double>& bounds,
225  std::vector<int>& ring_sizes,
226  std::vector<int>& poly_rings,
227  const bool promote_poly_to_mpoly = false);
228 
229  static bool getGeoColumns(const std::vector<uint8_t>& wkb,
230  SQLTypeInfo& ti,
231  std::vector<double>& coords,
232  std::vector<double>& bounds,
233  std::vector<int>& ring_sizes,
234  std::vector<int>& poly_rings,
235  const bool promote_poly_to_mpoly = false);
236 
237  static bool getGeoColumns(OGRGeometry* geom,
238  SQLTypeInfo& ti,
239  std::vector<double>& coords,
240  std::vector<double>& bounds,
241  std::vector<int>& ring_sizes,
242  std::vector<int>& poly_rings,
243  const bool promote_poly_to_mpoly = false);
244 
245  static bool getGeoColumns(const std::vector<std::string>* wkt_or_wkb_hex_column,
246  SQLTypeInfo& ti,
247  std::vector<std::vector<double>>& coords_column,
248  std::vector<std::vector<double>>& bounds_column,
249  std::vector<std::vector<int>>& ring_sizes_column,
250  std::vector<std::vector<int>>& poly_rings_column,
251  const bool promote_poly_to_mpoly = false);
252 
253  static void getNullGeoColumns(SQLTypeInfo& ti,
254  std::vector<double>& coords,
255  std::vector<double>& bounds,
256  std::vector<int>& ring_sizes,
257  std::vector<int>& poly_rings,
258  const bool promote_poly_to_mpoly = false);
259 
260  private:
261  static std::unique_ptr<Geo_namespace::GeoBase> createGeoTypeImpl(
262  OGRGeometry* geom,
263  const bool owns_geom_obj = true);
264  static void getGeoColumnsImpl(const std::unique_ptr<GeoBase>& geospatial_base,
265  SQLTypeInfo& ti,
266  std::vector<double>& coords,
267  std::vector<double>& bounds,
268  std::vector<int>& ring_sizes,
269  std::vector<int>& poly_rings,
270  const bool promote_poly_to_mpoly = false);
271 };
272 
273 } // namespace Geo_namespace
274 
275 #endif // GEOSPATIAL_TYPES_H_
GeoType getType() const final
Definition: geo_types.h:99
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: geo_types.h:163
GeoType getType() const final
Definition: geo_types.h:174
GeoType getType() const final
Definition: geo_types.h:130
GeoGeometry(const std::string &wkt)
Definition: geo_types.h:167
Constants for Builtin SQL Types supported by OmniSci.
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: geo_types.h:189
GeoType getType() const final
Definition: geo_types.h:200
GeoGeometry(OGRGeometry *geom, const bool owns_geom_obj)
Definition: geo_types.h:177
GeoMultiPolygon(OGRGeometry *geom, const bool owns_geom_obj)
Definition: geo_types.h:155
GeoBase(OGRGeometry *geom, const bool owns_geom_obj)
Definition: geo_types.h:82
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: geo_types.h:195
GeoType getType() const final
Definition: geo_types.h:152
const OGRGeometry * getOGRGeometry() const
Definition: geo_types.h:70
GeoPoint(OGRGeometry *geom, const bool owns_geom_obj)
Definition: geo_types.h:102
GeoTypesError(const std::string &type, const int ogr_err)
Definition: geo_types.h:33
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: geo_types.h:169
bool operator==(const SlotSize &lhs, const SlotSize &rhs)
GeoPolygon(OGRGeometry *geom, const bool owns_geom_obj)
Definition: geo_types.h:135
GeoLineString(OGRGeometry *geom, const bool owns_geom_obj)
Definition: geo_types.h:116
static bool run
GeoType getType() const final
Definition: geo_types.h:113
GeoTypesError(const std::string &type, const std::string &err)
Definition: geo_types.h:36
static std::string OGRErrorToStr(const int ogr_err)
Definition: geo_types.cpp:111
GeoGeometryCollection(OGRGeometry *geom, const bool owns_geom_obj)
Definition: geo_types.h:203