9 #include <simo/geom/geometry.hpp> 10 #include <simo/geom/linearring.hpp> 11 #include <simo/geom/detail/bounds.hpp> 18 template <
typename T,
typename AllocatorType = std::allocator<T>>
22 using base_type = std::vector<T, AllocatorType>;
24 using point_type =
typename T::point_type;
25 using point_iterator =
typename std::vector<T>::iterator;
26 using point_const_iterator =
typename std::vector<T>::const_iterator;
28 using coord_type =
typename T::coord_type;
29 using coord_iterator =
typename std::vector<coord_type>::iterator;
30 using coord_const_iterator =
typename std::vector<coord_type>::const_iterator;
36 : base_type(first, last)
40 basic_polygon(point_const_iterator first, point_const_iterator last)
41 : base_type(first, last)
46 : base_type(init.begin(), init.end()) {}
49 explicit basic_polygon(coord_const_iterator first, coord_const_iterator last)
52 size_t n = this->
ndim();
53 this->reserve(std::distance(first, last));
54 for (
auto it = first; it != last; it += n)
56 this->emplace_back(it, it + n);
63 size_t n = this->
ndim();
64 this->reserve(std::distance(first, last));
65 for (
auto it = first; it != last; it += n)
67 this->emplace_back(it, it + n);
71 template <
typename CoordIterator,
typename OffsetIterator>
72 basic_polygon(CoordIterator coord_first, CoordIterator coord_last, OffsetIterator offset_first, OffsetIterator offset_last)
74 if (std::distance(coord_first, coord_last) > 0)
76 auto n = this->
ndim();
77 this->reserve((coord_last - coord_first) / n);
79 for (
auto it = offset_first; it != offset_last; ++it)
82 this->emplace_back(coord_first + lo, coord_first + hi);
92 if (lhs.size() != rhs.size())
96 for (
size_t i = 0; i < lhs.size(); ++i)
108 return not operator==(lhs, rhs);
111 std::vector<std::tuple<double, double>> xy()
const 113 std::vector<std::tuple<double, double>> res;
114 res.reserve(this->size());
115 for (
const auto& p : *
this)
117 res.emplace_back(p.x, p.y);
124 return *this->begin();
127 T& interiors(
size_t pos)
129 assert(this->begin() + pos + 1 < this->end());
130 return *(this->begin() + pos + 1);
138 geometry_type geom_type_() const noexcept
142 return geometry_type::POLYGONZ;
146 return geometry_type::POLYGONM;
150 return geometry_type::POLYGONZM;
152 return geometry_type::POLYGON;
156 bool is_closed_() const noexcept
162 return *
this[0] == *
this[this->size() - 1];
166 void throw_for_invalid_()
const 175 for (
const auto& r : *
this)
177 auto r_bound = r.bounds();
184 std::string tagged_text_() const noexcept
196 auto j = nlohmann::json::parse(json);
197 auto geom_type = j.at(
"type").get<std::string>();
202 const auto& rings = j.at(
"coordinates");
204 res.reserve(rings.size());
205 std::vector<point_type> points;
206 for (
const auto& ring : rings)
208 if (not ring.empty())
210 const auto& coords = ring.get<std::vector<std::vector<double>>>();
211 points.reserve(coords.size());
212 std::for_each(std::begin(coords), std::end(coords),
213 [&points](
const std::vector<double>& coord) {
214 points.emplace_back(coord.begin(), coord.end());
216 res.emplace_back(points.begin(), points.end());
222 catch (
const nlohmann::json::exception& e)
233 std::string json_(std::int32_t precision = -1)
const 235 std::stringstream ss;
238 ss << std::setprecision(precision);
240 ss <<
"{\"type\":\"Polygon\",\"coordinates\":[";
242 for (
const auto& ls : *
this)
249 for (
size_t j = 0; j < ls.size(); ++j)
256 const auto& p = ls[j];
257 for (
size_t k = 0; k < p.size(); ++k)
281 auto result = reader.
read(wkt);
282 const auto& data = result.
data;
283 if (not utils::is_polygon(data.geom_type))
287 return basic_polygon<T>(result.data.coords.begin(), result.data.coords.end(), result.data.offsets.begin(),
288 result.data.offsets.end());
292 std::string wkt_(std::int32_t precision = -1)
const 294 std::stringstream ss;
297 ss << std::setprecision(precision);
310 for (
const auto& ls : *
this)
317 for (
size_t j = 0; j < ls.size(); ++j)
323 const auto& p = ls[j];
324 for (
size_t k = 0; k < p.size(); ++k)
345 template <
typename T>
353 template <
typename T>
361 template <
typename T>
369 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.
bool has_z() const noexcept
Whether the geometry has the z-coordinate.
bounds_t & extend(double x, double y)
Extends the bounds to contain the given 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.
bool has_m() const noexcept
Whether the geometry has the m-coordinate (measurement coordinate)
std::string wkt(std::int32_t precision=-1) const
Dumps the wkt representation of the geometry.
basic_polygon(coord_const_iterator first, coord_const_iterator last)
size_t ndim() const noexcept
Returns the number of dimensions of the geometry.
basic_polygon(coord_iterator first, coord_iterator last)