5 #include <initializer_list> 10 #include <type_traits> 14 #include <json/json.hpp> 15 #include <simo/geom/detail/geometry.hpp> 16 #include <simo/exceptions.hpp> 17 #include <simo/io/wkt_reader.hpp> 18 #include <simo/io/polyline.hpp> 26 # pragma GCC diagnostic push 27 # pragma GCC diagnostic ignored "-Wpedantic" 31 # pragma clang diagnostic push 32 # pragma clang diagnostic ignored "-Wgnu-anonymous-struct" 33 # pragma clang diagnostic ignored "-Wnested-anon-types" 37 # pragma warning(push) 38 # pragma warning(disable : 4201) 41 template <class T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
47 using const_reference =
const T&;
49 using const_iterator =
const T*;
50 using difference_type = std::ptrdiff_t;
51 using size_type = size_t;
52 using reverse_iterator = std::reverse_iterator<iterator>;
53 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
57 using coord_iterator =
typename std::vector<coord_type>::iterator;
58 using coord_const_iterator =
typename std::vector<coord_type>::const_iterator;
60 static const size_type N = 2;
93 explicit basic_point(coord_iterator begin, coord_iterator end)
96 assert(std::distance(begin, end) == N);
98 for (
auto it = begin; it != end; ++it)
104 explicit basic_point(coord_const_iterator begin, coord_const_iterator end)
107 assert(std::distance(begin, end) == N);
109 for (
auto it = begin; it != end; ++it)
115 size_type size()
const noexcept
122 reference operator[](
size_t pos)
130 return lhs.x == rhs.x and lhs.y == rhs.y;
135 return not operator==(lhs, rhs);
140 static basic_point<T> from_polyline(
const std::string& polyline, std::int32_t precision = 5)
142 auto coords = polyline::decode(polyline, precision);
143 if (coords.size() > N)
147 return {coords[0], coords[1]};
150 std::string polyline(std::int32_t precision = 5)
const 152 return polyline::encode(lat, precision) + polyline::encode(lng, precision);
155 size_type max_size()
const 162 return begin() == end();
177 const_iterator begin()
const 181 const_iterator end()
const 186 reverse_iterator rbegin()
188 return coords + N - 1;
191 reverse_iterator rend()
196 const_reverse_iterator rbegin()
const 198 return coords + N - 1;
201 const_reverse_iterator rend()
const 211 geometry_type geom_type_() const noexcept
213 return geometry_type::POINT;
217 bool is_closed_() const noexcept
223 void throw_for_invalid_()
const 242 auto j = nlohmann::json::parse(json);
243 auto geom_type = j.at(
"type").get<std::string>();
248 auto coords = j.at(
"coordinates").get<std::vector<double>>();
249 return {coords.at(0), coords.at(1)};
251 catch (
const std::out_of_range& e)
255 catch (
const nlohmann::json::exception& e)
266 std::string json_(std::int32_t precision = -1)
const 268 std::stringstream ss;
271 ss << std::setprecision(precision);
273 ss <<
"{\"type\":\"Point\",\"coordinates\":" 274 <<
"[" << x <<
"," << y <<
"]}";
284 auto result = reader.
read(wkt);
285 auto data = result.
data;
286 if (data.geom_type != geometry_type::POINT)
290 return {data.coords[0], data.coords[1]};
294 std::string wkt_(std::int32_t precision = -1)
const 296 std::stringstream ss;
299 ss << std::setprecision(precision);
302 <<
"(" << x <<
" " << y <<
")";
309 template <class T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
313 using value_type = T;
314 using reference = T&;
315 using const_reference =
const T&;
317 using const_iterator =
const T*;
318 using difference_type = std::ptrdiff_t;
319 using size_type = size_t;
320 using reverse_iterator = std::reverse_iterator<iterator>;
321 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
324 using coord_type = value_type;
325 using coord_iterator =
typename std::vector<coord_type>::iterator;
326 using coord_const_iterator =
typename std::vector<coord_type>::const_iterator;
328 static const size_type N = 3;
350 : x(0), y(0), z(0) {}
353 : x(x), y(y), z(z) {}
355 explicit basic_point_z(coord_iterator begin, coord_iterator end)
357 assert(std::distance(begin, end) == N);
359 for (
auto it = begin; it != end; ++it)
365 explicit basic_point_z(coord_const_iterator begin, coord_const_iterator end)
367 assert(std::distance(begin, end) == N);
369 for (
auto it = begin; it != end; ++it)
375 std::size_t size()
const noexcept
382 T& operator[](
size_t pos)
390 return lhs.x == rhs.x and lhs.y == rhs.y and lhs.z == rhs.z;
395 return not operator==(lhs, rhs);
398 size_type max_size()
const 405 return begin() == end();
420 const_iterator begin()
const 424 const_iterator end()
const 429 reverse_iterator rbegin()
431 return coords + N - 1;
434 reverse_iterator rend()
439 const_reverse_iterator rbegin()
const 441 return coords + N - 1;
444 const_reverse_iterator rend()
const 454 geometry_type geom_type_() const noexcept
456 return geometry_type::POINTZ;
460 bool is_closed_() const noexcept
466 void throw_for_invalid_()
const 485 auto j = nlohmann::json::parse(json);
486 auto geom_type = j.at(
"type").get<std::string>();
491 auto coords = j.at(
"coordinates").get<std::vector<double>>();
492 return {coords.at(0), coords.at(1), coords.at(2)};
494 catch (
const std::out_of_range& e)
498 catch (
const nlohmann::json::exception& e)
509 std::string json_(std::int32_t precision = -1)
const 511 std::stringstream ss;
514 ss << std::setprecision(precision);
516 ss <<
"{\"type\":\"Point\",\"coordinates\":" 517 <<
"[" << x <<
"," << y <<
"," << z <<
"]}";
527 auto result = reader.
read(wkt);
528 auto data = result.
data;
529 if (data.geom_type != geometry_type::POINTZ)
533 return {data.coords[0], data.coords[1], data.coords[2]};
537 std::string wkt_(std::int32_t precision = -1)
const 539 std::stringstream ss;
542 ss << std::setprecision(precision);
545 <<
"(" << x <<
" " << y <<
" " << z <<
")";
552 template <class T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
556 using value_type = T;
557 using reference = T&;
558 using const_reference =
const T&;
560 using const_iterator =
const T*;
561 using difference_type = std::ptrdiff_t;
562 using size_type = size_t;
563 using reverse_iterator = std::reverse_iterator<iterator>;
564 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
567 using coord_type = value_type;
568 using coord_iterator =
typename std::vector<coord_type>::iterator;
569 using coord_const_iterator =
typename std::vector<coord_type>::const_iterator;
571 static const size_type N = 3;
593 : x(0), y(0), m(0) {}
596 : x(x), y(y), m(m) {}
598 explicit basic_point_m(coord_iterator begin, coord_iterator end)
600 assert(std::distance(begin, end) == N);
602 for (
auto it = begin; it != end; ++it)
608 explicit basic_point_m(coord_const_iterator begin, coord_const_iterator end)
610 assert(std::distance(begin, end) == N);
612 for (
auto it = begin; it != end; ++it)
618 std::size_t size()
const noexcept
625 T& operator[](
size_t pos)
633 return lhs.x == rhs.x and lhs.y == rhs.y and lhs.m == rhs.m;
638 return not operator==(lhs, rhs);
641 size_type max_size()
const 648 return begin() == end();
663 const_iterator begin()
const 667 const_iterator end()
const 672 reverse_iterator rbegin()
674 return coords + N - 1;
677 reverse_iterator rend()
682 const_reverse_iterator rbegin()
const 684 return coords + N - 1;
687 const_reverse_iterator rend()
const 697 geometry_type geom_type_() const noexcept
699 return geometry_type::POINTM;
703 bool is_closed_() const noexcept
709 void throw_for_invalid_()
const 728 auto j = nlohmann::json::parse(json);
729 auto geom_type = j.at(
"type").get<std::string>();
734 auto coords = j.at(
"coordinates").get<std::vector<double>>();
735 return {coords.at(0), coords.at(1), coords.at(2)};
737 catch (
const std::out_of_range& e)
741 catch (
const nlohmann::json::exception& e)
752 std::string json_(std::int32_t precision = -1)
const 754 std::stringstream ss;
757 ss << std::setprecision(precision);
759 ss <<
"{\"type\":\"Point\",\"coordinates\":" 760 <<
"[" << x <<
"," << y <<
"," << m <<
"]}";
770 auto result = reader.
read(wkt);
771 auto data = result.
data;
772 if (data.geom_type != geometry_type::POINTM)
776 return {data.coords[0], data.coords[1], data.coords[2]};
779 std::string wkt_(std::int32_t precision = -1)
const 781 std::stringstream ss;
784 ss << std::setprecision(precision);
787 <<
"(" << x <<
" " << y <<
" " << m <<
")";
794 template <class T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
798 using value_type = T;
799 using reference = T&;
800 using const_reference =
const T&;
802 using const_iterator =
const T*;
803 using difference_type = std::ptrdiff_t;
804 using size_type = size_t;
805 using reverse_iterator = std::reverse_iterator<iterator>;
806 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
809 using coord_type = value_type;
810 using coord_iterator =
typename std::vector<coord_type>::iterator;
811 using coord_const_iterator =
typename std::vector<coord_type>::const_iterator;
813 static const size_type N = 4;
837 : x(0), y(0), z(0), m(0) {}
840 : x(x), y(y), z(z), m(m) {}
844 assert(std::distance(begin, end) == N);
846 for (
auto it = begin; it != end; ++it)
852 explicit basic_point_zm(coord_const_iterator begin, coord_const_iterator end)
854 assert(std::distance(begin, end) == N);
856 for (
auto it = begin; it != end; ++it)
862 std::size_t size()
const noexcept
869 T& operator[](
size_t pos)
877 return lhs.x == rhs.x and lhs.y == rhs.y and lhs.z == rhs.z and lhs.m == rhs.m;
882 return not operator==(lhs, rhs);
885 size_type max_size()
const 892 return begin() == end();
907 const_iterator begin()
const 911 const_iterator end()
const 916 reverse_iterator rbegin()
918 return coords + N - 1;
921 reverse_iterator rend()
926 const_reverse_iterator rbegin()
const 928 return coords + N - 1;
931 const_reverse_iterator rend()
const 941 geometry_type geom_type_() const noexcept
943 return geometry_type::POINTZM;
947 bool is_closed_() const noexcept
953 void throw_for_invalid_()
const 972 auto j = nlohmann::json::parse(json);
973 auto geom_type = j.at(
"type").get<std::string>();
978 auto coords = j.at(
"coordinates").get<std::vector<double>>();
979 return {coords.at(0), coords.at(1), coords.at(2), coords.at(3)};
981 catch (
const std::out_of_range& e)
985 catch (
const nlohmann::json::exception& e)
996 std::string json_(std::int32_t precision = -1)
const 998 std::stringstream ss;
1001 ss << std::setprecision(precision);
1003 ss <<
"{\"type\":\"Point\",\"coordinates\":" 1004 <<
"[" << x <<
"," << y <<
"," << z <<
"," << m <<
"]}";
1014 auto result = reader.
read(wkt);
1015 auto data = result.
data;
1016 if (data.geom_type != geometry_type::POINTZM)
1020 return {data.coords[0], data.coords[1], data.coords[2], data.coords[3]};
1024 std::string wkt_(std::int32_t precision = -1)
const 1026 std::stringstream ss;
1029 ss << std::setprecision(precision);
1032 <<
"(" << x <<
" " << y <<
" " << z <<
" " << m <<
")";
1038 # pragma GCC diagnostic pop 1042 # pragma clang diagnostic pop 1046 # pragma warning(pop) 1053 template <
typename T>
1061 template <
typename T>
1069 template <
typename T>
1077 template <
typename T>
geometry_type geom_type() const noexcept
Returns the geometry type.
Base class for all geometries.
wkt_data data
the parser result data
wkt_result read(const std::string &wkt)
parse the given wkt string
Exception thrown when a geometry error is found.
const char * what() const noexceptoverride
Returns the exception reason.
Represents an axis-aligned bounding box.
basic_point(T x, T y)
Creates a point from the given coordinates.
basic_point()
Creates a empty point.
Exception thrown when an error has been found while parsing.
std::string json(std::int32_t precision=-1) const
Dumps the geojson representation of the geometry.
std::string wkt(std::int32_t precision=-1) const
Dumps the wkt representation of the geometry.
basic_point(coord_iterator begin, coord_iterator end)
basic_point(coord_const_iterator begin, coord_const_iterator end)
size_t ndim() const noexcept
Returns the number of dimensions of the geometry.