Marble

GeoDataMultiGeometry.cpp
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 //
3 // SPDX-FileCopyrightText: 2008 Patrick Spendrin <[email protected]>
4 //
5 
6 
7 #include "GeoDataMultiGeometry.h"
8 #include "GeoDataMultiGeometry_p.h"
9 
10 #include "GeoDataLineString.h"
11 #include "GeoDataLinearRing.h"
12 #include "GeoDataPoint.h"
13 #include "GeoDataPolygon.h"
14 #include "GeoDataTypes.h"
15 
16 #include "MarbleDebug.h"
17 
18 #include <QDataStream>
19 
20 
21 namespace Marble
22 {
23 
24 GeoDataMultiGeometry::GeoDataMultiGeometry()
25  : GeoDataGeometry( new GeoDataMultiGeometryPrivate )
26 {
27 }
28 
29 GeoDataMultiGeometry::GeoDataMultiGeometry( const GeoDataGeometry& other )
30  : GeoDataGeometry( other )
31 {
32 }
33 
34 GeoDataMultiGeometry::~GeoDataMultiGeometry()
35 {
36 }
37 
38 const char *GeoDataMultiGeometry::nodeType() const
39 {
40  return GeoDataTypes::GeoDataMultiGeometryType;
41 }
42 
43 EnumGeometryId GeoDataMultiGeometry::geometryId() const
44 {
45  return GeoDataMultiGeometryId;
46 }
47 
48 GeoDataGeometry *GeoDataMultiGeometry::copy() const
49 {
50  return new GeoDataMultiGeometry(*this);
51 }
52 
53 bool GeoDataMultiGeometry::operator==(const GeoDataMultiGeometry &other) const
54 {
55  Q_D(const GeoDataMultiGeometry);
56  const GeoDataMultiGeometryPrivate *const other_d = other.d_func();
57  QVector<GeoDataGeometry*>::const_iterator thisBegin = d->m_vector.constBegin();
59  QVector<GeoDataGeometry*>::const_iterator otherBegin = other_d->m_vector.constBegin();
60  QVector<GeoDataGeometry*>::const_iterator otherEnd = other_d->m_vector.constEnd();
61 
62  for (; thisBegin != thisEnd && otherBegin != otherEnd; ++thisBegin, ++otherBegin) {
63  if (**thisBegin != **otherBegin) {
64  return false;
65  }
66  }
67 
68  return true;
69 }
70 
71 const GeoDataLatLonAltBox& GeoDataMultiGeometry::latLonAltBox() const
72 {
73  Q_D(const GeoDataMultiGeometry);
74 
76  QVector<GeoDataGeometry*>::const_iterator end = d->m_vector.constEnd();
77 
78  d->m_latLonAltBox.clear();
79  for (; it != end; ++it) {
80  if ( !(*it)->latLonAltBox().isEmpty() ) {
81  if ( d->m_latLonAltBox.isEmpty() ) {
82  d->m_latLonAltBox = (*it)->latLonAltBox();
83  }
84  else {
85  d->m_latLonAltBox |= (*it)->latLonAltBox();
86  }
87  }
88  }
89  return d->m_latLonAltBox;
90 }
91 
92 int GeoDataMultiGeometry::size() const
93 {
94  Q_D(const GeoDataMultiGeometry);
95  return d->m_vector.size();
96 }
97 
98 QVector<GeoDataGeometry *> GeoDataMultiGeometry::vector()
99 {
100  Q_D(const GeoDataMultiGeometry);
101 
102  return d->m_vector;
103 }
104 
105 GeoDataGeometry& GeoDataMultiGeometry::at( int pos )
106 {
107  mDebug() << "detaching!";
108  detach();
109 
110  Q_D(GeoDataMultiGeometry);
111  return *(d->m_vector[pos]);
112 }
113 
114 const GeoDataGeometry& GeoDataMultiGeometry::at( int pos ) const
115 {
116  Q_D(const GeoDataMultiGeometry);
117  return *(d->m_vector.at(pos));
118 }
119 
120 GeoDataGeometry& GeoDataMultiGeometry::operator[]( int pos )
121 {
122  detach();
123 
124  Q_D(GeoDataMultiGeometry);
125  return *(d->m_vector[pos]);
126 }
127 
128 const GeoDataGeometry& GeoDataMultiGeometry::operator[]( int pos ) const
129 {
130  Q_D(const GeoDataMultiGeometry);
131  return *(d->m_vector[pos]);
132 }
133 
134 GeoDataGeometry& GeoDataMultiGeometry::last()
135 {
136  detach();
137 
138  Q_D(GeoDataMultiGeometry);
139  return *(d->m_vector.last());
140 }
141 
142 GeoDataGeometry& GeoDataMultiGeometry::first()
143 {
144  detach();
145 
146  Q_D(GeoDataMultiGeometry);
147  return *(d->m_vector.first());
148 }
149 
150 const GeoDataGeometry& GeoDataMultiGeometry::last() const
151 {
152  Q_D(const GeoDataMultiGeometry);
153  return *(d->m_vector.last());
154 }
155 
156 const GeoDataGeometry& GeoDataMultiGeometry::first() const
157 {
158  Q_D(const GeoDataMultiGeometry);
159  return *(d->m_vector.first());
160 }
161 
162 QVector<GeoDataGeometry*>::Iterator GeoDataMultiGeometry::begin()
163 {
164  detach();
165 
166  Q_D(GeoDataMultiGeometry);
167  return d->m_vector.begin();
168 }
169 
170 QVector<GeoDataGeometry*>::Iterator GeoDataMultiGeometry::end()
171 {
172  detach();
173 
174  Q_D(GeoDataMultiGeometry);
175  return d->m_vector.end();
176 }
177 
178 QVector<GeoDataGeometry*>::ConstIterator GeoDataMultiGeometry::constBegin() const
179 {
180  Q_D(const GeoDataMultiGeometry);
181  return d->m_vector.constBegin();
182 }
183 
184 QVector<GeoDataGeometry*>::ConstIterator GeoDataMultiGeometry::constEnd() const
185 {
186  Q_D(const GeoDataMultiGeometry);
187  return d->m_vector.constEnd();
188 }
189 
190 /**
191  * @brief returns the requested child item
192  */
194 {
195  detach();
196 
198  return d->m_vector.at(i);
199 }
200 
202 {
203  Q_D(const GeoDataMultiGeometry);
204  return d->m_vector.at(i);
205 }
206 
207 /**
208  * @brief returns the position of an item in the list
209  */
211 {
212  Q_D(const GeoDataMultiGeometry);
213  for (int i = 0; i < d->m_vector.size(); ++i) {
214  if (d->m_vector.at(i) == object) {
215  return i;
216  }
217  }
218  return -1;
219 }
220 
221 /**
222 * @brief add an element
223 */
225 {
226  detach();
227 
229  other->setParent( this );
230  d->m_vector.append(other);
231 }
232 
233 
234 GeoDataMultiGeometry& GeoDataMultiGeometry::operator << ( const GeoDataGeometry& value )
235 {
236  detach();
237 
239  GeoDataGeometry *g = value.copy();
240  g->setParent( this );
241  d->m_vector.append(g);
242  return *this;
243 }
244 
245 void GeoDataMultiGeometry::clear()
246 {
247  detach();
248 
249  Q_D(GeoDataMultiGeometry);
250  qDeleteAll(d->m_vector);
251  d->m_vector.clear();
252 }
253 
254 void GeoDataMultiGeometry::pack( QDataStream& stream ) const
255 {
256  Q_D(const GeoDataMultiGeometry);
257 
258  GeoDataGeometry::pack( stream );
259 
260  stream << d->m_vector.size();
261 
263  = d->m_vector.constBegin();
264  iterator != d->m_vector.constEnd();
265  ++iterator ) {
266  const GeoDataGeometry *geometry = *iterator;
267  stream << geometry->geometryId();
268  geometry->pack( stream );
269  }
270 }
271 
272 void GeoDataMultiGeometry::unpack( QDataStream& stream )
273 {
274  detach();
275 
276  Q_D(GeoDataMultiGeometry);
277  GeoDataGeometry::unpack( stream );
278 
279  int size = 0;
280 
281  stream >> size;
282 
283  for( int i = 0; i < size; i++ ) {
284  int geometryId;
285  stream >> geometryId;
286  switch( geometryId ) {
287  case InvalidGeometryId:
288  break;
289  case GeoDataPointId:
290  {
291  GeoDataPoint *point = new GeoDataPoint;
292  point->unpack( stream );
293  d->m_vector.append(point);
294  }
295  break;
296  case GeoDataLineStringId:
297  {
298  GeoDataLineString *lineString = new GeoDataLineString;
299  lineString->unpack( stream );
300  d->m_vector.append(lineString);
301  }
302  break;
303  case GeoDataLinearRingId:
304  {
305  GeoDataLinearRing *linearRing = new GeoDataLinearRing;
306  linearRing->unpack( stream );
307  d->m_vector.append(linearRing);
308  }
309  break;
310  case GeoDataPolygonId:
311  {
312  GeoDataPolygon *polygon = new GeoDataPolygon;
313  polygon->unpack( stream );
314  d->m_vector.append(polygon);
315  }
316  break;
317  case GeoDataMultiGeometryId:
318  {
319  GeoDataMultiGeometry *multiGeometry = new GeoDataMultiGeometry;
320  multiGeometry->unpack( stream );
321  d->m_vector.append(multiGeometry);
322  }
323  break;
324  case GeoDataModelId:
325  break;
326  default: break;
327  };
328  }
329 }
330 
331 }
QVector::const_iterator constEnd() const const
A base class for all geodata features.
void unpack(QDataStream &stream) override
Unserialize the contents of the feature from stream.
void setParent(GeoDataObject *parent)
Sets the parent of the object.
void append(GeoDataGeometry *other)
add an element
GeoDataGeometry * child(int)
returns the requested child item
Binds a QML item to a specific geodetic location in screen coordinates.
QVector::const_iterator constBegin() const const
const char * nodeType() const override
Provides type information for downcasting a GeoNode.
void pack(QDataStream &stream) const override
Serialize the contents of the feature to stream.
A class that can contain other GeoDataGeometry objects.
int childPosition(const GeoDataGeometry *child) const
returns the position of an item in the list
const QList< QKeySequence > & end()
Q_D(Todo)
QDebug mDebug()
a function to replace qDebug() in Marble library code
Definition: MarbleDebug.cpp:31
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Sep 27 2023 04:09:06 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.