GEOS 3.14.0dev
CoordinateList.h
1/**********************************************************************
2 *
3 * GEOS - Geometry Engine Open Source
4 * http://geos.osgeo.org
5 *
6 * Copyright (C) 2010 Sandro Santilli <strk@kbt.io>
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 * Last port: geom/CoordinateList.java ?? (never been in complete sync)
17 *
18 **********************************************************************/
19
20#pragma once
21
22#include <geos/export.h>
23#include <geos/geom/Coordinate.h>
24#include <geos/geom/CoordinateSequence.h>
25#include <geos/util.h>
26
27#include <list>
28#include <ostream> // for operator<<
29#include <memory> // for unique_ptr
30
31#ifdef _MSC_VER
32#pragma warning(push)
33#pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
34#endif
35
36// Forward declarations
37namespace geos {
38namespace geom {
39//class Coordinate;
40}
41}
42
43
44namespace geos {
45namespace geom { // geos::geom
46
56class GEOS_DLL CoordinateList {
57
58public:
59
60 typedef std::list<Coordinate>::iterator iterator;
61 typedef std::list<Coordinate>::const_iterator const_iterator;
62
63 friend std::ostream& operator<< (std::ostream& os,
64 const CoordinateList& cl);
65
75 template<typename T>
76 CoordinateList(const T& v)
77 :
78 coords(v.begin(), v.end())
79 {
80 }
81
83 : CoordinateList(v.items<Coordinate>())
84 {
85 }
86
87 CoordinateList()
88 :
89 coords()
90 {
91 }
92
93 size_t
94 size() const
95 {
96 return coords.size();
97 }
98
99 bool
100 empty() const
101 {
102 return coords.empty();
103 }
104
105 iterator
106 begin()
107 {
108 return coords.begin();
109 }
110
111 iterator
112 end()
113 {
114 return coords.end();
115 }
116
117 const_iterator
118 begin() const
119 {
120 return coords.begin();
121 }
122
123 const_iterator
124 end() const
125 {
126 return coords.end();
127 }
128
142 iterator
143 insert(iterator pos, const Coordinate& c, bool allowRepeated)
144 {
145 if(!allowRepeated && pos != coords.begin()) {
146 iterator prev = pos;
147 --prev;
148 if(c.equals2D(*prev)) {
149 return prev;
150 }
151 }
152 return coords.insert(pos, c);
153 }
154
155 iterator
156 add(const Coordinate& c, bool allowRepeated)
157 {
158 return insert(coords.end(), c, allowRepeated);
159 }
160
161 iterator
162 insert(iterator pos, const Coordinate& c)
163 {
164 return coords.insert(pos, c);
165 }
166
167 iterator
168 erase(iterator pos)
169 {
170 return coords.erase(pos);
171 }
172
173 iterator
174 erase(iterator first, iterator last)
175 {
176 return coords.erase(first, last);
177 }
178
179 std::unique_ptr<Coordinate::Vect>
180 toCoordinateArray() const
181 {
182 std::unique_ptr<Coordinate::Vect> ret(new Coordinate::Vect);
183 ret->assign(coords.begin(), coords.end());
184 return ret;
185 }
186
187 std::unique_ptr<geom::CoordinateSequence>
188 toCoordinateSequence() const
189 {
190 auto ret = detail::make_unique<geom::CoordinateSequence>();
191 ret->add(begin(), end());
192 return ret;
193 }
194
195 void
196 closeRing()
197 {
198 if(!coords.empty() && !(*(coords.begin())).equals(*(coords.rbegin()))) {
199 const Coordinate& c = *(coords.begin());
200 coords.insert(coords.end(), c);
201 }
202 }
203
204 static void
205 closeRing(std::vector<Coordinate>& coords)
206 {
207 if(!coords.empty() && !(*(coords.begin())).equals(*(coords.rbegin()))) {
208 const Coordinate& c = *(coords.begin());
209 coords.insert(coords.end(), c);
210 }
211 }
212
213private:
214
215 std::list<Coordinate> coords;
216};
217
218inline
219std::ostream&
220operator<< (std::ostream& os, const CoordinateList& cl)
221{
222 os << "(";
223 for(CoordinateList::const_iterator
224 it = cl.begin(), end = cl.end();
225 it != end;
226 ++it) {
227 const Coordinate& c = *it;
228 if(it != cl.begin()) {
229 os << ", ";
230 }
231 os << c;
232 }
233 os << ")";
234
235 return os;
236}
237
238} // namespace geos::geom
239} // namespace geos
240
241#ifdef _MSC_VER
242#pragma warning(pop)
243#endif
244
A list of Coordinates, which may be set to prevent repeated coordinates from occurring in the list.
Definition CoordinateList.h:56
iterator insert(iterator pos, const Coordinate &c, bool allowRepeated)
Inserts the specified coordinate at the specified position in this list.
Definition CoordinateList.h:143
CoordinateList(const T &v)
Constructs a new list from an array of Coordinates, allowing repeated points.
Definition CoordinateList.h:76
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
Basic namespace for all GEOS functionalities.
Definition geos.h:39