18 #include <geos/export.h>
20 #include <geos/geom/Coordinate.h>
21 #include <geos/geom/CoordinateSequenceIterator.h>
33 class CoordinateFilter;
63 using iterator = CoordinateSequenceIterator<CoordinateSequence, Coordinate>;
64 using const_iterator = CoordinateSequenceIterator<const CoordinateSequence, const Coordinate>;
66 typedef std::unique_ptr<CoordinateSequence> Ptr;
162 std::unique_ptr<CoordinateSequence>
clone()
const;
187 assert(stride() == 2 || stride() == 3 || stride() == 4);
189 case 2:
return m_vect.size() / 2;
190 case 4:
return m_vect.size() / 4;
191 default :
return m_vect.size() / 3;
197 return m_vect.empty();
217 return m_hasdim ? m_hasz : (m_vect.empty() || !std::isnan(m_vect[2]));
235 case 4:
return CoordinateType::XYZM;
236 case 2:
return CoordinateType::XY;
237 default:
return hasM() ? CoordinateType::XYM : CoordinateType::XYZ;
248 template<
typename T=Coordinate>
249 const T&
getAt(std::size_t i)
const {
250 static_assert(std::is_base_of<CoordinateXY, T>::value,
"Must be a Coordinate class");
251 assert(
sizeof(T) <=
sizeof(
double) * stride());
252 assert(i*stride() < m_vect.size());
253 const T* orig =
reinterpret_cast<const T*
>(&m_vect[i*stride()]);
260 template<
typename T=Coordinate>
262 static_assert(std::is_base_of<CoordinateXY, T>::value,
"Must be a Coordinate class");
263 assert(
sizeof(T) <=
sizeof(
double) * stride());
264 assert(i*stride() < m_vect.size());
265 T* orig =
reinterpret_cast<T*
>(&m_vect[i*stride()]);
273 void getAt(std::size_t i, T& c)
const {
274 switch(getCoordinateType()) {
275 case CoordinateType::XY: c = getAt<CoordinateXY>(i);
break;
276 case CoordinateType::XYZ: c = getAt<Coordinate>(i);
break;
277 case CoordinateType::XYZM: c = getAt<CoordinateXYZM>(i);
break;
278 case CoordinateType::XYM: c = getAt<CoordinateXYM>(i);
break;
279 default: getAt<Coordinate>(i);
283 void getAt(std::size_t i, CoordinateXY& c)
const {
284 c = getAt<CoordinateXY>(i);
316 double getOrdinate(std::size_t index, std::size_t ordinateIndex)
const;
324 double getX(std::size_t index)
const
326 return m_vect[index * stride()];
335 double getY(std::size_t index)
const
337 return m_vect[index * stride() + 1];
341 template<
typename T=Coordinate>
344 return getAt<T>(size() - 1);
348 template<
typename T=Coordinate>
351 return getAt<T>(size() - 1);
355 template<
typename T=Coordinate>
358 return *(
reinterpret_cast<const T*
>(m_vect.data()));
362 template<
typename T=Coordinate>
365 return *(
reinterpret_cast<T*
>(m_vect.data()));
369 void toVector(std::vector<Coordinate>& coords)
const;
371 void toVector(std::vector<CoordinateXY>& coords)
const;
380 void setAt(
const T& c, std::size_t pos) {
381 switch(getCoordinateType()) {
382 case CoordinateType::XY: setAtImpl<CoordinateXY>(c, pos);
break;
383 case CoordinateType::XYZ: setAtImpl<Coordinate>(c, pos);
break;
384 case CoordinateType::XYZM: setAtImpl<CoordinateXYZM>(c, pos);
break;
385 case CoordinateType::XYM: setAtImpl<CoordinateXYM>(c, pos);
break;
386 default: setAtImpl<Coordinate>(c, pos);
398 void setOrdinate(std::size_t index, std::size_t ordinateIndex,
double value);
411 template<
typename T=Coordinate>
423 void add(
const T& c,
bool allowRepeated)
425 if(!allowRepeated && !isEmpty()) {
426 const CoordinateXY& last = back<CoordinateXY>();
427 if(last.equals2D(c)) {
444 void add(
const T& c, std::size_t pos)
446 static_assert(std::is_base_of<CoordinateXY, T>::value,
"Must be a Coordinate class");
450 if (m_vect.size() + stride() <= m_vect.capacity()) {
452 setAt(c,
static_cast<std::size_t
>(pos));
456 setAt(tmp,
static_cast<std::size_t
>(pos));
470 void add(std::size_t i,
const T& coord,
bool allowRepeated)
473 if(! allowRepeated) {
474 std::size_t sz = size();
477 const CoordinateXY& prev = getAt<CoordinateXY>(i - 1);
478 if(prev.equals2D(coord)) {
483 const CoordinateXY& next = getAt<CoordinateXY>(i);
484 if(next.equals2D(coord)) {
494 void add(
double x,
double y) {
495 CoordinateXY c(x, y);
499 void add(
const CoordinateSequence& cs);
501 void add(
const CoordinateSequence& cs,
bool allowRepeated);
503 void add(
const CoordinateSequence& cl,
bool allowRepeated,
bool forwardDirection);
505 void add(
const CoordinateSequence& cs, std::size_t from, std::size_t to);
507 void add(
const CoordinateSequence& cs, std::size_t from, std::size_t to,
bool allowRepeated);
509 template<
typename T,
typename... Args>
510 void add(T begin, T end, Args... args) {
511 for (
auto it = begin; it != end; ++it) {
517 void add(std::size_t i, T from, T to) {
518 auto npts =
static_cast<std::size_t
>(std::distance(from, to));
521 for (
auto it = from; it != to; ++it) {
535 void reserve(std::size_t capacity) {
536 m_vect.reserve(capacity * stride());
539 void resize(std::size_t capacity) {
540 m_vect.resize(capacity * stride());
562 static std::size_t
indexOf(
const CoordinateXY* coordinate,
615 void closeRing(
bool allowRepeated =
false);
621 template<
typename Filter>
622 void apply_rw(
const Filter* filter) {
623 switch(getCoordinateType()) {
624 case CoordinateType::XY:
625 for (
auto& c : items<CoordinateXY>()) {
626 if (filter->isDone())
break;
627 filter->filter_rw(&c);
630 case CoordinateType::XYZ:
631 for (
auto& c : items<Coordinate>()) {
632 if (filter->isDone())
break;
633 filter->filter_rw(&c);
636 case CoordinateType::XYM:
637 for (
auto& c : items<CoordinateXYM>()) {
638 if (filter->isDone())
break;
639 filter->filter_rw(&c);
642 case CoordinateType::XYZM:
643 for (
auto& c : items<CoordinateXYZM>()) {
644 if (filter->isDone())
break;
645 filter->filter_rw(&c);
649 m_hasdim = m_hasz =
false;
652 template<
typename Filter>
653 void apply_ro(Filter* filter)
const {
654 switch(getCoordinateType()) {
655 case CoordinateType::XY:
656 for (
const auto& c : items<CoordinateXY>()) {
657 if (filter->isDone())
break;
658 filter->filter_ro(&c);
661 case CoordinateType::XYZ:
662 for (
const auto& c : items<Coordinate>()) {
663 if (filter->isDone())
break;
664 filter->filter_ro(&c);
667 case CoordinateType::XYM:
668 for (
const auto& c : items<CoordinateXYM>()) {
669 if (filter->isDone())
break;
670 filter->filter_ro(&c);
673 case CoordinateType::XYZM:
674 for (
const auto& c : items<CoordinateXYZM>()) {
675 if (filter->isDone())
break;
676 filter->filter_ro(&c);
683 auto applyAt(
size_t i, F&& fun)
const {
684 switch(getCoordinateType()) {
685 case CoordinateType::XYZ:
return fun(getAt<Coordinate>(i));
686 case CoordinateType::XYM:
return fun(getAt<CoordinateXYM>(i));
687 case CoordinateType::XYZM:
return fun(getAt<CoordinateXYZM>(i));
688 default:
return fun(getAt<CoordinateXY>(i));
693 void forEach(F&& fun)
const {
694 switch(getCoordinateType()) {
695 case CoordinateType::XY:
for (
const auto& c : items<CoordinateXY>()) { fun(c); }
break;
696 case CoordinateType::XYZ:
for (
const auto& c : items<Coordinate>()) { fun(c); }
break;
697 case CoordinateType::XYM:
for (
const auto& c : items<CoordinateXYM>()) { fun(c); }
break;
698 case CoordinateType::XYZM:
for (
const auto& c : items<CoordinateXYZM>()) { fun(c); }
break;
702 template<
typename T,
typename F>
703 void forEach(F&& fun)
const
705 for (std::size_t i = 0; i < size(); i++) {
710 template<
typename T,
typename F>
711 void forEach(std::size_t from, std::size_t to, F&& fun)
const
713 for (std::size_t i = from; i <= to; i++) {
721 using SequenceType =
typename std::conditional<std::is_const<T>::value,
const CoordinateSequence, CoordinateSequence>::type;
723 explicit Coordinates(SequenceType* seq) : m_seq(seq) {}
725 CoordinateSequenceIterator<SequenceType, T> begin() {
729 CoordinateSequenceIterator<SequenceType, T> end() {
730 return {m_seq, m_seq->getSize()};
733 CoordinateSequenceIterator<const SequenceType, typename std::add_const<T>::type>
735 return CoordinateSequenceIterator<const SequenceType, typename std::add_const<T>::type>{m_seq};
738 CoordinateSequenceIterator<const SequenceType, typename std::add_const<T>::type>
740 return CoordinateSequenceIterator<const SequenceType, typename std::add_const<T>::type>{m_seq, m_seq->getSize()};
743 CoordinateSequenceIterator<const SequenceType, typename std::add_const<T>::type>
745 return CoordinateSequenceIterator<const SequenceType, typename std::add_const<T>::type>{m_seq};
748 CoordinateSequenceIterator<const SequenceType, typename std::add_const<T>::type>
750 return CoordinateSequenceIterator<const SequenceType, typename std::add_const<T>::type>{m_seq, m_seq->getSize()};
758 Coordinates<typename std::add_const<T>::type> items()
const {
759 return Coordinates<typename std::add_const<T>::type>(
this);
763 Coordinates<T> items() {
764 return Coordinates<T>(
this);
771 return m_vect.data();
774 const double* data()
const {
775 return m_vect.data();
779 std::vector<double> m_vect;
783 mutable bool m_hasdim;
791 template<
typename T1,
typename T2>
792 void setAtImpl(
const T2& c, std::size_t pos) {
793 auto& orig = getAt<T1>(pos);
797 void make_space(std::size_t pos, std::size_t n) {
798 m_vect.insert(std::next(m_vect.begin(),
static_cast<std::ptrdiff_t
>(pos * stride())),
803 std::uint8_t stride()
const {
809 GEOS_DLL std::ostream& operator<< (std::ostream& os,
const CoordinateSequence& cs);
811 GEOS_DLL
bool operator== (
const CoordinateSequence& s1,
const CoordinateSequence& s2);
813 GEOS_DLL
bool operator!= (
const CoordinateSequence& s1,
const CoordinateSequence& s2);
The internal representation of a list of coordinates inside a Geometry.
Definition: CoordinateSequence.h:56
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:217
An Envelope defines a rectangulare region of the 2D coordinate plane.
Definition: Envelope.h:59
void toVector(std::vector< Coordinate > &coords) const
Pushes all Coordinates of this sequence into the provided vector.
T & getAt(std::size_t i)
Returns a reference to Coordinate at position i.
Definition: CoordinateSequence.h:261
T & back()
Return last Coordinate in the sequence.
Definition: CoordinateSequence.h:349
double getOrdinate(std::size_t index, std::size_t ordinateIndex) const
T & front()
Return first Coordinate in the sequence.
Definition: CoordinateSequence.h:363
const T & front() const
Return first Coordinate in the sequence.
Definition: CoordinateSequence.h:356
void getAt(std::size_t i, T &c) const
Write Coordinate at position i to given Coordinate.
Definition: CoordinateSequence.h:273
const T & back() const
Return last Coordinate in the sequence.
Definition: CoordinateSequence.h:342
const Coordinate & operator[](std::size_t i) const
Definition: CoordinateSequence.h:291
const T & getAt(std::size_t i) const
Returns a read-only reference to Coordinate at position i.
Definition: CoordinateSequence.h:249
double getX(std::size_t index) const
Definition: CoordinateSequence.h:324
Coordinate & operator[](std::size_t i)
Definition: CoordinateSequence.h:301
double getY(std::size_t index) const
Definition: CoordinateSequence.h:335
void add(const T &c, bool allowRepeated)
Definition: CoordinateSequence.h:423
void add(std::size_t i, const T &coord, bool allowRepeated)
Inserts the specified coordinate at the specified position in this list.
Definition: CoordinateSequence.h:470
void add(const T &c)
Definition: CoordinateSequence.h:412
void add(const T &c, std::size_t pos)
Inserts the specified coordinate at the specified position in this sequence. If multiple coordinates ...
Definition: CoordinateSequence.h:444
CoordinateSequence(const std::initializer_list< CoordinateXYZM > &)
static CoordinateSequence XYM(std::size_t size)
Definition: CoordinateSequence.h:155
CoordinateSequence(const std::initializer_list< CoordinateXY > &)
std::unique_ptr< CoordinateSequence > clone() const
Returns a heap-allocated deep copy of this CoordinateSequence.
CoordinateSequence(std::size_t size, bool hasz, bool hasm, bool initialize=true)
static CoordinateSequence XYZM(std::size_t size)
Definition: CoordinateSequence.h:146
static CoordinateSequence XYZ(std::size_t size)
Definition: CoordinateSequence.h:137
static CoordinateSequence XY(std::size_t size)
Definition: CoordinateSequence.h:128
CoordinateSequence(std::size_t size, std::size_t dim=0)
CoordinateSequence(const std::initializer_list< CoordinateXYM > &)
CoordinateSequence(const std::initializer_list< Coordinate > &)
void setOrdinate(std::size_t index, std::size_t ordinateIndex, double value)
void setPoints(const std::vector< Coordinate > &v)
Substitute Coordinate list with a copy of the given vector.
void setAt(const T &c, std::size_t pos)
Copy Coordinate c to position pos.
Definition: CoordinateSequence.h:380
std::size_t getSize() const
Returns the number of Coordinates.
Definition: CoordinateSequence.h:178
Envelope getEnvelope() const
bool hasRepeatedOrInvalidPoints() const
Returns true if contains any NaN/Inf coordinates.
size_t size() const
Returns the number of Coordinates.
Definition: CoordinateSequence.h:185
bool hasRepeatedPoints() const
Returns true if contains any two consecutive points.
CoordinateType getCoordinateType() const
Definition: CoordinateSequence.h:233
bool isEmpty() const
Returns true if list contains no coordinates.
Definition: CoordinateSequence.h:196
bool isRing() const
Tests whether an a CoordinateSequence forms a ring, by checking length and closure....
std::size_t getDimension() const
bool equalsIdentical(const CoordinateSequence &other) const
Returns true if the two sequences are identical (pointwise equal in all dimensions,...
void reverse()
Reverse Coordinate order in given CoordinateSequence.
const CoordinateXY * minCoordinate() const
Returns lower-left Coordinate in list.
static void scroll(CoordinateSequence *cl, const CoordinateXY *firstCoordinate)
Scroll given CoordinateSequence so to start with given Coordinate.
static int increasingDirection(const CoordinateSequence &pts)
Determines which orientation of the Coordinate array is (overall) increasing.
void expandEnvelope(Envelope &env) const
std::string toString() const
Get a string representation of CoordinateSequence.
static CoordinateSequence * atLeastNCoordinatesOrNothing(std::size_t n, CoordinateSequence *c)
Returns either the given CoordinateSequence if its length is greater than the given amount,...
static std::size_t indexOf(const CoordinateXY *coordinate, const CoordinateSequence *cl)
Return position of a Coordinate.
static bool equals(const CoordinateSequence *cl1, const CoordinateSequence *cl2)
Returns true if the two arrays are identical, both null, or pointwise equal in two dimensions.
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25