Shapes
GIS made easy, a lightweight header-only planar geometry library for Modern C++
geometry.hpp
1 #pragma once
2 
3 #include <ciso646>
4 #include <vector>
5 #include <memory>
6 #include <simo/shapes_fwd.hpp>
7 #include <simo/geom/detail/bounds.hpp>
8 #include <simo/geom/detail/types.hpp>
9 #include <simo/geom/detail/utils.hpp>
10 #include <simo/exceptions.hpp>
11 
12 namespace simo
13 {
14 namespace shapes
15 {
16 
25 template <typename T>
27 {
28  public:
35  geometry_type geom_type() const noexcept
36  {
37  return static_cast<const T*>(this)->geom_type_();
38  }
39 
46  std::string tagged_text() const noexcept
47  {
48  switch (geom_type())
49  {
50  case geometry_type::POINT:
51  return "Point";
52  case geometry_type::POINTZ:
53  return "Point Z";
54  case geometry_type::POINTM:
55  return "Point M";
56  case geometry_type::POINTZM:
57  return "Point ZM";
58  case geometry_type::MULTIPOINT:
59  return "MultiPoint";
60  case geometry_type::MULTIPOINTZ:
61  return "MultiPoint Z";
62  case geometry_type::MULTIPOINTM:
63  return "MultiPoint M";
64  case geometry_type::MULTIPOINTZM:
65  return "MultiPoint ZM";
66  case geometry_type::LINESTRING:
67  return "LineString";
68  case geometry_type::LINESTRINGZ:
69  return "LineString Z";
70  case geometry_type::LINESTRINGM:
71  return "LineString M";
72  case geometry_type::LINESTRINGZM:
73  return "LineString ZM";
74  case geometry_type::MULTILINESTRING:
75  return "MultiLineString";
76  case geometry_type::MULTILINESTRINGZ:
77  return "MultiLineString Z";
78  case geometry_type::MULTILINESTRINGM:
79  return "MultiLineString M";
80  case geometry_type::MULTILINESTRINGZM:
81  return "MultiLineString ZM";
82  case geometry_type::POLYGON:
83  return "Polygon";
84  case geometry_type::POLYGONZ:
85  return "Polygon Z";
86  case geometry_type::POLYGONM:
87  return "Polygon M";
88  case geometry_type::POLYGONZM:
89  return "Polygon ZM";
90  case geometry_type::MULTIPOLYGON:
91  return "MultiPolygon";
92  case geometry_type::MULTIPOLYGONZ:
93  return "MultiPolygon Z";
94  case geometry_type::MULTIPOLYGONM:
95  return "MultiPolygon M";
96  case geometry_type::MULTIPOLYGONZM:
97  return "MultiPolygon ZM";
98  case geometry_type::GEOMETRYCOLLECTION:
99  return "GeometryCollection";
100  case geometry_type::GEOMETRYCOLLECTIONZ:
101  return "GeometryCollection Z";
102  case geometry_type::GEOMETRYCOLLECTIONM:
103  return "GeometryCollection M";
104  case geometry_type::GEOMETRYCOLLECTIONZM:
105  return "GeometryCollection ZM";
106  default:
107  return "Geometry";
108  }
109  }
110 
117  dimension_type dim() const noexcept
118  {
119  int value = static_cast<int>(geom_type());
120  if (value >= 1000 and value < 2000)
121  {
122  return dimension_type::XYZ;
123  }
124  if (value >= 2000 and value < 3000)
125  {
126  return dimension_type::XYM;
127  }
128  if (value >= 3000)
129  {
130  return dimension_type::XYZM;
131  }
132  return dimension_type::XY;
133  }
134 
142  size_t ndim() const noexcept
143  {
144  switch (dim())
145  {
146  case dimension_type::XYZM:
147  return 4;
148  case dimension_type::XYZ:
149  case dimension_type::XYM:
150  return 3;
151  default:
152  return 2;
153  }
154  }
155 
162  void throw_for_invalid() const
163  {
164  return static_cast<const T*>(this)->throw_for_invalid_();
165  }
166 
173  bool is_closed() const noexcept
174  {
175  return static_cast<const T*>(this)->is_closed_();
176  }
177 
184  bool is_valid() const noexcept
185  {
186  try
187  {
189  }
190  catch (const exceptions::geometry_error& e)
191  {
192  return false;
193  }
194  return true;
195  }
196 
203  bounds_t bounds() const
204  {
205  return static_cast<const T*>(this)->bounds_();
206  }
207 
214  bool has_z() const noexcept
215  {
216  int value = static_cast<int>(geom_type());
217  return (value >= 1000 and value < 2000) or value >= 3000;
218  }
219 
226  bool has_m() const noexcept
227  {
228  int value = static_cast<int>(geom_type());
229  return value >= 2000;
230  }
231 
232  // json
233 
242  static T from_json(const std::string& json)
243  {
244  return T::from_json_(json);
245  }
246 
255  std::string json(std::int32_t precision = -1) const
256  {
257  return static_cast<const T*>(this)->json_(precision);
258  }
259 
260  // wkt
261 
270  static T from_wkt(const std::string& wkt)
271  {
272  return T::from_wkt_(wkt);
273  }
274 
283  std::string wkt(std::int32_t precision = -1) const
284  {
285  return static_cast<const T*>(this)->wkt_(precision);
286  }
287 };
288 
289 } // namespace shapes
290 } // namespace simo
geometry_type geom_type() const noexcept
Returns the geometry type.
Definition: geometry.hpp:35
Base class for all geometries.
Definition: geometry.hpp:26
static T from_json(const std::string &json)
Creates a geometry from a geojson string.
Definition: geometry.hpp:242
Exception thrown when a geometry error is found.
Definition: exceptions.hpp:121
bool is_closed() const noexcept
Whether the geometry is closed.
Definition: geometry.hpp:173
static T from_wkt(const std::string &wkt)
Creates a Geometry from a wkt string.
Definition: geometry.hpp:270
Represents an axis-aligned bounding box.
Definition: bounds.hpp:18
dimension_type dim() const noexcept
Returns the dimension type for the geometry.
Definition: geometry.hpp:117
bool has_z() const noexcept
Whether the geometry has the z-coordinate.
Definition: geometry.hpp:214
bool is_valid() const noexcept
Whether the geometry is valid.
Definition: geometry.hpp:184
std::string json(std::int32_t precision=-1) const
Dumps the geojson representation of the geometry.
Definition: geometry.hpp:255
bool has_m() const noexcept
Whether the geometry has the m-coordinate (measurement coordinate)
Definition: geometry.hpp:226
std::string tagged_text() const noexcept
Returns the geometry type as a string.
Definition: geometry.hpp:46
std::string wkt(std::int32_t precision=-1) const
Dumps the wkt representation of the geometry.
Definition: geometry.hpp:283
bounds_t bounds() const
Returns the bounding box of the geometry.
Definition: geometry.hpp:203
void throw_for_invalid() const
Raise an error if the geometry is invalid.
Definition: geometry.hpp:162
size_t ndim() const noexcept
Returns the number of dimensions of the geometry.
Definition: geometry.hpp:142