9 #include <simo/geom/detail/geometry.hpp> 10 #include <simo/geom/detail/utils.hpp> 11 #include <simo/geom/detail/point.hpp> 12 #include <simo/geom/detail/bounds.hpp> 19 template <
typename T,
typename AllocatorType = std::allocator<T>>
23 using base_type = std::vector<T, AllocatorType>;
25 using point_type =
typename T::point_type;
26 using point_iterator =
typename std::vector<T>::iterator;
27 using point_const_iterator =
typename std::vector<T>::const_iterator;
29 using coord_type =
typename T::coord_type;
30 using coord_iterator =
typename std::vector<coord_type>::iterator;
31 using coord_const_iterator =
typename std::vector<coord_type>::const_iterator;
37 : base_type(init.begin(), init.end()) {}
42 size_t n = this->
ndim();
43 this->reserve(std::distance(first, last));
44 for (
auto it = first; it != last; it += n)
46 this->emplace_back(it, it + n);
53 size_t n = this->
ndim();
54 this->reserve(std::distance(first, last));
55 for (
auto it = first; it != last; it += n)
57 this->emplace_back(it, it + n);
62 : base_type(first, last)
67 : base_type(first, last)
82 if (lhs.size() != rhs.size())
86 for (
size_t i = 0; i < lhs.size(); ++i)
112 std::vector<std::tuple<double, double>>
xy()
const 114 std::vector<std::tuple<double, double>> res;
115 res.reserve(this->size());
116 for (
const auto& p : *
this)
118 res.emplace_back(p.x, p.y);
137 auto coords = polyline::decode(polyline, precision);
138 return {coords.begin(), coords.end()};
148 std::string
polyline(std::int32_t precision = 5)
const 153 res.reserve(this->size() * 6);
156 for (
const auto& p : *
this)
158 res += polyline::encode(p.lat - prev_lat, precision);
159 res += polyline::encode(p.lng - prev_lng, precision);
170 geometry_type geom_type_()
const noexcept
174 return geometry_type::LINESTRINGZ;
178 return geometry_type::LINESTRINGM;
182 return geometry_type::LINESTRINGZM;
184 return geometry_type::LINESTRING;
188 bool is_closed_()
const noexcept
194 return *
this[0] == *
this[this->size() - 1];
198 void throw_for_invalid_()
const 205 if (this->size() < 2)
210 if (this->size() == 2)
212 if (*
this[0] == *
this[1])
223 for (
const auto& p : *
this)
237 auto j = nlohmann::json::parse(json);
238 auto geom_type = j.at(
"type").get<std::string>();
243 const auto& coords = j.at(
"coordinates").get<std::vector<std::vector<double>>>();
244 std::vector<point_type> res;
245 res.reserve(coords.size());
246 std::for_each(std::begin(coords), std::end(coords), [&](
const std::vector<double>& coord) {
247 res.emplace_back(coord.begin(), coord.end());
251 catch (
const nlohmann::json::exception& e)
262 std::string json_(std::int32_t precision = -1)
const 264 std::stringstream ss;
267 ss << std::setprecision(precision);
269 ss <<
"{\"type\":\"LineString\",\"coordinates\":[";
271 for (
const auto& p : *
this)
278 for (
size_t j = 0; j < p.size(); ++j)
299 auto result = reader.
read(wkt);
300 const auto& data = result.
data;
301 if (not utils::is_linestring(data.geom_type))
309 std::string wkt_(std::int32_t precision = -1)
const 311 std::stringstream ss;
314 ss << std::setprecision(precision);
327 for (
const auto& p : *
this)
333 for (
size_t j = 0; j < p.size(); ++j)
352 template <
typename T>
360 template <
typename T>
368 template <
typename T>
376 template <
typename T>
geometry_type geom_type() const noexcept
Returns the geometry type.
basic_linestring(coord_const_iterator first, coord_const_iterator last)
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.
std::vector< std::tuple< double, double > > xy() const
DOCUMENT ME!
const char * what() const noexceptoverride
Returns the exception reason.
std::string polyline(std::int32_t precision=5) const
DOCUMENT ME!
Represents an axis-aligned bounding box.
friend bool operator==(const basic_linestring< T > &lhs, const basic_linestring< T > &rhs)
bool has_z() const noexcept
Whether the geometry has the z-coordinate.
static basic_linestring< T > from_polyline(const std::string &polyline, std::int32_t precision=5)
DOCUMENT ME!
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.
friend bool operator!=(const basic_linestring< T > &lhs, const basic_linestring< T > &rhs)
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.
size_t ndim() const noexcept
Returns the number of dimensions of the geometry.
basic_linestring(coord_iterator first, coord_iterator last)