GEOS  3.11.0beta2
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 
25 #include <list>
26 #include <ostream> // for operator<<
27 #include <memory> // for unique_ptr
28 
29 #ifdef _MSC_VER
30 #pragma warning(push)
31 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
32 #endif
33 
34 // Forward declarations
35 namespace geos {
36 namespace geom {
37 //class Coordinate;
38 }
39 }
40 
41 
42 namespace geos {
43 namespace geom { // geos::geom
44 
54 class GEOS_DLL CoordinateList {
55 
56 public:
57 
58  typedef std::list<Coordinate>::iterator iterator;
59  typedef std::list<Coordinate>::const_iterator const_iterator;
60 
61  friend std::ostream& operator<< (std::ostream& os,
62  const CoordinateList& cl);
63 
73  CoordinateList(const std::vector<Coordinate>& v)
74  :
75  coords(v.begin(), v.end())
76  {
77  }
78 
80  :
81  coords()
82  {
83  }
84 
85  size_t
86  size() const
87  {
88  return coords.size();
89  }
90 
91  bool
92  empty() const
93  {
94  return coords.empty();
95  }
96 
97  iterator
98  begin()
99  {
100  return coords.begin();
101  }
102 
103  iterator
104  end()
105  {
106  return coords.end();
107  }
108 
109  const_iterator
110  begin() const
111  {
112  return coords.begin();
113  }
114 
115  const_iterator
116  end() const
117  {
118  return coords.end();
119  }
120 
134  iterator
135  insert(iterator pos, const Coordinate& c, bool allowRepeated)
136  {
137  if(!allowRepeated && pos != coords.begin()) {
138  iterator prev = pos;
139  --prev;
140  if(c.equals2D(*prev)) {
141  return prev;
142  }
143  }
144  return coords.insert(pos, c);
145  }
146 
147  iterator
148  add(const Coordinate& c, bool allowRepeated)
149  {
150  return insert(coords.end(), c, allowRepeated);
151  }
152 
153  iterator
154  insert(iterator pos, const Coordinate& c)
155  {
156  return coords.insert(pos, c);
157  }
158 
159  iterator
160  erase(iterator pos)
161  {
162  return coords.erase(pos);
163  }
164 
165  iterator
166  erase(iterator first, iterator last)
167  {
168  return coords.erase(first, last);
169  }
170 
171  std::unique_ptr<Coordinate::Vect>
172  toCoordinateArray() const
173  {
174  std::unique_ptr<Coordinate::Vect> ret(new Coordinate::Vect);
175  ret->assign(coords.begin(), coords.end());
176  return ret;
177  }
178  void
179  closeRing()
180  {
181  if(!coords.empty() && !(*(coords.begin())).equals(*(coords.rbegin()))) {
182  const Coordinate& c = *(coords.begin());
183  coords.insert(coords.end(), c);
184  }
185  }
186 
187  static void
188  closeRing(std::vector<Coordinate>& coords)
189  {
190  if(!coords.empty() && !(*(coords.begin())).equals(*(coords.rbegin()))) {
191  const Coordinate& c = *(coords.begin());
192  coords.insert(coords.end(), c);
193  }
194  }
195 
196 private:
197 
198  std::list<Coordinate> coords;
199 };
200 
201 inline
202 std::ostream&
203 operator<< (std::ostream& os, const CoordinateList& cl)
204 {
205  os << "(";
206  for(CoordinateList::const_iterator
207  it = cl.begin(), end = cl.end();
208  it != end;
209  ++it) {
210  const Coordinate& c = *it;
211  if(it != cl.begin()) {
212  os << ", ";
213  }
214  os << c;
215  }
216  os << ")";
217 
218  return os;
219 }
220 
221 } // namespace geos::geom
222 } // namespace geos
223 
224 #ifdef _MSC_VER
225 #pragma warning(pop)
226 #endif
227 
A list of Coordinates, which may be set to prevent repeated coordinates from occuring in the list.
Definition: CoordinateList.h:54
CoordinateList(const std::vector< Coordinate > &v)
Constructs a new list from an array of Coordinates, allowing repeated points.
Definition: CoordinateList.h:73
iterator insert(iterator pos, const Coordinate &c, bool allowRepeated)
Inserts the specified coordinate at the specified position in this list.
Definition: CoordinateList.h:135
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:58
std::vector< Coordinate > Vect
A vector of Coordinate objects (real object, not pointers)
Definition: Coordinate.h:75
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25