GEOS 3.15.0dev
CoordinateSequence.h
1/**********************************************************************
2 *
3 * GEOS - Geometry Engine Open Source
4 * http://geos.osgeo.org
5 *
6 * Copyright (C) 2022 ISciences LLC
7 * Copyright (C) 2006 Refractions Research Inc.
8 *
9 * This is free software; you can redistribute and/or modify it under
10 * the terms of the GNU Lesser General Public Licence as published
11 * by the Free Software Foundation.
12 * See the COPYING file for more information.
13 *
14 **********************************************************************/
15
16#pragma once
17
18#include <geos/export.h>
19
20#include <geos/geom/Coordinate.h> // for applyCoordinateFilter
21#include <geos/geom/CoordinateSequenceIterator.h>
22
23#include <cassert>
24#include <vector>
25#include <iostream>
26#include <iosfwd> // ostream
27#include <memory> // for unique_ptr typedef
28
29// Forward declarations
30namespace geos {
31namespace geom {
32class Envelope;
33class CoordinateFilter;
34}
35}
36
37namespace geos {
38namespace geom { // geos::geom
39
56class GEOS_DLL CoordinateSequence {
57
58public:
59
61 enum { X, Y, Z, M };
62
63 using iterator = CoordinateSequenceIterator<CoordinateSequence, Coordinate>;
64 using const_iterator = CoordinateSequenceIterator<const CoordinateSequence, const Coordinate>;
65
66 typedef std::unique_ptr<CoordinateSequence> Ptr;
67
70
75
83 CoordinateSequence(std::size_t size, std::size_t dim = 0);
84
96 CoordinateSequence(std::size_t size, bool hasz, bool hasm, bool initialize = true);
97
103 CoordinateSequence(const std::initializer_list<Coordinate>&);
104
109 CoordinateSequence(const std::initializer_list<CoordinateXY>&);
110
116 CoordinateSequence(const std::initializer_list<CoordinateXYM>&);
117
121 CoordinateSequence(const std::initializer_list<CoordinateXYZM>&);
122
128 static CoordinateSequence XY(std::size_t size) {
129 return CoordinateSequence(size, false, false);
130 }
131
137 static CoordinateSequence XYZ(std::size_t size) {
138 return CoordinateSequence(size, true, false);
139 }
140
146 static CoordinateSequence XYZM(std::size_t size) {
147 return CoordinateSequence(size, true, true);
148 }
149
155 static CoordinateSequence XYM(std::size_t size) {
156 return CoordinateSequence(size, false, true);
157 }
158
162 std::unique_ptr<CoordinateSequence> clone() const;
163
167
174
178 std::size_t getSize() const {
179 return size();
180 }
181
185 size_t size() const
186 {
187 assert(stride() == 2 || stride() == 3 || stride() == 4);
188 switch(stride()) {
189 case 2: return m_vect.size() / 2;
190 case 4: return m_vect.size() / 4;
191 default : return m_vect.size() / 3;
192 }
193 }
194
196 bool isEmpty() const {
197 return m_vect.empty();
198 }
199
206 bool isRing() const;
207
214 std::size_t getDimension() const;
215
216 bool hasZ() const {
217 return m_hasdim ? m_hasz : (m_vect.empty() || !std::isnan(m_vect[2]));
218 }
219
220 bool hasM() const {
221 return m_hasm;
222 }
223
225 bool hasRepeatedPoints() const;
226
229
233 CoordinateType getCoordinateType() const {
234 switch(stride()) {
235 case 4: return CoordinateType::XYZM;
236 case 2: return CoordinateType::XY;
237 default: return hasM() ? CoordinateType::XYM : CoordinateType::XYZ;
238 }
239 }
240
244
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()]);
254 return *orig;
255 }
256
260 template<typename T=Coordinate>
261 T& getAt(std::size_t i) {
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()]);
266 return *orig;
267 }
268
272 template<typename T>
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);
280 }
281 }
282
283 void getAt(std::size_t i, CoordinateXY& c) const {
284 c = getAt<CoordinateXY>(i);
285 }
286
287 // TODO: change to return CoordinateXY
291 const Coordinate& operator[](std::size_t i) const
292 {
293 return getAt(i);
294 }
295
296 // TODO: change to return CoordinateXY
301 operator[](std::size_t i)
302 {
303 return getAt(i);
304 }
305
316 double getOrdinate(std::size_t index, std::size_t ordinateIndex) const;
317
324 double getX(std::size_t index) const
325 {
326 return m_vect[index * stride()];
327 }
328
335 double getY(std::size_t index) const
336 {
337 return m_vect[index * stride() + 1];
338 }
339
341 template<typename T=Coordinate>
342 const T& back() const
343 {
344 return getAt<T>(size() - 1);
345 }
346
348 template<typename T=Coordinate>
349 T& back()
350 {
351 return getAt<T>(size() - 1);
352 }
353
355 template<typename T=Coordinate>
356 const T& front() const
357 {
358 return *(reinterpret_cast<const T*>(m_vect.data()));
359 }
360
362 template<typename T=Coordinate>
363 T& front()
364 {
365 return *(reinterpret_cast<T*>(m_vect.data()));
366 }
367
369 void toVector(std::vector<Coordinate>& coords) const;
370
371 void toVector(std::vector<CoordinateXY>& coords) const;
372
373
377
379 template<typename T>
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);
387 }
388 }
389
398 void setOrdinate(std::size_t index, std::size_t ordinateIndex, double value);
399
401 void setPoints(const std::vector<Coordinate>& v);
402
404 void setPoints(const std::vector<CoordinateXY>& v);
405
409
414 template<typename T=Coordinate>
415 void add(const T& c) {
416 add(c, size());
417 }
418
425 template<typename T>
426 void add(const T& c, bool allowRepeated)
427 {
428 if(!allowRepeated && !isEmpty()) {
429 const CoordinateXY& last = back<CoordinateXY>();
430 if(last.equals2D(c)) {
431 return;
432 }
433 }
434
435 add(c);
436 }
437
446 template<typename T>
447 void add(const T& c, std::size_t pos)
448 {
449 static_assert(std::is_base_of<CoordinateXY, T>::value, "Must be a Coordinate class");
450
451 // c may be a reference inside m_vect, so we make sure it will not
452 // grow before adding it
453 if (m_vect.size() + stride() <= m_vect.capacity()) {
454 make_space(pos, 1);
455 setAt(c, static_cast<std::size_t>(pos));
456 } else {
457 T tmp{c};
458 make_space(pos, 1);
459 setAt(tmp, static_cast<std::size_t>(pos));
460 }
461 }
462
472 template<typename T>
473 void add(std::size_t i, const T& coord, bool allowRepeated)
474 {
475 // don't add duplicate coordinates
476 if(! allowRepeated) {
477 std::size_t sz = size();
478 if(sz > 0) {
479 if(i > 0) {
480 const CoordinateXY& prev = getAt<CoordinateXY>(i - 1);
481 if(prev.equals2D(coord)) {
482 return;
483 }
484 }
485 if(i < sz) {
486 const CoordinateXY& next = getAt<CoordinateXY>(i);
487 if(next.equals2D(coord)) {
488 return;
489 }
490 }
491 }
492 }
493
494 add(coord, i);
495 }
496
497 void add(double x, double y) {
498 CoordinateXY c(x, y);
499 add(c);
500 }
501
502 void add(const CoordinateSequence& cs);
503
504 void add(const CoordinateSequence& cs, bool allowRepeated);
505
506 void add(const CoordinateSequence& cl, bool allowRepeated, bool forwardDirection);
507
508 void add(const CoordinateSequence& cs, std::size_t from, std::size_t to);
509
510 void add(const CoordinateSequence& cs, std::size_t from, std::size_t to, bool allowRepeated);
511
512 template<typename T, typename... Args>
513 void add(T begin, T end, Args... args) {
514 for (auto it = begin; it != end; ++it) {
515 add(*it, args...);
516 }
517 }
518
519 template<typename T>
520 void add(std::size_t i, T from, T to) {
521 auto npts = static_cast<std::size_t>(std::distance(from, to));
522 make_space(i, npts);
523
524 for (auto it = from; it != to; ++it) {
525 setAt(*it, i);
526 i++;
527 }
528 }
529
533
534 void clear() {
535 m_vect.clear();
536 }
537
538 void reserve(std::size_t capacity) {
539 m_vect.reserve(capacity * stride());
540 }
541
542 void resize(std::size_t capacity) {
543 m_vect.resize(capacity * stride());
544 }
545
546 void pop_back();
547
549 std::string toString() const;
550
552 const CoordinateXY* minCoordinate() const;
553
560
562 //
565 static std::size_t indexOf(const CoordinateXY* coordinate,
566 const CoordinateSequence* cl);
567
573 static bool equals(const CoordinateSequence* cl1,
574 const CoordinateSequence* cl2);
575
581 bool equalsIdentical(const CoordinateSequence& other) const;
582
584 static void scroll(CoordinateSequence* cl, const CoordinateXY* firstCoordinate);
585
604
606 void reverse();
607
608 void sort();
609
610
616 void expandEnvelope(Envelope& env) const;
617
618 void closeRing(bool allowRepeated = false);
619
623
624 template<typename Filter>
625 void apply_rw(const Filter* filter) {
626 switch(getCoordinateType()) {
627 case CoordinateType::XY:
628 for (auto& c : items<CoordinateXY>()) {
629 if (filter->isDone()) break;
630 filter->filter_rw(&c);
631 }
632 break;
633 case CoordinateType::XYZ:
634 for (auto& c : items<Coordinate>()) {
635 if (filter->isDone()) break;
636 filter->filter_rw(&c);
637 }
638 break;
639 case CoordinateType::XYM:
640 for (auto& c : items<CoordinateXYM>()) {
641 if (filter->isDone()) break;
642 filter->filter_rw(&c);
643 }
644 break;
645 case CoordinateType::XYZM:
646 for (auto& c : items<CoordinateXYZM>()) {
647 if (filter->isDone()) break;
648 filter->filter_rw(&c);
649 }
650 break;
651 }
652 m_hasdim = m_hasz = false; // re-check (see http://trac.osgeo.org/geos/ticket/435)
653 }
654
655 template<typename Filter>
656 void apply_ro(Filter* filter) const {
657 switch(getCoordinateType()) {
658 case CoordinateType::XY:
659 for (const auto& c : items<CoordinateXY>()) {
660 if (filter->isDone()) break;
661 filter->filter_ro(&c);
662 }
663 break;
664 case CoordinateType::XYZ:
665 for (const auto& c : items<Coordinate>()) {
666 if (filter->isDone()) break;
667 filter->filter_ro(&c);
668 }
669 break;
670 case CoordinateType::XYM:
671 for (const auto& c : items<CoordinateXYM>()) {
672 if (filter->isDone()) break;
673 filter->filter_ro(&c);
674 }
675 break;
676 case CoordinateType::XYZM:
677 for (const auto& c : items<CoordinateXYZM>()) {
678 if (filter->isDone()) break;
679 filter->filter_ro(&c);
680 }
681 break;
682 }
683 }
684
685 template<typename F>
686 auto applyAt(size_t i, F&& fun) const {
687 switch(getCoordinateType()) {
688 case CoordinateType::XYZ: return fun(getAt<Coordinate>(i));
689 case CoordinateType::XYM: return fun(getAt<CoordinateXYM>(i));
690 case CoordinateType::XYZM: return fun(getAt<CoordinateXYZM>(i));
691 default: return fun(getAt<CoordinateXY>(i));
692 }
693 }
694
695 template<typename F>
696 void forEach(F&& fun) const {
697 switch(getCoordinateType()) {
698 case CoordinateType::XY: for (const auto& c : items<CoordinateXY>()) { fun(c); } break;
699 case CoordinateType::XYZ: for (const auto& c : items<Coordinate>()) { fun(c); } break;
700 case CoordinateType::XYM: for (const auto& c : items<CoordinateXYM>()) { fun(c); } break;
701 case CoordinateType::XYZM: for (const auto& c : items<CoordinateXYZM>()) { fun(c); } break;
702 }
703 }
704
705 template<typename T, typename F>
706 void forEach(F&& fun) const
707 {
708 for (std::size_t i = 0; i < size(); i++) {
709 fun(getAt<T>(i));
710 }
711 }
712
713 template<typename T, typename F>
714 void forEach(std::size_t from, std::size_t to, F&& fun) const
715 {
716 for (std::size_t i = from; i <= to; i++) {
717 fun(getAt<T>(i));
718 }
719 }
720
721 template<typename T>
722 class Coordinates {
723 public:
724 using SequenceType = typename std::conditional<std::is_const<T>::value, const CoordinateSequence, CoordinateSequence>::type;
725
726 explicit Coordinates(SequenceType* seq) : m_seq(seq) {}
727
728 CoordinateSequenceIterator<SequenceType, T> begin() {
729 return {m_seq};
730 }
731
732 CoordinateSequenceIterator<SequenceType, T> end() {
733 return {m_seq, m_seq->getSize()};
734 }
735
736 CoordinateSequenceIterator<const SequenceType, typename std::add_const<T>::type>
737 begin() const {
738 return CoordinateSequenceIterator<const SequenceType, typename std::add_const<T>::type>{m_seq};
739 }
740
741 CoordinateSequenceIterator<const SequenceType, typename std::add_const<T>::type>
742 end() const {
743 return CoordinateSequenceIterator<const SequenceType, typename std::add_const<T>::type>{m_seq, m_seq->getSize()};
744 }
745
746 CoordinateSequenceIterator<const SequenceType, typename std::add_const<T>::type>
747 cbegin() const {
748 return CoordinateSequenceIterator<const SequenceType, typename std::add_const<T>::type>{m_seq};
749 }
750
751 CoordinateSequenceIterator<const SequenceType, typename std::add_const<T>::type>
752 cend() const {
753 return CoordinateSequenceIterator<const SequenceType, typename std::add_const<T>::type>{m_seq, m_seq->getSize()};
754 }
755
756 private:
757 SequenceType* m_seq;
758 };
759
760 template<typename T>
761 Coordinates<typename std::add_const<T>::type> items() const {
762 return Coordinates<typename std::add_const<T>::type>(this);
763 }
764
765 template<typename T>
766 Coordinates<T> items() {
767 return Coordinates<T>(this);
768 }
769
770
772
773 double* data() {
774 return m_vect.data();
775 }
776
777 const double* data() const {
778 return m_vect.data();
779 }
780
781private:
782 std::vector<double> m_vect; // Vector to store values
783
784 uint8_t m_stride; // Stride of stored values, corresponding to underlying type
785
786 mutable bool m_hasdim; // Has the dimension of this sequence been determined? Or was it created with no
787 // explicit dimensionality, and we're waiting for getDimension() to be called
788 // after some coordinates have been added?
789 mutable bool m_hasz;
790 bool m_hasm;
791
792 void initialize();
793
794 template<typename T1, typename T2>
795 void setAtImpl(const T2& c, std::size_t pos) {
796 auto& orig = getAt<T1>(pos);
797 orig = c;
798 }
799
800 void make_space(std::size_t pos, std::size_t n) {
801 m_vect.insert(std::next(m_vect.begin(), static_cast<std::ptrdiff_t>(pos * stride())),
802 m_stride * n,
803 DoubleNotANumber);
804 }
805
806 std::uint8_t stride() const {
807 return m_stride;
808 }
809
810};
811
812GEOS_DLL std::ostream& operator<< (std::ostream& os, const CoordinateSequence& cs);
813
814GEOS_DLL bool operator== (const CoordinateSequence& s1, const CoordinateSequence& s2);
815
816GEOS_DLL bool operator!= (const CoordinateSequence& s1, const CoordinateSequence& s2);
817
818} // namespace geos::geom
819} // namespace geos
820
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
T & getAt(std::size_t i)
Returns a reference to Coordinate at position i.
Definition CoordinateSequence.h:261
Coordinate & operator[](std::size_t i)
Definition CoordinateSequence.h:301
void toVector(std::vector< Coordinate > &coords) const
Pushes all Coordinates of this sequence into the provided vector.
double getOrdinate(std::size_t index, std::size_t ordinateIndex) const
const T & front() const
Return first Coordinate in the sequence.
Definition CoordinateSequence.h:356
T & back()
Return last Coordinate in the sequence.
Definition CoordinateSequence.h:349
void getAt(std::size_t i, T &c) const
Write Coordinate at position i to given Coordinate.
Definition CoordinateSequence.h:273
const T & getAt(std::size_t i) const
Returns a read-only reference to Coordinate at position i.
Definition CoordinateSequence.h:249
T & front()
Return first Coordinate in the sequence.
Definition CoordinateSequence.h:363
double getX(std::size_t index) const
Definition CoordinateSequence.h:324
double getY(std::size_t index) const
Definition CoordinateSequence.h:335
const Coordinate & operator[](std::size_t i) const
Definition CoordinateSequence.h:291
const T & back() const
Return last Coordinate in the sequence.
Definition CoordinateSequence.h:342
void add(const T &c, bool allowRepeated)
Definition CoordinateSequence.h:426
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:473
void add(const T &c)
Definition CoordinateSequence.h:415
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:447
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
void setPoints(const std::vector< CoordinateXY > &v)
Substitute Coordinate list with a copy of the given vector.
std::size_t getSize() const
Returns the number of Coordinates.
Definition CoordinateSequence.h:178
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.
static void scroll(CoordinateSequence *cl, const CoordinateXY *firstCoordinate)
Scroll given CoordinateSequence so to start with given Coordinate.
const CoordinateXY * minCoordinate() const
Returns lower-left Coordinate in list.
static int increasingDirection(const CoordinateSequence &pts)
Determines which orientation of the Coordinate array is (overall) increasing.
void expandEnvelope(Envelope &env) const
static CoordinateSequence * atLeastNCoordinatesOrNothing(std::size_t n, CoordinateSequence *c)
Returns either the given CoordinateSequence if its length is greater than the given amount,...
std::string toString() const
Get a string representation of CoordinateSequence.
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 geos.h:38