1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2023-05-15
 * Description : geolocation engine based on Marble.
 *               (c) 2007-2022 Marble Team
 *               https://invent.kde.org/education/marble/-/raw/master/data/credits_authors.html
 *
 * SPDX-FileCopyrightText: 2023-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 * ============================================================ */

#include "GeoDataMultiGeometry.h"
#include "GeoDataMultiGeometry_p.h"

// Qt includes

#include <QDataStream>

// Local includes

#include "GeoDataLineString.h"
#include "GeoDataLinearRing.h"
#include "GeoDataPoint.h"
#include "GeoDataPolygon.h"
#include "GeoDataTypes.h"
#include "digikam_debug.h"

namespace Marble
{

GeoDataMultiGeometry::GeoDataMultiGeometry()
    : GeoDataGeometry(new GeoDataMultiGeometryPrivate)
{
}

GeoDataMultiGeometry::GeoDataMultiGeometry(const GeoDataGeometry& other)
    : GeoDataGeometry(other)
{
}

GeoDataMultiGeometry::~GeoDataMultiGeometry()
{
}

const char* GeoDataMultiGeometry::nodeType() const
{
    return GeoDataTypes::GeoDataMultiGeometryType;
}

EnumGeometryId GeoDataMultiGeometry::geometryId() const
{
    return GeoDataMultiGeometryId;
}

GeoDataGeometry* GeoDataMultiGeometry::copy() const
{
    return new GeoDataMultiGeometry(*this);
}

bool GeoDataMultiGeometry::operator==(const GeoDataMultiGeometry& other) const
{
    Q_D(const GeoDataMultiGeometry);
    const GeoDataMultiGeometryPrivate* const other_d = other.d_func();
    QVector<GeoDataGeometry*>::const_iterator thisBegin = d->m_vector.constBegin();
    QVector<GeoDataGeometry*>::const_iterator thisEnd = d->m_vector.constEnd();
    QVector<GeoDataGeometry*>::const_iterator otherBegin = other_d->m_vector.constBegin();
    QVector<GeoDataGeometry*>::const_iterator otherEnd = other_d->m_vector.constEnd();

    for (; thisBegin != thisEnd && otherBegin != otherEnd; ++thisBegin, ++otherBegin)
    {
        if (**thisBegin != **otherBegin)
        {
            return false;
        }
    }

    return true;
}

const GeoDataLatLonAltBox& GeoDataMultiGeometry::latLonAltBox() const
{
    Q_D(const GeoDataMultiGeometry);

    QVector<GeoDataGeometry*>::const_iterator it = d->m_vector.constBegin();
    QVector<GeoDataGeometry*>::const_iterator end = d->m_vector.constEnd();<--- Shadow local variable

    d->m_latLonAltBox.clear();

    for (; it != end; ++it)
    {
        if (!(*it)->latLonAltBox().isEmpty())
        {
            if (d->m_latLonAltBox.isEmpty())
            {
                d->m_latLonAltBox = (*it)->latLonAltBox();
            }

            else
            {
                d->m_latLonAltBox |= (*it)->latLonAltBox();
            }
        }
    }

    return d->m_latLonAltBox;
}

int GeoDataMultiGeometry::size() const
{
    Q_D(const GeoDataMultiGeometry);
    return d->m_vector.size();
}

QVector<GeoDataGeometry*> GeoDataMultiGeometry::vector()
{
    Q_D(const GeoDataMultiGeometry);

    return d->m_vector;
}

GeoDataGeometry& GeoDataMultiGeometry::at(int pos)
{
    qCDebug(DIGIKAM_GEOENGINE_LOG) << "detaching!";
    detach();

    Q_D(GeoDataMultiGeometry);
    return *(d->m_vector[pos]);
}

const GeoDataGeometry& GeoDataMultiGeometry::at(int pos) const
{
    Q_D(const GeoDataMultiGeometry);
    return *(d->m_vector.at(pos));
}

GeoDataGeometry& GeoDataMultiGeometry::operator[](int pos)
{
    detach();

    Q_D(GeoDataMultiGeometry);
    return *(d->m_vector[pos]);
}

const GeoDataGeometry& GeoDataMultiGeometry::operator[](int pos) const
{
    Q_D(const GeoDataMultiGeometry);
    return *(d->m_vector[pos]);
}

GeoDataGeometry& GeoDataMultiGeometry::last()
{
    detach();

    Q_D(GeoDataMultiGeometry);
    return *(d->m_vector.last());
}

GeoDataGeometry& GeoDataMultiGeometry::first()
{
    detach();

    Q_D(GeoDataMultiGeometry);
    return *(d->m_vector.first());
}

const GeoDataGeometry& GeoDataMultiGeometry::last() const
{
    Q_D(const GeoDataMultiGeometry);
    return *(d->m_vector.last());
}

const GeoDataGeometry& GeoDataMultiGeometry::first() const
{
    Q_D(const GeoDataMultiGeometry);
    return *(d->m_vector.first());
}

QVector<GeoDataGeometry*>::Iterator GeoDataMultiGeometry::begin()
{
    detach();

    Q_D(GeoDataMultiGeometry);
    return d->m_vector.begin();
}

QVector<GeoDataGeometry*>::Iterator GeoDataMultiGeometry::end()
{
    detach();

    Q_D(GeoDataMultiGeometry);
    return d->m_vector.end();
}

QVector<GeoDataGeometry*>::ConstIterator GeoDataMultiGeometry::constBegin() const
{
    Q_D(const GeoDataMultiGeometry);
    return d->m_vector.constBegin();
}

QVector<GeoDataGeometry*>::ConstIterator GeoDataMultiGeometry::constEnd() const
{
    Q_D(const GeoDataMultiGeometry);
    return d->m_vector.constEnd();
}

/**
 * @brief  returns the requested child item
 */
GeoDataGeometry* GeoDataMultiGeometry::child(int i)
{
    detach();

    Q_D(GeoDataMultiGeometry);
    return d->m_vector.at(i);
}

const GeoDataGeometry* GeoDataMultiGeometry::child(int i) const
{
    Q_D(const GeoDataMultiGeometry);
    return d->m_vector.at(i);
}

/**
 * @brief returns the position of an item in the list
 */
int GeoDataMultiGeometry::childPosition(const GeoDataGeometry* object) const
{
    Q_D(const GeoDataMultiGeometry);

    for (int i = 0; i < d->m_vector.size(); ++i)
    {
        if (d->m_vector.at(i) == object)
        {
            return i;
        }
    }

    return -1;
}

/**
* @brief add an element
*/
void GeoDataMultiGeometry::append(GeoDataGeometry* other)
{
    detach();

    Q_D(GeoDataMultiGeometry);
    other->setParent(this);
    d->m_vector.append(other);
}


GeoDataMultiGeometry& GeoDataMultiGeometry::operator << (const GeoDataGeometry& value)
{
    detach();

    Q_D(GeoDataMultiGeometry);
    GeoDataGeometry* g = value.copy();
    g->setParent(this);
    d->m_vector.append(g);
    return *this;
}

void GeoDataMultiGeometry::clear()
{
    detach();

    Q_D(GeoDataMultiGeometry);
    qDeleteAll(d->m_vector);
    d->m_vector.clear();
}

void GeoDataMultiGeometry::pack(QDataStream& stream) const
{
    Q_D(const GeoDataMultiGeometry);

    GeoDataGeometry::pack(stream);

    stream << d->m_vector.size();

    for (QVector<GeoDataGeometry*>::const_iterator iterator
         = d->m_vector.constBegin();
         iterator != d->m_vector.constEnd();
         ++iterator)
    {
        const GeoDataGeometry* geometry = *iterator;
        stream << geometry->geometryId();
        geometry->pack(stream);
    }
}

void GeoDataMultiGeometry::unpack(QDataStream& stream)
{
    detach();

    Q_D(GeoDataMultiGeometry);
    GeoDataGeometry::unpack(stream);

    int size = 0;<--- Shadow local variable

    stream >> size;

    for (int i = 0; i < size; i++)
    {
        int geometryId;<--- Shadow local variable
        stream >> geometryId;

        switch (geometryId)
        {
            case InvalidGeometryId:
                break;

            case GeoDataPointId:
            {
                GeoDataPoint* point = new GeoDataPoint;
                point->unpack(stream);
                d->m_vector.append(point);
            }
            break;

            case GeoDataLineStringId:
            {
                GeoDataLineString* lineString = new GeoDataLineString;
                lineString->unpack(stream);
                d->m_vector.append(lineString);
            }
            break;

            case GeoDataLinearRingId:
            {
                GeoDataLinearRing* linearRing = new GeoDataLinearRing;
                linearRing->unpack(stream);
                d->m_vector.append(linearRing);
            }
            break;

            case GeoDataPolygonId:
            {
                GeoDataPolygon* polygon = new GeoDataPolygon;
                polygon->unpack(stream);
                d->m_vector.append(polygon);
            }
            break;

            case GeoDataMultiGeometryId:
            {
                GeoDataMultiGeometry* multiGeometry = new GeoDataMultiGeometry;
                multiGeometry->unpack(stream);
                d->m_vector.append(multiGeometry);
            }
            break;

            case GeoDataModelId:
                break;

            default:
                break;
        };
    }
}

} // namespace Marble