Marble

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

KDE's Doxygen guidelines are available online.