OmniSciDB  c07336695a
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;
52  virtual GeoType getType() const = 0;
53 
54  virtual bool operator==(const GeoBase& other) const;
55 
56  protected:
57  GeoBase(OGRGeometry* geom, const bool owns_geom_obj)
58  : geom_(geom), owns_geom_obj_(owns_geom_obj) {}
59  OGRGeometry* geom_ = nullptr;
61 
62  static int createFromWktString(const std::string& wkt, OGRGeometry** geom);
63 
64  friend class GeoTypesFactory;
65 };
66 
67 class GeoPoint : public GeoBase {
68  public:
69  GeoPoint(const std::vector<double>& coords);
70  GeoPoint(const std::string& wkt);
71 
72  void getColumns(std::vector<double>& coords) const;
73  GeoType getType() const final { return GeoType::kPOINT; }
74 
75  protected:
76  GeoPoint(OGRGeometry* geom, const bool owns_geom_obj) : GeoBase(geom, owns_geom_obj) {}
77 
78  friend class GeoTypesFactory;
79 };
80 
81 class GeoLineString : public GeoBase {
82  public:
83  GeoLineString(const std::vector<double>& coords);
84  GeoLineString(const std::string& wkt);
85 
86  void getColumns(std::vector<double>& coords, std::vector<double>& bounds) const;
87  GeoType getType() const final { return GeoType::kLINESTRING; }
88 
89  protected:
90  GeoLineString(OGRGeometry* geom, const bool owns_geom_obj)
91  : GeoBase(geom, owns_geom_obj) {}
92 
93  friend class GeoTypesFactory;
94 };
95 
96 class GeoPolygon : public GeoBase {
97  public:
98  GeoPolygon(const std::vector<double>& coords, const std::vector<int32_t>& ring_sizes);
99  GeoPolygon(const std::string& wkt);
100 
101  void getColumns(std::vector<double>& coords,
102  std::vector<int32_t>& ring_sizes,
103  std::vector<double>& bounds) const;
104  GeoType getType() const final { return GeoType::kPOLYGON; }
105 
106  int32_t getNumInteriorRings() const;
107 
108  protected:
109  GeoPolygon(OGRGeometry* geom, const bool owns_geom_obj)
110  : GeoBase(geom, owns_geom_obj) {}
111 
112  friend class GeoTypesFactory;
113 };
114 
115 class GeoMultiPolygon : public GeoBase {
116  public:
117  GeoMultiPolygon(const std::vector<double>& coords,
118  const std::vector<int32_t>& ring_sizes,
119  const std::vector<int32_t>& poly_rings);
120  GeoMultiPolygon(const std::string& wkt);
121 
122  void getColumns(std::vector<double>& coords,
123  std::vector<int32_t>& ring_sizes,
124  std::vector<int32_t>& poly_rings,
125  std::vector<double>& bounds) const;
126  GeoType getType() const final { return GeoType::kMULTIPOLYGON; }
127 
128  protected:
129  GeoMultiPolygon(OGRGeometry* geom, const bool owns_geom_obj)
130  : GeoBase(geom, owns_geom_obj) {}
131 
132  friend class GeoTypesFactory;
133 };
134 
136  public:
137  static std::unique_ptr<GeoBase> createGeoType(const std::string& wkt);
138  static std::unique_ptr<GeoBase> createGeoType(OGRGeometry* geom);
139 
140  static bool getGeoColumns(const std::string& wkt,
141  SQLTypeInfo& ti,
142  std::vector<double>& coords,
143  std::vector<double>& bounds,
144  std::vector<int>& ring_sizes,
145  std::vector<int>& poly_rings,
146  const bool promote_poly_to_mpoly = false);
147 
148  static bool getGeoColumns(OGRGeometry* geom,
149  SQLTypeInfo& ti,
150  std::vector<double>& coords,
151  std::vector<double>& bounds,
152  std::vector<int>& ring_sizes,
153  std::vector<int>& poly_rings,
154  const bool promote_poly_to_mpoly = false);
155 
156  static bool getGeoColumns(const std::vector<std::string>* wkt_column,
157  SQLTypeInfo& ti,
158  std::vector<std::vector<double>>& coords_column,
159  std::vector<std::vector<double>>& bounds_column,
160  std::vector<std::vector<int>>& ring_sizes_column,
161  std::vector<std::vector<int>>& poly_rings_column,
162  const bool promote_poly_to_mpoly = false);
163 
164  private:
165  static std::unique_ptr<Geo_namespace::GeoBase> createGeoTypeImpl(
166  OGRGeometry* geom,
167  const bool owns_geom_obj = true);
168  static void getGeoColumnsImpl(const std::unique_ptr<GeoBase>& geospatial_base,
169  SQLTypeInfo& ti,
170  std::vector<double>& coords,
171  std::vector<double>& bounds,
172  std::vector<int>& ring_sizes,
173  std::vector<int>& poly_rings,
174  const bool promote_poly_to_mpoly = false);
175 };
176 
177 } // namespace Geo_namespace
178 
179 #endif // GEOSPATIAL_TYPES_H_
GeoType getType() const final
Definition: geo_types.h:73
GeoType getType() const final
Definition: geo_types.h:104
Constants for Builtin SQL Types supported by MapD.
GeoMultiPolygon(OGRGeometry *geom, const bool owns_geom_obj)
Definition: geo_types.h:129
GeoBase(OGRGeometry *geom, const bool owns_geom_obj)
Definition: geo_types.h:57
GeoType getType() const final
Definition: geo_types.h:126
GeoPoint(OGRGeometry *geom, const bool owns_geom_obj)
Definition: geo_types.h:76
GeoTypesError(const std::string &type, const int ogr_err)
Definition: geo_types.h:33
bool operator==(const SlotSize &lhs, const SlotSize &rhs)
GeoPolygon(OGRGeometry *geom, const bool owns_geom_obj)
Definition: geo_types.h:109
GeoLineString(OGRGeometry *geom, const bool owns_geom_obj)
Definition: geo_types.h:90
GeoType getType() const final
Definition: geo_types.h:87
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