Coverage Report

Created: 2026-02-14 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/ogr/ogrfeature.cpp
Line
Count
Source
1
/******************************************************************************
2
 *
3
 * Project:  OpenGIS Simple Features Reference Implementation
4
 * Purpose:  The OGRFeature class implementation.
5
 * Author:   Frank Warmerdam, warmerda@home.com
6
 *
7
 ******************************************************************************
8
 * Copyright (c) 1999,  Les Technologies SoftMap Inc.
9
 * Copyright (c) 2008-2013, Even Rouault <even dot rouault at spatialys.com>
10
 *
11
 * SPDX-License-Identifier: MIT
12
 ****************************************************************************/
13
14
#include "cpl_port.h"
15
#include "ogr_api.h"
16
#include "ogr_feature.h"
17
18
#include <cerrno>
19
#include <cinttypes>
20
#include <climits>
21
#include <cstdio>
22
#include <cstdlib>
23
#include <cstring>
24
#include <cmath>
25
#include <ctime>
26
27
#include <algorithm>
28
#include <limits>
29
#include <map>
30
#include <new>
31
#include <vector>
32
33
#include "cpl_conv.h"
34
#include "cpl_error.h"
35
#include "cpl_string.h"
36
#include "cpl_time.h"
37
#include "cpl_vsi.h"
38
#include "ogr_core.h"
39
#include "ogr_featurestyle.h"
40
#include "ogr_geometry.h"
41
#include "ogr_p.h"
42
#include "ogrlibjsonutils.h"
43
44
#include "cpl_json_header.h"
45
46
// Too many false positives from gcc 13.2.1 in that file...
47
#if defined(__GNUC__)
48
#pragma GCC diagnostic push
49
#pragma GCC diagnostic ignored "-Wnull-dereference"
50
#endif
51
52
/************************************************************************/
53
/*                             OGRFeature()                             */
54
/************************************************************************/
55
56
/**
57
 * \brief Constructor
58
 *
59
 * Note that the OGRFeature will increment the reference count of its
60
 * defining OGRFeatureDefn.  Destruction of the OGRFeatureDefn before
61
 * destruction of all OGRFeatures that depend on it is likely to result in
62
 * a crash.
63
 *
64
 * This method is the same as the C function OGR_F_Create().
65
 *
66
 * @param poDefnIn feature class (layer) definition to which the feature will
67
 * adhere.
68
 */
69
70
OGRFeature::OGRFeature(const OGRFeatureDefn *poDefnIn)
71
0
    : nFID(OGRNullFID), poDefn(poDefnIn), papoGeometries(nullptr),
72
0
      pauFields(nullptr), m_pszNativeData(nullptr),
73
0
      m_pszNativeMediaType(nullptr), m_pszStyleString(nullptr),
74
0
      m_poStyleTable(nullptr), m_pszTmpFieldValue(nullptr)
75
0
{
76
0
    const_cast<OGRFeatureDefn *>(poDefnIn)->Reference();
77
78
0
    const int nFieldCount = poDefn->GetFieldCount();
79
0
    pauFields = static_cast<OGRField *>(
80
0
        VSI_MALLOC_VERBOSE(nFieldCount * sizeof(OGRField)));
81
82
0
    papoGeometries = static_cast<OGRGeometry **>(
83
0
        VSI_CALLOC_VERBOSE(poDefn->GetGeomFieldCount(), sizeof(OGRGeometry *)));
84
85
    // Initialize array to the unset special value.
86
0
    if (pauFields != nullptr)
87
0
    {
88
0
        for (int i = 0; i < nFieldCount; i++)
89
0
        {
90
0
            pauFields[i].Set.nMarker1 = OGRUnsetMarker;
91
0
            pauFields[i].Set.nMarker2 = OGRUnsetMarker;
92
0
            pauFields[i].Set.nMarker3 = OGRUnsetMarker;
93
0
        }
94
0
    }
95
0
}
96
97
/************************************************************************/
98
/*                            OGR_F_Create()                            */
99
/************************************************************************/
100
/**
101
 * \brief Feature factory.
102
 *
103
 * Note that the OGRFeature will increment the reference count of its
104
 * defining OGRFeatureDefn.  Destruction of the OGRFeatureDefn before
105
 * destruction of all OGRFeatures that depend on it is likely to result in
106
 * a crash.
107
 *
108
 * This function is the same as the C++ method OGRFeature::OGRFeature().
109
 *
110
 * @param hDefn handle to the feature class (layer) definition to
111
 * which the feature will adhere.
112
 *
113
 * @return a handle to the new feature object with null fields and no geometry,
114
 * or, NULL in case out of memory situation.
115
 */
116
117
OGRFeatureH OGR_F_Create(OGRFeatureDefnH hDefn)
118
119
0
{
120
0
    VALIDATE_POINTER1(hDefn, "OGR_F_Create", nullptr);
121
0
    return OGRFeature::ToHandle(
122
0
        OGRFeature::CreateFeature(OGRFeatureDefn::FromHandle(hDefn)));
123
0
}
124
125
/************************************************************************/
126
/*                            ~OGRFeature()                             */
127
/************************************************************************/
128
129
OGRFeature::~OGRFeature()
130
131
0
{
132
0
    if (pauFields != nullptr)
133
0
    {
134
        // We can call GetFieldCountUnsafe() as the constructor has called
135
        // the regular GetFieldCount()
136
0
        const int nFieldcount = poDefn->GetFieldCountUnsafe();
137
0
        for (int i = 0; i < nFieldcount; i++)
138
0
        {
139
0
            const OGRFieldDefn *poFDefn = poDefn->GetFieldDefnUnsafe(i);
140
141
0
            if (!IsFieldSetAndNotNullUnsafe(i))
142
0
                continue;
143
144
0
            switch (poFDefn->GetType())
145
0
            {
146
0
                case OFTString:
147
0
                    if (pauFields[i].String != nullptr)
148
0
                        VSIFree(pauFields[i].String);
149
0
                    break;
150
151
0
                case OFTBinary:
152
0
                    if (pauFields[i].Binary.paData != nullptr)
153
0
                        VSIFree(pauFields[i].Binary.paData);
154
0
                    break;
155
156
0
                case OFTStringList:
157
0
                    CSLDestroy(pauFields[i].StringList.paList);
158
0
                    break;
159
160
0
                case OFTIntegerList:
161
0
                case OFTInteger64List:
162
0
                case OFTRealList:
163
0
                    CPLFree(pauFields[i].IntegerList.paList);
164
0
                    break;
165
166
0
                default:
167
                    // TODO(schwehr): Add support for wide strings.
168
0
                    break;
169
0
            }
170
0
        }
171
0
    }
172
173
0
    if (papoGeometries != nullptr)
174
0
    {
175
0
        const int nGeomFieldCount = poDefn->GetGeomFieldCount();
176
177
0
        for (int i = 0; i < nGeomFieldCount; i++)
178
0
        {
179
0
            delete papoGeometries[i];
180
0
        }
181
0
    }
182
183
0
    if (poDefn)
184
0
        const_cast<OGRFeatureDefn *>(poDefn)->Release();
185
186
0
    CPLFree(pauFields);
187
0
    CPLFree(papoGeometries);
188
0
    CPLFree(m_pszStyleString);
189
0
    CPLFree(m_pszTmpFieldValue);
190
0
    CPLFree(m_pszNativeData);
191
0
    CPLFree(m_pszNativeMediaType);
192
0
}
193
194
/************************************************************************/
195
/*                           OGR_F_Destroy()                            */
196
/************************************************************************/
197
/**
198
 * \brief Destroy feature
199
 *
200
 * The feature is deleted, but within the context of the GDAL/OGR heap.
201
 * This is necessary when higher level applications use GDAL/OGR from a
202
 * DLL and they want to delete a feature created within the DLL.  If the
203
 * delete is done in the calling application the memory will be freed onto
204
 * the application heap which is inappropriate.
205
 *
206
 * This function is the same as the C++ method OGRFeature::DestroyFeature().
207
 *
208
 * @param hFeat handle to the feature to destroy.
209
 */
210
211
void OGR_F_Destroy(OGRFeatureH hFeat)
212
213
0
{
214
0
    delete OGRFeature::FromHandle(hFeat);
215
0
}
216
217
/************************************************************************/
218
/*                           CreateFeature()                            */
219
/************************************************************************/
220
221
/**
222
 * \brief Feature factory.
223
 *
224
 * This is essentially a feature factory, useful for
225
 * applications creating features but wanting to ensure they
226
 * are created out of the OGR/GDAL heap.
227
 *
228
 * This method is the same as the C function OGR_F_Create().
229
 *
230
 * @param poDefn Feature definition defining schema.
231
 *
232
 * @return new feature object with null fields and no geometry, or
233
 * NULL in case of out of memory situation.  May be deleted with
234
 * DestroyFeature().
235
 */
236
237
OGRFeature *OGRFeature::CreateFeature(const OGRFeatureDefn *poDefn)
238
239
0
{
240
0
    OGRFeature *poFeature = new (std::nothrow) OGRFeature(poDefn);
241
0
    if (poFeature == nullptr)
242
0
        return nullptr;
243
244
0
    if ((poFeature->pauFields == nullptr &&
245
0
         poDefn->GetFieldCountUnsafe() != 0) ||
246
0
        (poFeature->papoGeometries == nullptr &&
247
0
         poDefn->GetGeomFieldCount() != 0))
248
0
    {
249
0
        delete poFeature;
250
0
        return nullptr;
251
0
    }
252
253
0
    return poFeature;
254
0
}
255
256
/************************************************************************/
257
/*                           DestroyFeature()                           */
258
/************************************************************************/
259
260
/**
261
 * \brief Destroy feature
262
 *
263
 * The feature is deleted, but within the context of the GDAL/OGR heap.
264
 * This is necessary when higher level applications use GDAL/OGR from a
265
 * DLL and they want to delete a feature created within the DLL.  If the
266
 * delete is done in the calling application the memory will be freed onto
267
 * the application heap which is inappropriate.
268
 *
269
 * This method is the same as the C function OGR_F_Destroy().
270
 *
271
 * @param poFeature the feature to delete.
272
 */
273
274
void OGRFeature::DestroyFeature(OGRFeature *poFeature)
275
276
0
{
277
0
    delete poFeature;
278
0
}
279
280
/************************************************************************/
281
/*                               Reset()                                */
282
/************************************************************************/
283
284
/** Reset the state of a OGRFeature to its state after construction.
285
 *
286
 * This enables recycling existing OGRFeature instances.
287
 *
288
 * @since GDAL 3.5
289
 */
290
void OGRFeature::Reset()
291
0
{
292
0
    nFID = OGRNullFID;
293
294
0
    if (pauFields != nullptr)
295
0
    {
296
0
        const int nFieldcount = poDefn->GetFieldCountUnsafe();
297
0
        for (int i = 0; i < nFieldcount; i++)
298
0
        {
299
0
            if (!IsFieldSetAndNotNullUnsafe(i))
300
0
                continue;
301
302
0
            const OGRFieldDefn *poFDefn = poDefn->GetFieldDefnUnsafe(i);
303
0
            switch (poFDefn->GetType())
304
0
            {
305
0
                case OFTString:
306
0
                    if (pauFields[i].String != nullptr)
307
0
                        VSIFree(pauFields[i].String);
308
0
                    break;
309
310
0
                case OFTBinary:
311
0
                    if (pauFields[i].Binary.paData != nullptr)
312
0
                        VSIFree(pauFields[i].Binary.paData);
313
0
                    break;
314
315
0
                case OFTStringList:
316
0
                    CSLDestroy(pauFields[i].StringList.paList);
317
0
                    break;
318
319
0
                case OFTIntegerList:
320
0
                case OFTInteger64List:
321
0
                case OFTRealList:
322
0
                    CPLFree(pauFields[i].IntegerList.paList);
323
0
                    break;
324
325
0
                default:
326
                    // TODO(schwehr): Add support for wide strings.
327
0
                    break;
328
0
            }
329
330
0
            pauFields[i].Set.nMarker1 = OGRUnsetMarker;
331
0
            pauFields[i].Set.nMarker2 = OGRUnsetMarker;
332
0
            pauFields[i].Set.nMarker3 = OGRUnsetMarker;
333
0
        }
334
0
    }
335
336
0
    if (papoGeometries != nullptr)
337
0
    {
338
0
        const int nGeomFieldCount = poDefn->GetGeomFieldCount();
339
340
0
        for (int i = 0; i < nGeomFieldCount; i++)
341
0
        {
342
0
            delete papoGeometries[i];
343
0
            papoGeometries[i] = nullptr;
344
0
        }
345
0
    }
346
347
0
    if (m_pszStyleString)
348
0
    {
349
0
        CPLFree(m_pszStyleString);
350
0
        m_pszStyleString = nullptr;
351
0
    }
352
353
0
    if (m_pszNativeData)
354
0
    {
355
0
        CPLFree(m_pszNativeData);
356
0
        m_pszNativeData = nullptr;
357
0
    }
358
359
0
    if (m_pszNativeMediaType)
360
0
    {
361
0
        CPLFree(m_pszNativeMediaType);
362
0
        m_pszNativeMediaType = nullptr;
363
0
    }
364
0
}
365
366
/************************************************************************/
367
/*                           SetFDefnUnsafe()                           */
368
/************************************************************************/
369
370
//! @cond Doxygen_Suppress
371
void OGRFeature::SetFDefnUnsafe(OGRFeatureDefn *poNewFDefn)
372
0
{
373
0
    poNewFDefn->Reference();
374
0
    const_cast<OGRFeatureDefn *>(poDefn)->Release();
375
0
    poDefn = poNewFDefn;
376
0
}
377
378
//! @endcond
379
380
/************************************************************************/
381
/*                             GetDefnRef()                             */
382
/************************************************************************/
383
384
/**
385
 * \fn OGRFeatureDefn *OGRFeature::GetDefnRef();
386
 *
387
 * \brief Fetch feature definition.
388
 *
389
 * This method is the same as the C function OGR_F_GetDefnRef().
390
 *
391
 * @return a reference to the feature definition object.
392
 */
393
394
/**
395
 * \fn const OGRFeatureDefn *OGRFeature::GetDefnRef() const;
396
 *
397
 * \brief Fetch feature definition.
398
 *
399
 * This method is the same as the C function OGR_F_GetDefnRef().
400
 *
401
 * @return a reference to the feature definition object.
402
 */
403
404
/************************************************************************/
405
/*                          OGR_F_GetDefnRef()                          */
406
/************************************************************************/
407
408
/**
409
 * \brief Fetch feature definition.
410
 *
411
 * This function is the same as the C++ method OGRFeature::GetDefnRef().
412
 *
413
 * @param hFeat handle to the feature to get the feature definition from.
414
 *
415
 * @return a handle to the feature definition object on which feature
416
 * depends.
417
 */
418
419
OGRFeatureDefnH OGR_F_GetDefnRef(OGRFeatureH hFeat)
420
421
0
{
422
0
    VALIDATE_POINTER1(hFeat, "OGR_F_GetDefnRef", nullptr);
423
424
0
    return OGRFeatureDefn::ToHandle(const_cast<OGRFeatureDefn *>(
425
0
        OGRFeature::FromHandle(hFeat)->GetDefnRef()));
426
0
}
427
428
/************************************************************************/
429
/*                        SetGeometryDirectly()                         */
430
/************************************************************************/
431
432
/**
433
 * \brief Set feature geometry.
434
 *
435
 * This method updates the features geometry, and operates the same as
436
 * SetGeometry(), except that this method assumes ownership of the
437
 * passed geometry (even in case of failure of that function).
438
 *
439
 * This method is the same as the C function OGR_F_SetGeometryDirectly().
440
 *
441
 * @note This method has only an effect on the in-memory feature object. If
442
 * this object comes from a layer and the modifications must be serialized back
443
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
444
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
445
 *
446
 * @param poGeomIn new geometry to apply to feature. Passing NULL value here
447
 * is correct and it will result in deallocation of currently assigned geometry
448
 * without assigning new one.
449
 *
450
 * @return OGRERR_NONE if successful, or OGR_UNSUPPORTED_GEOMETRY_TYPE if
451
 * the geometry type is illegal for the OGRFeatureDefn (checking not yet
452
 * implemented).
453
 */
454
455
OGRErr OGRFeature::SetGeometryDirectly(OGRGeometry *poGeomIn)
456
457
0
{
458
0
    if (poGeomIn == GetGeometryRef())
459
0
    {
460
0
        return OGRERR_NONE;
461
0
    }
462
463
0
    return SetGeomField(0, std::unique_ptr<OGRGeometry>(poGeomIn));
464
0
}
465
466
/************************************************************************/
467
/*                     OGR_F_SetGeometryDirectly()                      */
468
/************************************************************************/
469
470
/**
471
 * \brief Set feature geometry.
472
 *
473
 * This function updates the features geometry, and operates the same as
474
 * SetGeometry(), except that this function assumes ownership of the
475
 * passed geometry (even in case of failure of that function).
476
 *
477
 * This function is the same as the C++ method
478
 * OGRFeature::SetGeometryDirectly.
479
 *
480
 * @note This method has only an effect on the in-memory feature object. If
481
 * this object comes from a layer and the modifications must be serialized back
482
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
483
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
484
 *
485
 * @param hFeat handle to the feature on which to apply the geometry.
486
 * @param hGeom handle to the new geometry to apply to feature.
487
 *
488
 * @return OGRERR_NONE if successful, or OGR_UNSUPPORTED_GEOMETRY_TYPE if
489
 * the geometry type is illegal for the OGRFeatureDefn (checking not yet
490
 * implemented).
491
 */
492
493
OGRErr OGR_F_SetGeometryDirectly(OGRFeatureH hFeat, OGRGeometryH hGeom)
494
495
0
{
496
0
    VALIDATE_POINTER1(hFeat, "OGR_F_SetGeometryDirectly", OGRERR_FAILURE);
497
498
0
    return OGRFeature::FromHandle(hFeat)->SetGeometryDirectly(
499
0
        OGRGeometry::FromHandle(hGeom));
500
0
}
501
502
/************************************************************************/
503
/*                            SetGeometry()                             */
504
/************************************************************************/
505
506
/**
507
 * \brief Set feature geometry.
508
 *
509
 * This method updates the features geometry, and operates the same as
510
 * SetGeometryDirectly(), except that this method does not assume ownership
511
 * of the passed geometry, but instead makes a copy of it.
512
 *
513
 * This method is the same as the C function OGR_F_SetGeometry().
514
 *
515
 * @note This method has only an effect on the in-memory feature object. If
516
 * this object comes from a layer and the modifications must be serialized back
517
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
518
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
519
 *
520
 * @param poGeomIn new geometry to apply to feature. Passing NULL value here
521
 * is correct and it will result in deallocation of currently assigned geometry
522
 * without assigning new one.
523
 *
524
 * @return OGRERR_NONE if successful, or OGR_UNSUPPORTED_GEOMETRY_TYPE if
525
 * the geometry type is illegal for the OGRFeatureDefn (checking not yet
526
 * implemented).
527
 */
528
529
OGRErr OGRFeature::SetGeometry(const OGRGeometry *poGeomIn)
530
531
0
{
532
0
    if (GetGeomFieldCount() < 1)
533
0
        return OGRERR_FAILURE;
534
535
0
    return SetGeomField(0, poGeomIn);
536
0
}
537
538
/************************************************************************/
539
/*                         OGR_F_SetGeometry()                          */
540
/************************************************************************/
541
542
/**
543
 * \brief Set feature geometry.
544
 *
545
 * This function updates the features geometry, and operates the same as
546
 * SetGeometryDirectly(), except that this function does not assume ownership
547
 * of the passed geometry, but instead makes a copy of it.
548
 *
549
 * This function is the same as the C++ OGRFeature::SetGeometry().
550
 *
551
 * @note This method has only an effect on the in-memory feature object. If
552
 * this object comes from a layer and the modifications must be serialized back
553
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
554
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
555
 *
556
 * @param hFeat handle to the feature on which new geometry is applied to.
557
 * @param hGeom handle to the new geometry to apply to feature.
558
 *
559
 * @return OGRERR_NONE if successful, or OGR_UNSUPPORTED_GEOMETRY_TYPE if
560
 * the geometry type is illegal for the OGRFeatureDefn (checking not yet
561
 * implemented).
562
 */
563
564
OGRErr OGR_F_SetGeometry(OGRFeatureH hFeat, OGRGeometryH hGeom)
565
566
0
{
567
0
    VALIDATE_POINTER1(hFeat, "OGR_F_SetGeometry", OGRERR_FAILURE);
568
569
0
    return OGRFeature::FromHandle(hFeat)->SetGeometry(
570
0
        OGRGeometry::FromHandle(hGeom));
571
0
}
572
573
/************************************************************************/
574
/*                            SetGeometry()                             */
575
/************************************************************************/
576
577
/**
578
 * \brief Set feature geometry.
579
 *
580
 * This method is the same as the C function OGR_F_SetGeometryDirectly().
581
 *
582
 * @note This method has only an effect on the in-memory feature object. If
583
 * this object comes from a layer and the modifications must be serialized back
584
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
585
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
586
 *
587
 * @param poGeomIn new geometry to apply to feature. Passing NULL value here
588
 * is correct and it will result in deallocation of currently assigned geometry
589
 * without assigning new one.
590
 *
591
 * @return OGRERR_NONE if successful, or OGR_UNSUPPORTED_GEOMETRY_TYPE if
592
 * the geometry type is illegal for the OGRFeatureDefn (checking not yet
593
 * implemented).
594
 *
595
 * @since GDAL 3.11
596
 */
597
598
OGRErr OGRFeature::SetGeometry(std::unique_ptr<OGRGeometry> poGeomIn)
599
600
0
{
601
0
    return SetGeomField(0, std::move(poGeomIn));
602
0
}
603
604
/************************************************************************/
605
/*                           StealGeometry()                            */
606
/************************************************************************/
607
608
/**
609
 * \brief Take away ownership of geometry.
610
 *
611
 * Fetch the geometry from this feature, and clear the reference to the
612
 * geometry on the feature.  This is a mechanism for the application to
613
 * take over ownership of the geometry from the feature without copying.
614
 * Sort of an inverse to SetGeometryDirectly().
615
 *
616
 * After this call the OGRFeature will have a NULL geometry.
617
 *
618
 * @return the pointer to the geometry.
619
 */
620
621
OGRGeometry *OGRFeature::StealGeometry()
622
623
0
{
624
0
    if (GetGeomFieldCount() > 0)
625
0
    {
626
0
        OGRGeometry *poReturn = papoGeometries[0];
627
0
        papoGeometries[0] = nullptr;
628
0
        return poReturn;
629
0
    }
630
631
0
    return nullptr;
632
0
}
633
634
/**
635
 * \brief Take away ownership of geometry.
636
 *
637
 * Fetch the geometry from this feature, and clear the reference to the
638
 * geometry on the feature.  This is a mechanism for the application to
639
 * take over ownership of the geometry from the feature without copying.
640
 * Sort of an inverse to SetGeometryDirectly().
641
 *
642
 * After this call the OGRFeature will have a NULL geometry.
643
 *
644
 * @param iGeomField index of the geometry field.
645
 *
646
 * @return the pointer to the geometry.
647
 */
648
649
OGRGeometry *OGRFeature::StealGeometry(int iGeomField)
650
651
0
{
652
0
    if (iGeomField >= 0 && iGeomField < GetGeomFieldCount())
653
0
    {
654
0
        OGRGeometry *poReturn = papoGeometries[iGeomField];
655
0
        papoGeometries[iGeomField] = nullptr;
656
0
        return poReturn;
657
0
    }
658
659
0
    return nullptr;
660
0
}
661
662
/************************************************************************/
663
/*                        OGR_F_StealGeometry()                         */
664
/************************************************************************/
665
666
/**
667
 * \brief Take away ownership of geometry.
668
 *
669
 * Fetch the geometry from this feature, and clear the reference to the
670
 * geometry on the feature.  This is a mechanism for the application to
671
 * take over ownership of the geometry from the feature without copying.
672
 * Sort of an inverse to OGR_FSetGeometryDirectly().
673
 *
674
 * After this call the OGRFeature will have a NULL geometry.
675
 *
676
 * @param hFeat feature from which to steal the first geometry.
677
 * @return the pointer to the stolen geometry.
678
 */
679
680
OGRGeometryH OGR_F_StealGeometry(OGRFeatureH hFeat)
681
682
0
{
683
0
    VALIDATE_POINTER1(hFeat, "OGR_F_StealGeometry", nullptr);
684
685
0
    return OGRGeometry::ToHandle(
686
0
        OGRFeature::FromHandle(hFeat)->StealGeometry());
687
0
}
688
689
/************************************************************************/
690
/*                       OGR_F_StealGeometryEx()                        */
691
/************************************************************************/
692
693
/**
694
 * \brief Take away ownership of geometry.
695
 *
696
 * Fetch the geometry from this feature, and clear the reference to the
697
 * geometry on the feature.  This is a mechanism for the application to
698
 * take over ownership of the geometry from the feature without copying.
699
 * This is the functional opposite of OGR_F_SetGeomFieldDirectly.
700
 *
701
 * After this call the OGRFeature will have a NULL geometry for the
702
 * geometry field of index iGeomField.
703
 *
704
 * @param hFeat feature from which to steal a geometry.
705
 * @param iGeomField index of the geometry field to steal.
706
 * @return the pointer to the stolen geometry.
707
 * @since GDAL 3.5
708
 */
709
710
OGRGeometryH OGR_F_StealGeometryEx(OGRFeatureH hFeat, int iGeomField)
711
712
0
{
713
0
    VALIDATE_POINTER1(hFeat, "OGR_F_StealGeometryEx", nullptr);
714
715
0
    return OGRGeometry::ToHandle(
716
0
        OGRFeature::FromHandle(hFeat)->StealGeometry(iGeomField));
717
0
}
718
719
/************************************************************************/
720
/*                           GetGeometryRef()                           */
721
/************************************************************************/
722
723
/**
724
 * \fn OGRGeometry *OGRFeature::GetGeometryRef();
725
 *
726
 * \brief Fetch pointer to feature geometry.
727
 *
728
 * This method is essentially the same as the C function OGR_F_GetGeometryRef().
729
 * (the only difference is that the C function honours
730
 * OGRGetNonLinearGeometriesEnabledFlag())
731
 *
732
 * This is equivalent to calling OGRFeature::GetGeomFieldRef(0).
733
 *
734
 * @return pointer to internal feature geometry.  This object should
735
 * not be modified.
736
 */
737
OGRGeometry *OGRFeature::GetGeometryRef()
738
739
0
{
740
0
    if (GetGeomFieldCount() > 0)
741
0
        return GetGeomFieldRef(0);
742
743
0
    return nullptr;
744
0
}
745
746
/**
747
 * \fn const OGRGeometry *OGRFeature::GetGeometryRef() const;
748
 *
749
 * \brief Fetch pointer to feature geometry.
750
 *
751
 * This method is essentially the same as the C function OGR_F_GetGeometryRef().
752
 * (the only difference is that the C function honours
753
 * OGRGetNonLinearGeometriesEnabledFlag())
754
 *
755
 * @return pointer to internal feature geometry.  This object should
756
 * not be modified.
757
 */
758
const OGRGeometry *OGRFeature::GetGeometryRef() const
759
760
0
{
761
0
    if (GetGeomFieldCount() > 0)
762
0
        return GetGeomFieldRef(0);
763
764
0
    return nullptr;
765
0
}
766
767
/************************************************************************/
768
/*                        OGR_F_GetGeometryRef()                        */
769
/************************************************************************/
770
771
/**
772
 * \brief Fetch a handle to feature geometry.
773
 *
774
 * This function is essentially the same as the C++ method
775
 * OGRFeature::GetGeometryRef() (the only difference is that this C function
776
 * honours OGRGetNonLinearGeometriesEnabledFlag())
777
 *
778
 * @param hFeat handle to the feature to get geometry from.
779
 * @return a handle to internal feature geometry.  This object should
780
 * not be modified.
781
 */
782
783
OGRGeometryH OGR_F_GetGeometryRef(OGRFeatureH hFeat)
784
785
0
{
786
0
    VALIDATE_POINTER1(hFeat, "OGR_F_GetGeometryRef", nullptr);
787
788
0
    OGRFeature *poFeature = OGRFeature::FromHandle(hFeat);
789
0
    OGRGeometry *poGeom = poFeature->GetGeometryRef();
790
791
0
    if (!OGRGetNonLinearGeometriesEnabledFlag() && poGeom != nullptr &&
792
0
        OGR_GT_IsNonLinear(poGeom->getGeometryType()))
793
0
    {
794
0
        const OGRwkbGeometryType eTargetType =
795
0
            OGR_GT_GetLinear(poGeom->getGeometryType());
796
0
        auto poNewGeom = OGRGeometryFactory::forceTo(
797
0
            std::unique_ptr<OGRGeometry>(poFeature->StealGeometry()),
798
0
            eTargetType);
799
0
        poFeature->SetGeomField(0, std::move(poNewGeom));
800
0
        poGeom = poFeature->GetGeometryRef();
801
0
    }
802
803
0
    return OGRGeometry::ToHandle(poGeom);
804
0
}
805
806
/************************************************************************/
807
/*                          GetGeomFieldRef()                           */
808
/************************************************************************/
809
810
/**
811
 * \brief Fetch pointer to feature geometry.
812
 *
813
 * This method is the same as the C function OGR_F_GetGeomFieldRef().
814
 *
815
 * @param iField geometry field to get.
816
 *
817
 * @return pointer to internal feature geometry.  This object should
818
 * not be modified.
819
 *
820
 */
821
OGRGeometry *OGRFeature::GetGeomFieldRef(int iField)
822
823
0
{
824
0
    if (iField < 0 || iField >= GetGeomFieldCount())
825
0
        return nullptr;
826
0
    else
827
0
        return papoGeometries[iField];
828
0
}
829
830
/**
831
 * \brief Fetch pointer to feature geometry.
832
 *
833
 * This method is the same as the C function OGR_F_GetGeomFieldRef().
834
 *
835
 * @param iField geometry field to get.
836
 *
837
 * @return pointer to internal feature geometry.  This object should
838
 * not be modified.
839
 */
840
const OGRGeometry *OGRFeature::GetGeomFieldRef(int iField) const
841
842
0
{
843
0
    if (iField < 0 || iField >= GetGeomFieldCount())
844
0
        return nullptr;
845
0
    else
846
0
        return papoGeometries[iField];
847
0
}
848
849
/************************************************************************/
850
/*                          GetGeomFieldRef()                           */
851
/************************************************************************/
852
853
/**
854
 * \brief Fetch pointer to feature geometry.
855
 *
856
 * @param pszFName name of geometry field to get.
857
 *
858
 * @return pointer to internal feature geometry.  This object should
859
 * not be modified.
860
 *
861
 */
862
OGRGeometry *OGRFeature::GetGeomFieldRef(const char *pszFName)
863
864
0
{
865
0
    const int iField = GetGeomFieldIndex(pszFName);
866
0
    if (iField < 0)
867
0
        return nullptr;
868
869
0
    return papoGeometries[iField];
870
0
}
871
872
/**
873
 * \brief Fetch pointer to feature geometry.
874
 *
875
 * @param pszFName name of geometry field to get.
876
 *
877
 * @return pointer to internal feature geometry.  This object should
878
 * not be modified.
879
 */
880
const OGRGeometry *OGRFeature::GetGeomFieldRef(const char *pszFName) const
881
882
0
{
883
0
    const int iField = GetGeomFieldIndex(pszFName);
884
0
    if (iField < 0)
885
0
        return nullptr;
886
887
0
    return papoGeometries[iField];
888
0
}
889
890
/************************************************************************/
891
/*                       OGR_F_GetGeomFieldRef()                        */
892
/************************************************************************/
893
894
/**
895
 * \brief Fetch a handle to feature geometry.
896
 *
897
 * This function is the same as the C++ method OGRFeature::GetGeomFieldRef().
898
 *
899
 * @param hFeat handle to the feature to get geometry from.
900
 * @param iField geometry field to get.
901
 * @return a handle to internal feature geometry.  This object should
902
 * not be modified.
903
 *
904
 */
905
906
OGRGeometryH OGR_F_GetGeomFieldRef(OGRFeatureH hFeat, int iField)
907
908
0
{
909
0
    VALIDATE_POINTER1(hFeat, "OGR_F_GetGeomFieldRef", nullptr);
910
911
0
    OGRFeature *poFeature = OGRFeature::FromHandle(hFeat);
912
0
    OGRGeometry *poGeom = poFeature->GetGeomFieldRef(iField);
913
914
0
    if (!OGRGetNonLinearGeometriesEnabledFlag() && poGeom != nullptr &&
915
0
        OGR_GT_IsNonLinear(poGeom->getGeometryType()))
916
0
    {
917
0
        const OGRwkbGeometryType eTargetType =
918
0
            OGR_GT_GetLinear(poGeom->getGeometryType());
919
0
        auto poNewGeom = OGRGeometryFactory::forceTo(
920
0
            std::unique_ptr<OGRGeometry>(poFeature->StealGeometry(iField)),
921
0
            eTargetType);
922
0
        poFeature->SetGeomField(iField, std::move(poNewGeom));
923
0
        poGeom = poFeature->GetGeomFieldRef(iField);
924
0
    }
925
926
0
    return OGRGeometry::ToHandle(poGeom);
927
0
}
928
929
/************************************************************************/
930
/*                        SetGeomFieldDirectly()                        */
931
/************************************************************************/
932
933
/**
934
 * \brief Set feature geometry of a specified geometry field.
935
 *
936
 * This method updates the features geometry, and operates the same as
937
 * SetGeomField(), except that this method assumes ownership of the
938
 * passed geometry (even in case of failure of that function).
939
 *
940
 * This method is the same as the C function OGR_F_SetGeomFieldDirectly().
941
 *
942
 * @param iField geometry field to set.
943
 * @param poGeomIn new geometry to apply to feature. Passing NULL value here
944
 * is correct and it will result in deallocation of currently assigned geometry
945
 * without assigning new one.
946
 *
947
 * @return OGRERR_NONE if successful, or OGRERR_FAILURE if the index is invalid,
948
 * or OGRERR_UNSUPPORTED_GEOMETRY_TYPE if the geometry type is illegal for the
949
 * OGRFeatureDefn (checking not yet implemented).
950
 *
951
 */
952
953
OGRErr OGRFeature::SetGeomFieldDirectly(int iField, OGRGeometry *poGeomIn)
954
0
{
955
0
    if (poGeomIn && poGeomIn == GetGeomFieldRef(iField))
956
0
    {
957
0
        return OGRERR_NONE;
958
0
    }
959
960
0
    return SetGeomField(iField, std::unique_ptr<OGRGeometry>(poGeomIn));
961
0
}
962
963
/************************************************************************/
964
/*                     OGR_F_SetGeomFieldDirectly()                     */
965
/************************************************************************/
966
967
/**
968
 * \brief Set feature geometry of a specified geometry field.
969
 *
970
 * This function updates the features geometry, and operates the same as
971
 * SetGeomField(), except that this function assumes ownership of the
972
 * passed geometry (even in case of failure of that function).
973
 *
974
 * This function is the same as the C++ method
975
 * OGRFeature::SetGeomFieldDirectly.
976
 *
977
 * @param hFeat handle to the feature on which to apply the geometry.
978
 * @param iField geometry field to set.
979
 * @param hGeom handle to the new geometry to apply to feature.
980
 *
981
 * @return OGRERR_NONE if successful, or OGRERR_FAILURE if the index is invalid,
982
 * or OGR_UNSUPPORTED_GEOMETRY_TYPE if the geometry type is illegal for the
983
 * OGRFeatureDefn (checking not yet implemented).
984
 *
985
 */
986
987
OGRErr OGR_F_SetGeomFieldDirectly(OGRFeatureH hFeat, int iField,
988
                                  OGRGeometryH hGeom)
989
990
0
{
991
0
    VALIDATE_POINTER1(hFeat, "OGR_F_SetGeomFieldDirectly", OGRERR_FAILURE);
992
993
0
    return OGRFeature::FromHandle(hFeat)->SetGeomFieldDirectly(
994
0
        iField, OGRGeometry::FromHandle(hGeom));
995
0
}
996
997
/************************************************************************/
998
/*                            SetGeomField()                            */
999
/************************************************************************/
1000
1001
/**
1002
 * \brief Set feature geometry of a specified geometry field.
1003
 *
1004
 * This method updates the features geometry, and operates the same as
1005
 * SetGeomFieldDirectly(), except that this method does not assume ownership
1006
 * of the passed geometry, but instead makes a copy of it.
1007
 *
1008
 * This method is the same as the C function OGR_F_SetGeomField().
1009
 *
1010
 * @param iField geometry field to set.
1011
 * @param poGeomIn new geometry to apply to feature. Passing NULL value here
1012
 * is correct and it will result in deallocation of currently assigned geometry
1013
 * without assigning new one.
1014
 *
1015
 * @return OGRERR_NONE if successful, or OGRERR_FAILURE if the index is invalid,
1016
 * or OGR_UNSUPPORTED_GEOMETRY_TYPE if the geometry type is illegal for the
1017
 * OGRFeatureDefn (checking not yet implemented).
1018
 *
1019
 */
1020
1021
OGRErr OGRFeature::SetGeomField(int iField, const OGRGeometry *poGeomIn)
1022
1023
0
{
1024
0
    if (iField < 0 || iField >= GetGeomFieldCount())
1025
0
        return OGRERR_FAILURE;
1026
1027
0
    if (papoGeometries[iField] != poGeomIn)
1028
0
    {
1029
0
        delete papoGeometries[iField];
1030
1031
0
        if (poGeomIn != nullptr)
1032
0
            papoGeometries[iField] = poGeomIn->clone();
1033
0
        else
1034
0
            papoGeometries[iField] = nullptr;
1035
0
    }
1036
1037
    // TODO(schwehr): Verify that the geometry matches the defn's type.
1038
1039
0
    return OGRERR_NONE;
1040
0
}
1041
1042
/************************************************************************/
1043
/*                         OGR_F_SetGeomField()                         */
1044
/************************************************************************/
1045
1046
/**
1047
 * \brief Set feature geometry of a specified geometry field.
1048
 *
1049
 * This function updates the features geometry, and operates the same as
1050
 * SetGeometryDirectly(), except that this function does not assume ownership
1051
 * of the passed geometry, but instead makes a copy of it.
1052
 *
1053
 * This function is the same as the C++ OGRFeature::SetGeomField().
1054
 *
1055
 * @param hFeat handle to the feature on which new geometry is applied to.
1056
 * @param iField geometry field to set.
1057
 * @param hGeom handle to the new geometry to apply to feature.
1058
 *
1059
 * @return OGRERR_NONE if successful, or OGR_UNSUPPORTED_GEOMETRY_TYPE if
1060
 * the geometry type is illegal for the OGRFeatureDefn (checking not yet
1061
 * implemented).
1062
 */
1063
1064
OGRErr OGR_F_SetGeomField(OGRFeatureH hFeat, int iField, OGRGeometryH hGeom)
1065
1066
0
{
1067
0
    VALIDATE_POINTER1(hFeat, "OGR_F_SetGeomField", OGRERR_FAILURE);
1068
1069
0
    return OGRFeature::FromHandle(hFeat)->SetGeomField(
1070
0
        iField, OGRGeometry::FromHandle(hGeom));
1071
0
}
1072
1073
/************************************************************************/
1074
/*                            SetGeomField()                            */
1075
/************************************************************************/
1076
1077
/**
1078
 * \brief Set feature geometry of a specified geometry field.
1079
 *
1080
 * This method is the same as the C function OGR_F_SetGeomFieldDirectly().
1081
 *
1082
 * @param iField geometry field to set.
1083
 * @param poGeomIn new geometry to apply to feature. Passing NULL value here
1084
 * is correct and it will result in deallocation of currently assigned geometry
1085
 * without assigning new one.
1086
 *
1087
 * @return OGRERR_NONE if successful, or OGRERR_FAILURE if the index is invalid,
1088
 * or OGRERR_UNSUPPORTED_GEOMETRY_TYPE if the geometry type is illegal for the
1089
 * OGRFeatureDefn (checking not yet implemented).
1090
 *
1091
 * @since GDAL 3.11
1092
 */
1093
1094
OGRErr OGRFeature::SetGeomField(int iField,
1095
                                std::unique_ptr<OGRGeometry> poGeomIn)
1096
1097
0
{
1098
0
    if (iField < 0 || iField >= GetGeomFieldCount())
1099
0
    {
1100
0
        return OGRERR_FAILURE;
1101
0
    }
1102
1103
0
    if (papoGeometries[iField] != poGeomIn.get())
1104
0
    {
1105
0
        delete papoGeometries[iField];
1106
0
        papoGeometries[iField] = poGeomIn.release();
1107
0
    }
1108
1109
0
    return OGRERR_NONE;
1110
0
}
1111
1112
/************************************************************************/
1113
/*                               Clone()                                */
1114
/************************************************************************/
1115
1116
/**
1117
 * \brief Duplicate feature.
1118
 *
1119
 * The newly created feature is owned by the caller, and will have its own
1120
 * reference to the OGRFeatureDefn.
1121
 *
1122
 * This method is the same as the C function OGR_F_Clone().
1123
 *
1124
 * @return new feature, exactly matching this feature. Or, starting with GDAL
1125
 * 2.1, NULL in case of out of memory situation.
1126
 */
1127
1128
OGRFeature *OGRFeature::Clone() const
1129
1130
0
{
1131
0
    OGRFeature *poNew = CreateFeature(poDefn);
1132
0
    if (poNew == nullptr)
1133
0
        return nullptr;
1134
1135
0
    if (!CopySelfTo(poNew))
1136
0
    {
1137
0
        delete poNew;
1138
0
        return nullptr;
1139
0
    }
1140
1141
0
    return poNew;
1142
0
}
1143
1144
/************************************************************************/
1145
/*                            OGR_F_Clone()                             */
1146
/************************************************************************/
1147
1148
/**
1149
 * \brief Duplicate feature.
1150
 *
1151
 * The newly created feature is owned by the caller, and will have its own
1152
 * reference to the OGRFeatureDefn.
1153
 *
1154
 * This function is the same as the C++ method OGRFeature::Clone().
1155
 *
1156
 * @param hFeat handle to the feature to clone.
1157
 * @return a handle to the new feature, exactly matching this feature.
1158
 */
1159
1160
OGRFeatureH OGR_F_Clone(OGRFeatureH hFeat)
1161
1162
0
{
1163
0
    VALIDATE_POINTER1(hFeat, "OGR_F_Clone", nullptr);
1164
1165
0
    return OGRFeature::ToHandle(OGRFeature::FromHandle(hFeat)->Clone());
1166
0
}
1167
1168
/************************************************************************/
1169
/*                             CopySelfTo()                             */
1170
/************************************************************************/
1171
1172
/**
1173
 * \brief Copies the innards of this OGRFeature into the supplied object.
1174
 *
1175
 * This is mainly intended to allow derived classes to implement their own
1176
 * Clone functions.
1177
 *
1178
 * @param poNew The object into which to copy the data of this object.
1179
 * @return True if successful, false if the copy failed.
1180
 */
1181
1182
bool OGRFeature::CopySelfTo(OGRFeature *poNew) const
1183
0
{
1184
0
    const int nFieldCount = poDefn->GetFieldCountUnsafe();
1185
0
    for (int i = 0; i < nFieldCount; i++)
1186
0
    {
1187
0
        if (!poNew->SetFieldInternal(i, pauFields + i))
1188
0
        {
1189
0
            return false;
1190
0
        }
1191
0
    }
1192
0
    if (poNew->papoGeometries)
1193
0
    {
1194
0
        for (int i = 0; i < poDefn->GetGeomFieldCount(); i++)
1195
0
        {
1196
0
            if (papoGeometries[i] != nullptr)
1197
0
            {
1198
0
                poNew->papoGeometries[i] = papoGeometries[i]->clone();
1199
0
                if (poNew->papoGeometries[i] == nullptr)
1200
0
                {
1201
0
                    return false;
1202
0
                }
1203
0
            }
1204
0
        }
1205
0
    }
1206
1207
0
    if (m_pszStyleString != nullptr)
1208
0
    {
1209
0
        poNew->m_pszStyleString = VSI_STRDUP_VERBOSE(m_pszStyleString);
1210
0
        if (poNew->m_pszStyleString == nullptr)
1211
0
        {
1212
0
            return false;
1213
0
        }
1214
0
    }
1215
1216
0
    poNew->SetFID(GetFID());
1217
1218
0
    if (m_pszNativeData != nullptr)
1219
0
    {
1220
0
        poNew->m_pszNativeData = VSI_STRDUP_VERBOSE(m_pszNativeData);
1221
0
        if (poNew->m_pszNativeData == nullptr)
1222
0
        {
1223
0
            return false;
1224
0
        }
1225
0
    }
1226
1227
0
    if (m_pszNativeMediaType != nullptr)
1228
0
    {
1229
0
        poNew->m_pszNativeMediaType = VSI_STRDUP_VERBOSE(m_pszNativeMediaType);
1230
0
        if (poNew->m_pszNativeMediaType == nullptr)
1231
0
        {
1232
0
            return false;
1233
0
        }
1234
0
    }
1235
1236
0
    return true;
1237
0
}
1238
1239
/************************************************************************/
1240
/*                           GetFieldCount()                            */
1241
/************************************************************************/
1242
1243
/**
1244
 * \fn int OGRFeature::GetFieldCount() const;
1245
 *
1246
 * \brief Fetch number of fields on this feature.
1247
 * This will always be the same
1248
 * as the field count for the OGRFeatureDefn.
1249
 *
1250
 * This method is the same as the C function OGR_F_GetFieldCount().
1251
 *
1252
 * @return count of fields.
1253
 */
1254
1255
/************************************************************************/
1256
/*                        OGR_F_GetFieldCount()                         */
1257
/************************************************************************/
1258
1259
/**
1260
 * \brief Fetch number of fields on this feature
1261
 * This will always be the same
1262
 * as the field count for the OGRFeatureDefn.
1263
 *
1264
 * This function is the same as the C++ method OGRFeature::GetFieldCount().
1265
 *
1266
 * @param hFeat handle to the feature to get the fields count from.
1267
 * @return count of fields.
1268
 */
1269
1270
int OGR_F_GetFieldCount(OGRFeatureH hFeat)
1271
1272
0
{
1273
0
    VALIDATE_POINTER1(hFeat, "OGR_F_GetFieldCount", 0);
1274
1275
0
    return OGRFeature::FromHandle(hFeat)->GetFieldCount();
1276
0
}
1277
1278
/************************************************************************/
1279
/*                          GetFieldDefnRef()                           */
1280
/************************************************************************/
1281
1282
/**
1283
 * \fn const OGRFieldDefn *OGRFeature::GetFieldDefnRef( int iField ) const;
1284
 *
1285
 * \brief Fetch definition for this field.
1286
 *
1287
 * This method is the same as the C function OGR_F_GetFieldDefnRef().
1288
 *
1289
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
1290
 *
1291
 * @return the field definition (from the OGRFeatureDefn).  This is an
1292
 * internal reference, and should not be deleted or modified.
1293
 */
1294
1295
/************************************************************************/
1296
/*                       OGR_F_GetFieldDefnRef()                        */
1297
/************************************************************************/
1298
1299
/**
1300
 * \brief Fetch definition for this field.
1301
 *
1302
 * This function is the same as the C++ method OGRFeature::GetFieldDefnRef().
1303
 *
1304
 * @param hFeat handle to the feature on which the field is found.
1305
 * @param i the field to fetch, from 0 to GetFieldCount()-1.
1306
 *
1307
 * @return a handle to the field definition (from the OGRFeatureDefn).
1308
 * This is an internal reference, and should not be deleted or modified.
1309
 */
1310
1311
OGRFieldDefnH OGR_F_GetFieldDefnRef(OGRFeatureH hFeat, int i)
1312
1313
0
{
1314
0
    VALIDATE_POINTER1(hFeat, "OGR_F_GetFieldDefnRef", nullptr);
1315
1316
0
    OGRFeature *poFeat = OGRFeature::FromHandle(hFeat);
1317
1318
0
    return OGRFieldDefn::ToHandle(
1319
0
        const_cast<OGRFieldDefn *>(poFeat->GetFieldDefnRef(i)));
1320
0
}
1321
1322
/************************************************************************/
1323
/*                           GetFieldIndex()                            */
1324
/************************************************************************/
1325
1326
/**
1327
 * \fn int OGRFeature::GetFieldIndex( const char * pszName ) const;
1328
 *
1329
 * \brief Fetch the field index given field name.
1330
 *
1331
 * This is a cover for the OGRFeatureDefn::GetFieldIndex() method.
1332
 *
1333
 * This method is the same as the C function OGR_F_GetFieldIndex().
1334
 *
1335
 * @param pszName the name of the field to search for.
1336
 *
1337
 * @return the field index, or -1 if no matching field is found.
1338
 */
1339
1340
/************************************************************************/
1341
/*                        OGR_F_GetFieldIndex()                         */
1342
/************************************************************************/
1343
1344
/**
1345
 * \brief Fetch the field index given field name.
1346
 *
1347
 * This is a cover for the OGRFeatureDefn::GetFieldIndex() method.
1348
 *
1349
 * This function is the same as the C++ method OGRFeature::GetFieldIndex().
1350
 *
1351
 * @param hFeat handle to the feature on which the field is found.
1352
 * @param pszName the name of the field to search for.
1353
 *
1354
 * @return the field index, or -1 if no matching field is found.
1355
 */
1356
1357
int OGR_F_GetFieldIndex(OGRFeatureH hFeat, const char *pszName)
1358
1359
0
{
1360
0
    VALIDATE_POINTER1(hFeat, "OGR_F_GetFieldIndex", 0);
1361
1362
0
    return OGRFeature::FromHandle(hFeat)->GetFieldIndex(pszName);
1363
0
}
1364
1365
/************************************************************************/
1366
/*                         GetGeomFieldCount()                          */
1367
/************************************************************************/
1368
1369
/**
1370
 * \fn int OGRFeature::GetGeomFieldCount() const;
1371
 *
1372
 * \brief Fetch number of geometry fields on this feature.
1373
 * This will always be the same
1374
 * as the geometry field count for the OGRFeatureDefn.
1375
 *
1376
 * This method is the same as the C function OGR_F_GetGeomFieldCount().
1377
 *
1378
 * @return count of geometry fields.
1379
 *
1380
 */
1381
1382
/************************************************************************/
1383
/*                      OGR_F_GetGeomFieldCount()                       */
1384
/************************************************************************/
1385
1386
/**
1387
 * \brief Fetch number of geometry fields on this feature
1388
 * This will always be the same
1389
 * as the geometry field count for the OGRFeatureDefn.
1390
 *
1391
 * This function is the same as the C++ method OGRFeature::GetGeomFieldCount().
1392
 *
1393
 * @param hFeat handle to the feature to get the geometry fields count from.
1394
 * @return count of geometry fields.
1395
 *
1396
 */
1397
1398
int OGR_F_GetGeomFieldCount(OGRFeatureH hFeat)
1399
1400
0
{
1401
0
    VALIDATE_POINTER1(hFeat, "OGR_F_GetGeomFieldCount", 0);
1402
1403
0
    return OGRFeature::FromHandle(hFeat)->GetGeomFieldCount();
1404
0
}
1405
1406
/************************************************************************/
1407
/*                        GetGeomFieldDefnRef()                         */
1408
/************************************************************************/
1409
1410
/* clang-format off */
1411
/**
1412
 * \fn const OGRGeomFieldDefn *OGRFeature::GetGeomFieldDefnRef( int iGeomField ) const;
1413
 *
1414
 * \brief Fetch definition for this geometry field.
1415
 *
1416
 * This method is the same as the C function OGR_F_GetGeomFieldDefnRef().
1417
 *
1418
 * @param iGeomField the field to fetch, from 0 to GetGeomFieldCount()-1.
1419
 *
1420
 * @return the field definition (from the OGRFeatureDefn).  This is an
1421
 * internal reference, and should not be deleted or modified.
1422
 *
1423
 */
1424
/* clang-format on */
1425
1426
/************************************************************************/
1427
/*                     OGR_F_GetGeomFieldDefnRef()                      */
1428
/************************************************************************/
1429
1430
/**
1431
 * \brief Fetch definition for this geometry field.
1432
 *
1433
 * This function is the same as the C++ method
1434
 * OGRFeature::GetGeomFieldDefnRef().
1435
 *
1436
 * @param hFeat handle to the feature on which the field is found.
1437
 * @param i the field to fetch, from 0 to GetGeomFieldCount()-1.
1438
 *
1439
 * @return a handle to the field definition (from the OGRFeatureDefn).
1440
 * This is an internal reference, and should not be deleted or modified.
1441
 *
1442
 */
1443
1444
OGRGeomFieldDefnH OGR_F_GetGeomFieldDefnRef(OGRFeatureH hFeat, int i)
1445
1446
0
{
1447
0
    VALIDATE_POINTER1(hFeat, "OGR_F_GetGeomFieldDefnRef", nullptr);
1448
1449
0
    return OGRGeomFieldDefn::ToHandle(const_cast<OGRGeomFieldDefn *>(
1450
0
        OGRFeature::FromHandle(hFeat)->GetGeomFieldDefnRef(i)));
1451
0
}
1452
1453
/************************************************************************/
1454
/*                         GetGeomFieldIndex()                          */
1455
/************************************************************************/
1456
1457
/**
1458
 * \fn int OGRFeature::GetGeomFieldIndex( const char * pszName );
1459
 *
1460
 * \brief Fetch the geometry field index given geometry field name.
1461
 *
1462
 * This is a cover for the OGRFeatureDefn::GetGeomFieldIndex() method.
1463
 *
1464
 * This method is the same as the C function OGR_F_GetGeomFieldIndex().
1465
 *
1466
 * @param pszName the name of the geometry field to search for.
1467
 *
1468
 * @return the geometry field index, or -1 if no matching geometry field is
1469
 * found.
1470
 *
1471
 */
1472
1473
/************************************************************************/
1474
/*                      OGR_F_GetGeomFieldIndex()                       */
1475
/************************************************************************/
1476
1477
/**
1478
 * \brief Fetch the geometry field index given geometry field name.
1479
 *
1480
 * This is a cover for the OGRFeatureDefn::GetGeomFieldIndex() method.
1481
 *
1482
 * This function is the same as the C++ method OGRFeature::GetGeomFieldIndex().
1483
 *
1484
 * @param hFeat handle to the feature on which the geometry field is found.
1485
 * @param pszName the name of the geometry field to search for.
1486
 *
1487
 * @return the geometry field index, or -1 if no matching geometry field is
1488
 * found.
1489
 *
1490
 */
1491
1492
int OGR_F_GetGeomFieldIndex(OGRFeatureH hFeat, const char *pszName)
1493
1494
0
{
1495
0
    VALIDATE_POINTER1(hFeat, "OGR_F_GetGeomFieldIndex", 0);
1496
1497
0
    return OGRFeature::FromHandle(hFeat)->GetGeomFieldIndex(pszName);
1498
0
}
1499
1500
/************************************************************************/
1501
/*                             IsFieldSet()                             */
1502
/************************************************************************/
1503
1504
/**
1505
 * \brief Test if a field has ever been assigned a value or not.
1506
 *
1507
 * This method is the same as the C function OGR_F_IsFieldSet().
1508
 *
1509
 * @param iField the field to test.
1510
 *
1511
 * @return TRUE if the field has been set, otherwise false.
1512
 */
1513
1514
int OGRFeature::IsFieldSet(int iField) const
1515
1516
0
{
1517
0
    const int iSpecialField = iField - poDefn->GetFieldCountUnsafe();
1518
0
    if (iSpecialField >= 0)
1519
0
    {
1520
        // Special field value accessors.
1521
0
        switch (iSpecialField)
1522
0
        {
1523
0
            case SPF_FID:
1524
0
                return GetFID() != OGRNullFID;
1525
1526
0
            case SPF_OGR_GEOM_WKT:
1527
0
            case SPF_OGR_GEOMETRY:
1528
0
                return GetGeomFieldCount() > 0 && papoGeometries[0] != nullptr;
1529
1530
0
            case SPF_OGR_STYLE:
1531
0
                return GetStyleString() != nullptr;
1532
1533
0
            case SPF_OGR_GEOM_AREA:
1534
0
                if (GetGeomFieldCount() == 0 || papoGeometries[0] == nullptr)
1535
0
                    return FALSE;
1536
1537
0
                return OGR_G_Area(OGRGeometry::ToHandle(papoGeometries[0])) !=
1538
0
                       0.0;
1539
1540
0
            default:
1541
0
                return FALSE;
1542
0
        }
1543
0
    }
1544
0
    else
1545
0
    {
1546
0
        return !OGR_RawField_IsUnset(&pauFields[iField]);
1547
0
    }
1548
0
}
1549
1550
/************************************************************************/
1551
/*                          OGR_F_IsFieldSet()                          */
1552
/************************************************************************/
1553
1554
/**
1555
 * \brief Test if a field has ever been assigned a value or not.
1556
 *
1557
 * This function is the same as the C++ method OGRFeature::IsFieldSet().
1558
 *
1559
 * @param hFeat handle to the feature on which the field is.
1560
 * @param iField the field to test.
1561
 *
1562
 * @return TRUE if the field has been set, otherwise false.
1563
 */
1564
1565
int OGR_F_IsFieldSet(OGRFeatureH hFeat, int iField)
1566
1567
0
{
1568
0
    VALIDATE_POINTER1(hFeat, "OGR_F_IsFieldSet", 0);
1569
1570
0
    const OGRFeature *poFeature = OGRFeature::FromHandle(hFeat);
1571
0
    if (iField < 0 || iField >= poFeature->GetFieldCount())
1572
0
    {
1573
0
        CPLError(CE_Failure, CPLE_AppDefined, "Invalid index : %d", iField);
1574
0
        return FALSE;
1575
0
    }
1576
1577
0
    return poFeature->IsFieldSet(iField);
1578
0
}
1579
1580
/************************************************************************/
1581
/*                             UnsetField()                             */
1582
/************************************************************************/
1583
1584
/**
1585
 * \brief Clear a field, marking it as unset.
1586
 *
1587
 * This method is the same as the C function OGR_F_UnsetField().
1588
 *
1589
 * @param iField the field to unset.
1590
 */
1591
1592
void OGRFeature::UnsetField(int iField)
1593
1594
0
{
1595
0
    const OGRFieldDefn *poFDefn = poDefn->GetFieldDefn(iField);
1596
1597
0
    if (poFDefn == nullptr || !IsFieldSet(iField))
1598
0
        return;
1599
1600
0
    if (!IsFieldNull(iField))
1601
0
    {
1602
0
        switch (poFDefn->GetType())
1603
0
        {
1604
0
            case OFTRealList:
1605
0
            case OFTIntegerList:
1606
0
            case OFTInteger64List:
1607
0
                CPLFree(pauFields[iField].IntegerList.paList);
1608
0
                break;
1609
1610
0
            case OFTStringList:
1611
0
                CSLDestroy(pauFields[iField].StringList.paList);
1612
0
                break;
1613
1614
0
            case OFTString:
1615
0
                CPLFree(pauFields[iField].String);
1616
0
                break;
1617
1618
0
            case OFTBinary:
1619
0
                CPLFree(pauFields[iField].Binary.paData);
1620
0
                break;
1621
1622
0
            default:
1623
0
                break;
1624
0
        }
1625
0
    }
1626
1627
0
    OGR_RawField_SetUnset(&pauFields[iField]);
1628
0
}
1629
1630
/************************************************************************/
1631
/*                          OGR_F_UnsetField()                          */
1632
/************************************************************************/
1633
1634
/**
1635
 * \brief Clear a field, marking it as unset.
1636
 *
1637
 * This function is the same as the C++ method OGRFeature::UnsetField().
1638
 *
1639
 * @param hFeat handle to the feature on which the field is.
1640
 * @param iField the field to unset.
1641
 */
1642
1643
void OGR_F_UnsetField(OGRFeatureH hFeat, int iField)
1644
1645
0
{
1646
0
    VALIDATE_POINTER0(hFeat, "OGR_F_UnsetField");
1647
1648
0
    OGRFeature::FromHandle(hFeat)->UnsetField(iField);
1649
0
}
1650
1651
/************************************************************************/
1652
/*                            IsFieldNull()                             */
1653
/************************************************************************/
1654
1655
/**
1656
 * \brief Test if a field is null.
1657
 *
1658
 * This method is the same as the C function OGR_F_IsFieldNull().
1659
 *
1660
 * @param iField the field to test.
1661
 *
1662
 * @return TRUE if the field is null, otherwise false.
1663
 *
1664
 */
1665
1666
bool OGRFeature::IsFieldNull(int iField) const
1667
1668
0
{
1669
0
    const int iSpecialField = iField - poDefn->GetFieldCountUnsafe();
1670
0
    if (iSpecialField >= 0)
1671
0
    {
1672
        // FIXME?
1673
0
        return false;
1674
0
    }
1675
0
    else
1676
0
    {
1677
0
        return CPL_TO_BOOL(OGR_RawField_IsNull(&pauFields[iField]));
1678
0
    }
1679
0
}
1680
1681
/************************************************************************/
1682
/*                         OGR_F_IsFieldNull()                          */
1683
/************************************************************************/
1684
1685
/**
1686
 * \brief Test if a field is null.
1687
 *
1688
 * This function is the same as the C++ method OGRFeature::IsFieldNull().
1689
 *
1690
 * @param hFeat handle to the feature on which the field is.
1691
 * @param iField the field to test.
1692
 *
1693
 * @return TRUE if the field is null, otherwise false.
1694
 *
1695
 */
1696
1697
int OGR_F_IsFieldNull(OGRFeatureH hFeat, int iField)
1698
1699
0
{
1700
0
    VALIDATE_POINTER1(hFeat, "OGR_F_IsFieldNull", 0);
1701
1702
0
    const OGRFeature *poFeature = OGRFeature::FromHandle(hFeat);
1703
0
    if (iField < 0 || iField >= poFeature->GetFieldCount())
1704
0
    {
1705
0
        CPLError(CE_Failure, CPLE_AppDefined, "Invalid index : %d", iField);
1706
0
        return FALSE;
1707
0
    }
1708
1709
0
    return poFeature->IsFieldNull(iField);
1710
0
}
1711
1712
/************************************************************************/
1713
/*                         IsFieldSetAndNull()                          */
1714
/************************************************************************/
1715
1716
/**
1717
 * \brief Test if a field is set and not null.
1718
 *
1719
 * This method is the same as the C function OGR_F_IsFieldSetAndNotNull().
1720
 *
1721
 * @param iField the field to test.
1722
 *
1723
 * @return TRUE if the field is set and not null, otherwise false.
1724
 *
1725
 */
1726
1727
bool OGRFeature::IsFieldSetAndNotNull(int iField) const
1728
1729
0
{
1730
0
    const int iSpecialField = iField - poDefn->GetFieldCountUnsafe();
1731
0
    if (iSpecialField >= 0)
1732
0
    {
1733
0
        return CPL_TO_BOOL(IsFieldSet(iField));
1734
0
    }
1735
0
    else
1736
0
    {
1737
0
        return IsFieldSetAndNotNullUnsafe(iField);
1738
0
    }
1739
0
}
1740
1741
/************************************************************************/
1742
/*                     OGR_F_IsFieldSetAndNotNull()                     */
1743
/************************************************************************/
1744
1745
/**
1746
 * \brief Test if a field is set and not null.
1747
 *
1748
 * This function is the same as the C++ method
1749
 * OGRFeature::IsFieldSetAndNotNull().
1750
 *
1751
 * @param hFeat handle to the feature on which the field is.
1752
 * @param iField the field to test.
1753
 *
1754
 * @return TRUE if the field is set and not null, otherwise false.
1755
 *
1756
 */
1757
1758
int OGR_F_IsFieldSetAndNotNull(OGRFeatureH hFeat, int iField)
1759
1760
0
{
1761
0
    VALIDATE_POINTER1(hFeat, "OGR_F_IsFieldSetAndNotNull", 0);
1762
1763
0
    OGRFeature *poFeature = OGRFeature::FromHandle(hFeat);
1764
1765
0
    if (iField < 0 || iField >= poFeature->GetFieldCount())
1766
0
    {
1767
0
        CPLError(CE_Failure, CPLE_AppDefined, "Invalid index : %d", iField);
1768
0
        return FALSE;
1769
0
    }
1770
1771
0
    return poFeature->IsFieldSetAndNotNull(iField);
1772
0
}
1773
1774
/************************************************************************/
1775
/*                            SetFieldNull()                            */
1776
/************************************************************************/
1777
1778
/**
1779
 * \brief Clear a field, marking it as null.
1780
 *
1781
 * This method is the same as the C function OGR_F_SetFieldNull().
1782
 *
1783
 * @param iField the field to set to null.
1784
 *
1785
 */
1786
1787
void OGRFeature::SetFieldNull(int iField)
1788
1789
0
{
1790
0
    const OGRFieldDefn *poFDefn = poDefn->GetFieldDefn(iField);
1791
1792
0
    if (poFDefn == nullptr || IsFieldNull(iField))
1793
0
        return;
1794
1795
0
    if (IsFieldSet(iField))
1796
0
    {
1797
0
        switch (poFDefn->GetType())
1798
0
        {
1799
0
            case OFTRealList:
1800
0
            case OFTIntegerList:
1801
0
            case OFTInteger64List:
1802
0
                CPLFree(pauFields[iField].IntegerList.paList);
1803
0
                break;
1804
1805
0
            case OFTStringList:
1806
0
                CSLDestroy(pauFields[iField].StringList.paList);
1807
0
                break;
1808
1809
0
            case OFTString:
1810
0
                CPLFree(pauFields[iField].String);
1811
0
                break;
1812
1813
0
            case OFTBinary:
1814
0
                CPLFree(pauFields[iField].Binary.paData);
1815
0
                break;
1816
1817
0
            default:
1818
0
                break;
1819
0
        }
1820
0
    }
1821
1822
0
    OGR_RawField_SetNull(&pauFields[iField]);
1823
0
}
1824
1825
/************************************************************************/
1826
/*                         OGR_F_SetFieldNull()                         */
1827
/************************************************************************/
1828
1829
/**
1830
 * \brief Clear a field, marking it as null.
1831
 *
1832
 * This function is the same as the C++ method OGRFeature::SetFieldNull().
1833
 *
1834
 * @param hFeat handle to the feature on which the field is.
1835
 * @param iField the field to set to null.
1836
 *
1837
 */
1838
1839
void OGR_F_SetFieldNull(OGRFeatureH hFeat, int iField)
1840
1841
0
{
1842
0
    VALIDATE_POINTER0(hFeat, "OGR_F_SetFieldNull");
1843
1844
0
    OGRFeature::FromHandle(hFeat)->SetFieldNull(iField);
1845
0
}
1846
1847
/************************************************************************/
1848
/*                              operator[]                              */
1849
/************************************************************************/
1850
1851
/**
1852
 * \brief Return a field value.
1853
 *
1854
 * @param iField the field to fetch, from 0 to GetFieldCount()-1. This is not
1855
 *               checked by the method !
1856
 *
1857
 * @return the field value.
1858
 */
1859
const OGRFeature::FieldValue OGRFeature::operator[](int iField) const
1860
0
{
1861
0
    return {this, iField};
1862
0
}
1863
1864
/**
1865
 * \brief Return a field value.
1866
 *
1867
 * @param iField the field to fetch, from 0 to GetFieldCount()-1. This is not
1868
 *               checked by the method !
1869
 *
1870
 * @return the field value.
1871
 */
1872
OGRFeature::FieldValue OGRFeature::operator[](int iField)
1873
0
{
1874
0
    return {this, iField};
1875
0
}
1876
1877
/**
1878
 * \brief Return a field value.
1879
 *
1880
 * @param pszFieldName field name
1881
 *
1882
 * @return the field value, or throw a FieldNotFoundException if not found.
1883
 */
1884
const OGRFeature::FieldValue
1885
OGRFeature::operator[](const char *pszFieldName) const
1886
0
{
1887
0
    int iField = GetFieldIndex(pszFieldName);
1888
0
    if (iField < 0)
1889
0
        throw FieldNotFoundException();
1890
0
    return {this, iField};
1891
0
}
1892
1893
/**
1894
 * \brief Return a field value.
1895
 *
1896
 * @param pszFieldName field name
1897
 *
1898
 * @return the field value, or throw a FieldNotFoundException if not found.
1899
 */
1900
OGRFeature::FieldValue OGRFeature::operator[](const char *pszFieldName)
1901
0
{
1902
0
    int iField = GetFieldIndex(pszFieldName);
1903
0
    if (iField < 0)
1904
0
        throw FieldNotFoundException();
1905
0
    return {this, iField};
1906
0
}
1907
1908
/************************************************************************/
1909
/*                           GetRawFieldRef()                           */
1910
/************************************************************************/
1911
1912
/**
1913
 * \fn OGRField *OGRFeature::GetRawFieldRef( int iField );
1914
 *
1915
 * \brief Fetch a pointer to the internal field value given the index.
1916
 *
1917
 * This method is the same as the C function OGR_F_GetRawFieldRef().
1918
 *
1919
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
1920
 *
1921
 * @return the returned pointer is to an internal data structure, and should
1922
 * not be freed, or modified.
1923
 */
1924
1925
/**
1926
 * \fn const OGRField *OGRFeature::GetRawFieldRef( int iField ) const;
1927
 *
1928
 * \brief Fetch a pointer to the internal field value given the index.
1929
 *
1930
 * This method is the same as the C function OGR_F_GetRawFieldRef().
1931
 *
1932
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
1933
 *
1934
 * @return the returned pointer is to an internal data structure, and should
1935
 * not be freed, or modified.
1936
 */
1937
1938
/************************************************************************/
1939
/*                        OGR_F_GetRawFieldRef()                        */
1940
/************************************************************************/
1941
1942
/**
1943
 * \brief Fetch a handle to the internal field value given the index.
1944
 *
1945
 * This function is the same as the C++ method OGRFeature::GetRawFieldRef().
1946
 *
1947
 * @param hFeat handle to the feature on which field is found.
1948
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
1949
 *
1950
 * @return the returned handle is to an internal data structure, and should
1951
 * not be freed, or modified.
1952
 */
1953
1954
OGRField *OGR_F_GetRawFieldRef(OGRFeatureH hFeat, int iField)
1955
1956
0
{
1957
0
    VALIDATE_POINTER1(hFeat, "OGR_F_GetRawFieldRef", nullptr);
1958
1959
0
    return OGRFeature::FromHandle(hFeat)->GetRawFieldRef(iField);
1960
0
}
1961
1962
/************************************************************************/
1963
/*                         GetFieldAsInteger()                          */
1964
/************************************************************************/
1965
1966
/**
1967
 * \fn OGRFeature::GetFieldAsInteger( const char* pszFName ) const
1968
 * \brief Fetch field value as integer.
1969
 *
1970
 * OFTString features will be translated using atoi().  OFTReal fields
1971
 * will be cast to integer. OFTInteger64 are demoted to 32 bit, with
1972
 * clamping if out-of-range. Other field types, or errors will result in
1973
 * a return value of zero.
1974
 *
1975
 * @param pszFName the name of the field to fetch.
1976
 *
1977
 * @return the field value.
1978
 */
1979
1980
/**
1981
 * \brief Fetch field value as integer.
1982
 *
1983
 * OFTString features will be translated using atoi().  OFTReal fields
1984
 * will be cast to integer. OFTInteger64 are demoted to 32 bit, with
1985
 * clamping if out-of-range. Other field types, or errors will result in
1986
 * a return value of zero.
1987
 *
1988
 * This method is the same as the C function OGR_F_GetFieldAsInteger().
1989
 *
1990
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
1991
 *
1992
 * @return the field value.
1993
 */
1994
1995
int OGRFeature::GetFieldAsInteger(int iField) const
1996
1997
0
{
1998
0
    int iSpecialField = iField - poDefn->GetFieldCountUnsafe();
1999
0
    if (iSpecialField >= 0)
2000
0
    {
2001
        // Special field value accessors.
2002
0
        switch (iSpecialField)
2003
0
        {
2004
0
            case SPF_FID:
2005
0
            {
2006
0
                const int nVal = nFID > INT_MAX   ? INT_MAX
2007
0
                                 : nFID < INT_MIN ? INT_MIN
2008
0
                                                  : static_cast<int>(nFID);
2009
2010
0
                if (nVal != nFID)
2011
0
                {
2012
0
                    CPLError(CE_Warning, CPLE_AppDefined,
2013
0
                             "Field %s.FID: Integer overflow occurred when "
2014
0
                             "trying to return 64 bit integer %" PRId64
2015
0
                             ". Use GetFieldAsInteger64() instead",
2016
0
                             poDefn->GetName(), static_cast<int64_t>(nVal));
2017
0
                }
2018
0
                return nVal;
2019
0
            }
2020
2021
0
            case SPF_OGR_GEOM_AREA:
2022
0
                if (GetGeomFieldCount() == 0 || papoGeometries[0] == nullptr)
2023
0
                    return 0;
2024
0
                return static_cast<int>(
2025
0
                    OGR_G_Area(OGRGeometry::ToHandle(papoGeometries[0])));
2026
2027
0
            default:
2028
0
                return 0;
2029
0
        }
2030
0
    }
2031
2032
0
    const OGRFieldDefn *poFDefn = poDefn->GetFieldDefn(iField);
2033
2034
0
    if (poFDefn == nullptr)
2035
0
        return 0;
2036
2037
0
    if (!IsFieldSetAndNotNullUnsafe(iField))
2038
0
        return 0;
2039
2040
0
    const OGRFieldType eType = poFDefn->GetType();
2041
0
    if (eType == OFTInteger)
2042
0
    {
2043
0
        return pauFields[iField].Integer;
2044
0
    }
2045
0
    else if (eType == OFTInteger64)
2046
0
    {
2047
0
        const GIntBig nVal64 = pauFields[iField].Integer64;
2048
0
        const int nVal = nVal64 > INT_MAX   ? INT_MAX
2049
0
                         : nVal64 < INT_MIN ? INT_MIN
2050
0
                                            : static_cast<int>(nVal64);
2051
2052
0
        if (nVal != nVal64)
2053
0
        {
2054
0
            CPLError(CE_Warning, CPLE_AppDefined,
2055
0
                     "Field %s.%s: Integer overflow occurred when trying to "
2056
0
                     "return 64 bit integer %" PRId64
2057
0
                     ". Use GetFieldAsInteger64() instead",
2058
0
                     poDefn->GetName(), poFDefn->GetNameRef(),
2059
0
                     static_cast<int64_t>(nVal64));
2060
0
        }
2061
0
        return nVal;
2062
0
    }
2063
0
    else if (eType == OFTReal)
2064
0
    {
2065
0
        return static_cast<int>(pauFields[iField].Real);
2066
0
    }
2067
0
    else if (eType == OFTString)
2068
0
    {
2069
0
        if (pauFields[iField].String == nullptr)
2070
0
            return 0;
2071
0
        else
2072
0
            return atoi(pauFields[iField].String);
2073
0
    }
2074
2075
0
    return 0;
2076
0
}
2077
2078
/************************************************************************/
2079
/*                      OGR_F_GetFieldAsInteger()                       */
2080
/************************************************************************/
2081
2082
/**
2083
 * \brief Fetch field value as integer.
2084
 *
2085
 * OFTString features will be translated using atoi().  OFTReal fields
2086
 * will be cast to integer.   Other field types, or errors will result in
2087
 * a return value of zero.
2088
 *
2089
 * This function is the same as the C++ method OGRFeature::GetFieldAsInteger().
2090
 *
2091
 * @param hFeat handle to the feature that owned the field.
2092
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
2093
 *
2094
 * @return the field value.
2095
 */
2096
2097
int OGR_F_GetFieldAsInteger(OGRFeatureH hFeat, int iField)
2098
2099
0
{
2100
0
    VALIDATE_POINTER1(hFeat, "OGR_F_GetFieldAsInteger", 0);
2101
2102
0
    return OGRFeature::FromHandle(hFeat)->GetFieldAsInteger(iField);
2103
0
}
2104
2105
/************************************************************************/
2106
/*                        GetFieldAsInteger64()                         */
2107
/************************************************************************/
2108
2109
/**
2110
 * \fn OGRFeature::GetFieldAsInteger64( const char* pszFName ) const
2111
 * \brief Fetch field value as integer 64 bit.
2112
 *
2113
 * OFTInteger are promoted to 64 bit.
2114
 * OFTString features will be translated using CPLAtoGIntBig().  OFTReal fields
2115
 * will be cast to integer.   Other field types, or errors will result in
2116
 * a return value of zero.
2117
 *
2118
 * @param pszFName the name of the field to fetch.
2119
 *
2120
 * @return the field value.
2121
 */
2122
2123
/**
2124
 * \brief Fetch field value as integer 64 bit.
2125
 *
2126
 * OFTInteger are promoted to 64 bit.
2127
 * OFTString features will be translated using CPLAtoGIntBig().  OFTReal fields
2128
 * will be cast to integer.   Other field types, or errors will result in
2129
 * a return value of zero.
2130
 *
2131
 * This method is the same as the C function OGR_F_GetFieldAsInteger64().
2132
 *
2133
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
2134
 *
2135
 * @return the field value.
2136
 */
2137
2138
GIntBig OGRFeature::GetFieldAsInteger64(int iField) const
2139
2140
0
{
2141
0
    const int iSpecialField = iField - poDefn->GetFieldCountUnsafe();
2142
0
    if (iSpecialField >= 0)
2143
0
    {
2144
        // Special field value accessors.
2145
0
        switch (iSpecialField)
2146
0
        {
2147
0
            case SPF_FID:
2148
0
                return nFID;
2149
2150
0
            case SPF_OGR_GEOM_AREA:
2151
0
                if (GetGeomFieldCount() == 0 || papoGeometries[0] == nullptr)
2152
0
                    return 0;
2153
0
                return static_cast<int>(
2154
0
                    OGR_G_Area(OGRGeometry::ToHandle(papoGeometries[0])));
2155
2156
0
            default:
2157
0
                return 0;
2158
0
        }
2159
0
    }
2160
2161
0
    const OGRFieldDefn *poFDefn = poDefn->GetFieldDefn(iField);
2162
2163
0
    if (poFDefn == nullptr)
2164
0
        return 0;
2165
2166
0
    if (!IsFieldSetAndNotNullUnsafe(iField))
2167
0
        return 0;
2168
2169
0
    OGRFieldType eType = poFDefn->GetType();
2170
0
    if (eType == OFTInteger)
2171
0
    {
2172
0
        return static_cast<GIntBig>(pauFields[iField].Integer);
2173
0
    }
2174
0
    else if (eType == OFTInteger64)
2175
0
    {
2176
0
        return pauFields[iField].Integer64;
2177
0
    }
2178
0
    else if (eType == OFTReal)
2179
0
    {
2180
0
        return static_cast<GIntBig>(pauFields[iField].Real);
2181
0
    }
2182
0
    else if (eType == OFTString)
2183
0
    {
2184
0
        if (pauFields[iField].String == nullptr)
2185
0
            return 0;
2186
0
        else
2187
0
        {
2188
0
            return CPLAtoGIntBigEx(pauFields[iField].String, TRUE, nullptr);
2189
0
        }
2190
0
    }
2191
2192
0
    return 0;
2193
0
}
2194
2195
/************************************************************************/
2196
/*                     OGR_F_GetFieldAsInteger64()                      */
2197
/************************************************************************/
2198
2199
/**
2200
 * \brief Fetch field value as integer 64 bit.
2201
 *
2202
 * OFTInteger are promoted to 64 bit.
2203
 * OFTString features will be translated using CPLAtoGIntBig().  OFTReal fields
2204
 * will be cast to integer.   Other field types, or errors will result in
2205
 * a return value of zero.
2206
 *
2207
 * This function is the same as the C++ method
2208
 * OGRFeature::GetFieldAsInteger64().
2209
 *
2210
 * @param hFeat handle to the feature that owned the field.
2211
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
2212
 *
2213
 * @return the field value.
2214
 */
2215
2216
GIntBig OGR_F_GetFieldAsInteger64(OGRFeatureH hFeat, int iField)
2217
2218
0
{
2219
0
    VALIDATE_POINTER1(hFeat, "OGR_F_GetFieldAsInteger64", 0);
2220
2221
0
    return OGRFeature::FromHandle(hFeat)->GetFieldAsInteger64(iField);
2222
0
}
2223
2224
/************************************************************************/
2225
/*                          GetFieldAsDouble()                          */
2226
/************************************************************************/
2227
2228
/**
2229
 * \fn OGRFeature::GetFieldAsDouble( const char* pszFName ) const
2230
 * \brief Fetch field value as a double.
2231
 *
2232
 * OFTString features will be translated using CPLAtof().  OFTInteger and
2233
 * OFTInteger64 fields will be cast to double.  Other field types, or errors
2234
 * will result in a return value of zero.
2235
 *
2236
 * @param pszFName the name of the field to fetch.
2237
 *
2238
 * @return the field value.
2239
 */
2240
2241
/**
2242
 * \brief Fetch field value as a double.
2243
 *
2244
 * OFTString features will be translated using CPLAtof().  OFTInteger and
2245
 * OFTInteger64 fields will be cast to double.  Other field types, or errors
2246
 * will result in a return value of zero.
2247
 *
2248
 * This method is the same as the C function OGR_F_GetFieldAsDouble().
2249
 *
2250
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
2251
 *
2252
 * @return the field value.
2253
 */
2254
2255
double OGRFeature::GetFieldAsDouble(int iField) const
2256
2257
0
{
2258
0
    const int iSpecialField = iField - poDefn->GetFieldCountUnsafe();
2259
0
    if (iSpecialField >= 0)
2260
0
    {
2261
        // Special field value accessors.
2262
0
        switch (iSpecialField)
2263
0
        {
2264
0
            case SPF_FID:
2265
0
                return static_cast<double>(GetFID());
2266
2267
0
            case SPF_OGR_GEOM_AREA:
2268
0
                if (GetGeomFieldCount() == 0 || papoGeometries[0] == nullptr)
2269
0
                    return 0.0;
2270
0
                return OGR_G_Area(OGRGeometry::ToHandle(papoGeometries[0]));
2271
2272
0
            default:
2273
0
                return 0.0;
2274
0
        }
2275
0
    }
2276
2277
0
    const OGRFieldDefn *poFDefn = poDefn->GetFieldDefn(iField);
2278
2279
0
    if (poFDefn == nullptr)
2280
0
        return 0.0;
2281
2282
0
    if (!IsFieldSetAndNotNullUnsafe(iField))
2283
0
        return 0.0;
2284
2285
0
    const OGRFieldType eType = poFDefn->GetType();
2286
0
    if (eType == OFTReal)
2287
0
    {
2288
0
        return pauFields[iField].Real;
2289
0
    }
2290
0
    else if (eType == OFTInteger)
2291
0
    {
2292
0
        return pauFields[iField].Integer;
2293
0
    }
2294
0
    else if (eType == OFTInteger64)
2295
0
    {
2296
0
        return static_cast<double>(pauFields[iField].Integer64);
2297
0
    }
2298
0
    else if (eType == OFTString)
2299
0
    {
2300
0
        if (pauFields[iField].String == nullptr)
2301
0
            return 0;
2302
0
        else
2303
0
            return CPLAtof(pauFields[iField].String);
2304
0
    }
2305
2306
0
    return 0.0;
2307
0
}
2308
2309
/************************************************************************/
2310
/*                       OGR_F_GetFieldAsDouble()                       */
2311
/************************************************************************/
2312
2313
/**
2314
 * \brief Fetch field value as a double.
2315
 *
2316
 * OFTString features will be translated using CPLAtof().  OFTInteger fields
2317
 * will be cast to double.   Other field types, or errors will result in
2318
 * a return value of zero.
2319
 *
2320
 * This function is the same as the C++ method OGRFeature::GetFieldAsDouble().
2321
 *
2322
 * @param hFeat handle to the feature that owned the field.
2323
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
2324
 *
2325
 * @return the field value.
2326
 */
2327
2328
double OGR_F_GetFieldAsDouble(OGRFeatureH hFeat, int iField)
2329
2330
0
{
2331
0
    VALIDATE_POINTER1(hFeat, "OGR_F_GetFieldAsDouble", 0);
2332
2333
0
    return OGRFeature::FromHandle(hFeat)->GetFieldAsDouble(iField);
2334
0
}
2335
2336
/************************************************************************/
2337
/*                   OGRFeatureFormatDateTimeBuffer()                   */
2338
/************************************************************************/
2339
2340
static void OGRFeatureFormatDateTimeBuffer(char *szTempBuffer, size_t nMaxSize,
2341
                                           int nYear, int nMonth, int nDay,
2342
                                           int nHour, int nMinute,
2343
                                           float fSecond, int nTZFlag)
2344
0
{
2345
0
    const int ms = OGR_GET_MS(fSecond);
2346
0
    if (ms != 0)
2347
0
        CPLsnprintf(szTempBuffer, nMaxSize, "%04d/%02d/%02d %02d:%02d:%06.3f",
2348
0
                    nYear, nMonth, nDay, nHour, nMinute,
2349
0
                    static_cast<double>(fSecond));
2350
0
    else  // Default format.
2351
0
    {
2352
0
        if (std::isnan(fSecond) || fSecond < 0.0f || fSecond > 62.0f)
2353
0
        {
2354
0
            fSecond = 0.0;
2355
0
            CPLError(CE_Failure, CPLE_NotSupported,
2356
0
                     "OGRFeatureFormatDateTimeBuffer: fSecond is invalid.  "
2357
0
                     "Forcing '%f' to 0.0.",
2358
0
                     static_cast<double>(fSecond));
2359
0
        }
2360
0
        snprintf(szTempBuffer, nMaxSize, "%04d/%02d/%02d %02d:%02d:%02d", nYear,
2361
0
                 nMonth, nDay, nHour, nMinute, static_cast<int>(fSecond));
2362
0
    }
2363
2364
0
    if (nTZFlag > 1)
2365
0
    {
2366
0
        char chSign;
2367
0
        const int nOffset = (nTZFlag - 100) * 15;
2368
0
        int nHours = static_cast<int>(nOffset / 60);  // Round towards zero.
2369
0
        const int nMinutes = std::abs(nOffset - nHours * 60);
2370
2371
0
        if (nOffset < 0)
2372
0
        {
2373
0
            chSign = '-';
2374
0
            nHours = std::abs(nHours);
2375
0
        }
2376
0
        else
2377
0
        {
2378
0
            chSign = '+';
2379
0
        }
2380
2381
0
        if (nMinutes == 0)
2382
0
            snprintf(szTempBuffer + strlen(szTempBuffer),
2383
0
                     nMaxSize - strlen(szTempBuffer), "%c%02d", chSign, nHours);
2384
0
        else
2385
0
            snprintf(szTempBuffer + strlen(szTempBuffer),
2386
0
                     nMaxSize - strlen(szTempBuffer), "%c%02d%02d", chSign,
2387
0
                     nHours, nMinutes);
2388
0
    }
2389
0
}
2390
2391
/************************************************************************/
2392
/*                          GetFieldAsString()                          */
2393
/************************************************************************/
2394
2395
/**
2396
 * \fn OGRFeature::GetFieldAsString( const char* pszFName ) const
2397
 * \brief Fetch field value as a string.
2398
 *
2399
 * OFTReal, OFTInteger, OFTInteger64 fields will be translated to string using
2400
 * sprintf(), but not necessarily using the established formatting rules.
2401
 * OFTDateTime fields are formatted with "YYYY/MM/DD HH:MM:SS[.sss]+ZZ"
2402
 * (note this is not a ISO-8601 compliant string. Use
2403
 * GetFieldAsISO8601DateTime())
2404
 * OFTDate fields are formatted as "YYYY/MM/DD"
2405
 * OFTTime fields are formatted as "HH:MM:SS[.sss]"
2406
 * OFTRealList, OFTIntegerList, OFTInteger64List, OFTStringList fields are
2407
 * formatted as "(number_of_values:val1,val2,...,valN)"
2408
 * OFTBinary fields are formatted as an hexadecimal representation.
2409
 * Other field types, or errors will result in a return of an empty string.
2410
 *
2411
 * @param pszFName the name of the field to fetch.
2412
 *
2413
 * @return the field value.  This string is internal, and should not be
2414
 * modified, or freed.  Its lifetime may be very brief.
2415
 */
2416
2417
/**
2418
 * \brief Fetch field value as a string.
2419
 *
2420
 * OFTReal, OFTInteger, OFTInteger64 fields will be translated to string using
2421
 * sprintf(), but not necessarily using the established formatting rules.
2422
 * OFTDateTime fields are formatted with "YYYY/MM/DD HH:MM:SS[.sss]+ZZ"
2423
 * (note this is not a ISO-8601 compliant string. Use
2424
 * GetFieldAsISO8601DateTime())
2425
 * OFTDate fields are formatted as "YYYY/MM/DD"
2426
 * OFTTime fields are formatted as "HH:MM:SS[.sss]"
2427
 * OFTRealList, OFTIntegerList, OFTInteger64List, OFTStringList fields are
2428
 * formatted as "(number_of_values:val1,val2,...,valN)"
2429
 * OFTBinary fields are formatted as an hexadecimal representation.
2430
 * Other field types, or errors will result in a return of an empty string.
2431
 *
2432
 * This method is the same as the C function OGR_F_GetFieldAsString().
2433
 *
2434
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
2435
 *
2436
 * @return the field value.  This string is internal, and should not be
2437
 * modified, or freed.  Its lifetime may be very brief.
2438
 */
2439
2440
const char *OGRFeature::GetFieldAsString(int iField) const
2441
2442
0
{
2443
0
    CPLFree(m_pszTmpFieldValue);
2444
0
    m_pszTmpFieldValue = nullptr;
2445
2446
0
    const int iSpecialField = iField - poDefn->GetFieldCountUnsafe();
2447
0
    if (iSpecialField >= 0)
2448
0
    {
2449
        // Special field value accessors.
2450
0
        switch (iSpecialField)
2451
0
        {
2452
0
            case SPF_FID:
2453
0
            {
2454
0
                constexpr size_t MAX_SIZE = 20 + 1;
2455
0
                m_pszTmpFieldValue = static_cast<char *>(CPLMalloc(MAX_SIZE));
2456
0
                CPLsnprintf(m_pszTmpFieldValue, MAX_SIZE, CPL_FRMT_GIB,
2457
0
                            GetFID());
2458
0
                return m_pszTmpFieldValue;
2459
0
            }
2460
2461
0
            case SPF_OGR_GEOMETRY:
2462
0
                if (GetGeomFieldCount() > 0 && papoGeometries[0] != nullptr)
2463
0
                    return papoGeometries[0]->getGeometryName();
2464
0
                else
2465
0
                    return "";
2466
2467
0
            case SPF_OGR_STYLE:
2468
0
                if (GetStyleString() == nullptr)
2469
0
                    return "";
2470
0
                else
2471
0
                    return GetStyleString();
2472
2473
0
            case SPF_OGR_GEOM_WKT:
2474
0
            {
2475
0
                if (GetGeomFieldCount() == 0 || papoGeometries[0] == nullptr)
2476
0
                    return "";
2477
2478
0
                if (papoGeometries[0]->exportToWkt(&m_pszTmpFieldValue) ==
2479
0
                    OGRERR_NONE)
2480
0
                    return m_pszTmpFieldValue;
2481
0
                else
2482
0
                    return "";
2483
0
            }
2484
2485
0
            case SPF_OGR_GEOM_AREA:
2486
0
            {
2487
0
                if (GetGeomFieldCount() == 0 || papoGeometries[0] == nullptr)
2488
0
                    return "";
2489
2490
0
                constexpr size_t MAX_SIZE = 20 + 1;
2491
0
                m_pszTmpFieldValue = static_cast<char *>(CPLMalloc(MAX_SIZE));
2492
0
                CPLsnprintf(
2493
0
                    m_pszTmpFieldValue, MAX_SIZE, "%.16g",
2494
0
                    OGR_G_Area(OGRGeometry::ToHandle(papoGeometries[0])));
2495
0
                return m_pszTmpFieldValue;
2496
0
            }
2497
2498
0
            default:
2499
0
                return "";
2500
0
        }
2501
0
    }
2502
2503
0
    const OGRFieldDefn *poFDefn = poDefn->GetFieldDefn(iField);
2504
2505
0
    if (poFDefn == nullptr)
2506
0
        return "";
2507
2508
0
    if (!IsFieldSetAndNotNullUnsafe(iField))
2509
0
        return "";
2510
2511
0
    OGRFieldType eType = poFDefn->GetType();
2512
0
    if (eType == OFTString)
2513
0
    {
2514
0
        if (pauFields[iField].String == nullptr)
2515
0
            return "";
2516
0
        else
2517
0
            return pauFields[iField].String;
2518
0
    }
2519
0
    else if (eType == OFTInteger)
2520
0
    {
2521
0
        constexpr size_t MAX_SIZE = 11 + 1;
2522
0
        m_pszTmpFieldValue = static_cast<char *>(CPLMalloc(MAX_SIZE));
2523
0
        snprintf(m_pszTmpFieldValue, MAX_SIZE, "%d", pauFields[iField].Integer);
2524
0
        return m_pszTmpFieldValue;
2525
0
    }
2526
0
    else if (eType == OFTInteger64)
2527
0
    {
2528
0
        constexpr size_t MAX_SIZE = 20 + 1;
2529
0
        m_pszTmpFieldValue = static_cast<char *>(CPLMalloc(MAX_SIZE));
2530
0
        CPLsnprintf(m_pszTmpFieldValue, MAX_SIZE, CPL_FRMT_GIB,
2531
0
                    pauFields[iField].Integer64);
2532
0
        return m_pszTmpFieldValue;
2533
0
    }
2534
0
    else if (eType == OFTReal)
2535
0
    {
2536
0
        char szFormat[32] = {};
2537
0
        constexpr int TEMP_BUFFER_SIZE = 80;
2538
0
        char szTempBuffer[TEMP_BUFFER_SIZE] = {};
2539
2540
0
        if (poFDefn->GetWidth() != 0)
2541
0
        {
2542
0
            snprintf(szFormat, sizeof(szFormat), "%%.%df",
2543
0
                     poFDefn->GetPrecision());
2544
2545
0
            CPLsnprintf(szTempBuffer, TEMP_BUFFER_SIZE, szFormat,
2546
0
                        pauFields[iField].Real);
2547
0
        }
2548
0
        else
2549
0
        {
2550
0
            if (poFDefn->GetSubType() == OFSTFloat32)
2551
0
            {
2552
0
                OGRFormatFloat(szTempBuffer, TEMP_BUFFER_SIZE,
2553
0
                               static_cast<float>(pauFields[iField].Real), -1,
2554
0
                               'g');
2555
0
            }
2556
0
            else
2557
0
            {
2558
0
                strcpy(szFormat, "%.15g");
2559
2560
0
                CPLsnprintf(szTempBuffer, TEMP_BUFFER_SIZE, szFormat,
2561
0
                            pauFields[iField].Real);
2562
0
            }
2563
0
        }
2564
2565
0
        m_pszTmpFieldValue = VSI_STRDUP_VERBOSE(szTempBuffer);
2566
0
        if (m_pszTmpFieldValue == nullptr)
2567
0
            return "";
2568
0
        return m_pszTmpFieldValue;
2569
0
    }
2570
0
    else if (eType == OFTDateTime)
2571
0
    {
2572
        // "YYYY/MM/DD HH:MM:SS.sss+ZZ"
2573
0
        constexpr size_t EXTRA_SPACE_FOR_NEGATIVE_OR_LARGE_YEARS = 5;
2574
0
        constexpr size_t MAX_SIZE =
2575
0
            26 + EXTRA_SPACE_FOR_NEGATIVE_OR_LARGE_YEARS + 1;
2576
0
        m_pszTmpFieldValue = static_cast<char *>(CPLMalloc(MAX_SIZE));
2577
0
        OGRFeatureFormatDateTimeBuffer(
2578
0
            m_pszTmpFieldValue, MAX_SIZE, pauFields[iField].Date.Year,
2579
0
            pauFields[iField].Date.Month, pauFields[iField].Date.Day,
2580
0
            pauFields[iField].Date.Hour, pauFields[iField].Date.Minute,
2581
0
            pauFields[iField].Date.Second, pauFields[iField].Date.TZFlag);
2582
2583
0
        return m_pszTmpFieldValue;
2584
0
    }
2585
0
    else if (eType == OFTDate)
2586
0
    {
2587
0
        constexpr size_t EXTRA_SPACE_FOR_NEGATIVE_OR_LARGE_YEARS = 5;
2588
0
        constexpr size_t MAX_SIZE =
2589
0
            10 + EXTRA_SPACE_FOR_NEGATIVE_OR_LARGE_YEARS + 1;
2590
0
        m_pszTmpFieldValue = static_cast<char *>(CPLMalloc(MAX_SIZE));
2591
0
        snprintf(m_pszTmpFieldValue, MAX_SIZE, "%04d/%02d/%02d",
2592
0
                 pauFields[iField].Date.Year, pauFields[iField].Date.Month,
2593
0
                 pauFields[iField].Date.Day);
2594
0
        return m_pszTmpFieldValue;
2595
0
    }
2596
0
    else if (eType == OFTTime)
2597
0
    {
2598
0
        constexpr size_t EXTRA_SPACE_TO_MAKE_GCC_HAPPY = 2;
2599
0
        constexpr size_t MAX_SIZE = 12 + EXTRA_SPACE_TO_MAKE_GCC_HAPPY + 1;
2600
0
        m_pszTmpFieldValue = static_cast<char *>(CPLMalloc(MAX_SIZE));
2601
0
        const int ms = OGR_GET_MS(pauFields[iField].Date.Second);
2602
0
        if (ms != 0 || std::isnan(pauFields[iField].Date.Second))
2603
0
            snprintf(m_pszTmpFieldValue, MAX_SIZE, "%02d:%02d:%06.3f",
2604
0
                     pauFields[iField].Date.Hour, pauFields[iField].Date.Minute,
2605
0
                     static_cast<double>(pauFields[iField].Date.Second));
2606
0
        else
2607
0
            snprintf(m_pszTmpFieldValue, MAX_SIZE, "%02d:%02d:%02d",
2608
0
                     pauFields[iField].Date.Hour, pauFields[iField].Date.Minute,
2609
0
                     static_cast<int>(pauFields[iField].Date.Second));
2610
2611
0
        return m_pszTmpFieldValue;
2612
0
    }
2613
0
    else if (eType == OFTIntegerList)
2614
0
    {
2615
0
        char szItem[32] = {};
2616
0
        const int nCount = pauFields[iField].IntegerList.nCount;
2617
0
        CPLString osBuffer;
2618
2619
0
        osBuffer.Printf("(%d:", nCount);
2620
0
        for (int i = 0; i < nCount; i++)
2621
0
        {
2622
0
            snprintf(szItem, sizeof(szItem), "%d",
2623
0
                     pauFields[iField].IntegerList.paList[i]);
2624
0
            if (i > 0)
2625
0
                osBuffer += ',';
2626
0
            osBuffer += szItem;
2627
0
        }
2628
0
        osBuffer += ')';
2629
2630
0
        m_pszTmpFieldValue = VSI_STRDUP_VERBOSE(osBuffer.c_str());
2631
0
        if (m_pszTmpFieldValue == nullptr)
2632
0
            return "";
2633
0
        return m_pszTmpFieldValue;
2634
0
    }
2635
0
    else if (eType == OFTInteger64List)
2636
0
    {
2637
0
        char szItem[32] = {};
2638
0
        const int nCount = pauFields[iField].Integer64List.nCount;
2639
0
        CPLString osBuffer;
2640
2641
0
        osBuffer.Printf("(%d:", nCount);
2642
0
        for (int i = 0; i < nCount; i++)
2643
0
        {
2644
0
            CPLsnprintf(szItem, sizeof(szItem), CPL_FRMT_GIB,
2645
0
                        pauFields[iField].Integer64List.paList[i]);
2646
0
            if (i > 0)
2647
0
                osBuffer += ',';
2648
0
            osBuffer += szItem;
2649
0
        }
2650
0
        osBuffer += ')';
2651
2652
0
        m_pszTmpFieldValue = VSI_STRDUP_VERBOSE(osBuffer.c_str());
2653
0
        if (m_pszTmpFieldValue == nullptr)
2654
0
            return "";
2655
0
        return m_pszTmpFieldValue;
2656
0
    }
2657
0
    else if (eType == OFTRealList)
2658
0
    {
2659
0
        char szItem[40] = {};
2660
0
        char szFormat[64] = {};
2661
0
        const int nCount = pauFields[iField].RealList.nCount;
2662
0
        const bool bIsFloat32 = poFDefn->GetSubType() == OFSTFloat32;
2663
0
        const bool bIsZeroWidth = poFDefn->GetWidth() == 0;
2664
2665
0
        if (!bIsZeroWidth)
2666
0
        {
2667
0
            snprintf(szFormat, sizeof(szFormat), "%%%d.%df",
2668
0
                     poFDefn->GetWidth(), poFDefn->GetPrecision());
2669
0
        }
2670
0
        else
2671
0
            strcpy(szFormat, "%.16g");
2672
2673
0
        CPLString osBuffer;
2674
2675
0
        osBuffer.Printf("(%d:", nCount);
2676
2677
0
        for (int i = 0; i < nCount; i++)
2678
0
        {
2679
0
            if (bIsFloat32 && bIsZeroWidth)
2680
0
            {
2681
0
                OGRFormatFloat(
2682
0
                    szItem, sizeof(szItem),
2683
0
                    static_cast<float>(pauFields[iField].RealList.paList[i]),
2684
0
                    -1, 'g');
2685
0
            }
2686
0
            else
2687
0
            {
2688
0
                CPLsnprintf(szItem, sizeof(szItem), szFormat,
2689
0
                            pauFields[iField].RealList.paList[i]);
2690
0
            }
2691
0
            if (i > 0)
2692
0
                osBuffer += ',';
2693
0
            osBuffer += szItem;
2694
0
        }
2695
0
        osBuffer += ')';
2696
2697
0
        m_pszTmpFieldValue = VSI_STRDUP_VERBOSE(osBuffer.c_str());
2698
0
        if (m_pszTmpFieldValue == nullptr)
2699
0
            return "";
2700
0
        return m_pszTmpFieldValue;
2701
0
    }
2702
0
    else if (eType == OFTStringList)
2703
0
    {
2704
0
        const int nCount = pauFields[iField].StringList.nCount;
2705
2706
0
        CPLString osBuffer;
2707
2708
0
        osBuffer.Printf("(%d:", nCount);
2709
0
        for (int i = 0; i < nCount; i++)
2710
0
        {
2711
0
            const char *pszItem = pauFields[iField].StringList.paList[i];
2712
0
            if (i > 0)
2713
0
                osBuffer += ',';
2714
0
            osBuffer += pszItem;
2715
0
        }
2716
0
        osBuffer += ')';
2717
2718
0
        m_pszTmpFieldValue = VSI_STRDUP_VERBOSE(osBuffer.c_str());
2719
0
        if (m_pszTmpFieldValue == nullptr)
2720
0
            return "";
2721
0
        return m_pszTmpFieldValue;
2722
0
    }
2723
0
    else if (eType == OFTBinary)
2724
0
    {
2725
0
        const int nCount = pauFields[iField].Binary.nCount;
2726
0
        m_pszTmpFieldValue =
2727
0
            CPLBinaryToHex(nCount, pauFields[iField].Binary.paData);
2728
0
        if (m_pszTmpFieldValue == nullptr)
2729
0
            return "";
2730
0
        return m_pszTmpFieldValue;
2731
0
    }
2732
2733
0
    return "";
2734
0
}
2735
2736
/************************************************************************/
2737
/*                       OGR_F_GetFieldAsString()                       */
2738
/************************************************************************/
2739
2740
/**
2741
 * \brief Fetch field value as a string.
2742
 *
2743
 * OFTReal and OFTInteger fields will be translated to string using
2744
 * sprintf(), but not necessarily using the established formatting rules.
2745
 * Other field types, or errors will result in a return value of zero.
2746
 *
2747
 * This function is the same as the C++ method OGRFeature::GetFieldAsString().
2748
 *
2749
 * @param hFeat handle to the feature that owned the field.
2750
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
2751
 *
2752
 * @return the field value.  This string is internal, and should not be
2753
 * modified, or freed.  Its lifetime may be very brief.
2754
 */
2755
2756
const char *OGR_F_GetFieldAsString(OGRFeatureH hFeat, int iField)
2757
2758
0
{
2759
0
    VALIDATE_POINTER1(hFeat, "OGR_F_GetFieldAsString", nullptr);
2760
2761
0
    return OGRFeature::FromHandle(hFeat)->GetFieldAsString(iField);
2762
0
}
2763
2764
/************************************************************************/
2765
/*                     GetFieldAsISO8601DateTime()                      */
2766
/************************************************************************/
2767
2768
/* clang-format off */
2769
/**
2770
 * \fn OGRFeature::GetFieldAsISO8601DateTime( const char* pszFName, CSLConstList papszOptions ) const
2771
 * \brief Fetch OFTDateTime field value as a ISO8601 representation.
2772
 *
2773
 * Return a string like "YYYY-MM-DDTHH:MM:SS(.sss)?(Z|([+|-]HH:MM))?"
2774
 * Milliseconds are omitted if equal to zero.
2775
 * Other field types, or errors will result in a return of an empty string.
2776
 *
2777
 * @param pszFName the name of the field to fetch.
2778
 * @param papszOptions NULL terminated list of strings, or NULL.
2779
 * No options are defined currently.
2780
 *
2781
 * @return the field value.  This string is internal, and should not be
2782
 * modified, or freed.  Its lifetime may be very brief.
2783
 *
2784
 * @since GDAL 3.7
2785
 */
2786
/* clang-format on */
2787
2788
/**
2789
 * \brief Fetch OFTDateTime field value as a ISO8601 representation.
2790
 *
2791
 * Return a string like "YYYY-MM-DDTHH:MM:SS(.sss)?(Z|([+|-]HH:MM))?"
2792
 * Milliseconds are omitted if equal to zero.
2793
 * Other field types, or errors will result in a return of an empty string.
2794
 *
2795
 * This method is the same as the C function OGR_F_GetFieldAsISO8601DateTime().
2796
 *
2797
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
2798
 * @param papszOptions NULL terminated list of strings, or NULL.
2799
 * No options are defined currently.
2800
 *
2801
 * @return the field value.  This string is internal, and should not be
2802
 * modified, or freed.  Its lifetime may be very brief.
2803
 *
2804
 * @since GDAL 3.7
2805
 */
2806
2807
const char *OGRFeature::GetFieldAsISO8601DateTime(
2808
    int iField, CPL_UNUSED CSLConstList papszOptions) const
2809
2810
0
{
2811
0
    CPLFree(m_pszTmpFieldValue);
2812
0
    m_pszTmpFieldValue = nullptr;
2813
2814
0
    const int iSpecialField = iField - poDefn->GetFieldCountUnsafe();
2815
0
    if (iSpecialField >= 0)
2816
0
    {
2817
0
        return "";
2818
0
    }
2819
2820
0
    const OGRFieldDefn *poFDefn = poDefn->GetFieldDefn(iField);
2821
2822
0
    if (poFDefn == nullptr)
2823
0
        return "";
2824
2825
0
    if (!IsFieldSetAndNotNullUnsafe(iField))
2826
0
        return "";
2827
2828
0
    OGRFieldType eType = poFDefn->GetType();
2829
0
    if (eType != OFTDateTime)
2830
0
        return "";
2831
2832
0
    m_pszTmpFieldValue =
2833
0
        static_cast<char *>(CPLMalloc(OGR_SIZEOF_ISO8601_DATETIME_BUFFER));
2834
0
    constexpr bool bAlwaysMillisecond = false;
2835
0
    OGRGetISO8601DateTime(&pauFields[iField], bAlwaysMillisecond,
2836
0
                          m_pszTmpFieldValue);
2837
0
    return m_pszTmpFieldValue;
2838
0
    ;
2839
0
}
2840
2841
/************************************************************************/
2842
/*                  OGR_F_GetFieldAsISO8601DateTime()                   */
2843
/************************************************************************/
2844
2845
/**
2846
 * \brief Fetch OFTDateTime field value as a ISO8601 representation.
2847
 *
2848
 * Return a string like "YYYY-MM6DDTHH:MM:SS(.sss)?(Z|([+|-]HH:MM))?"
2849
 * Milliseconds are omitted if equal to zero.
2850
 * Other field types, or errors will result in a return of an empty string.
2851
 *
2852
 * This function is the same as the C++ method OGRFeature::GetFieldAsISO8601DateTime().
2853
 *
2854
 * @param hFeat handle to the feature that owned the field.
2855
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
2856
 * @param papszOptions NULL terminated list of strings, or NULL.
2857
 * No options are defined currently.
2858
 *
2859
 * @return the field value.  This string is internal, and should not be
2860
 * modified, or freed.  Its lifetime may be very brief.
2861
 *
2862
 * @since GDAL 3.7
2863
 */
2864
2865
const char *OGR_F_GetFieldAsISO8601DateTime(OGRFeatureH hFeat, int iField,
2866
                                            CSLConstList papszOptions)
2867
2868
0
{
2869
0
    VALIDATE_POINTER1(hFeat, "OGR_F_GetFieldAsISO8601DateTime", nullptr);
2870
2871
0
    return OGRFeature::FromHandle(hFeat)->GetFieldAsISO8601DateTime(
2872
0
        iField, papszOptions);
2873
0
}
2874
2875
/************************************************************************/
2876
/*                       GetFieldAsIntegerList()                        */
2877
/************************************************************************/
2878
2879
/* clang-format off */
2880
/**
2881
 * \fn OGRFeature::GetFieldAsIntegerList( const char* pszFName, int *pnCount ) const
2882
 * \brief Fetch field value as a list of integers.
2883
 *
2884
 * Currently this method only works for OFTIntegerList fields.
2885
2886
 * @param pszFName the name of the field to fetch.
2887
 * @param pnCount an integer to put the list count (number of integers) into.
2888
 *
2889
 * @return the field value.  This list is internal, and should not be
2890
 * modified, or freed.  Its lifetime may be very brief.  If *pnCount is zero
2891
 * on return the returned pointer may be NULL or non-NULL.
2892
 * OFTReal and OFTInteger fields will be translated to string using
2893
 * sprintf(), but not necessarily using the established formatting rules.
2894
 * Other field types, or errors will result in a return value of zero.
2895
 */
2896
/* clang-format on */
2897
2898
/**
2899
 * \brief Fetch field value as a list of integers.
2900
 *
2901
 * Currently this method only works for OFTIntegerList fields.
2902
 *
2903
 * This method is the same as the C function OGR_F_GetFieldAsIntegerList().
2904
 *
2905
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
2906
 * @param pnCount an integer to put the list count (number of integers) into.
2907
 *
2908
 * @return the field value.  This list is internal, and should not be
2909
 * modified, or freed.  Its lifetime may be very brief.  If *pnCount is zero
2910
 * on return the returned pointer may be NULL or non-NULL.
2911
 */
2912
2913
const int *OGRFeature::GetFieldAsIntegerList(int iField, int *pnCount) const
2914
2915
0
{
2916
0
    const OGRFieldDefn *poFDefn = poDefn->GetFieldDefn(iField);
2917
2918
0
    if (poFDefn != nullptr && IsFieldSetAndNotNullUnsafe(iField) &&
2919
0
        poFDefn->GetType() == OFTIntegerList)
2920
0
    {
2921
0
        if (pnCount != nullptr)
2922
0
            *pnCount = pauFields[iField].IntegerList.nCount;
2923
2924
0
        return pauFields[iField].IntegerList.paList;
2925
0
    }
2926
2927
0
    if (pnCount != nullptr)
2928
0
        *pnCount = 0;
2929
2930
0
    return nullptr;
2931
0
}
2932
2933
/************************************************************************/
2934
/*                    OGR_F_GetFieldAsIntegerList()                     */
2935
/************************************************************************/
2936
2937
/**
2938
 * \brief Fetch field value as a list of integers.
2939
 *
2940
 * Currently this function only works for OFTIntegerList fields.
2941
 *
2942
 * This function is the same as the C++ method
2943
 * OGRFeature::GetFieldAsIntegerList().
2944
 *
2945
 * @param hFeat handle to the feature that owned the field.
2946
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
2947
 * @param pnCount an integer to put the list count (number of integers) into.
2948
 *
2949
 * @return the field value.  This list is internal, and should not be
2950
 * modified, or freed.  Its lifetime may be very brief.  If *pnCount is zero
2951
 * on return the returned pointer may be NULL or non-NULL.
2952
 */
2953
2954
const int *OGR_F_GetFieldAsIntegerList(OGRFeatureH hFeat, int iField,
2955
                                       int *pnCount)
2956
2957
0
{
2958
0
    VALIDATE_POINTER1(hFeat, "OGR_F_GetFieldAsIntegerList", nullptr);
2959
2960
0
    return OGRFeature::FromHandle(hFeat)->GetFieldAsIntegerList(iField,
2961
0
                                                                pnCount);
2962
0
}
2963
2964
/************************************************************************/
2965
/*                      GetFieldAsInteger64List()                       */
2966
/************************************************************************/
2967
2968
/* clang-format off */
2969
/**
2970
 * \fn OGRFeature::GetFieldAsInteger64List( const char* pszFName, int *pnCount ) const
2971
 * \brief Fetch field value as a list of 64 bit integers.
2972
 *
2973
 * Currently this method only works for OFTInteger64List fields.
2974
2975
 * @param pszFName the name of the field to fetch.
2976
 * @param pnCount an integer to put the list count (number of integers) into.
2977
 *
2978
 * @return the field value.  This list is internal, and should not be
2979
 * modified, or freed.  Its lifetime may be very brief.  If *pnCount is zero
2980
 * on return the returned pointer may be NULL or non-NULL.
2981
 */
2982
/* clang-format on */
2983
2984
/**
2985
 * \brief Fetch field value as a list of 64 bit integers.
2986
 *
2987
 * Currently this method only works for OFTInteger64List fields.
2988
 *
2989
 * This method is the same as the C function OGR_F_GetFieldAsInteger64List().
2990
 *
2991
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
2992
 * @param pnCount an integer to put the list count (number of integers) into.
2993
 *
2994
 * @return the field value.  This list is internal, and should not be
2995
 * modified, or freed.  Its lifetime may be very brief.  If *pnCount is zero
2996
 * on return the returned pointer may be NULL or non-NULL.
2997
 */
2998
2999
const GIntBig *OGRFeature::GetFieldAsInteger64List(int iField,
3000
                                                   int *pnCount) const
3001
3002
0
{
3003
0
    const OGRFieldDefn *poFDefn = poDefn->GetFieldDefn(iField);
3004
3005
0
    if (poFDefn != nullptr && IsFieldSetAndNotNullUnsafe(iField) &&
3006
0
        poFDefn->GetType() == OFTInteger64List)
3007
0
    {
3008
0
        if (pnCount != nullptr)
3009
0
            *pnCount = pauFields[iField].Integer64List.nCount;
3010
3011
0
        return pauFields[iField].Integer64List.paList;
3012
0
    }
3013
3014
0
    if (pnCount != nullptr)
3015
0
        *pnCount = 0;
3016
3017
0
    return nullptr;
3018
0
}
3019
3020
/************************************************************************/
3021
/*                   OGR_F_GetFieldAsInteger64List()                    */
3022
/************************************************************************/
3023
3024
/**
3025
 * \brief Fetch field value as a list of 64 bit integers.
3026
 *
3027
 * Currently this function only works for OFTInteger64List fields.
3028
 *
3029
 * This function is the same as the C++ method
3030
 * OGRFeature::GetFieldAsInteger64List().
3031
 *
3032
 * @param hFeat handle to the feature that owned the field.
3033
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
3034
 * @param pnCount an integer to put the list count (number of integers) into.
3035
 *
3036
 * @return the field value.  This list is internal, and should not be
3037
 * modified, or freed.  Its lifetime may be very brief.  If *pnCount is zero
3038
 * on return the returned pointer may be NULL or non-NULL.
3039
 */
3040
3041
const GIntBig *OGR_F_GetFieldAsInteger64List(OGRFeatureH hFeat, int iField,
3042
                                             int *pnCount)
3043
3044
0
{
3045
0
    VALIDATE_POINTER1(hFeat, "OGR_F_GetFieldAsInteger64List", nullptr);
3046
3047
0
    return OGRFeature::FromHandle(hFeat)->GetFieldAsInteger64List(iField,
3048
0
                                                                  pnCount);
3049
0
}
3050
3051
/************************************************************************/
3052
/*                        GetFieldAsDoubleList()                        */
3053
/************************************************************************/
3054
3055
/* clang-format off */
3056
/**
3057
 * \fn OGRFeature::GetFieldAsDoubleList( const char* pszFName, int *pnCount ) const
3058
 * \brief Fetch field value as a list of doubles.
3059
 *
3060
 * Currently this method only works for OFTRealList fields.
3061
3062
 * @param pszFName the name of the field to fetch.
3063
 * @param pnCount an integer to put the list count (number of doubles) into.
3064
 *
3065
 * @return the field value.  This list is internal, and should not be
3066
 * modified, or freed.  Its lifetime may be very brief.  If *pnCount is zero
3067
 * on return the returned pointer may be NULL or non-NULL.
3068
 */
3069
/* clang-format on */
3070
3071
/**
3072
 * \brief Fetch field value as a list of doubles.
3073
 *
3074
 * Currently this method only works for OFTRealList fields.
3075
 *
3076
 * This method is the same as the C function OGR_F_GetFieldAsDoubleList().
3077
 *
3078
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
3079
 * @param pnCount an integer to put the list count (number of doubles) into.
3080
 *
3081
 * @return the field value.  This list is internal, and should not be
3082
 * modified, or freed.  Its lifetime may be very brief.  If *pnCount is zero
3083
 * on return the returned pointer may be NULL or non-NULL.
3084
 */
3085
3086
const double *OGRFeature::GetFieldAsDoubleList(int iField, int *pnCount) const
3087
3088
0
{
3089
0
    const OGRFieldDefn *poFDefn = poDefn->GetFieldDefn(iField);
3090
3091
0
    if (poFDefn != nullptr && IsFieldSetAndNotNullUnsafe(iField) &&
3092
0
        poFDefn->GetType() == OFTRealList)
3093
0
    {
3094
0
        if (pnCount != nullptr)
3095
0
            *pnCount = pauFields[iField].RealList.nCount;
3096
3097
0
        return pauFields[iField].RealList.paList;
3098
0
    }
3099
3100
0
    if (pnCount != nullptr)
3101
0
        *pnCount = 0;
3102
3103
0
    return nullptr;
3104
0
}
3105
3106
/************************************************************************/
3107
/*                     OGR_F_GetFieldAsDoubleList()                     */
3108
/************************************************************************/
3109
3110
/**
3111
 * \brief Fetch field value as a list of doubles.
3112
 *
3113
 * Currently this function only works for OFTRealList fields.
3114
 *
3115
 * This function is the same as the C++ method
3116
 * OGRFeature::GetFieldAsDoubleList().
3117
 *
3118
 * @param hFeat handle to the feature that owned the field.
3119
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
3120
 * @param pnCount an integer to put the list count (number of doubles) into.
3121
 *
3122
 * @return the field value.  This list is internal, and should not be
3123
 * modified, or freed.  Its lifetime may be very brief.  If *pnCount is zero
3124
 * on return the returned pointer may be NULL or non-NULL.
3125
 */
3126
3127
const double *OGR_F_GetFieldAsDoubleList(OGRFeatureH hFeat, int iField,
3128
                                         int *pnCount)
3129
3130
0
{
3131
0
    VALIDATE_POINTER1(hFeat, "OGR_F_GetFieldAsDoubleList", nullptr);
3132
3133
0
    return OGRFeature::FromHandle(hFeat)->GetFieldAsDoubleList(iField, pnCount);
3134
0
}
3135
3136
/************************************************************************/
3137
/*                        GetFieldAsStringList()                        */
3138
/************************************************************************/
3139
/**
3140
 * \fn OGRFeature::GetFieldAsStringList( const char* pszFName ) const
3141
 * \brief Fetch field value as a list of strings.
3142
 *
3143
 * Currently this method only works for OFTStringList fields.
3144
 *
3145
 * The returned list is terminated by a NULL pointer. The number of
3146
 * elements can also be calculated using CSLCount().
3147
 *
3148
 * @param pszFName the name of the field to fetch.
3149
 *
3150
 * @return the field value.  This list is internal, and should not be
3151
 * modified, or freed.  Its lifetime may be very brief.
3152
 */
3153
3154
/**
3155
 * \brief Fetch field value as a list of strings.
3156
 *
3157
 * Currently this method only works for OFTStringList fields.
3158
 *
3159
 * The returned list is terminated by a NULL pointer. The number of
3160
 * elements can also be calculated using CSLCount().
3161
 *
3162
 * This method is the same as the C function OGR_F_GetFieldAsStringList().
3163
 *
3164
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
3165
 *
3166
 * @return the field value.  This list is internal, and should not be
3167
 * modified, or freed.  Its lifetime may be very brief.
3168
 */
3169
3170
char **OGRFeature::GetFieldAsStringList(int iField) const
3171
3172
0
{
3173
0
    const OGRFieldDefn *poFDefn = poDefn->GetFieldDefn(iField);
3174
3175
0
    if (poFDefn == nullptr)
3176
0
        return nullptr;
3177
3178
0
    if (!IsFieldSetAndNotNullUnsafe(iField))
3179
0
        return nullptr;
3180
3181
0
    if (poFDefn->GetType() == OFTStringList)
3182
0
    {
3183
0
        return pauFields[iField].StringList.paList;
3184
0
    }
3185
3186
0
    return nullptr;
3187
0
}
3188
3189
/************************************************************************/
3190
/*                     OGR_F_GetFieldAsStringList()                     */
3191
/************************************************************************/
3192
3193
/**
3194
 * \brief Fetch field value as a list of strings.
3195
 *
3196
 * Currently this method only works for OFTStringList fields.
3197
 *
3198
 * The returned list is terminated by a NULL pointer. The number of
3199
 * elements can also be calculated using CSLCount().
3200
 *
3201
 * This function is the same as the C++ method
3202
 * OGRFeature::GetFieldAsStringList().
3203
 *
3204
 * @param hFeat handle to the feature that owned the field.
3205
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
3206
 *
3207
 * @return the field value.  This list is internal, and should not be
3208
 * modified, or freed.  Its lifetime may be very brief.
3209
 */
3210
3211
char **OGR_F_GetFieldAsStringList(OGRFeatureH hFeat, int iField)
3212
3213
0
{
3214
0
    VALIDATE_POINTER1(hFeat, "OGR_F_GetFieldAsStringList", nullptr);
3215
3216
0
    return OGRFeature::FromHandle(hFeat)->GetFieldAsStringList(iField);
3217
0
}
3218
3219
/************************************************************************/
3220
/*                          GetFieldAsBinary()                          */
3221
/************************************************************************/
3222
3223
/**
3224
 * \brief Fetch field value as binary data.
3225
 *
3226
 * This method only works for OFTBinary and OFTString fields.
3227
 *
3228
 * This method is the same as the C function OGR_F_GetFieldAsBinary().
3229
 *
3230
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
3231
 * @param pnBytes location to put the number of bytes returned.
3232
 *
3233
 * @return the field value.  This data is internal, and should not be
3234
 * modified, or freed.  Its lifetime may be very brief.
3235
 */
3236
3237
GByte *OGRFeature::GetFieldAsBinary(int iField, int *pnBytes) const
3238
3239
0
{
3240
0
    const OGRFieldDefn *poFDefn = poDefn->GetFieldDefn(iField);
3241
3242
0
    *pnBytes = 0;
3243
3244
0
    if (poFDefn == nullptr)
3245
0
        return nullptr;
3246
3247
0
    if (!IsFieldSetAndNotNullUnsafe(iField))
3248
0
        return nullptr;
3249
3250
0
    if (poFDefn->GetType() == OFTBinary)
3251
0
    {
3252
0
        *pnBytes = pauFields[iField].Binary.nCount;
3253
0
        return pauFields[iField].Binary.paData;
3254
0
    }
3255
0
    else if (poFDefn->GetType() == OFTString)
3256
0
    {
3257
0
        *pnBytes = static_cast<int>(strlen(pauFields[iField].String));
3258
0
        return reinterpret_cast<GByte *>(pauFields[iField].String);
3259
0
    }
3260
3261
0
    return nullptr;
3262
0
}
3263
3264
/************************************************************************/
3265
/*                       OGR_F_GetFieldAsBinary()                       */
3266
/************************************************************************/
3267
3268
/**
3269
 * \brief Fetch field value as binary.
3270
 *
3271
 * This method only works for OFTBinary and OFTString fields.
3272
 *
3273
 * This function is the same as the C++ method
3274
 * OGRFeature::GetFieldAsBinary().
3275
 *
3276
 * @param hFeat handle to the feature that owned the field.
3277
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
3278
 * @param pnBytes location to place count of bytes returned.
3279
 *
3280
 * @return the field value.  This list is internal, and should not be
3281
 * modified, or freed.  Its lifetime may be very brief.
3282
 */
3283
3284
GByte *OGR_F_GetFieldAsBinary(OGRFeatureH hFeat, int iField, int *pnBytes)
3285
3286
0
{
3287
0
    VALIDATE_POINTER1(hFeat, "OGR_F_GetFieldAsBinary", nullptr);
3288
0
    VALIDATE_POINTER1(pnBytes, "OGR_F_GetFieldAsBinary", nullptr);
3289
3290
0
    return OGRFeature::FromHandle(hFeat)->GetFieldAsBinary(iField, pnBytes);
3291
0
}
3292
3293
/************************************************************************/
3294
/*                         GetFieldAsDateTime()                         */
3295
/************************************************************************/
3296
3297
/**
3298
 * \brief Fetch field value as date and time.
3299
 *
3300
 * Currently this method only works for OFTDate, OFTTime and OFTDateTime fields.
3301
 *
3302
 * This method is the same as the C function OGR_F_GetFieldAsDateTime().
3303
 *
3304
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
3305
 * @param pnYear (including century)
3306
 * @param pnMonth (1-12)
3307
 * @param pnDay (1-31)
3308
 * @param pnHour (0-23)
3309
 * @param pnMinute (0-59)
3310
 * @param pfSecond (0-59 with millisecond accuracy)
3311
 * @param pnTZFlag (0=unknown, 1=localtime, 100=GMT, see data model for details)
3312
 *
3313
 * @return TRUE on success or FALSE on failure.
3314
 */
3315
3316
int OGRFeature::GetFieldAsDateTime(int iField, int *pnYear, int *pnMonth,
3317
                                   int *pnDay, int *pnHour, int *pnMinute,
3318
                                   float *pfSecond, int *pnTZFlag) const
3319
3320
0
{
3321
0
    const OGRFieldDefn *poFDefn = poDefn->GetFieldDefn(iField);
3322
3323
0
    if (poFDefn == nullptr)
3324
0
        return FALSE;
3325
3326
0
    if (!IsFieldSetAndNotNullUnsafe(iField))
3327
0
        return FALSE;
3328
3329
0
    if (poFDefn->GetType() == OFTDate || poFDefn->GetType() == OFTTime ||
3330
0
        poFDefn->GetType() == OFTDateTime)
3331
0
    {
3332
0
        if (pnYear)
3333
0
            *pnYear = pauFields[iField].Date.Year;
3334
0
        if (pnMonth)
3335
0
            *pnMonth = pauFields[iField].Date.Month;
3336
0
        if (pnDay)
3337
0
            *pnDay = pauFields[iField].Date.Day;
3338
0
        if (pnHour)
3339
0
            *pnHour = pauFields[iField].Date.Hour;
3340
0
        if (pnMinute)
3341
0
            *pnMinute = pauFields[iField].Date.Minute;
3342
0
        if (pfSecond)
3343
0
            *pfSecond = pauFields[iField].Date.Second;
3344
0
        if (pnTZFlag)
3345
0
            *pnTZFlag = pauFields[iField].Date.TZFlag;
3346
3347
0
        return TRUE;
3348
0
    }
3349
3350
0
    return FALSE;
3351
0
}
3352
3353
/**
3354
 * \brief Fetch field value as date and time.
3355
 *
3356
 * Currently this method only works for OFTDate, OFTTime and OFTDateTime fields.
3357
 *
3358
 * This method is the same as the C function OGR_F_GetFieldAsDateTime().
3359
 *
3360
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
3361
 * @param pnYear (including century)
3362
 * @param pnMonth (1-12)
3363
 * @param pnDay (1-31)
3364
 * @param pnHour (0-23)
3365
 * @param pnMinute (0-59)
3366
 * @param pnSecond (0-59)
3367
 * @param pnTZFlag (0=unknown, 1=localtime, 100=GMT, see data model for details)
3368
 *
3369
 * @return TRUE on success or FALSE on failure.
3370
 */
3371
3372
int OGRFeature::GetFieldAsDateTime(int iField, int *pnYear, int *pnMonth,
3373
                                   int *pnDay, int *pnHour, int *pnMinute,
3374
                                   int *pnSecond, int *pnTZFlag) const
3375
0
{
3376
0
    float fSecond = 0.0f;
3377
0
    const bool bRet = CPL_TO_BOOL(GetFieldAsDateTime(
3378
0
        iField, pnYear, pnMonth, pnDay, pnHour, pnMinute, &fSecond, pnTZFlag));
3379
0
    if (bRet && pnSecond)
3380
0
        *pnSecond = static_cast<int>(fSecond);
3381
0
    return bRet;
3382
0
}
3383
3384
/************************************************************************/
3385
/*                      OGR_F_GetFieldAsDateTime()                      */
3386
/************************************************************************/
3387
3388
/**
3389
 * \brief Fetch field value as date and time.
3390
 *
3391
 * Currently this method only works for OFTDate, OFTTime and OFTDateTime fields.
3392
 *
3393
 * This function is the same as the C++ method
3394
 * OGRFeature::GetFieldAsDateTime().
3395
 *
3396
 * @param hFeat handle to the feature that owned the field.
3397
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
3398
 * @param pnYear (including century)
3399
 * @param pnMonth (1-12)
3400
 * @param pnDay (1-31)
3401
 * @param pnHour (0-23)
3402
 * @param pnMinute (0-59)
3403
 * @param pnSecond (0-59)
3404
 * @param pnTZFlag (0=unknown, 1=localtime, 100=GMT, see data model for details)
3405
 *
3406
 * @return TRUE on success or FALSE on failure.
3407
 *
3408
 * @see Use OGR_F_GetFieldAsDateTimeEx() for second with millisecond accuracy.
3409
 */
3410
3411
int OGR_F_GetFieldAsDateTime(OGRFeatureH hFeat, int iField, int *pnYear,
3412
                             int *pnMonth, int *pnDay, int *pnHour,
3413
                             int *pnMinute, int *pnSecond, int *pnTZFlag)
3414
3415
0
{
3416
0
    VALIDATE_POINTER1(hFeat, "OGR_F_GetFieldAsDateTime", 0);
3417
3418
0
    float fSecond = 0.0f;
3419
0
    const bool bRet =
3420
0
        CPL_TO_BOOL(OGRFeature::FromHandle(hFeat)->GetFieldAsDateTime(
3421
0
            iField, pnYear, pnMonth, pnDay, pnHour, pnMinute, &fSecond,
3422
0
            pnTZFlag));
3423
0
    if (bRet && pnSecond)
3424
0
        *pnSecond = static_cast<int>(fSecond);
3425
0
    return bRet;
3426
0
}
3427
3428
/************************************************************************/
3429
/*                     OGR_F_GetFieldAsDateTimeEx()                     */
3430
/************************************************************************/
3431
3432
/**
3433
 * \brief Fetch field value as date and time.
3434
 *
3435
 * Currently this method only works for OFTDate, OFTTime and OFTDateTime fields.
3436
 *
3437
 * This function is the same as the C++ method
3438
 * OGRFeature::GetFieldAsDateTime().
3439
 *
3440
 * @param hFeat handle to the feature that owned the field.
3441
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
3442
 * @param pnYear (including century)
3443
 * @param pnMonth (1-12)
3444
 * @param pnDay (1-31)
3445
 * @param pnHour (0-23)
3446
 * @param pnMinute (0-59)
3447
 * @param pfSecond (0-59 with millisecond accuracy)
3448
 * @param pnTZFlag (0=unknown, 1=localtime, 100=GMT, see data model for details)
3449
 *
3450
 * @return TRUE on success or FALSE on failure.
3451
 */
3452
3453
int OGR_F_GetFieldAsDateTimeEx(OGRFeatureH hFeat, int iField, int *pnYear,
3454
                               int *pnMonth, int *pnDay, int *pnHour,
3455
                               int *pnMinute, float *pfSecond, int *pnTZFlag)
3456
3457
0
{
3458
0
    VALIDATE_POINTER1(hFeat, "OGR_F_GetFieldAsDateTimeEx", 0);
3459
3460
0
    return OGRFeature::FromHandle(hFeat)->GetFieldAsDateTime(
3461
0
        iField, pnYear, pnMonth, pnDay, pnHour, pnMinute, pfSecond, pnTZFlag);
3462
0
}
3463
3464
/************************************************************************/
3465
/*                     OGRFeatureGetIntegerValue()                      */
3466
/************************************************************************/
3467
3468
static int OGRFeatureGetIntegerValue(const OGRFeatureDefn *poDefn,
3469
                                     const OGRFieldDefn *poFDefn, int nValue)
3470
0
{
3471
0
    if (poFDefn->GetSubType() == OFSTBoolean && nValue != 0 && nValue != 1)
3472
0
    {
3473
0
        CPLError(CE_Warning, CPLE_AppDefined,
3474
0
                 "Field %s.%s: Only 0 or 1 should be passed for a OFSTBoolean "
3475
0
                 "subtype. Considering non-zero value %d as 1.",
3476
0
                 poDefn->GetName(), poFDefn->GetNameRef(), nValue);
3477
0
        nValue = 1;
3478
0
    }
3479
0
    else if (poFDefn->GetSubType() == OFSTInt16)
3480
0
    {
3481
0
        if (nValue < -32768)
3482
0
        {
3483
0
            CPLError(CE_Warning, CPLE_AppDefined,
3484
0
                     "Field %s.%s: Out-of-range value for a OFSTInt16 subtype. "
3485
0
                     "Considering value %d as -32768.",
3486
0
                     poDefn->GetName(), poFDefn->GetNameRef(), nValue);
3487
0
            nValue = -32768;
3488
0
        }
3489
0
        else if (nValue > 32767)
3490
0
        {
3491
0
            CPLError(CE_Warning, CPLE_AppDefined,
3492
0
                     "Field %s.%s: Out-of-range value for a OFSTInt16 subtype. "
3493
0
                     "Considering value %d as 32767.",
3494
0
                     poDefn->GetName(), poFDefn->GetNameRef(), nValue);
3495
0
            nValue = 32767;
3496
0
        }
3497
0
    }
3498
0
    return nValue;
3499
0
}
3500
3501
/************************************************************************/
3502
/*                      GetFieldAsSerializedJSon()                      */
3503
/************************************************************************/
3504
3505
/**
3506
 * \brief Fetch field value as a serialized JSon object.
3507
 *
3508
 * Currently this method only works for OFTString with OFSTJSON subtype,
3509
 * OFTStringList, OFTIntegerList,
3510
 * OFTInteger64List and OFTRealList
3511
 *
3512
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
3513
 *
3514
 * @return a string that must be de-allocate with CPLFree()
3515
 */
3516
char *OGRFeature::GetFieldAsSerializedJSon(int iField) const
3517
3518
0
{
3519
0
    const int iSpecialField = iField - poDefn->GetFieldCountUnsafe();
3520
0
    if (iSpecialField >= 0)
3521
0
    {
3522
0
        return nullptr;
3523
0
    }
3524
3525
0
    const OGRFieldDefn *poFDefn = poDefn->GetFieldDefn(iField);
3526
3527
0
    if (poFDefn == nullptr)
3528
0
        return nullptr;
3529
3530
0
    if (!IsFieldSetAndNotNullUnsafe(iField))
3531
0
        return nullptr;
3532
3533
0
    char *pszRet = nullptr;
3534
0
    OGRFieldType eType = poFDefn->GetType();
3535
0
    if (eType == OFTString && poFDefn->GetSubType() == OFSTJSON)
3536
0
    {
3537
0
        if (pauFields[iField].String[0] != '[' &&
3538
0
            pauFields[iField].String[0] != '{' &&
3539
0
            strcmp(pauFields[iField].String, "true") != 0 &&
3540
0
            strcmp(pauFields[iField].String, "false") != 0 &&
3541
0
            CPLGetValueType(pauFields[iField].String) == CPL_VALUE_STRING)
3542
0
        {
3543
0
            pszRet = CPLStrdup(('"' +
3544
0
                                CPLString(pauFields[iField].String)
3545
0
                                    .replaceAll('\\', "\\\\")
3546
0
                                    .replaceAll('"', "\\\"") +
3547
0
                                '"')
3548
0
                                   .c_str());
3549
0
        }
3550
0
        else
3551
0
        {
3552
0
            pszRet = CPLStrdup(pauFields[iField].String);
3553
0
        }
3554
0
    }
3555
0
    else if (eType == OFTStringList)
3556
0
    {
3557
0
        char **papszValues = GetFieldAsStringList(iField);
3558
0
        if (papszValues == nullptr)
3559
0
        {
3560
0
            pszRet = CPLStrdup("[]");
3561
0
        }
3562
0
        else
3563
0
        {
3564
0
            json_object *poObj = json_object_new_array();
3565
0
            for (int i = 0; papszValues[i] != nullptr; i++)
3566
0
            {
3567
0
                json_object_array_add(poObj,
3568
0
                                      json_object_new_string(papszValues[i]));
3569
0
            }
3570
0
            pszRet = CPLStrdup(json_object_to_json_string(poObj));
3571
0
            json_object_put(poObj);
3572
0
        }
3573
0
    }
3574
0
    else if (eType == OFTIntegerList)
3575
0
    {
3576
0
        json_object *poObj = json_object_new_array();
3577
0
        int nCount = 0;
3578
0
        const int *panValues = GetFieldAsIntegerList(iField, &nCount);
3579
0
        if (poFDefn->GetSubType() == OFSTBoolean)
3580
0
        {
3581
0
            for (int i = 0; i < nCount; i++)
3582
0
            {
3583
0
                json_object_array_add(
3584
0
                    poObj, json_object_new_boolean(panValues[i] != 0));
3585
0
            }
3586
0
        }
3587
0
        else
3588
0
        {
3589
0
            for (int i = 0; i < nCount; i++)
3590
0
            {
3591
0
                json_object_array_add(poObj, json_object_new_int(panValues[i]));
3592
0
            }
3593
0
        }
3594
0
        pszRet = CPLStrdup(json_object_to_json_string(poObj));
3595
0
        json_object_put(poObj);
3596
0
    }
3597
0
    else if (eType == OFTInteger64List)
3598
0
    {
3599
0
        json_object *poObj = json_object_new_array();
3600
0
        int nCount = 0;
3601
0
        const GIntBig *panValues = GetFieldAsInteger64List(iField, &nCount);
3602
0
        for (int i = 0; i < nCount; i++)
3603
0
        {
3604
0
            json_object_array_add(poObj, json_object_new_int64(panValues[i]));
3605
0
        }
3606
0
        pszRet = CPLStrdup(json_object_to_json_string(poObj));
3607
0
        json_object_put(poObj);
3608
0
    }
3609
0
    else if (eType == OFTRealList)
3610
0
    {
3611
0
        json_object *poObj = json_object_new_array();
3612
0
        int nCount = 0;
3613
0
        const double *padfValues = GetFieldAsDoubleList(iField, &nCount);
3614
0
        for (int i = 0; i < nCount; i++)
3615
0
        {
3616
0
            json_object_array_add(poObj, json_object_new_double(padfValues[i]));
3617
0
        }
3618
0
        pszRet = CPLStrdup(json_object_to_json_string(poObj));
3619
0
        json_object_put(poObj);
3620
0
    }
3621
3622
0
    return pszRet;
3623
0
}
3624
3625
/************************************************************************/
3626
/*                              SetField()                              */
3627
/************************************************************************/
3628
3629
/**
3630
 * \fn OGRFeature::SetField( const char* pszFName, int nValue )
3631
 * \brief Set field to integer value.
3632
 * OFTInteger, OFTInteger64 and OFTReal fields will be set directly.  OFTString
3633
 * fields will be assigned a string representation of the value, but not
3634
 * necessarily taking into account formatting constraints on this field.  Other
3635
 * field types may be unaffected.
3636
 *
3637
 * @note This method has only an effect on the in-memory feature object. If
3638
 * this object comes from a layer and the modifications must be serialized back
3639
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
3640
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
3641
 *
3642
 * @param pszFName the name of the field to set.
3643
 * @param nValue the value to assign.
3644
 */
3645
3646
/**
3647
 * \brief Set field to integer value.
3648
 *
3649
 * OFTInteger, OFTInteger64 and OFTReal fields will be set directly.  OFTString
3650
 * fields will be assigned a string representation of the value, but not
3651
 * necessarily taking into account formatting constraints on this field.  Other
3652
 * field types may be unaffected.
3653
 *
3654
 * This method is the same as the C function OGR_F_SetFieldInteger().
3655
 *
3656
 * @note This method has only an effect on the in-memory feature object. If
3657
 * this object comes from a layer and the modifications must be serialized back
3658
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
3659
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
3660
 *
3661
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
3662
 * @param nValue the value to assign.
3663
 */
3664
3665
void OGRFeature::SetField(int iField, int nValue)
3666
3667
0
{
3668
0
    const OGRFieldDefn *poFDefn = poDefn->GetFieldDefn(iField);
3669
3670
0
    if (poFDefn == nullptr)
3671
0
        return;
3672
3673
0
    OGRFieldType eType = poFDefn->GetType();
3674
0
    if (eType == OFTInteger)
3675
0
    {
3676
0
        pauFields[iField].Integer =
3677
0
            OGRFeatureGetIntegerValue(poDefn, poFDefn, nValue);
3678
0
        pauFields[iField].Set.nMarker2 = 0;
3679
0
        pauFields[iField].Set.nMarker3 = 0;
3680
0
    }
3681
0
    else if (eType == OFTInteger64)
3682
0
    {
3683
0
        pauFields[iField].Integer64 =
3684
0
            OGRFeatureGetIntegerValue(poDefn, poFDefn, nValue);
3685
0
    }
3686
0
    else if (eType == OFTReal)
3687
0
    {
3688
0
        pauFields[iField].Real = nValue;
3689
0
    }
3690
0
    else if (eType == OFTIntegerList)
3691
0
    {
3692
0
        SetField(iField, 1, &nValue);
3693
0
    }
3694
0
    else if (eType == OFTInteger64List)
3695
0
    {
3696
0
        GIntBig nVal64 = nValue;
3697
0
        SetField(iField, 1, &nVal64);
3698
0
    }
3699
0
    else if (eType == OFTRealList)
3700
0
    {
3701
0
        double dfValue = nValue;
3702
0
        SetField(iField, 1, &dfValue);
3703
0
    }
3704
0
    else if (eType == OFTString)
3705
0
    {
3706
0
        char szTempBuffer[64] = {};
3707
3708
0
        snprintf(szTempBuffer, sizeof(szTempBuffer), "%d", nValue);
3709
3710
0
        if (IsFieldSetAndNotNullUnsafe(iField))
3711
0
            CPLFree(pauFields[iField].String);
3712
3713
0
        pauFields[iField].String = VSI_STRDUP_VERBOSE(szTempBuffer);
3714
0
        if (pauFields[iField].String == nullptr)
3715
0
        {
3716
0
            OGR_RawField_SetUnset(&pauFields[iField]);
3717
0
        }
3718
0
    }
3719
0
    else if (eType == OFTStringList)
3720
0
    {
3721
0
        char szTempBuffer[64] = {};
3722
3723
0
        snprintf(szTempBuffer, sizeof(szTempBuffer), "%d", nValue);
3724
0
        char *apszValues[2] = {szTempBuffer, nullptr};
3725
0
        SetField(iField, apszValues);
3726
0
    }
3727
0
    else
3728
0
    {
3729
        // Do nothing for other field types.
3730
0
    }
3731
0
}
3732
3733
/************************************************************************/
3734
/*                       OGR_F_SetFieldInteger()                        */
3735
/************************************************************************/
3736
3737
/**
3738
 * \brief Set field to integer value.
3739
 *
3740
 * OFTInteger, OFTInteger64 and OFTReal fields will be set directly.  OFTString
3741
 * fields will be assigned a string representation of the value, but not
3742
 * necessarily taking into account formatting constraints on this field.  Other
3743
 * field types may be unaffected.
3744
 *
3745
 * This function is the same as the C++ method OGRFeature::SetField().
3746
 *
3747
 * @note This method has only an effect on the in-memory feature object. If
3748
 * this object comes from a layer and the modifications must be serialized back
3749
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
3750
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
3751
 *
3752
 * @param hFeat handle to the feature that owned the field.
3753
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
3754
 * @param nValue the value to assign.
3755
 */
3756
3757
void OGR_F_SetFieldInteger(OGRFeatureH hFeat, int iField, int nValue)
3758
3759
0
{
3760
0
    VALIDATE_POINTER0(hFeat, "OGR_F_SetFieldInteger");
3761
3762
0
    OGRFeature::FromHandle(hFeat)->SetField(iField, nValue);
3763
0
}
3764
3765
/************************************************************************/
3766
/*                              SetField()                              */
3767
/************************************************************************/
3768
3769
/**
3770
 * \fn OGRFeature::SetField( const char* pszFName, GIntBig nValue )
3771
 * \brief Set field to 64 bit integer value.
3772
 * OFTInteger, OFTInteger64 and OFTReal fields will be set directly.  OFTString
3773
 * fields will be assigned a string representation of the value, but not
3774
 * necessarily taking into account formatting constraints on this field.  Other
3775
 * field types may be unaffected.
3776
 *
3777
 * @note This method has only an effect on the in-memory feature object. If
3778
 * this object comes from a layer and the modifications must be serialized back
3779
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
3780
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
3781
 *
3782
 * @param pszFName the name of the field to set.
3783
 * @param nValue the value to assign.
3784
 */
3785
3786
/**
3787
 * \brief Set field to 64 bit integer value.
3788
 *
3789
 * OFTInteger, OFTInteger64 and OFTReal fields will be set directly.  OFTString
3790
 * fields will be assigned a string representation of the value, but not
3791
 * necessarily taking into account formatting constraints on this field.  Other
3792
 * field types may be unaffected.
3793
 *
3794
 * This method is the same as the C function OGR_F_SetFieldInteger64().
3795
 *
3796
 * @note This method has only an effect on the in-memory feature object. If
3797
 * this object comes from a layer and the modifications must be serialized back
3798
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
3799
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
3800
 *
3801
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
3802
 * @param nValue the value to assign.
3803
 */
3804
3805
void OGRFeature::SetField(int iField, GIntBig nValue)
3806
3807
0
{
3808
0
    const OGRFieldDefn *poFDefn = poDefn->GetFieldDefn(iField);
3809
3810
0
    if (poFDefn == nullptr)
3811
0
        return;
3812
3813
0
    OGRFieldType eType = poFDefn->GetType();
3814
0
    if (eType == OFTInteger)
3815
0
    {
3816
0
        const int nVal32 = nValue < INT_MIN   ? INT_MIN
3817
0
                           : nValue > INT_MAX ? INT_MAX
3818
0
                                              : static_cast<int>(nValue);
3819
3820
0
        if (nVal32 != nValue)
3821
0
        {
3822
0
            CPLError(
3823
0
                CE_Warning, CPLE_AppDefined,
3824
0
                "Field %s.%s: integer overflow occurred when trying to set "
3825
0
                "%" PRId64 " as 32 bit integer.",
3826
0
                poDefn->GetName(), poFDefn->GetNameRef(),
3827
0
                static_cast<int64_t>(nValue));
3828
0
        }
3829
0
        SetField(iField, nVal32);
3830
0
    }
3831
0
    else if (eType == OFTInteger64)
3832
0
    {
3833
0
        pauFields[iField].Integer64 = nValue;
3834
0
    }
3835
0
    else if (eType == OFTReal)
3836
0
    {
3837
0
        pauFields[iField].Real = static_cast<double>(nValue);
3838
        // Values in the range [INT64_MAX - 1023, INT64_MAX - 1]
3839
        // get converted to a double that once cast to int64_t is
3840
        // INT64_MAX + 1 ...
3841
0
        if (pauFields[iField].Real >=
3842
0
                static_cast<double>(std::numeric_limits<int64_t>::max()) ||
3843
0
            static_cast<GIntBig>(pauFields[iField].Real) != nValue)
3844
0
        {
3845
0
            CPLError(
3846
0
                CE_Warning, CPLE_AppDefined,
3847
0
                "Field %s.%s: Lossy conversion occurred when trying to set "
3848
0
                "a real field from 64 bit integer value %" PRId64 ".",
3849
0
                poDefn->GetName(), poFDefn->GetNameRef(),
3850
0
                static_cast<int64_t>(nValue));
3851
0
        }
3852
0
    }
3853
0
    else if (eType == OFTIntegerList)
3854
0
    {
3855
0
        int nVal32 = nValue < INT_MIN   ? INT_MIN
3856
0
                     : nValue > INT_MAX ? INT_MAX
3857
0
                                        : static_cast<int>(nValue);
3858
3859
0
        if (nVal32 != nValue)
3860
0
        {
3861
0
            CPLError(
3862
0
                CE_Warning, CPLE_AppDefined,
3863
0
                "Field %s.%s: Integer overflow occurred when trying to set "
3864
0
                "%" PRId64 " as 32 bit value.",
3865
0
                poDefn->GetName(), poFDefn->GetNameRef(),
3866
0
                static_cast<int64_t>(nValue));
3867
0
        }
3868
0
        SetField(iField, 1, &nVal32);
3869
0
    }
3870
0
    else if (eType == OFTInteger64List)
3871
0
    {
3872
0
        SetField(iField, 1, &nValue);
3873
0
    }
3874
0
    else if (eType == OFTRealList)
3875
0
    {
3876
0
        double dfValue = static_cast<double>(nValue);
3877
0
        SetField(iField, 1, &dfValue);
3878
0
    }
3879
0
    else if (eType == OFTString)
3880
0
    {
3881
0
        char szTempBuffer[64] = {};
3882
3883
0
        CPLsnprintf(szTempBuffer, sizeof(szTempBuffer), CPL_FRMT_GIB, nValue);
3884
3885
0
        if (IsFieldSetAndNotNullUnsafe(iField))
3886
0
            CPLFree(pauFields[iField].String);
3887
3888
0
        pauFields[iField].String = VSI_STRDUP_VERBOSE(szTempBuffer);
3889
0
        if (pauFields[iField].String == nullptr)
3890
0
        {
3891
0
            OGR_RawField_SetUnset(&pauFields[iField]);
3892
0
        }
3893
0
    }
3894
0
    else if (eType == OFTStringList)
3895
0
    {
3896
0
        char szTempBuffer[64] = {};
3897
3898
0
        CPLsnprintf(szTempBuffer, sizeof(szTempBuffer), CPL_FRMT_GIB, nValue);
3899
0
        char *apszValues[2] = {szTempBuffer, nullptr};
3900
0
        SetField(iField, apszValues);
3901
0
    }
3902
0
    else
3903
0
    {
3904
        // Do nothing for other field types.
3905
0
    }
3906
0
}
3907
3908
/************************************************************************/
3909
/*                      OGR_F_SetFieldInteger64()                       */
3910
/************************************************************************/
3911
3912
/**
3913
 * \brief Set field to 64 bit integer value.
3914
 *
3915
 * OFTInteger, OFTInteger64 and OFTReal fields will be set directly.  OFTString
3916
 * fields will be assigned a string representation of the value, but not
3917
 * necessarily taking into account formatting constraints on this field.  Other
3918
 * field types may be unaffected.
3919
 *
3920
 * This function is the same as the C++ method OGRFeature::SetField().
3921
 *
3922
 * @note This method has only an effect on the in-memory feature object. If
3923
 * this object comes from a layer and the modifications must be serialized back
3924
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
3925
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
3926
 *
3927
 * @param hFeat handle to the feature that owned the field.
3928
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
3929
 * @param nValue the value to assign.
3930
 */
3931
3932
void OGR_F_SetFieldInteger64(OGRFeatureH hFeat, int iField, GIntBig nValue)
3933
3934
0
{
3935
0
    VALIDATE_POINTER0(hFeat, "OGR_F_SetFieldInteger64");
3936
3937
0
    OGRFeature::FromHandle(hFeat)->SetField(iField, nValue);
3938
0
}
3939
3940
/************************************************************************/
3941
/*                              SetField()                              */
3942
/************************************************************************/
3943
3944
/**
3945
 * \fn OGRFeature::SetField( const char* pszFName, double dfValue )
3946
 * \brief Set field to double value.
3947
 *
3948
 * OFTInteger, OFTInteger64 and OFTReal fields will be set directly.  OFTString
3949
 * fields will be assigned a string representation of the value, but not
3950
 * necessarily taking into account formatting constraints on this field.  Other
3951
 * field types may be unaffected.
3952
 *
3953
 * @note This method has only an effect on the in-memory feature object. If
3954
 * this object comes from a layer and the modifications must be serialized back
3955
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
3956
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
3957
 *
3958
 * @param pszFName the name of the field to set.
3959
 * @param dfValue the value to assign.
3960
 */
3961
3962
/**
3963
 * \brief Set field to double value.
3964
 *
3965
 * OFTInteger, OFTInteger64 and OFTReal fields will be set directly.  OFTString
3966
 * fields will be assigned a string representation of the value, but not
3967
 * necessarily taking into account formatting constraints on this field.  Other
3968
 * field types may be unaffected.
3969
 *
3970
 * This method is the same as the C function OGR_F_SetFieldDouble().
3971
 *
3972
 * @note This method has only an effect on the in-memory feature object. If
3973
 * this object comes from a layer and the modifications must be serialized back
3974
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
3975
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
3976
 *
3977
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
3978
 * @param dfValue the value to assign.
3979
 */
3980
3981
void OGRFeature::SetField(int iField, double dfValue)
3982
3983
0
{
3984
0
    const OGRFieldDefn *poFDefn = poDefn->GetFieldDefn(iField);
3985
3986
0
    if (poFDefn == nullptr)
3987
0
        return;
3988
3989
0
    const OGRFieldType eType = poFDefn->GetType();
3990
0
    if (eType == OFTReal)
3991
0
    {
3992
        // if( poFDefn->GetSubType() == OFSTFloat32 &&
3993
        //     dfValue != (double)(float)dfValue )
3994
        // {
3995
        //     CPLError(CE_Warning, CPLE_AppDefined,
3996
        //              "Passed value cannot be exactly representing as "
3997
        //              "a single-precision floating point value.");
3998
        //     dfValue = (double)(float)dfValue;
3999
        // }
4000
0
        pauFields[iField].Real = dfValue;
4001
0
    }
4002
0
    else if (eType == OFTInteger)
4003
0
    {
4004
0
        constexpr int nMin = std::numeric_limits<int>::min();
4005
0
        if (std::isnan(dfValue))
4006
0
        {
4007
0
            pauFields[iField].Integer = nMin;
4008
0
            CPLError(
4009
0
                CE_Warning, CPLE_AppDefined,
4010
0
                "Field %s.%s: Lossy conversion occurred when trying to set "
4011
0
                "32 bit integer field from real value %.17g.",
4012
0
                poDefn->GetName(), poFDefn->GetNameRef(), dfValue);
4013
0
        }
4014
0
        else
4015
0
        {
4016
0
            constexpr int nMax = std::numeric_limits<int>::max();
4017
0
            const int nVal = dfValue < nMin   ? nMin
4018
0
                             : dfValue > nMax ? nMax
4019
0
                                              : static_cast<int>(dfValue);
4020
0
            pauFields[iField].Integer =
4021
0
                OGRFeatureGetIntegerValue(poDefn, poFDefn, nVal);
4022
0
            if (!(nVal == dfValue))
4023
0
            {
4024
0
                CPLError(
4025
0
                    CE_Warning, CPLE_AppDefined,
4026
0
                    "Field %s.%s: Lossy conversion occurred when trying to set "
4027
0
                    "32 bit integer field from real value %.17g.",
4028
0
                    poDefn->GetName(), poFDefn->GetNameRef(), dfValue);
4029
0
            }
4030
0
        }
4031
0
        pauFields[iField].Set.nMarker2 = 0;
4032
0
        pauFields[iField].Set.nMarker3 = 0;
4033
0
    }
4034
0
    else if (eType == OFTInteger64)
4035
0
    {
4036
0
        constexpr auto nMin = std::numeric_limits<GIntBig>::min();
4037
0
        if (std::isnan(dfValue))
4038
0
        {
4039
0
            pauFields[iField].Integer64 = nMin;
4040
0
            CPLError(
4041
0
                CE_Warning, CPLE_AppDefined,
4042
0
                "Field %s.%s: Lossy conversion occurred when trying to set "
4043
0
                "64 bit integer field from real value %.17g.",
4044
0
                poDefn->GetName(), poFDefn->GetNameRef(), dfValue);
4045
0
        }
4046
0
        else
4047
0
        {
4048
0
            constexpr auto nMax = std::numeric_limits<GIntBig>::max();
4049
0
            const auto nVal = dfValue < static_cast<double>(nMin) ? nMin
4050
0
                              : dfValue > static_cast<double>(nMax)
4051
0
                                  ? nMax
4052
0
                                  : static_cast<GIntBig>(dfValue);
4053
0
            pauFields[iField].Integer64 = nVal;
4054
0
            if (!(static_cast<double>(nVal) == dfValue))
4055
0
            {
4056
0
                CPLError(
4057
0
                    CE_Warning, CPLE_AppDefined,
4058
0
                    "Field %s.%s: Lossy conversion occurred when trying to set "
4059
0
                    "64 bit integer field from real value %.17g.",
4060
0
                    poDefn->GetName(), poFDefn->GetNameRef(), dfValue);
4061
0
            }
4062
0
        }
4063
0
        pauFields[iField].Set.nMarker3 = 0;
4064
0
    }
4065
0
    else if (eType == OFTRealList)
4066
0
    {
4067
0
        SetField(iField, 1, &dfValue);
4068
0
    }
4069
0
    else if (eType == OFTIntegerList)
4070
0
    {
4071
0
        int nValue = static_cast<int>(dfValue);
4072
0
        SetField(iField, 1, &nValue);
4073
0
    }
4074
0
    else if (eType == OFTInteger64List)
4075
0
    {
4076
0
        GIntBig nValue = static_cast<GIntBig>(dfValue);
4077
0
        SetField(iField, 1, &nValue);
4078
0
    }
4079
0
    else if (eType == OFTString)
4080
0
    {
4081
0
        char szTempBuffer[128] = {};
4082
4083
0
        CPLsnprintf(szTempBuffer, sizeof(szTempBuffer), "%.16g", dfValue);
4084
4085
0
        if (IsFieldSetAndNotNullUnsafe(iField))
4086
0
            CPLFree(pauFields[iField].String);
4087
4088
0
        pauFields[iField].String = VSI_STRDUP_VERBOSE(szTempBuffer);
4089
0
        if (pauFields[iField].String == nullptr)
4090
0
        {
4091
0
            OGR_RawField_SetUnset(&pauFields[iField]);
4092
0
        }
4093
0
    }
4094
0
    else if (eType == OFTStringList)
4095
0
    {
4096
0
        char szTempBuffer[64] = {};
4097
4098
0
        CPLsnprintf(szTempBuffer, sizeof(szTempBuffer), "%.16g", dfValue);
4099
0
        char *apszValues[2] = {szTempBuffer, nullptr};
4100
0
        SetField(iField, apszValues);
4101
0
    }
4102
0
    else
4103
0
    {
4104
        // Do nothing for other field types.
4105
0
    }
4106
0
}
4107
4108
/************************************************************************/
4109
/*                        OGR_F_SetFieldDouble()                        */
4110
/************************************************************************/
4111
4112
/**
4113
 * \brief Set field to double value.
4114
 *
4115
 * OFTInteger, OFTInteger64 and OFTReal fields will be set directly.  OFTString
4116
 * fields will be assigned a string representation of the value, but not
4117
 * necessarily taking into account formatting constraints on this field.  Other
4118
 * field types may be unaffected.
4119
 *
4120
 * This function is the same as the C++ method OGRFeature::SetField().
4121
 *
4122
 * @note This method has only an effect on the in-memory feature object. If
4123
 * this object comes from a layer and the modifications must be serialized back
4124
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
4125
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
4126
 *
4127
 * @param hFeat handle to the feature that owned the field.
4128
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
4129
 * @param dfValue the value to assign.
4130
 */
4131
4132
void OGR_F_SetFieldDouble(OGRFeatureH hFeat, int iField, double dfValue)
4133
4134
0
{
4135
0
    VALIDATE_POINTER0(hFeat, "OGR_F_SetFieldDouble");
4136
4137
0
    OGRFeature::FromHandle(hFeat)->SetField(iField, dfValue);
4138
0
}
4139
4140
/************************************************************************/
4141
/*                              SetField()                              */
4142
/************************************************************************/
4143
4144
/**
4145
 * \fn OGRFeature::SetField( const char* pszFName, const char * pszValue )
4146
 * \brief Set field to string value.
4147
 *
4148
 * OFTInteger fields will be set based on an atoi() conversion of the string.
4149
 * OFTInteger64 fields will be set based on an CPLAtoGIntBig() conversion of the
4150
 * string.  OFTReal fields will be set based on an CPLAtof() conversion of the
4151
 * string.  Other field types may be unaffected.
4152
 *
4153
 * @note This method has only an effect on the in-memory feature object. If
4154
 * this object comes from a layer and the modifications must be serialized back
4155
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
4156
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
4157
 *
4158
 * @param pszFName the name of the field to set.
4159
 * @param pszValue the value to assign.
4160
 */
4161
4162
/**
4163
 * \brief Set field to string value.
4164
 *
4165
 * OFTInteger fields will be set based on an atoi() conversion of the string.
4166
 * OFTInteger64 fields will be set based on an CPLAtoGIntBig() conversion of the
4167
 * string.  OFTReal fields will be set based on an CPLAtof() conversion of the
4168
 * string.  Other field types may be unaffected.
4169
 *
4170
 * This method is the same as the C function OGR_F_SetFieldString().
4171
 *
4172
 * @note This method has only an effect on the in-memory feature object. If
4173
 * this object comes from a layer and the modifications must be serialized back
4174
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
4175
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
4176
 *
4177
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
4178
 * @param pszValue the value to assign.
4179
 */
4180
4181
void OGRFeature::SetField(int iField, const char *pszValue)
4182
4183
0
{
4184
0
    static int bWarn = -1;
4185
0
    if (bWarn < 0)
4186
0
        bWarn = CPLTestBool(
4187
0
            CPLGetConfigOption("OGR_SETFIELD_NUMERIC_WARNING", "YES"));
4188
4189
0
    const OGRFieldDefn *poFDefn = poDefn->GetFieldDefn(iField);
4190
0
    if (poFDefn == nullptr)
4191
0
        return;
4192
4193
0
    char *pszLast = nullptr;
4194
4195
0
    OGRFieldType eType = poFDefn->GetType();
4196
0
    if (eType == OFTString)
4197
0
    {
4198
0
        if (IsFieldSetAndNotNullUnsafe(iField))
4199
0
            CPLFree(pauFields[iField].String);
4200
4201
0
        pauFields[iField].String = VSI_STRDUP_VERBOSE(pszValue ? pszValue : "");
4202
0
        if (pauFields[iField].String == nullptr)
4203
0
        {
4204
0
            OGR_RawField_SetUnset(&pauFields[iField]);
4205
0
        }
4206
0
    }
4207
0
    else if (eType == OFTInteger)
4208
0
    {
4209
0
        if (poFDefn->GetSubType() == OFSTBoolean)
4210
0
        {
4211
0
            constexpr char DIGIT_ZERO = '0';
4212
0
            if ((pszValue[0] == '1' && pszValue[1] == '\0') ||
4213
0
                EQUAL(pszValue, "true") || EQUAL(pszValue, "on") ||
4214
0
                EQUAL(pszValue, "yes"))
4215
0
            {
4216
0
                pauFields[iField].Integer = 1;
4217
0
                pauFields[iField].Set.nMarker2 = 0;
4218
0
                pauFields[iField].Set.nMarker3 = 0;
4219
0
            }
4220
0
            else if ((pszValue[0] == DIGIT_ZERO && pszValue[1] == '\0') ||
4221
0
                     EQUAL(pszValue, "false") || EQUAL(pszValue, "off") ||
4222
0
                     EQUAL(pszValue, "no"))
4223
0
            {
4224
0
                pauFields[iField].Integer = 0;
4225
0
                pauFields[iField].Set.nMarker2 = 0;
4226
0
                pauFields[iField].Set.nMarker3 = 0;
4227
0
            }
4228
0
            else
4229
0
            {
4230
0
                if (CPLGetValueType(pszValue) == CPL_VALUE_STRING)
4231
0
                {
4232
0
                    CPLError(CE_Warning, CPLE_AppDefined,
4233
0
                             "Invalid value '%s' for boolean field %s.%s. "
4234
0
                             "Assuming it to be false.",
4235
0
                             pszValue, poDefn->GetName(),
4236
0
                             poFDefn->GetNameRef());
4237
0
                    pauFields[iField].Integer = 0;
4238
0
                    pauFields[iField].Set.nMarker2 = 0;
4239
0
                    pauFields[iField].Set.nMarker3 = 0;
4240
0
                }
4241
0
                else
4242
0
                {
4243
0
                    CPLError(CE_Warning, CPLE_AppDefined,
4244
0
                             "Invalid value '%s' for boolean field %s.%s. "
4245
0
                             "Assuming it to be true.",
4246
0
                             pszValue, poDefn->GetName(),
4247
0
                             poFDefn->GetNameRef());
4248
0
                    pauFields[iField].Integer = 1;
4249
0
                    pauFields[iField].Set.nMarker2 = 0;
4250
0
                    pauFields[iField].Set.nMarker3 = 0;
4251
0
                }
4252
0
            }
4253
0
        }
4254
0
        else
4255
0
        {
4256
            // As allowed by C standard, some systems like MSVC do not reset errno.
4257
0
            errno = 0;
4258
4259
0
            long long nVal64 = std::strtoll(pszValue, &pszLast, 10);
4260
0
            int nVal32 = nVal64 > INT_MAX   ? INT_MAX
4261
0
                         : nVal64 < INT_MIN ? INT_MIN
4262
0
                                            : static_cast<int>(nVal64);
4263
0
            pauFields[iField].Integer =
4264
0
                OGRFeatureGetIntegerValue(poDefn, poFDefn, nVal32);
4265
0
            if (bWarn && pauFields[iField].Integer == nVal32 &&
4266
0
                (errno == ERANGE || nVal32 != nVal64 || !pszLast || *pszLast))
4267
0
                CPLError(CE_Warning, CPLE_AppDefined,
4268
0
                         "Value '%s' of field %s.%s parsed incompletely to "
4269
0
                         "integer %d.",
4270
0
                         pszValue, poDefn->GetName(), poFDefn->GetNameRef(),
4271
0
                         pauFields[iField].Integer);
4272
0
            pauFields[iField].Set.nMarker2 = 0;
4273
0
            pauFields[iField].Set.nMarker3 = 0;
4274
0
        }
4275
0
    }
4276
0
    else if (eType == OFTInteger64)
4277
0
    {
4278
0
        pauFields[iField].Integer64 = CPLAtoGIntBigEx(pszValue, bWarn, nullptr);
4279
0
        pauFields[iField].Set.nMarker3 = 0;
4280
0
    }
4281
0
    else if (eType == OFTReal)
4282
0
    {
4283
0
        pauFields[iField].Real = CPLStrtod(pszValue, &pszLast);
4284
0
        if (bWarn && (!pszLast || *pszLast))
4285
0
            CPLError(
4286
0
                CE_Warning, CPLE_AppDefined,
4287
0
                "Value '%s' of field %s.%s parsed incompletely to real %.16g.",
4288
0
                pszValue, poDefn->GetName(), poFDefn->GetNameRef(),
4289
0
                pauFields[iField].Real);
4290
0
    }
4291
0
    else if (eType == OFTDate || eType == OFTTime || eType == OFTDateTime)
4292
0
    {
4293
0
        OGRField sWrkField;
4294
4295
0
        if (OGRParseDate(pszValue, &sWrkField, 0))
4296
0
            memcpy(pauFields + iField, &sWrkField, sizeof(sWrkField));
4297
0
    }
4298
0
    else if (eType == OFTIntegerList || eType == OFTInteger64List ||
4299
0
             eType == OFTRealList)
4300
0
    {
4301
0
        json_object *poJSonObj = nullptr;
4302
0
        if (pszValue[0] == '[' && pszValue[strlen(pszValue) - 1] == ']' &&
4303
0
            OGRJSonParse(pszValue, &poJSonObj, false))
4304
0
        {
4305
0
            const auto nLength = json_object_array_length(poJSonObj);
4306
0
            if (eType == OFTIntegerList && nLength > 0)
4307
0
            {
4308
0
                std::vector<int> anValues;
4309
0
                for (auto i = decltype(nLength){0}; i < nLength; i++)
4310
0
                {
4311
0
                    json_object *poItem =
4312
0
                        json_object_array_get_idx(poJSonObj, i);
4313
0
                    anValues.push_back(json_object_get_int(poItem));
4314
0
                }
4315
0
                SetField(iField, static_cast<int>(nLength), &(anValues[0]));
4316
0
            }
4317
0
            else if (eType == OFTInteger64List && nLength > 0)
4318
0
            {
4319
0
                std::vector<GIntBig> anValues;
4320
0
                for (auto i = decltype(nLength){0}; i < nLength; i++)
4321
0
                {
4322
0
                    json_object *poItem =
4323
0
                        json_object_array_get_idx(poJSonObj, i);
4324
0
                    anValues.push_back(json_object_get_int64(poItem));
4325
0
                }
4326
0
                SetField(iField, static_cast<int>(nLength), &(anValues[0]));
4327
0
            }
4328
0
            else if (eType == OFTRealList && nLength > 0)
4329
0
            {
4330
0
                std::vector<double> adfValues;
4331
0
                for (auto i = decltype(nLength){0}; i < nLength; i++)
4332
0
                {
4333
0
                    json_object *poItem =
4334
0
                        json_object_array_get_idx(poJSonObj, i);
4335
0
                    adfValues.push_back(json_object_get_double(poItem));
4336
0
                }
4337
0
                SetField(iField, static_cast<int>(nLength), &(adfValues[0]));
4338
0
            }
4339
4340
0
            json_object_put(poJSonObj);
4341
0
        }
4342
0
        else
4343
0
        {
4344
0
            char **papszValueList = nullptr;
4345
4346
0
            if (pszValue[0] == '(' && strchr(pszValue, ':') != nullptr)
4347
0
            {
4348
0
                papszValueList = CSLTokenizeString2(pszValue, ",:()", 0);
4349
0
            }
4350
4351
0
            if (papszValueList == nullptr || *papszValueList == nullptr ||
4352
0
                atoi(papszValueList[0]) != CSLCount(papszValueList) - 1)
4353
0
            {
4354
                // Do nothing - the count does not match entries.
4355
0
            }
4356
0
            else if (eType == OFTIntegerList)
4357
0
            {
4358
0
                const int nCount = atoi(papszValueList[0]);
4359
0
                std::vector<int> anValues;
4360
0
                if (nCount == CSLCount(papszValueList) - 1)
4361
0
                {
4362
0
                    for (int i = 0; i < nCount; i++)
4363
0
                    {
4364
                        // As allowed by C standard, some systems like
4365
                        // MSVC do not reset errno.
4366
0
                        errno = 0;
4367
0
                        int nVal = atoi(papszValueList[i + 1]);
4368
0
                        if (errno == ERANGE)
4369
0
                        {
4370
0
                            CPLError(
4371
0
                                CE_Warning, CPLE_AppDefined,
4372
0
                                "Field %s.%s: 32 bit integer overflow when "
4373
0
                                "converting %s",
4374
0
                                poDefn->GetName(), poFDefn->GetNameRef(),
4375
0
                                pszValue);
4376
0
                        }
4377
0
                        anValues.push_back(nVal);
4378
0
                    }
4379
0
                    if (nCount > 0)
4380
0
                        SetField(iField, nCount, &(anValues[0]));
4381
0
                }
4382
0
            }
4383
0
            else if (eType == OFTInteger64List)
4384
0
            {
4385
0
                const int nCount = atoi(papszValueList[0]);
4386
0
                std::vector<GIntBig> anValues;
4387
0
                if (nCount == CSLCount(papszValueList) - 1)
4388
0
                {
4389
0
                    for (int i = 0; i < nCount; i++)
4390
0
                    {
4391
0
                        const GIntBig nVal = CPLAtoGIntBigEx(
4392
0
                            papszValueList[i + 1], TRUE, nullptr);
4393
0
                        anValues.push_back(nVal);
4394
0
                    }
4395
0
                    if (nCount > 0)
4396
0
                        SetField(iField, nCount, &(anValues[0]));
4397
0
                }
4398
0
            }
4399
0
            else if (eType == OFTRealList)
4400
0
            {
4401
0
                int nCount = atoi(papszValueList[0]);
4402
0
                std::vector<double> adfValues;
4403
0
                if (nCount == CSLCount(papszValueList) - 1)
4404
0
                {
4405
0
                    for (int i = 0; i < nCount; i++)
4406
0
                        adfValues.push_back(CPLAtof(papszValueList[i + 1]));
4407
0
                    if (nCount > 0)
4408
0
                        SetField(iField, nCount, &(adfValues[0]));
4409
0
                }
4410
0
            }
4411
4412
0
            CSLDestroy(papszValueList);
4413
0
        }
4414
0
    }
4415
0
    else if (eType == OFTStringList)
4416
0
    {
4417
0
        if (pszValue && *pszValue)
4418
0
        {
4419
0
            json_object *poJSonObj = nullptr;
4420
0
            if (pszValue[0] == '(' && strchr(pszValue, ':') != nullptr &&
4421
0
                pszValue[strlen(pszValue) - 1] == ')')
4422
0
            {
4423
0
                char **papszValueList = CSLTokenizeString2(pszValue, ",:()", 0);
4424
0
                const int nCount =
4425
0
                    papszValueList[0] == nullptr ? 0 : atoi(papszValueList[0]);
4426
0
                std::vector<char *> aosValues;
4427
0
                if (nCount == CSLCount(papszValueList) - 1)
4428
0
                {
4429
0
                    for (int i = 0; i < nCount; i++)
4430
0
                        aosValues.push_back(papszValueList[i + 1]);
4431
0
                    aosValues.push_back(nullptr);
4432
0
                    SetField(iField, &(aosValues[0]));
4433
0
                }
4434
0
                CSLDestroy(papszValueList);
4435
0
            }
4436
            // Is this a JSon array?
4437
0
            else if (pszValue[0] == '[' &&
4438
0
                     pszValue[strlen(pszValue) - 1] == ']' &&
4439
0
                     OGRJSonParse(pszValue, &poJSonObj, false))
4440
0
            {
4441
0
                CPLStringList aoList;
4442
0
                const auto nLength = json_object_array_length(poJSonObj);
4443
0
                for (auto i = decltype(nLength){0}; i < nLength; i++)
4444
0
                {
4445
0
                    json_object *poItem =
4446
0
                        json_object_array_get_idx(poJSonObj, i);
4447
0
                    if (!poItem)
4448
0
                        aoList.AddString("");
4449
0
                    else
4450
0
                        aoList.AddString(json_object_get_string(poItem));
4451
0
                }
4452
0
                SetField(iField, aoList.List());
4453
0
                json_object_put(poJSonObj);
4454
0
            }
4455
0
            else
4456
0
            {
4457
0
                const char *const papszValues[2] = {pszValue, nullptr};
4458
0
                SetField(iField, papszValues);
4459
0
            }
4460
0
        }
4461
0
    }
4462
0
    else
4463
0
    {
4464
        // Do nothing for other field types.
4465
0
    }
4466
0
}
4467
4468
/************************************************************************/
4469
/*                        OGR_F_SetFieldString()                        */
4470
/************************************************************************/
4471
4472
/**
4473
 * \brief Set field to string value.
4474
 *
4475
 * OFTInteger fields will be set based on an atoi() conversion of the string.
4476
 * OFTInteger64 fields will be set based on an CPLAtoGIntBig() conversion of the
4477
 * string.  OFTReal fields will be set based on an CPLAtof() conversion of the
4478
 * string.  Other field types may be unaffected.
4479
 *
4480
 * This function is the same as the C++ method OGRFeature::SetField().
4481
 *
4482
 * @note This method has only an effect on the in-memory feature object. If
4483
 * this object comes from a layer and the modifications must be serialized back
4484
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
4485
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
4486
 *
4487
 * @param hFeat handle to the feature that owned the field.
4488
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
4489
 * @param pszValue the value to assign.
4490
 */
4491
4492
void OGR_F_SetFieldString(OGRFeatureH hFeat, int iField, const char *pszValue)
4493
4494
0
{
4495
0
    VALIDATE_POINTER0(hFeat, "OGR_F_SetFieldString");
4496
4497
0
    OGRFeature::FromHandle(hFeat)->SetField(iField, pszValue);
4498
0
}
4499
4500
/************************************************************************/
4501
/*                              SetField()                              */
4502
/************************************************************************/
4503
4504
/**
4505
 * \fn OGRFeature::SetField( const char* pszFName, int nCount, const int
4506
 * *panValues ) \brief Set field to list of integers value.
4507
 *
4508
 * This method currently on has an effect of OFTIntegerList, OFTInteger64List
4509
 * and OFTRealList fields.
4510
 *
4511
 * @note This method has only an effect on the in-memory feature object. If
4512
 * this object comes from a layer and the modifications must be serialized back
4513
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
4514
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
4515
 *
4516
 * @param pszFName the name of the field to set.
4517
 * @param nCount the number of values in the list being assigned.
4518
 * @param panValues the values to assign.
4519
 */
4520
4521
/**
4522
 * \brief Set field to list of integers value.
4523
 *
4524
 * This method currently on has an effect of OFTIntegerList, OFTInteger64List
4525
 * and OFTRealList fields.
4526
 *
4527
 * This method is the same as the C function OGR_F_SetFieldIntegerList().
4528
 *
4529
 * @note This method has only an effect on the in-memory feature object. If
4530
 * this object comes from a layer and the modifications must be serialized back
4531
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
4532
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
4533
 *
4534
 * @param iField the field to set, from 0 to GetFieldCount()-1.
4535
 * @param nCount the number of values in the list being assigned.
4536
 * @param panValues the values to assign.
4537
 */
4538
4539
void OGRFeature::SetField(int iField, int nCount, const int *panValues)
4540
4541
0
{
4542
0
    const OGRFieldDefn *poFDefn = poDefn->GetFieldDefn(iField);
4543
4544
0
    if (poFDefn == nullptr)
4545
0
        return;
4546
4547
0
    OGRFieldType eType = poFDefn->GetType();
4548
0
    if (eType == OFTIntegerList)
4549
0
    {
4550
0
        OGRField uField;
4551
0
        int *panValuesMod = nullptr;
4552
4553
0
        if (poFDefn->GetSubType() == OFSTBoolean ||
4554
0
            poFDefn->GetSubType() == OFSTInt16)
4555
0
        {
4556
0
            for (int i = 0; i < nCount; i++)
4557
0
            {
4558
0
                int nVal =
4559
0
                    OGRFeatureGetIntegerValue(poDefn, poFDefn, panValues[i]);
4560
0
                if (panValues[i] != nVal)
4561
0
                {
4562
0
                    if (panValuesMod == nullptr)
4563
0
                    {
4564
0
                        panValuesMod = static_cast<int *>(
4565
0
                            VSI_MALLOC_VERBOSE(nCount * sizeof(int)));
4566
0
                        if (panValuesMod == nullptr)
4567
0
                            return;
4568
0
                        if (nCount > 0)
4569
0
                            memcpy(panValuesMod, panValues,
4570
0
                                   nCount * sizeof(int));
4571
0
                    }
4572
0
                    panValuesMod[i] = nVal;
4573
0
                }
4574
0
            }
4575
0
        }
4576
4577
0
        uField.IntegerList.nCount = nCount;
4578
0
        uField.Set.nMarker2 = 0;
4579
0
        uField.Set.nMarker3 = 0;
4580
0
        uField.IntegerList.paList =
4581
0
            panValuesMod ? panValuesMod : const_cast<int *>(panValues);
4582
4583
0
        SetField(iField, &uField);
4584
0
        CPLFree(panValuesMod);
4585
0
    }
4586
0
    else if (eType == OFTInteger64List)
4587
0
    {
4588
0
        std::vector<GIntBig> anValues;
4589
0
        anValues.reserve(nCount);
4590
0
        for (int i = 0; i < nCount; i++)
4591
0
            anValues.push_back(panValues[i]);
4592
0
        if (nCount > 0)
4593
0
            SetField(iField, nCount, &anValues[0]);
4594
0
    }
4595
0
    else if (eType == OFTRealList)
4596
0
    {
4597
0
        std::vector<double> adfValues;
4598
0
        adfValues.reserve(nCount);
4599
0
        for (int i = 0; i < nCount; i++)
4600
0
            adfValues.push_back(static_cast<double>(panValues[i]));
4601
0
        if (nCount > 0)
4602
0
            SetField(iField, nCount, &adfValues[0]);
4603
0
    }
4604
0
    else if ((eType == OFTInteger || eType == OFTInteger64 ||
4605
0
              eType == OFTReal) &&
4606
0
             nCount == 1)
4607
0
    {
4608
0
        SetField(iField, panValues[0]);
4609
0
    }
4610
0
    else if (eType == OFTStringList)
4611
0
    {
4612
0
        char **papszValues = static_cast<char **>(
4613
0
            VSI_MALLOC_VERBOSE((nCount + 1) * sizeof(char *)));
4614
0
        if (papszValues == nullptr)
4615
0
            return;
4616
0
        for (int i = 0; i < nCount; i++)
4617
0
            papszValues[i] = VSI_STRDUP_VERBOSE(CPLSPrintf("%d", panValues[i]));
4618
0
        papszValues[nCount] = nullptr;
4619
0
        SetField(iField, papszValues);
4620
0
        CSLDestroy(papszValues);
4621
0
    }
4622
0
}
4623
4624
/************************************************************************/
4625
/*                     OGR_F_SetFieldIntegerList()                      */
4626
/************************************************************************/
4627
4628
/**
4629
 * \brief Set field to list of integers value.
4630
 *
4631
 * This function currently on has an effect of OFTIntegerList, OFTInteger64List
4632
 * and OFTRealList fields.
4633
 *
4634
 * This function is the same as the C++ method OGRFeature::SetField().
4635
 *
4636
 * @note This method has only an effect on the in-memory feature object. If
4637
 * this object comes from a layer and the modifications must be serialized back
4638
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
4639
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
4640
 *
4641
 * @param hFeat handle to the feature that owned the field.
4642
 * @param iField the field to set, from 0 to GetFieldCount()-1.
4643
 * @param nCount the number of values in the list being assigned.
4644
 * @param panValues the values to assign.
4645
 */
4646
4647
void OGR_F_SetFieldIntegerList(OGRFeatureH hFeat, int iField, int nCount,
4648
                               const int *panValues)
4649
4650
0
{
4651
0
    VALIDATE_POINTER0(hFeat, "OGR_F_SetFieldIntegerList");
4652
4653
0
    OGRFeature::FromHandle(hFeat)->SetField(iField, nCount, panValues);
4654
0
}
4655
4656
/************************************************************************/
4657
/*                              SetField()                              */
4658
/************************************************************************/
4659
4660
/**
4661
 * \fn OGRFeature::SetField( const char* pszFName, int nCount, const GIntBig
4662
 * *panValues )
4663
 * \brief Set field to list of 64 bit integers value.
4664
 *
4665
 * This method currently on has an effect of OFTIntegerList, OFTInteger64List
4666
 * and OFTRealList fields.
4667
 *
4668
 * @note This method has only an effect on the in-memory feature object. If
4669
 * this object comes from a layer and the modifications must be serialized back
4670
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
4671
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
4672
 *
4673
 * @param pszFName the name of the field to set.
4674
 * @param nCount the number of values in the list being assigned.
4675
 * @param panValues the values to assign.
4676
 */
4677
4678
/**
4679
 * \brief Set field to list of 64 bit integers value.
4680
 *
4681
 * This method currently on has an effect of OFTIntegerList, OFTInteger64List
4682
 * and OFTRealList fields.
4683
 *
4684
 * This method is the same as the C function OGR_F_SetFieldInteger64List().
4685
 *
4686
 * @note This method has only an effect on the in-memory feature object. If
4687
 * this object comes from a layer and the modifications must be serialized back
4688
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
4689
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
4690
 *
4691
 * @param iField the field to set, from 0 to GetFieldCount()-1.
4692
 * @param nCount the number of values in the list being assigned.
4693
 * @param panValues the values to assign.
4694
 */
4695
4696
void OGRFeature::SetField(int iField, int nCount, const GIntBig *panValues)
4697
4698
0
{
4699
0
    const OGRFieldDefn *poFDefn = poDefn->GetFieldDefn(iField);
4700
4701
0
    if (poFDefn == nullptr)
4702
0
        return;
4703
4704
0
    OGRFieldType eType = poFDefn->GetType();
4705
0
    if (eType == OFTIntegerList)
4706
0
    {
4707
0
        std::vector<int> anValues;
4708
4709
0
        for (int i = 0; i < nCount; i++)
4710
0
        {
4711
0
            const GIntBig nValue = panValues[i];
4712
0
            const int nVal32 = nValue < INT_MIN   ? INT_MIN
4713
0
                               : nValue > INT_MAX ? INT_MAX
4714
0
                                                  : static_cast<int>(nValue);
4715
4716
0
            if (nVal32 != nValue)
4717
0
            {
4718
0
                CPLError(
4719
0
                    CE_Warning, CPLE_AppDefined,
4720
0
                    "Field %s.%s: Integer overflow occurred when trying to "
4721
0
                    "set %" PRId64 " as 32 bit value.",
4722
0
                    poDefn->GetName(), poFDefn->GetNameRef(),
4723
0
                    static_cast<int64_t>(nValue));
4724
0
            }
4725
0
            anValues.push_back(nVal32);
4726
0
        }
4727
0
        if (nCount > 0)
4728
0
            SetField(iField, nCount, &anValues[0]);
4729
0
    }
4730
0
    else if (eType == OFTInteger64List)
4731
0
    {
4732
0
        OGRField uField;
4733
0
        uField.Integer64List.nCount = nCount;
4734
0
        uField.Set.nMarker2 = 0;
4735
0
        uField.Set.nMarker3 = 0;
4736
0
        uField.Integer64List.paList = const_cast<GIntBig *>(panValues);
4737
4738
0
        SetField(iField, &uField);
4739
0
    }
4740
0
    else if (eType == OFTRealList)
4741
0
    {
4742
0
        std::vector<double> adfValues;
4743
0
        adfValues.reserve(nCount);
4744
0
        for (int i = 0; i < nCount; i++)
4745
0
            adfValues.push_back(static_cast<double>(panValues[i]));
4746
0
        if (nCount > 0)
4747
0
            SetField(iField, nCount, &adfValues[0]);
4748
0
    }
4749
0
    else if ((eType == OFTInteger || eType == OFTInteger64 ||
4750
0
              eType == OFTReal) &&
4751
0
             nCount == 1)
4752
0
    {
4753
0
        SetField(iField, panValues[0]);
4754
0
    }
4755
0
    else if (eType == OFTStringList)
4756
0
    {
4757
0
        char **papszValues = static_cast<char **>(
4758
0
            VSI_MALLOC_VERBOSE((nCount + 1) * sizeof(char *)));
4759
0
        if (papszValues == nullptr)
4760
0
            return;
4761
0
        for (int i = 0; i < nCount; i++)
4762
0
            papszValues[i] =
4763
0
                VSI_STRDUP_VERBOSE(CPLSPrintf(CPL_FRMT_GIB, panValues[i]));
4764
0
        papszValues[nCount] = nullptr;
4765
0
        SetField(iField, papszValues);
4766
0
        CSLDestroy(papszValues);
4767
0
    }
4768
0
}
4769
4770
/************************************************************************/
4771
/*                    OGR_F_SetFieldInteger64List()                     */
4772
/************************************************************************/
4773
4774
/**
4775
 * \brief Set field to list of 64 bit integers value.
4776
 *
4777
 * This function currently on has an effect of OFTIntegerList, OFTInteger64List
4778
 * and OFTRealList fields.
4779
 *
4780
 * This function is the same as the C++ method OGRFeature::SetField().
4781
 *
4782
 * @note This method has only an effect on the in-memory feature object. If
4783
 * this object comes from a layer and the modifications must be serialized back
4784
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
4785
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
4786
 *
4787
 * @param hFeat handle to the feature that owned the field.
4788
 * @param iField the field to set, from 0 to GetFieldCount()-1.
4789
 * @param nCount the number of values in the list being assigned.
4790
 * @param panValues the values to assign.
4791
 */
4792
4793
void OGR_F_SetFieldInteger64List(OGRFeatureH hFeat, int iField, int nCount,
4794
                                 const GIntBig *panValues)
4795
4796
0
{
4797
0
    VALIDATE_POINTER0(hFeat, "OGR_F_SetFieldInteger64List");
4798
4799
0
    OGRFeature::FromHandle(hFeat)->SetField(iField, nCount, panValues);
4800
0
}
4801
4802
/************************************************************************/
4803
/*                              SetField()                              */
4804
/************************************************************************/
4805
4806
/**
4807
 * \fn OGRFeature::SetField( const char* pszFName, int nCount, const double *
4808
 * padfValues ) \brief Set field to list of doubles value.
4809
 *
4810
 * This method currently on has an effect of OFTIntegerList, OFTInteger64List,
4811
 * OFTRealList fields.
4812
 *
4813
 * @note This method has only an effect on the in-memory feature object. If
4814
 * this object comes from a layer and the modifications must be serialized back
4815
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
4816
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
4817
 *
4818
 * @param pszFName the name of the field to set.
4819
 * @param nCount the number of values in the list being assigned.
4820
 * @param padfValues the values to assign.
4821
 */
4822
4823
/**
4824
 * \brief Set field to list of doubles value.
4825
 *
4826
 * This method currently on has an effect of OFTIntegerList, OFTInteger64List,
4827
 * OFTRealList fields.
4828
 *
4829
 * This method is the same as the C function OGR_F_SetFieldDoubleList().
4830
 *
4831
 * @note This method has only an effect on the in-memory feature object. If
4832
 * this object comes from a layer and the modifications must be serialized back
4833
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
4834
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
4835
 *
4836
 * @param iField the field to set, from 0 to GetFieldCount()-1.
4837
 * @param nCount the number of values in the list being assigned.
4838
 * @param padfValues the values to assign.
4839
 */
4840
4841
void OGRFeature::SetField(int iField, int nCount, const double *padfValues)
4842
4843
0
{
4844
0
    const OGRFieldDefn *poFDefn = poDefn->GetFieldDefn(iField);
4845
4846
0
    if (poFDefn == nullptr)
4847
0
        return;
4848
4849
0
    OGRFieldType eType = poFDefn->GetType();
4850
0
    if (eType == OFTRealList)
4851
0
    {
4852
0
        OGRField uField;
4853
4854
0
        uField.RealList.nCount = nCount;
4855
0
        uField.Set.nMarker2 = 0;
4856
0
        uField.Set.nMarker3 = 0;
4857
0
        uField.RealList.paList = const_cast<double *>(padfValues);
4858
4859
0
        SetField(iField, &uField);
4860
0
    }
4861
0
    else if (eType == OFTIntegerList)
4862
0
    {
4863
0
        std::vector<int> anValues;
4864
0
        anValues.reserve(nCount);
4865
0
        for (int i = 0; i < nCount; i++)
4866
0
            anValues.push_back(static_cast<int>(padfValues[i]));
4867
4868
0
        if (nCount > 0)
4869
0
            SetField(iField, nCount, &anValues[0]);
4870
0
    }
4871
0
    else if (eType == OFTInteger64List)
4872
0
    {
4873
0
        std::vector<GIntBig> anValues;
4874
0
        anValues.reserve(nCount);
4875
0
        for (int i = 0; i < nCount; i++)
4876
0
            anValues.push_back(static_cast<GIntBig>(padfValues[i]));
4877
0
        if (nCount > 0)
4878
0
            SetField(iField, nCount, &anValues[0]);
4879
0
    }
4880
0
    else if ((eType == OFTInteger || eType == OFTInteger64 ||
4881
0
              eType == OFTReal) &&
4882
0
             nCount == 1)
4883
0
    {
4884
0
        SetField(iField, padfValues[0]);
4885
0
    }
4886
0
    else if (eType == OFTStringList)
4887
0
    {
4888
0
        char **papszValues = static_cast<char **>(
4889
0
            VSI_MALLOC_VERBOSE((nCount + 1) * sizeof(char *)));
4890
0
        if (papszValues == nullptr)
4891
0
            return;
4892
0
        for (int i = 0; i < nCount; i++)
4893
0
            papszValues[i] =
4894
0
                VSI_STRDUP_VERBOSE(CPLSPrintf("%.16g", padfValues[i]));
4895
0
        papszValues[nCount] = nullptr;
4896
0
        SetField(iField, papszValues);
4897
0
        CSLDestroy(papszValues);
4898
0
    }
4899
0
}
4900
4901
/************************************************************************/
4902
/*                      OGR_F_SetFieldDoubleList()                      */
4903
/************************************************************************/
4904
4905
/**
4906
 * \brief Set field to list of doubles value.
4907
 *
4908
 * This function currently on has an effect of OFTIntegerList, OFTInteger64List,
4909
 * OFTRealList fields.
4910
 *
4911
 * This function is the same as the C++ method OGRFeature::SetField().
4912
 *
4913
 * @note This method has only an effect on the in-memory feature object. If
4914
 * this object comes from a layer and the modifications must be serialized back
4915
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
4916
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
4917
 *
4918
 * @param hFeat handle to the feature that owned the field.
4919
 * @param iField the field to set, from 0 to GetFieldCount()-1.
4920
 * @param nCount the number of values in the list being assigned.
4921
 * @param padfValues the values to assign.
4922
 */
4923
4924
void OGR_F_SetFieldDoubleList(OGRFeatureH hFeat, int iField, int nCount,
4925
                              const double *padfValues)
4926
4927
0
{
4928
0
    VALIDATE_POINTER0(hFeat, "OGR_F_SetFieldDoubleList");
4929
4930
0
    OGRFeature::FromHandle(hFeat)->SetField(iField, nCount, padfValues);
4931
0
}
4932
4933
/************************************************************************/
4934
/*                              SetField()                              */
4935
/************************************************************************/
4936
4937
/**
4938
 * \fn OGRFeature::SetField( const char* pszFName,  const char * const *
4939
 * papszValues ) \brief Set field to list of strings value.
4940
 *
4941
 * This method currently on has an effect of OFTStringList fields.
4942
 *
4943
 * @note This method has only an effect on the in-memory feature object. If
4944
 * this object comes from a layer and the modifications must be serialized back
4945
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
4946
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
4947
 *
4948
 * @param pszFName the name of the field to set.
4949
 * @param papszValues the values to assign. List of NUL-terminated string,
4950
 * ending with a NULL pointer.
4951
 */
4952
4953
/**
4954
 * \brief Set field to list of strings value.
4955
 *
4956
 * This method currently on has an effect of OFTStringList fields.
4957
 *
4958
 * This method is the same as the C function OGR_F_SetFieldStringList().
4959
 *
4960
 * @note This method has only an effect on the in-memory feature object. If
4961
 * this object comes from a layer and the modifications must be serialized back
4962
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
4963
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
4964
 *
4965
 * @param iField the field to set, from 0 to GetFieldCount()-1.
4966
 * @param papszValues the values to assign. List of NUL-terminated string,
4967
 * ending with a NULL pointer.
4968
 */
4969
4970
void OGRFeature::SetField(int iField, const char *const *papszValues)
4971
4972
0
{
4973
0
    const OGRFieldDefn *poFDefn = poDefn->GetFieldDefn(iField);
4974
4975
0
    if (poFDefn == nullptr)
4976
0
        return;
4977
4978
0
    OGRFieldType eType = poFDefn->GetType();
4979
0
    if (eType == OFTStringList)
4980
0
    {
4981
0
        if (!IsFieldSetAndNotNullUnsafe(iField) ||
4982
0
            papszValues != pauFields[iField].StringList.paList)
4983
0
        {
4984
0
            OGRField uField;
4985
4986
0
            uField.StringList.nCount = CSLCount(papszValues);
4987
0
            uField.Set.nMarker2 = 0;
4988
0
            uField.Set.nMarker3 = 0;
4989
0
            uField.StringList.paList = const_cast<char **>(papszValues);
4990
4991
0
            SetField(iField, &uField);
4992
0
        }
4993
0
    }
4994
0
    else if (eType == OFTIntegerList)
4995
0
    {
4996
0
        const int nValues = CSLCount(papszValues);
4997
0
        int *panValues =
4998
0
            static_cast<int *>(VSI_MALLOC_VERBOSE(nValues * sizeof(int)));
4999
0
        if (panValues == nullptr)
5000
0
            return;
5001
0
        for (int i = 0; i < nValues; i++)
5002
0
        {
5003
            // As allowed by C standard, some systems like MSVC do not
5004
            // reset errno.
5005
0
            errno = 0;
5006
5007
0
            int nVal = atoi(papszValues[i]);
5008
0
            if (errno == ERANGE)
5009
0
            {
5010
0
                CPLError(
5011
0
                    CE_Warning, CPLE_AppDefined,
5012
0
                    "Field %s.%s: 32 bit integer overflow when converting %s",
5013
0
                    poDefn->GetName(), poFDefn->GetNameRef(), papszValues[i]);
5014
0
                if (papszValues[i][0] == '-')
5015
0
                    nVal = INT_MIN;
5016
0
                else
5017
0
                    nVal = INT_MAX;
5018
0
            }
5019
0
            panValues[i] = nVal;
5020
0
        }
5021
0
        SetField(iField, nValues, panValues);
5022
0
        CPLFree(panValues);
5023
0
    }
5024
0
    else if (eType == OFTInteger64List)
5025
0
    {
5026
0
        const int nValues = CSLCount(papszValues);
5027
0
        GIntBig *panValues = static_cast<GIntBig *>(
5028
0
            VSI_MALLOC_VERBOSE(nValues * sizeof(GIntBig)));
5029
0
        if (panValues == nullptr)
5030
0
            return;
5031
0
        for (int i = 0; i < nValues; i++)
5032
0
        {
5033
0
            panValues[i] = CPLAtoGIntBigEx(papszValues[i], TRUE, nullptr);
5034
0
        }
5035
0
        SetField(iField, nValues, panValues);
5036
0
        CPLFree(panValues);
5037
0
    }
5038
0
    else if (eType == OFTRealList)
5039
0
    {
5040
0
        const int nValues = CSLCount(papszValues);
5041
0
        double *padfValues =
5042
0
            static_cast<double *>(VSI_MALLOC_VERBOSE(nValues * sizeof(double)));
5043
0
        if (padfValues == nullptr)
5044
0
            return;
5045
0
        for (int i = 0; i < nValues; i++)
5046
0
        {
5047
0
            padfValues[i] = CPLAtof(papszValues[i]);
5048
0
        }
5049
0
        SetField(iField, nValues, padfValues);
5050
0
        CPLFree(padfValues);
5051
0
    }
5052
0
}
5053
5054
/************************************************************************/
5055
/*                      OGR_F_SetFieldStringList()                      */
5056
/************************************************************************/
5057
5058
/**
5059
 * \brief Set field to list of strings value.
5060
 *
5061
 * This function currently on has an effect of OFTStringList fields.
5062
 *
5063
 * This function is the same as the C++ method OGRFeature::SetField().
5064
 *
5065
 * @note This method has only an effect on the in-memory feature object. If
5066
 * this object comes from a layer and the modifications must be serialized back
5067
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
5068
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
5069
 *
5070
 * @param hFeat handle to the feature that owned the field.
5071
 * @param iField the field to set, from 0 to GetFieldCount()-1.
5072
 * @param papszValues the values to assign. List of NUL-terminated string,
5073
 * ending with a NULL pointer.
5074
 */
5075
5076
void OGR_F_SetFieldStringList(OGRFeatureH hFeat, int iField,
5077
                              CSLConstList papszValues)
5078
5079
0
{
5080
0
    VALIDATE_POINTER0(hFeat, "OGR_F_SetFieldStringList");
5081
5082
0
    OGRFeature::FromHandle(hFeat)->SetField(iField, papszValues);
5083
0
}
5084
5085
/************************************************************************/
5086
/*                              SetField()                              */
5087
/************************************************************************/
5088
5089
/**
5090
 * \brief Set field to binary data.
5091
 *
5092
 * This method currently on has an effect of OFTBinary fields.
5093
 *
5094
 * This method is the same as the C function OGR_F_SetFieldBinary().
5095
 *
5096
 * @note This method has only an effect on the in-memory feature object. If
5097
 * this object comes from a layer and the modifications must be serialized back
5098
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
5099
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
5100
 *
5101
 * @param iField the field to set, from 0 to GetFieldCount()-1.
5102
 * @param nBytes bytes of data being set.
5103
 * @param pabyData the raw data being applied.
5104
 */
5105
5106
void OGRFeature::SetField(int iField, int nBytes, const void *pabyData)
5107
5108
0
{
5109
0
    const OGRFieldDefn *poFDefn = poDefn->GetFieldDefn(iField);
5110
5111
0
    if (poFDefn == nullptr)
5112
0
        return;
5113
5114
0
    OGRFieldType eType = poFDefn->GetType();
5115
0
    if (eType == OFTBinary)
5116
0
    {
5117
0
        OGRField uField;
5118
5119
0
        uField.Binary.nCount = nBytes;
5120
0
        uField.Set.nMarker2 = 0;
5121
0
        uField.Set.nMarker3 = 0;
5122
0
        uField.Binary.paData =
5123
0
            const_cast<GByte *>(static_cast<const GByte *>(pabyData));
5124
5125
0
        SetField(iField, &uField);
5126
0
    }
5127
0
    else if (eType == OFTString || eType == OFTStringList)
5128
0
    {
5129
0
        char *pszStr = static_cast<char *>(VSI_MALLOC_VERBOSE(nBytes + 1));
5130
0
        if (pszStr == nullptr)
5131
0
            return;
5132
0
        if (nBytes > 0)
5133
0
            memcpy(pszStr, pabyData, nBytes);
5134
0
        pszStr[nBytes] = 0;
5135
0
        SetField(iField, pszStr);
5136
0
        CPLFree(pszStr);
5137
0
    }
5138
0
}
5139
5140
/************************************************************************/
5141
/*                        OGR_F_SetFieldBinary()                        */
5142
/************************************************************************/
5143
5144
/**
5145
 * \brief Set field to binary data.
5146
 *
5147
 * This function currently on has an effect of OFTBinary fields.
5148
 *
5149
 * This function is the same as the C++ method OGRFeature::SetField().
5150
 *
5151
 * @note This method has only an effect on the in-memory feature object. If
5152
 * this object comes from a layer and the modifications must be serialized back
5153
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
5154
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
5155
 *
5156
 * @param hFeat handle to the feature that owned the field.
5157
 * @param iField the field to set, from 0 to GetFieldCount()-1.
5158
 * @param nBytes the number of bytes in pabyData array.
5159
 * @param pabyData the data to apply.
5160
 */
5161
5162
void OGR_F_SetFieldBinary(OGRFeatureH hFeat, int iField, int nBytes,
5163
                          const void *pabyData)
5164
5165
0
{
5166
0
    VALIDATE_POINTER0(hFeat, "OGR_F_SetFieldBinary");
5167
5168
0
    OGRFeature::FromHandle(hFeat)->SetField(iField, nBytes, pabyData);
5169
0
}
5170
5171
/************************************************************************/
5172
/*                              SetField()                              */
5173
/************************************************************************/
5174
5175
/**
5176
 * \fn OGRFeature::SetField( const char* pszFName, int nYear, int nMonth,
5177
 *                           int nDay, int nHour, int nMinute, float fSecond,
5178
 *                           int nTZFlag )
5179
 * \brief Set field to date.
5180
 *
5181
 * This method currently only has an effect for OFTDate, OFTTime and OFTDateTime
5182
 * fields.
5183
 *
5184
 * @note This method has only an effect on the in-memory feature object. If
5185
 * this object comes from a layer and the modifications must be serialized back
5186
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
5187
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
5188
 *
5189
 * @param pszFName the name of the field to set.
5190
 * @param nYear (including century)
5191
 * @param nMonth (1-12)
5192
 * @param nDay (1-31)
5193
 * @param nHour (0-23)
5194
 * @param nMinute (0-59)
5195
 * @param fSecond (0-59, with millisecond accuracy)
5196
 * @param nTZFlag (0=unknown, 1=localtime, 100=GMT, see data model for details)
5197
 */
5198
5199
/**
5200
 * \brief Set field to date.
5201
 *
5202
 * This method currently only has an effect for OFTDate, OFTTime and OFTDateTime
5203
 * fields.
5204
 *
5205
 * This method is the same as the C function OGR_F_SetFieldDateTime().
5206
 *
5207
 * @note This method has only an effect on the in-memory feature object. If
5208
 * this object comes from a layer and the modifications must be serialized back
5209
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
5210
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
5211
 *
5212
 * @param iField the field to set, from 0 to GetFieldCount()-1.
5213
 * @param nYear (including century)
5214
 * @param nMonth (1-12)
5215
 * @param nDay (1-31)
5216
 * @param nHour (0-23)
5217
 * @param nMinute (0-59)
5218
 * @param fSecond (0-59, with millisecond accuracy)
5219
 * @param nTZFlag (0=unknown, 1=localtime, 100=GMT, see data model for details)
5220
 */
5221
5222
void OGRFeature::SetField(int iField, int nYear, int nMonth, int nDay,
5223
                          int nHour, int nMinute, float fSecond, int nTZFlag)
5224
5225
0
{
5226
0
    const OGRFieldDefn *poFDefn = poDefn->GetFieldDefn(iField);
5227
5228
0
    if (poFDefn == nullptr)
5229
0
        return;
5230
5231
0
    OGRFieldType eType = poFDefn->GetType();
5232
0
    if (eType == OFTDate || eType == OFTTime || eType == OFTDateTime)
5233
0
    {
5234
0
        if (static_cast<GInt16>(nYear) != nYear)
5235
0
        {
5236
0
            CPLError(CE_Failure, CPLE_NotSupported,
5237
0
                     "Years < -32768 or > 32767 are not supported");
5238
0
            return;
5239
0
        }
5240
5241
0
        pauFields[iField].Date.Year = static_cast<GInt16>(nYear);
5242
0
        pauFields[iField].Date.Month = static_cast<GByte>(nMonth);
5243
0
        pauFields[iField].Date.Day = static_cast<GByte>(nDay);
5244
0
        pauFields[iField].Date.Hour = static_cast<GByte>(nHour);
5245
0
        pauFields[iField].Date.Minute = static_cast<GByte>(nMinute);
5246
0
        pauFields[iField].Date.Second = fSecond;
5247
0
        pauFields[iField].Date.TZFlag = static_cast<GByte>(nTZFlag);
5248
0
    }
5249
0
    else if (eType == OFTString || eType == OFTStringList)
5250
0
    {
5251
        // "YYYY/MM/DD HH:MM:SS.sss+ZZ"
5252
0
        constexpr size_t MAX_SIZE = 26 + 1;
5253
0
        char szTempBuffer[MAX_SIZE] = {};
5254
0
        OGRFeatureFormatDateTimeBuffer(szTempBuffer, MAX_SIZE, nYear, nMonth,
5255
0
                                       nDay, nHour, nMinute, fSecond, nTZFlag);
5256
0
        SetField(iField, szTempBuffer);
5257
0
    }
5258
0
}
5259
5260
/************************************************************************/
5261
/*                       OGR_F_SetFieldDateTime()                       */
5262
/************************************************************************/
5263
5264
/**
5265
 * \brief Set field to datetime.
5266
 *
5267
 * This method currently only has an effect for OFTDate, OFTTime and OFTDateTime
5268
 * fields.
5269
 *
5270
 * @note This method has only an effect on the in-memory feature object. If
5271
 * this object comes from a layer and the modifications must be serialized back
5272
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
5273
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
5274
 *
5275
 * @param hFeat handle to the feature that owned the field.
5276
 * @param iField the field to set, from 0 to GetFieldCount()-1.
5277
 * @param nYear (including century)
5278
 * @param nMonth (1-12)
5279
 * @param nDay (1-31)
5280
 * @param nHour (0-23)
5281
 * @param nMinute (0-59)
5282
 * @param nSecond (0-59)
5283
 * @param nTZFlag (0=unknown, 1=localtime, 100=GMT, see data model for details)
5284
 *
5285
 * @see Use OGR_F_SetFieldDateTimeEx() for second with millisecond accuracy.
5286
 */
5287
5288
void OGR_F_SetFieldDateTime(OGRFeatureH hFeat, int iField, int nYear,
5289
                            int nMonth, int nDay, int nHour, int nMinute,
5290
                            int nSecond, int nTZFlag)
5291
5292
0
{
5293
0
    VALIDATE_POINTER0(hFeat, "OGR_F_SetFieldDateTime");
5294
5295
0
    OGRFeature::FromHandle(hFeat)->SetField(
5296
0
        iField, nYear, nMonth, nDay, nHour, nMinute,
5297
0
        static_cast<float>(nSecond), nTZFlag);
5298
0
}
5299
5300
/************************************************************************/
5301
/*                      OGR_F_SetFieldDateTimeEx()                      */
5302
/************************************************************************/
5303
5304
/**
5305
 * \brief Set field to datetime.
5306
 *
5307
 * This method currently only has an effect for OFTDate, OFTTime and OFTDateTime
5308
 * fields.
5309
 *
5310
 * @note This method has only an effect on the in-memory feature object. If
5311
 * this object comes from a layer and the modifications must be serialized back
5312
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
5313
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
5314
 *
5315
 * @param hFeat handle to the feature that owned the field.
5316
 * @param iField the field to set, from 0 to GetFieldCount()-1.
5317
 * @param nYear (including century)
5318
 * @param nMonth (1-12)
5319
 * @param nDay (1-31)
5320
 * @param nHour (0-23)
5321
 * @param nMinute (0-59)
5322
 * @param fSecond (0-59, with millisecond accuracy)
5323
 * @param nTZFlag (0=unknown, 1=localtime, 100=GMT, see data model for details)
5324
 *
5325
 */
5326
5327
void OGR_F_SetFieldDateTimeEx(OGRFeatureH hFeat, int iField, int nYear,
5328
                              int nMonth, int nDay, int nHour, int nMinute,
5329
                              float fSecond, int nTZFlag)
5330
5331
0
{
5332
0
    VALIDATE_POINTER0(hFeat, "OGR_F_SetFieldDateTimeEx");
5333
5334
0
    OGRFeature::FromHandle(hFeat)->SetField(iField, nYear, nMonth, nDay, nHour,
5335
0
                                            nMinute, fSecond, nTZFlag);
5336
0
}
5337
5338
/************************************************************************/
5339
/*                              SetField()                              */
5340
/************************************************************************/
5341
5342
/**
5343
 * \fn OGRFeature::SetField( const char* pszFName, const OGRField * puValue )
5344
 * \brief Set field.
5345
 *
5346
 * The passed value OGRField must be of exactly the same type as the
5347
 * target field, or an application crash may occur.  The passed value
5348
 * is copied, and will not be affected.  It remains the responsibility of
5349
 * the caller.
5350
 *
5351
 * @note This method has only an effect on the in-memory feature object. If
5352
 * this object comes from a layer and the modifications must be serialized back
5353
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
5354
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
5355
 *
5356
 * @param pszFName the name of the field to set.
5357
 * @param puValue the value to assign.
5358
 */
5359
5360
/**
5361
 * \brief Set field.
5362
 *
5363
 * The passed value OGRField must be of exactly the same type as the
5364
 * target field, or an application crash may occur.  The passed value
5365
 * is copied, and will not be affected.  It remains the responsibility of
5366
 * the caller.
5367
 *
5368
 * This method is the same as the C function OGR_F_SetFieldRaw().
5369
 *
5370
 * @note This method has only an effect on the in-memory feature object. If
5371
 * this object comes from a layer and the modifications must be serialized back
5372
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
5373
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
5374
 *
5375
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
5376
 * @param puValue the value to assign.
5377
 */
5378
5379
void OGRFeature::SetField(int iField, const OGRField *puValue)
5380
5381
0
{
5382
0
    SetFieldInternal(iField, puValue);
5383
0
}
5384
5385
bool OGRFeature::SetFieldInternal(int iField, const OGRField *puValue)
5386
5387
0
{
5388
0
    const OGRFieldDefn *poFDefn = poDefn->GetFieldDefn(iField);
5389
0
    if (iField < 0 || poFDefn == nullptr)
5390
0
        return false;
5391
5392
0
    if (poFDefn->GetType() == OFTInteger)
5393
0
    {
5394
0
        pauFields[iField] = *puValue;
5395
0
    }
5396
0
    else if (poFDefn->GetType() == OFTInteger64)
5397
0
    {
5398
0
        pauFields[iField] = *puValue;
5399
0
    }
5400
0
    else if (poFDefn->GetType() == OFTReal)
5401
0
    {
5402
0
        pauFields[iField] = *puValue;
5403
0
    }
5404
0
    else if (poFDefn->GetType() == OFTString)
5405
0
    {
5406
0
        if (IsFieldSetAndNotNullUnsafe(iField))
5407
0
            CPLFree(pauFields[iField].String);
5408
5409
0
        if (puValue->String == nullptr)
5410
0
            pauFields[iField].String = nullptr;
5411
0
        else if (OGR_RawField_IsUnset(puValue) || OGR_RawField_IsNull(puValue))
5412
0
            pauFields[iField] = *puValue;
5413
0
        else
5414
0
        {
5415
0
            pauFields[iField].String = VSI_STRDUP_VERBOSE(puValue->String);
5416
0
            if (pauFields[iField].String == nullptr)
5417
0
            {
5418
0
                OGR_RawField_SetUnset(&pauFields[iField]);
5419
0
                return false;
5420
0
            }
5421
0
        }
5422
0
    }
5423
0
    else if (poFDefn->GetType() == OFTDate || poFDefn->GetType() == OFTTime ||
5424
0
             poFDefn->GetType() == OFTDateTime)
5425
0
    {
5426
0
        memcpy(pauFields + iField, puValue, sizeof(OGRField));
5427
0
    }
5428
0
    else if (poFDefn->GetType() == OFTIntegerList)
5429
0
    {
5430
0
        const int nCount = puValue->IntegerList.nCount;
5431
5432
0
        if (IsFieldSetAndNotNullUnsafe(iField))
5433
0
            CPLFree(pauFields[iField].IntegerList.paList);
5434
5435
0
        if (OGR_RawField_IsUnset(puValue) || OGR_RawField_IsNull(puValue))
5436
0
        {
5437
0
            pauFields[iField] = *puValue;
5438
0
        }
5439
0
        else
5440
0
        {
5441
0
            pauFields[iField].IntegerList.paList =
5442
0
                static_cast<int *>(VSI_MALLOC_VERBOSE(sizeof(int) * nCount));
5443
0
            if (pauFields[iField].IntegerList.paList == nullptr)
5444
0
            {
5445
0
                OGR_RawField_SetUnset(&pauFields[iField]);
5446
0
                return false;
5447
0
            }
5448
0
            if (nCount > 0)
5449
0
            {
5450
0
                memcpy(pauFields[iField].IntegerList.paList,
5451
0
                       puValue->IntegerList.paList, sizeof(int) * nCount);
5452
0
            }
5453
0
            pauFields[iField].IntegerList.nCount = nCount;
5454
0
        }
5455
0
    }
5456
0
    else if (poFDefn->GetType() == OFTInteger64List)
5457
0
    {
5458
0
        const int nCount = puValue->Integer64List.nCount;
5459
5460
0
        if (IsFieldSetAndNotNullUnsafe(iField))
5461
0
            CPLFree(pauFields[iField].Integer64List.paList);
5462
5463
0
        if (OGR_RawField_IsUnset(puValue) || OGR_RawField_IsNull(puValue))
5464
0
        {
5465
0
            pauFields[iField] = *puValue;
5466
0
        }
5467
0
        else
5468
0
        {
5469
0
            pauFields[iField].Integer64List.paList = static_cast<GIntBig *>(
5470
0
                VSI_MALLOC_VERBOSE(sizeof(GIntBig) * nCount));
5471
0
            if (pauFields[iField].Integer64List.paList == nullptr)
5472
0
            {
5473
0
                OGR_RawField_SetUnset(&pauFields[iField]);
5474
0
                return false;
5475
0
            }
5476
0
            if (nCount > 0)
5477
0
            {
5478
0
                memcpy(pauFields[iField].Integer64List.paList,
5479
0
                       puValue->Integer64List.paList, sizeof(GIntBig) * nCount);
5480
0
            }
5481
0
            pauFields[iField].Integer64List.nCount = nCount;
5482
0
        }
5483
0
    }
5484
0
    else if (poFDefn->GetType() == OFTRealList)
5485
0
    {
5486
0
        const int nCount = puValue->RealList.nCount;
5487
5488
0
        if (IsFieldSetAndNotNullUnsafe(iField))
5489
0
            CPLFree(pauFields[iField].RealList.paList);
5490
5491
0
        if (OGR_RawField_IsUnset(puValue) || OGR_RawField_IsNull(puValue))
5492
0
        {
5493
0
            pauFields[iField] = *puValue;
5494
0
        }
5495
0
        else
5496
0
        {
5497
0
            pauFields[iField].RealList.paList = static_cast<double *>(
5498
0
                VSI_MALLOC_VERBOSE(sizeof(double) * nCount));
5499
0
            if (pauFields[iField].RealList.paList == nullptr)
5500
0
            {
5501
0
                OGR_RawField_SetUnset(&pauFields[iField]);
5502
0
                return false;
5503
0
            }
5504
0
            if (nCount > 0)
5505
0
            {
5506
0
                memcpy(pauFields[iField].RealList.paList,
5507
0
                       puValue->RealList.paList, sizeof(double) * nCount);
5508
0
            }
5509
0
            pauFields[iField].RealList.nCount = nCount;
5510
0
        }
5511
0
    }
5512
0
    else if (poFDefn->GetType() == OFTStringList)
5513
0
    {
5514
0
        if (IsFieldSetAndNotNullUnsafe(iField))
5515
0
            CSLDestroy(pauFields[iField].StringList.paList);
5516
5517
0
        if (OGR_RawField_IsUnset(puValue) || OGR_RawField_IsNull(puValue))
5518
0
        {
5519
0
            pauFields[iField] = *puValue;
5520
0
        }
5521
0
        else
5522
0
        {
5523
0
            char **papszNewList = nullptr;
5524
0
            for (char **papszIter = puValue->StringList.paList;
5525
0
                 papszIter != nullptr && *papszIter != nullptr; ++papszIter)
5526
0
            {
5527
0
                char **papszNewList2 =
5528
0
                    CSLAddStringMayFail(papszNewList, *papszIter);
5529
0
                if (papszNewList2 == nullptr)
5530
0
                {
5531
0
                    CSLDestroy(papszNewList);
5532
0
                    OGR_RawField_SetUnset(&pauFields[iField]);
5533
0
                    return false;
5534
0
                }
5535
0
                papszNewList = papszNewList2;
5536
0
            }
5537
0
            pauFields[iField].StringList.paList = papszNewList;
5538
0
            pauFields[iField].StringList.nCount = puValue->StringList.nCount;
5539
0
            CPLAssert(CSLCount(puValue->StringList.paList) ==
5540
0
                      puValue->StringList.nCount);
5541
0
        }
5542
0
    }
5543
0
    else if (poFDefn->GetType() == OFTBinary)
5544
0
    {
5545
0
        if (IsFieldSetAndNotNullUnsafe(iField))
5546
0
            CPLFree(pauFields[iField].Binary.paData);
5547
5548
0
        if (OGR_RawField_IsUnset(puValue) || OGR_RawField_IsNull(puValue))
5549
0
        {
5550
0
            pauFields[iField] = *puValue;
5551
0
        }
5552
0
        else
5553
0
        {
5554
0
            pauFields[iField].Binary.paData = static_cast<GByte *>(
5555
0
                VSI_MALLOC_VERBOSE(puValue->Binary.nCount));
5556
0
            if (pauFields[iField].Binary.paData == nullptr)
5557
0
            {
5558
0
                OGR_RawField_SetUnset(&pauFields[iField]);
5559
0
                return false;
5560
0
            }
5561
0
            if (puValue->Binary.nCount > 0)
5562
0
            {
5563
0
                memcpy(pauFields[iField].Binary.paData, puValue->Binary.paData,
5564
0
                       puValue->Binary.nCount);
5565
0
            }
5566
0
            pauFields[iField].Binary.nCount = puValue->Binary.nCount;
5567
0
        }
5568
0
    }
5569
0
    else
5570
0
    {
5571
        // Do nothing for other field types.
5572
0
    }
5573
0
    return true;
5574
0
}
5575
5576
/************************************************************************/
5577
/*                         OGR_F_SetFieldRaw()                          */
5578
/************************************************************************/
5579
5580
/**
5581
 * \brief Set field.
5582
 *
5583
 * The passed value OGRField must be of exactly the same type as the
5584
 * target field, or an application crash may occur.  The passed value
5585
 * is copied, and will not be affected.  It remains the responsibility of
5586
 * the caller.
5587
 *
5588
 * This function is the same as the C++ method OGRFeature::SetField().
5589
 *
5590
 * @note This method has only an effect on the in-memory feature object. If
5591
 * this object comes from a layer and the modifications must be serialized back
5592
 * to the datasource, OGR_L_SetFeature() must be used afterwards. Or if this is
5593
 * a new feature, OGR_L_CreateFeature() must be used afterwards.
5594
 *
5595
 * @param hFeat handle to the feature that owned the field.
5596
 * @param iField the field to fetch, from 0 to GetFieldCount()-1.
5597
 * @param psValue handle on the value to assign.
5598
 */
5599
5600
void OGR_F_SetFieldRaw(OGRFeatureH hFeat, int iField, const OGRField *psValue)
5601
5602
0
{
5603
0
    VALIDATE_POINTER0(hFeat, "OGR_F_SetFieldRaw");
5604
5605
0
    OGRFeature::FromHandle(hFeat)->SetField(iField, psValue);
5606
0
}
5607
5608
/************************************************************************/
5609
/*                            DumpReadable()                            */
5610
/************************************************************************/
5611
5612
/**
5613
 * \brief Dump this feature in a human readable form.
5614
 *
5615
 * This dumps the attributes, and geometry; however, it doesn't definition
5616
 * information (other than field types and names), nor does it report the
5617
 * geometry spatial reference system.
5618
 *
5619
 * A few options can be defined to change the default dump :
5620
 * <ul>
5621
 * <li>DISPLAY_FIELDS=NO : to hide the dump of the attributes</li>
5622
 * <li>DISPLAY_STYLE=NO : to hide the dump of the style string</li>
5623
 * <li>DISPLAY_GEOMETRY=NO : to hide the dump of the geometry</li>
5624
 * <li>DISPLAY_GEOMETRY=SUMMARY : to get only a summary of the geometry</li>
5625
 * </ul>
5626
 *
5627
 * This method is the same as the C function OGR_F_DumpReadable().
5628
 *
5629
 * @param fpOut the stream to write to, such as stdout.  If NULL stdout will
5630
 * be used.
5631
 * @param papszOptions NULL terminated list of options (may be NULL)
5632
 */
5633
5634
void OGRFeature::DumpReadable(FILE *fpOut, CSLConstList papszOptions) const
5635
5636
0
{
5637
0
    if (fpOut == nullptr)
5638
0
        fpOut = stdout;
5639
5640
0
    const auto osStr = DumpReadableAsString(papszOptions);
5641
0
    fprintf(fpOut, "%s", osStr.c_str());
5642
0
}
5643
5644
/************************************************************************/
5645
/*                        DumpReadableAsString()                        */
5646
/************************************************************************/
5647
5648
/**
5649
 * \brief Dump this feature in a human readable form.
5650
 *
5651
 * This dumps the attributes, and geometry; however, it doesn't definition
5652
 * information (other than field types and names), nor does it report the
5653
 * geometry spatial reference system.
5654
 *
5655
 * A few options can be defined to change the default dump :
5656
 * <ul>
5657
 * <li>DISPLAY_FIELDS=NO : to hide the dump of the attributes</li>
5658
 * <li>DISPLAY_STYLE=NO : to hide the dump of the style string</li>
5659
 * <li>DISPLAY_GEOMETRY=NO : to hide the dump of the geometry</li>
5660
 * <li>DISPLAY_GEOMETRY=SUMMARY : to get only a summary of the geometry</li>
5661
 * </ul>
5662
 *
5663
 * @param papszOptions NULL terminated list of options (may be NULL)
5664
 * @return a string with the feature representation.
5665
 * @since GDAL 3.7
5666
 */
5667
5668
std::string OGRFeature::DumpReadableAsString(CSLConstList papszOptions) const
5669
0
{
5670
0
    std::string osRet;
5671
5672
0
    osRet += CPLOPrintf("OGRFeature(%s):" CPL_FRMT_GIB "\n", poDefn->GetName(),
5673
0
                        GetFID());
5674
5675
0
    const char *pszDisplayFields =
5676
0
        CSLFetchNameValue(papszOptions, "DISPLAY_FIELDS");
5677
0
    if (pszDisplayFields == nullptr || CPLTestBool(pszDisplayFields))
5678
0
    {
5679
0
        const int nFieldCount = GetFieldCount();
5680
0
        for (int iField = 0; iField < nFieldCount; iField++)
5681
0
        {
5682
0
            if (!IsFieldSet(iField))
5683
0
                continue;
5684
0
            const OGRFieldDefn *poFDefn = poDefn->GetFieldDefnUnsafe(iField);
5685
5686
0
            const char *pszType =
5687
0
                (poFDefn->GetSubType() != OFSTNone)
5688
0
                    ? CPLSPrintf(
5689
0
                          "%s(%s)",
5690
0
                          poFDefn->GetFieldTypeName(poFDefn->GetType()),
5691
0
                          poFDefn->GetFieldSubTypeName(poFDefn->GetSubType()))
5692
0
                    : poFDefn->GetFieldTypeName(poFDefn->GetType());
5693
5694
0
            osRet += CPLOPrintf("  %s (%s) = ", poFDefn->GetNameRef(), pszType);
5695
5696
0
            if (IsFieldNull(iField))
5697
0
                osRet += "(null)\n";
5698
0
            else
5699
0
                osRet += CPLOPrintf("%s\n", GetFieldAsString(iField));
5700
0
        }
5701
0
    }
5702
5703
0
    if (GetStyleString() != nullptr)
5704
0
    {
5705
0
        const char *pszDisplayStyle =
5706
0
            CSLFetchNameValue(papszOptions, "DISPLAY_STYLE");
5707
0
        if (pszDisplayStyle == nullptr || CPLTestBool(pszDisplayStyle))
5708
0
        {
5709
0
            osRet += CPLOPrintf("  Style = %s\n", GetStyleString());
5710
0
        }
5711
0
    }
5712
5713
0
    const int nGeomFieldCount = GetGeomFieldCount();
5714
0
    if (nGeomFieldCount > 0)
5715
0
    {
5716
0
        const char *pszDisplayGeometry =
5717
0
            CSLFetchNameValue(papszOptions, "DISPLAY_GEOMETRY");
5718
0
        if (!(pszDisplayGeometry != nullptr && EQUAL(pszDisplayGeometry, "NO")))
5719
0
        {
5720
0
            for (int iField = 0; iField < nGeomFieldCount; iField++)
5721
0
            {
5722
0
                const OGRGeomFieldDefn *poFDefn =
5723
0
                    poDefn->GetGeomFieldDefn(iField);
5724
5725
0
                if (papoGeometries[iField] != nullptr)
5726
0
                {
5727
0
                    CPLStringList aosGeomOptions(papszOptions);
5728
5729
0
                    const auto &oCoordPrec = poFDefn->GetCoordinatePrecision();
5730
5731
0
                    if (oCoordPrec.dfXYResolution !=
5732
0
                        OGRGeomCoordinatePrecision::UNKNOWN)
5733
0
                    {
5734
0
                        aosGeomOptions.SetNameValue(
5735
0
                            "XY_COORD_PRECISION",
5736
0
                            CPLSPrintf("%d",
5737
0
                                       OGRGeomCoordinatePrecision::
5738
0
                                           ResolutionToPrecision(
5739
0
                                               oCoordPrec.dfXYResolution)));
5740
0
                    }
5741
0
                    if (oCoordPrec.dfZResolution !=
5742
0
                        OGRGeomCoordinatePrecision::UNKNOWN)
5743
0
                    {
5744
0
                        aosGeomOptions.SetNameValue(
5745
0
                            "Z_COORD_PRECISION",
5746
0
                            CPLSPrintf("%d",
5747
0
                                       OGRGeomCoordinatePrecision::
5748
0
                                           ResolutionToPrecision(
5749
0
                                               oCoordPrec.dfZResolution)));
5750
0
                    }
5751
5752
0
                    osRet += "  ";
5753
0
                    if (strlen(poFDefn->GetNameRef()) > 0 &&
5754
0
                        GetGeomFieldCount() > 1)
5755
0
                        osRet += CPLOPrintf("%s = ", poFDefn->GetNameRef());
5756
0
                    osRet += papoGeometries[iField]->dumpReadable(
5757
0
                        nullptr, aosGeomOptions.List());
5758
0
                }
5759
0
            }
5760
0
        }
5761
0
    }
5762
5763
0
    osRet += "\n";
5764
0
    return osRet;
5765
0
}
5766
5767
/************************************************************************/
5768
/*                         OGR_F_DumpReadable()                         */
5769
/************************************************************************/
5770
5771
/**
5772
 * \brief Dump this feature in a human readable form.
5773
 *
5774
 * This dumps the attributes, and geometry; however, it doesn't definition
5775
 * information (other than field types and names), nor does it report the
5776
 * geometry spatial reference system.
5777
 *
5778
 * This function is the same as the C++ method OGRFeature::DumpReadable().
5779
 *
5780
 * @param hFeat handle to the feature to dump.
5781
 * @param fpOut the stream to write to, such as strout.
5782
 */
5783
5784
void OGR_F_DumpReadable(OGRFeatureH hFeat, FILE *fpOut)
5785
5786
0
{
5787
0
    VALIDATE_POINTER0(hFeat, "OGR_F_DumpReadable");
5788
5789
0
    OGRFeature::FromHandle(hFeat)->DumpReadable(fpOut);
5790
0
}
5791
5792
/************************************************************************/
5793
/*                     OGR_F_DumpReadableAsString()                     */
5794
/************************************************************************/
5795
5796
/**
5797
 * \brief Dump this feature in a human readable form.
5798
 *
5799
 * This dumps the attributes, and geometry; however, it doesn't include
5800
 * definition information (other than field types and names), nor does
5801
 * it report the geometry spatial reference system.
5802
 *
5803
 * A few options can be defined to change the default dump :
5804
 * <ul>
5805
 * <li>DISPLAY_FIELDS=NO : to hide the dump of the attributes</li>
5806
 * <li>DISPLAY_STYLE=NO : to hide the dump of the style string</li>
5807
 * <li>DISPLAY_GEOMETRY=NO : to hide the dump of the geometry</li>
5808
 * <li>DISPLAY_GEOMETRY=SUMMARY : to get only a summary of the geometry</li>
5809
 * </ul>
5810
 *
5811
 * @param hFeat handle to the feature to dump.
5812
 * @param papszOptions NULL terminated list of options (may be NULL)
5813
 * @return a string with the feature representation (to be freed with CPLFree())
5814
 * @since GDAL 3.8
5815
 */
5816
5817
char *OGR_F_DumpReadableAsString(OGRFeatureH hFeat, CSLConstList papszOptions)
5818
0
{
5819
0
    VALIDATE_POINTER1(hFeat, "OGR_F_DumpReadableAsString", nullptr);
5820
5821
0
    return CPLStrdup(OGRFeature::FromHandle(hFeat)
5822
0
                         ->DumpReadableAsString(papszOptions)
5823
0
                         .c_str());
5824
0
}
5825
5826
/************************************************************************/
5827
/*                               GetFID()                               */
5828
/************************************************************************/
5829
5830
/**
5831
 * \fn GIntBig OGRFeature::GetFID() const;
5832
 *
5833
 * \brief Get feature identifier.
5834
 *
5835
 * This method is the same as the C function OGR_F_GetFID().
5836
 *
5837
 * @return feature id or OGRNullFID if none has been assigned.
5838
 */
5839
5840
/************************************************************************/
5841
/*                            OGR_F_GetFID()                            */
5842
/************************************************************************/
5843
5844
/**
5845
 * \brief Get feature identifier.
5846
 *
5847
 * This function is the same as the C++ method OGRFeature::GetFID().
5848
 *
5849
 * @param hFeat handle to the feature from which to get the feature
5850
 * identifier.
5851
 * @return feature id or OGRNullFID if none has been assigned.
5852
 */
5853
5854
GIntBig OGR_F_GetFID(OGRFeatureH hFeat)
5855
5856
0
{
5857
0
    VALIDATE_POINTER1(hFeat, "OGR_F_GetFID", 0);
5858
5859
0
    return OGRFeature::FromHandle(hFeat)->GetFID();
5860
0
}
5861
5862
/************************************************************************/
5863
/*                               SetFID()                               */
5864
/************************************************************************/
5865
5866
/**
5867
 * \brief Set the feature identifier.
5868
 *
5869
 * For specific types of features this operation may fail on illegal
5870
 * features ids.  Generally it always succeeds.  Feature ids should be
5871
 * greater than or equal to zero, with the exception of OGRNullFID (-1)
5872
 * indicating that the feature id is unknown.
5873
 *
5874
 * This method is the same as the C function OGR_F_SetFID().
5875
 *
5876
 * @param nFIDIn the new feature identifier value to assign.
5877
 *
5878
 * @return On success OGRERR_NONE, or on failure some other value.
5879
 */
5880
5881
OGRErr OGRFeature::SetFID(GIntBig nFIDIn)
5882
5883
0
{
5884
0
    nFID = nFIDIn;
5885
5886
0
    return OGRERR_NONE;
5887
0
}
5888
5889
/************************************************************************/
5890
/*                            OGR_F_SetFID()                            */
5891
/************************************************************************/
5892
5893
/**
5894
 * \brief Set the feature identifier.
5895
 *
5896
 * For specific types of features this operation may fail on illegal
5897
 * features ids.  Generally it always succeeds.  Feature ids should be
5898
 * greater than or equal to zero, with the exception of OGRNullFID (-1)
5899
 * indicating that the feature id is unknown.
5900
 *
5901
 * This function is the same as the C++ method OGRFeature::SetFID().
5902
 *
5903
 * @param hFeat handle to the feature to set the feature id to.
5904
 * @param nFID the new feature identifier value to assign.
5905
 *
5906
 * @return On success OGRERR_NONE, or on failure some other value.
5907
 */
5908
5909
OGRErr OGR_F_SetFID(OGRFeatureH hFeat, GIntBig nFID)
5910
5911
0
{
5912
0
    VALIDATE_POINTER1(hFeat, "OGR_F_SetFID", OGRERR_FAILURE);
5913
5914
0
    return OGRFeature::FromHandle(hFeat)->SetFID(nFID);
5915
0
}
5916
5917
/************************************************************************/
5918
/*                               Equal()                                */
5919
/************************************************************************/
5920
5921
/**
5922
 * \brief Test if two features are the same.
5923
 *
5924
 * Two features are considered equal if the share them (pointer equality)
5925
 * same OGRFeatureDefn, have the same field values, and the same geometry
5926
 * (as tested by OGRGeometry::Equal()) as well as the same feature id.
5927
 *
5928
 * This method is the same as the C function OGR_F_Equal().
5929
 *
5930
 * @param poFeature the other feature to test this one against.
5931
 *
5932
 * @return TRUE if they are equal, otherwise FALSE.
5933
 */
5934
5935
OGRBoolean OGRFeature::Equal(const OGRFeature *poFeature) const
5936
5937
0
{
5938
0
    if (poFeature == this)
5939
0
        return TRUE;
5940
5941
0
    if (GetFID() != poFeature->GetFID())
5942
0
        return FALSE;
5943
5944
0
    if (GetDefnRef() != poFeature->GetDefnRef())
5945
0
        return FALSE;
5946
5947
0
    const int nFields = GetDefnRef()->GetFieldCountUnsafe();
5948
0
    for (int i = 0; i < nFields; i++)
5949
0
    {
5950
0
        if (IsFieldSet(i) != poFeature->IsFieldSet(i))
5951
0
            return FALSE;
5952
0
        if (IsFieldNull(i) != poFeature->IsFieldNull(i))
5953
0
            return FALSE;
5954
0
        if (!IsFieldSetAndNotNullUnsafe(i))
5955
0
            continue;
5956
5957
0
        switch (GetDefnRef()->GetFieldDefnUnsafe(i)->GetType())
5958
0
        {
5959
0
            case OFTInteger:
5960
0
                if (GetFieldAsInteger(i) != poFeature->GetFieldAsInteger(i))
5961
0
                    return FALSE;
5962
0
                break;
5963
5964
0
            case OFTInteger64:
5965
0
                if (GetFieldAsInteger64(i) != poFeature->GetFieldAsInteger64(i))
5966
0
                    return FALSE;
5967
0
                break;
5968
5969
0
            case OFTReal:
5970
0
            {
5971
0
                const double dfVal1 = GetFieldAsDouble(i);
5972
0
                const double dfVal2 = poFeature->GetFieldAsDouble(i);
5973
0
                if (std::isnan(dfVal1))
5974
0
                {
5975
0
                    if (!std::isnan(dfVal2))
5976
0
                        return FALSE;
5977
0
                }
5978
0
                else if (std::isnan(dfVal2))
5979
0
                {
5980
0
                    return FALSE;
5981
0
                }
5982
0
                else if (dfVal1 != dfVal2)
5983
0
                {
5984
0
                    return FALSE;
5985
0
                }
5986
0
                break;
5987
0
            }
5988
5989
0
            case OFTString:
5990
0
                if (strcmp(GetFieldAsString(i),
5991
0
                           poFeature->GetFieldAsString(i)) != 0)
5992
0
                    return FALSE;
5993
0
                break;
5994
5995
0
            case OFTIntegerList:
5996
0
            {
5997
0
                int nCount1 = 0;
5998
0
                int nCount2 = 0;
5999
0
                const int *pnList1 = GetFieldAsIntegerList(i, &nCount1);
6000
0
                const int *pnList2 =
6001
0
                    poFeature->GetFieldAsIntegerList(i, &nCount2);
6002
0
                if (nCount1 != nCount2)
6003
0
                    return FALSE;
6004
0
                for (int j = 0; j < nCount1; j++)
6005
0
                {
6006
0
                    if (pnList1[j] != pnList2[j])
6007
0
                        return FALSE;
6008
0
                }
6009
0
                break;
6010
0
            }
6011
6012
0
            case OFTInteger64List:
6013
0
            {
6014
0
                int nCount1 = 0;
6015
0
                int nCount2 = 0;
6016
0
                const GIntBig *pnList1 = GetFieldAsInteger64List(i, &nCount1);
6017
0
                const GIntBig *pnList2 =
6018
0
                    poFeature->GetFieldAsInteger64List(i, &nCount2);
6019
0
                if (nCount1 != nCount2)
6020
0
                    return FALSE;
6021
0
                for (int j = 0; j < nCount1; j++)
6022
0
                {
6023
0
                    if (pnList1[j] != pnList2[j])
6024
0
                        return FALSE;
6025
0
                }
6026
0
                break;
6027
0
            }
6028
6029
0
            case OFTRealList:
6030
0
            {
6031
0
                int nCount1 = 0;
6032
0
                int nCount2 = 0;
6033
0
                const double *padfList1 = GetFieldAsDoubleList(i, &nCount1);
6034
0
                const double *padfList2 =
6035
0
                    poFeature->GetFieldAsDoubleList(i, &nCount2);
6036
0
                if (nCount1 != nCount2)
6037
0
                    return FALSE;
6038
0
                for (int j = 0; j < nCount1; j++)
6039
0
                {
6040
0
                    const double dfVal1 = padfList1[j];
6041
0
                    const double dfVal2 = padfList2[j];
6042
0
                    if (std::isnan(dfVal1))
6043
0
                    {
6044
0
                        if (!std::isnan(dfVal2))
6045
0
                            return FALSE;
6046
0
                    }
6047
0
                    else if (std::isnan(dfVal2))
6048
0
                    {
6049
0
                        return FALSE;
6050
0
                    }
6051
0
                    else if (dfVal1 != dfVal2)
6052
0
                    {
6053
0
                        return FALSE;
6054
0
                    }
6055
0
                }
6056
0
                break;
6057
0
            }
6058
6059
0
            case OFTStringList:
6060
0
            {
6061
0
                int nCount1 = 0;
6062
0
                int nCount2 = 0;
6063
0
                char **papszList1 = GetFieldAsStringList(i);
6064
0
                char **papszList2 = poFeature->GetFieldAsStringList(i);
6065
0
                nCount1 = CSLCount(papszList1);
6066
0
                nCount2 = CSLCount(papszList2);
6067
0
                if (nCount1 != nCount2)
6068
0
                    return FALSE;
6069
0
                for (int j = 0; j < nCount1; j++)
6070
0
                {
6071
0
                    if (strcmp(papszList1[j], papszList2[j]) != 0)
6072
0
                        return FALSE;
6073
0
                }
6074
0
                break;
6075
0
            }
6076
6077
0
            case OFTTime:
6078
0
            case OFTDate:
6079
0
            case OFTDateTime:
6080
0
            {
6081
0
                int nYear1 = 0;
6082
0
                int nMonth1 = 0;
6083
0
                int nDay1 = 0;
6084
0
                int nHour1 = 0;
6085
0
                int nMinute1 = 0;
6086
0
                int nTZFlag1 = 0;
6087
0
                int nYear2 = 0;
6088
0
                int nMonth2 = 0;
6089
0
                int nDay2 = 0;
6090
0
                int nHour2 = 0;
6091
0
                int nMinute2 = 0;
6092
0
                int nTZFlag2 = 0;
6093
0
                float fSecond1 = 0.0f;
6094
0
                float fSecond2 = 0.0f;
6095
0
                GetFieldAsDateTime(i, &nYear1, &nMonth1, &nDay1, &nHour1,
6096
0
                                   &nMinute1, &fSecond1, &nTZFlag1);
6097
0
                poFeature->GetFieldAsDateTime(i, &nYear2, &nMonth2, &nDay2,
6098
0
                                              &nHour2, &nMinute2, &fSecond2,
6099
0
                                              &nTZFlag2);
6100
6101
0
                if (!(nYear1 == nYear2 && nMonth1 == nMonth2 &&
6102
0
                      nDay1 == nDay2 && nHour1 == nHour2 &&
6103
0
                      nMinute1 == nMinute2 && fSecond1 == fSecond2 &&
6104
0
                      nTZFlag1 == nTZFlag2))
6105
0
                    return FALSE;
6106
0
                break;
6107
0
            }
6108
6109
0
            case OFTBinary:
6110
0
            {
6111
0
                int nCount1 = 0;
6112
0
                int nCount2 = 0;
6113
0
                GByte *pabyData1 = GetFieldAsBinary(i, &nCount1);
6114
0
                GByte *pabyData2 = poFeature->GetFieldAsBinary(i, &nCount2);
6115
0
                if (nCount1 != nCount2)
6116
0
                    return FALSE;
6117
0
                if (memcmp(pabyData1, pabyData2, nCount1) != 0)
6118
0
                    return FALSE;
6119
0
                break;
6120
0
            }
6121
6122
0
            default:
6123
0
                if (strcmp(GetFieldAsString(i),
6124
0
                           poFeature->GetFieldAsString(i)) != 0)
6125
0
                    return FALSE;
6126
0
                break;
6127
0
        }
6128
0
    }
6129
6130
0
    const int nGeomFieldCount = GetGeomFieldCount();
6131
0
    for (int i = 0; i < nGeomFieldCount; i++)
6132
0
    {
6133
0
        const OGRGeometry *poThisGeom = GetGeomFieldRef(i);
6134
0
        const OGRGeometry *poOtherGeom = poFeature->GetGeomFieldRef(i);
6135
6136
0
        if (poThisGeom == nullptr && poOtherGeom != nullptr)
6137
0
            return FALSE;
6138
6139
0
        if (poThisGeom != nullptr && poOtherGeom == nullptr)
6140
0
            return FALSE;
6141
6142
0
        if (poThisGeom != nullptr && poOtherGeom != nullptr &&
6143
0
            (!poThisGeom->Equals(poOtherGeom)))
6144
0
            return FALSE;
6145
0
    }
6146
6147
0
    return TRUE;
6148
0
}
6149
6150
/************************************************************************/
6151
/*                            OGR_F_Equal()                             */
6152
/************************************************************************/
6153
6154
/**
6155
 * \brief Test if two features are the same.
6156
 *
6157
 * Two features are considered equal if the share them (handle equality)
6158
 * same OGRFeatureDefn, have the same field values, and the same geometry
6159
 * (as tested by OGR_G_Equal()) as well as the same feature id.
6160
 *
6161
 * This function is the same as the C++ method OGRFeature::Equal().
6162
 *
6163
 * @param hFeat handle to one of the feature.
6164
 * @param hOtherFeat handle to the other feature to test this one against.
6165
 *
6166
 * @return TRUE if they are equal, otherwise FALSE.
6167
 */
6168
6169
int OGR_F_Equal(OGRFeatureH hFeat, OGRFeatureH hOtherFeat)
6170
6171
0
{
6172
0
    VALIDATE_POINTER1(hFeat, "OGR_F_Equal", 0);
6173
0
    VALIDATE_POINTER1(hOtherFeat, "OGR_F_Equal", 0);
6174
6175
0
    return OGRFeature::FromHandle(hFeat)->Equal(
6176
0
        OGRFeature::FromHandle(hOtherFeat));
6177
0
}
6178
6179
/************************************************************************/
6180
/*                              SetFrom()                               */
6181
/************************************************************************/
6182
6183
/**
6184
 * \brief Set one feature from another.
6185
 *
6186
 * Overwrite the contents of this feature from the geometry and attributes
6187
 * of another.  The poSrcFeature does not need to have the same
6188
 * OGRFeatureDefn.  Field values are copied by corresponding field names.
6189
 * Field types do not have to exactly match.  SetField() method conversion
6190
 * rules will be applied as needed.
6191
 *
6192
 * This method is the same as the C function OGR_F_SetFrom().
6193
 *
6194
 * @param poSrcFeature the feature from which geometry, and field values will
6195
 * be copied.
6196
 *
6197
 * @param bForgiving TRUE if the operation should continue despite lacking
6198
 * output fields matching some of the source fields.
6199
 *
6200
 * @return OGRERR_NONE if the operation succeeds, even if some values are
6201
 * not transferred, otherwise an error code.
6202
 */
6203
6204
OGRErr OGRFeature::SetFrom(const OGRFeature *poSrcFeature, int bForgiving)
6205
6206
0
{
6207
0
    const auto &oMap = poDefn->ComputeMapForSetFrom(poSrcFeature->GetDefnRef(),
6208
0
                                                    CPL_TO_BOOL(bForgiving));
6209
0
    if (oMap.empty())
6210
0
    {
6211
0
        if (poSrcFeature->GetFieldCount())
6212
0
            return OGRERR_FAILURE;
6213
0
        int dummy = 0;
6214
0
        return SetFrom(poSrcFeature, &dummy, bForgiving);
6215
0
    }
6216
0
    return SetFrom(poSrcFeature, oMap.data(), bForgiving);
6217
0
}
6218
6219
/************************************************************************/
6220
/*                           OGR_F_SetFrom()                            */
6221
/************************************************************************/
6222
6223
/**
6224
 * \brief Set one feature from another.
6225
 *
6226
 * Overwrite the contents of this feature from the geometry and attributes
6227
 * of another.  The hOtherFeature does not need to have the same
6228
 * OGRFeatureDefn.  Field values are copied by corresponding field names.
6229
 * Field types do not have to exactly match.  OGR_F_SetField*() function
6230
 * conversion rules will be applied as needed.
6231
 *
6232
 * This function is the same as the C++ method OGRFeature::SetFrom().
6233
 *
6234
 * @param hFeat handle to the feature to set to.
6235
 * @param hOtherFeat handle to the feature from which geometry,
6236
 * and field values will be copied.
6237
 *
6238
 * @param bForgiving TRUE if the operation should continue despite lacking
6239
 * output fields matching some of the source fields.
6240
 *
6241
 * @return OGRERR_NONE if the operation succeeds, even if some values are
6242
 * not transferred, otherwise an error code.
6243
 */
6244
6245
OGRErr OGR_F_SetFrom(OGRFeatureH hFeat, OGRFeatureH hOtherFeat, int bForgiving)
6246
6247
0
{
6248
0
    VALIDATE_POINTER1(hFeat, "OGR_F_SetFrom", OGRERR_FAILURE);
6249
0
    VALIDATE_POINTER1(hOtherFeat, "OGR_F_SetFrom", OGRERR_FAILURE);
6250
6251
0
    return OGRFeature::FromHandle(hFeat)->SetFrom(
6252
0
        OGRFeature::FromHandle(hOtherFeat), bForgiving);
6253
0
}
6254
6255
/************************************************************************/
6256
/*                              SetFrom()                               */
6257
/************************************************************************/
6258
6259
/**
6260
 * \brief Set one feature from another.
6261
 *
6262
 * Overwrite the contents of this feature from the geometry and attributes
6263
 * of another.  The poSrcFeature does not need to have the same
6264
 * OGRFeatureDefn.  Field values are copied according to the provided indices
6265
 * map. Field types do not have to exactly match.  SetField() method
6266
 * conversion rules will be applied as needed. This is more efficient than
6267
 * OGR_F_SetFrom() in that this doesn't lookup the fields by their names.
6268
 * Particularly useful when the field names don't match.
6269
 *
6270
 * This method is the same as the C function OGR_F_SetFromWithMap().
6271
 *
6272
 * @param poSrcFeature the feature from which geometry, and field values will
6273
 * be copied.
6274
 *
6275
 * @param panMap Array of the indices of the feature's fields
6276
 * stored at the corresponding index of the source feature's fields. A value of
6277
 * -1 should be used to ignore the source's field. The array should not be NULL
6278
 * and be as long as the number of fields in the source feature.
6279
 *
6280
 * @param bForgiving TRUE if the operation should continue despite lacking
6281
 * output fields matching some of the source fields.
6282
 *
6283
 * @param bUseISO8601ForDateTimeAsString true if datetime fields
6284
 * converted to string should use ISO8601 formatting rather than OGR own format.
6285
 *
6286
 * @return OGRERR_NONE if the operation succeeds, even if some values are
6287
 * not transferred, otherwise an error code.
6288
 */
6289
6290
OGRErr OGRFeature::SetFrom(const OGRFeature *poSrcFeature, const int *panMap,
6291
                           int bForgiving, bool bUseISO8601ForDateTimeAsString)
6292
6293
0
{
6294
0
    if (poSrcFeature == this)
6295
0
        return OGRERR_FAILURE;
6296
6297
0
    SetFID(OGRNullFID);
6298
6299
    /* -------------------------------------------------------------------- */
6300
    /*      Set the geometry.                                               */
6301
    /* -------------------------------------------------------------------- */
6302
0
    if (GetGeomFieldCount() == 1)
6303
0
    {
6304
0
        const OGRGeomFieldDefn *poGFieldDefn = GetGeomFieldDefnRef(0);
6305
6306
0
        int iSrc = poSrcFeature->GetGeomFieldIndex(poGFieldDefn->GetNameRef());
6307
0
        if (iSrc >= 0)
6308
0
            SetGeomField(0, poSrcFeature->GetGeomFieldRef(iSrc));
6309
0
        else
6310
            // Whatever the geometry field names are.  For backward
6311
            // compatibility.
6312
0
            SetGeomField(0, poSrcFeature->GetGeomFieldRef(0));
6313
0
    }
6314
0
    else
6315
0
    {
6316
0
        for (int i = 0; i < GetGeomFieldCount(); i++)
6317
0
        {
6318
0
            const OGRGeomFieldDefn *poGFieldDefn = GetGeomFieldDefnRef(i);
6319
6320
0
            const int iSrc =
6321
0
                poSrcFeature->GetGeomFieldIndex(poGFieldDefn->GetNameRef());
6322
0
            if (iSrc >= 0)
6323
0
                SetGeomField(i, poSrcFeature->GetGeomFieldRef(iSrc));
6324
0
            else
6325
0
                SetGeomField(i, nullptr);
6326
0
        }
6327
0
    }
6328
6329
    /* -------------------------------------------------------------------- */
6330
    /*      Copy feature style string.                                      */
6331
    /* -------------------------------------------------------------------- */
6332
0
    SetStyleString(poSrcFeature->GetStyleString());
6333
6334
    /* -------------------------------------------------------------------- */
6335
    /*      Copy native data.                                               */
6336
    /* -------------------------------------------------------------------- */
6337
0
    SetNativeData(poSrcFeature->GetNativeData());
6338
0
    SetNativeMediaType(poSrcFeature->GetNativeMediaType());
6339
6340
    /* -------------------------------------------------------------------- */
6341
    /*      Set the fields by name.                                         */
6342
    /* -------------------------------------------------------------------- */
6343
6344
0
    const OGRErr eErr = SetFieldsFrom(poSrcFeature, panMap, bForgiving,
6345
0
                                      bUseISO8601ForDateTimeAsString);
6346
0
    if (eErr != OGRERR_NONE)
6347
0
        return eErr;
6348
6349
0
    return OGRERR_NONE;
6350
0
}
6351
6352
/************************************************************************/
6353
/*                        OGR_F_SetFromWithMap()                        */
6354
/************************************************************************/
6355
6356
/**
6357
 * \brief Set one feature from another.
6358
 *
6359
 * Overwrite the contents of this feature from the geometry and attributes
6360
 * of another.  The hOtherFeature does not need to have the same
6361
 * OGRFeatureDefn.  Field values are copied according to the provided indices
6362
 * map. Field types do not have to exactly match.  OGR_F_SetField*() function
6363
 * conversion rules will be applied as needed. This is more efficient than
6364
 * OGR_F_SetFrom() in that this doesn't lookup the fields by their names.
6365
 * Particularly useful when the field names don't match.
6366
 *
6367
 * This function is the same as the C++ method OGRFeature::SetFrom().
6368
 *
6369
 * @param hFeat handle to the feature to set to.
6370
 * @param hOtherFeat handle to the feature from which geometry,
6371
 * and field values will be copied.
6372
 *
6373
 * @param panMap Array of the indices of the destination feature's fields
6374
 * stored at the corresponding index of the source feature's fields. A value of
6375
 * -1 should be used to ignore the source's field. The array should not be NULL
6376
 * and be as long as the number of fields in the source feature.
6377
 *
6378
 * @param bForgiving TRUE if the operation should continue despite lacking
6379
 * output fields matching some of the source fields.
6380
 *
6381
 * @return OGRERR_NONE if the operation succeeds, even if some values are
6382
 * not transferred, otherwise an error code.
6383
 */
6384
6385
OGRErr OGR_F_SetFromWithMap(OGRFeatureH hFeat, OGRFeatureH hOtherFeat,
6386
                            int bForgiving, const int *panMap)
6387
6388
0
{
6389
0
    VALIDATE_POINTER1(hFeat, "OGR_F_SetFrom", OGRERR_FAILURE);
6390
0
    VALIDATE_POINTER1(hOtherFeat, "OGR_F_SetFrom", OGRERR_FAILURE);
6391
0
    VALIDATE_POINTER1(panMap, "OGR_F_SetFrom", OGRERR_FAILURE);
6392
6393
0
    return OGRFeature::FromHandle(hFeat)->SetFrom(
6394
0
        OGRFeature::FromHandle(hOtherFeat), panMap, bForgiving);
6395
0
}
6396
6397
/************************************************************************/
6398
/*                           SetFieldsFrom()                            */
6399
/************************************************************************/
6400
6401
/**
6402
 * \brief Set fields from another feature.
6403
 *
6404
 * Overwrite the fields of this feature from the attributes of
6405
 * another. The FID and the style string are not set. The poSrcFeature
6406
 * does not need to have the same OGRFeatureDefn.  Field values are
6407
 * copied according to the provided indices map. Field types do not
6408
 * have to exactly match.  SetField() method conversion rules will be
6409
 * applied as needed. This is more efficient than OGR_F_SetFrom() in
6410
 * that this doesn't lookup the fields by their names.  Particularly
6411
 * useful when the field names don't match.
6412
 *
6413
 * @param poSrcFeature the feature from which geometry, and field values will
6414
 * be copied.
6415
 *
6416
 * @param panMap Array of the indices of the feature's fields
6417
 * stored at the corresponding index of the source feature's fields. A value of
6418
 * -1 should be used to ignore the source's field. The array should not be NULL
6419
 * and be as long as the number of fields in the source feature.
6420
 *
6421
 * @param bForgiving TRUE if the operation should continue despite lacking
6422
 * output fields matching some of the source fields.
6423
 *
6424
 * @param bUseISO8601ForDateTimeAsString true if datetime fields
6425
 * converted to string should use ISO8601 formatting rather than OGR own format.
6426
 *
6427
 * @return OGRERR_NONE if the operation succeeds, even if some values are
6428
 * not transferred, otherwise an error code.
6429
 */
6430
6431
OGRErr OGRFeature::SetFieldsFrom(const OGRFeature *poSrcFeature,
6432
                                 const int *panMap, int bForgiving,
6433
                                 bool bUseISO8601ForDateTimeAsString)
6434
6435
0
{
6436
0
    const int nSrcFieldCount = poSrcFeature->poDefn->GetFieldCountUnsafe();
6437
0
    const int nFieldCount = poDefn->GetFieldCountUnsafe();
6438
0
    for (int iField = 0; iField < nSrcFieldCount; iField++)
6439
0
    {
6440
0
        const int iDstField = panMap[iField];
6441
6442
0
        if (iDstField < 0)
6443
0
            continue;
6444
6445
0
        if (nFieldCount <= iDstField)
6446
0
            return OGRERR_FAILURE;
6447
6448
0
        if (!poSrcFeature->IsFieldSetUnsafe(iField))
6449
0
        {
6450
0
            UnsetField(iDstField);
6451
0
            continue;
6452
0
        }
6453
6454
0
        if (poSrcFeature->IsFieldNullUnsafe(iField))
6455
0
        {
6456
0
            SetFieldNull(iDstField);
6457
0
            continue;
6458
0
        }
6459
6460
0
        const auto eSrcType =
6461
0
            poSrcFeature->poDefn->GetFieldDefnUnsafe(iField)->GetType();
6462
0
        const auto eDstType = poDefn->GetFieldDefnUnsafe(iDstField)->GetType();
6463
0
        if (eSrcType == eDstType)
6464
0
        {
6465
0
            if (eSrcType == OFTInteger)
6466
0
            {
6467
0
                SetFieldSameTypeUnsafe(
6468
0
                    iDstField, poSrcFeature->GetFieldAsIntegerUnsafe(iField));
6469
0
                continue;
6470
0
            }
6471
0
            if (eSrcType == OFTInteger64)
6472
0
            {
6473
0
                SetFieldSameTypeUnsafe(
6474
0
                    iDstField, poSrcFeature->GetFieldAsInteger64Unsafe(iField));
6475
0
                continue;
6476
0
            }
6477
0
            if (eSrcType == OFTReal)
6478
0
            {
6479
0
                SetFieldSameTypeUnsafe(
6480
0
                    iDstField, poSrcFeature->GetFieldAsDoubleUnsafe(iField));
6481
0
                continue;
6482
0
            }
6483
0
            if (eSrcType == OFTString)
6484
0
            {
6485
0
                if (IsFieldSetAndNotNullUnsafe(iDstField))
6486
0
                    CPLFree(pauFields[iDstField].String);
6487
6488
0
                SetFieldSameTypeUnsafe(
6489
0
                    iDstField,
6490
0
                    VSI_STRDUP_VERBOSE(
6491
0
                        poSrcFeature->GetFieldAsStringUnsafe(iField)));
6492
0
                continue;
6493
0
            }
6494
0
        }
6495
6496
        // Check if we must convert list types to JSON
6497
0
        if (eDstType == OFTString)
6498
0
        {
6499
0
            const auto eDstSubType =
6500
0
                poDefn->GetFieldDefnUnsafe(iDstField)->GetSubType();
6501
0
            if (eDstSubType == OFSTJSON &&
6502
0
                (eSrcType == OFTIntegerList || eSrcType == OFTInteger64List ||
6503
0
                 eSrcType == OFTRealList || eSrcType == OFTStringList))
6504
0
            {
6505
0
                char *pszVal = poSrcFeature->GetFieldAsSerializedJSon(iField);
6506
0
                if (pszVal)
6507
0
                {
6508
0
                    SetField(iDstField, pszVal);
6509
0
                    CPLFree(pszVal);
6510
0
                    continue;
6511
0
                }
6512
0
            }
6513
0
        }
6514
6515
0
        switch (eSrcType)
6516
0
        {
6517
0
            case OFTInteger:
6518
0
                SetField(iDstField,
6519
0
                         poSrcFeature->GetFieldAsIntegerUnsafe(iField));
6520
0
                break;
6521
6522
0
            case OFTInteger64:
6523
0
                SetField(iDstField,
6524
0
                         poSrcFeature->GetFieldAsInteger64Unsafe(iField));
6525
0
                break;
6526
6527
0
            case OFTReal:
6528
0
                SetField(iDstField,
6529
0
                         poSrcFeature->GetFieldAsDoubleUnsafe(iField));
6530
0
                break;
6531
6532
0
            case OFTString:
6533
0
                SetField(iDstField,
6534
0
                         poSrcFeature->GetFieldAsStringUnsafe(iField));
6535
0
                break;
6536
6537
0
            case OFTIntegerList:
6538
0
            {
6539
0
                if (eDstType == OFTString)
6540
0
                {
6541
0
                    SetField(iDstField, poSrcFeature->GetFieldAsString(iField));
6542
0
                }
6543
0
                else
6544
0
                {
6545
0
                    int nCount = 0;
6546
0
                    const int *panValues =
6547
0
                        poSrcFeature->GetFieldAsIntegerList(iField, &nCount);
6548
0
                    SetField(iDstField, nCount, panValues);
6549
0
                }
6550
0
            }
6551
0
            break;
6552
6553
0
            case OFTInteger64List:
6554
0
            {
6555
0
                if (eDstType == OFTString)
6556
0
                {
6557
0
                    SetField(iDstField, poSrcFeature->GetFieldAsString(iField));
6558
0
                }
6559
0
                else
6560
0
                {
6561
0
                    int nCount = 0;
6562
0
                    const GIntBig *panValues =
6563
0
                        poSrcFeature->GetFieldAsInteger64List(iField, &nCount);
6564
0
                    SetField(iDstField, nCount, panValues);
6565
0
                }
6566
0
            }
6567
0
            break;
6568
6569
0
            case OFTRealList:
6570
0
            {
6571
0
                if (eDstType == OFTString)
6572
0
                {
6573
0
                    SetField(iDstField, poSrcFeature->GetFieldAsString(iField));
6574
0
                }
6575
0
                else
6576
0
                {
6577
0
                    int nCount = 0;
6578
0
                    const double *padfValues =
6579
0
                        poSrcFeature->GetFieldAsDoubleList(iField, &nCount);
6580
0
                    SetField(iDstField, nCount, padfValues);
6581
0
                }
6582
0
            }
6583
0
            break;
6584
6585
0
            case OFTDate:
6586
0
            case OFTDateTime:
6587
0
            case OFTTime:
6588
0
            {
6589
0
                if (eDstType == OFTDate || eDstType == OFTTime ||
6590
0
                    eDstType == OFTDateTime)
6591
0
                {
6592
0
                    SetField(iDstField, poSrcFeature->GetRawFieldRef(iField));
6593
0
                }
6594
0
                else if (eDstType == OFTString || eDstType == OFTStringList)
6595
0
                {
6596
0
                    SetField(iDstField,
6597
0
                             eSrcType == OFTDateTime &&
6598
0
                                     bUseISO8601ForDateTimeAsString
6599
0
                                 ? poSrcFeature->GetFieldAsISO8601DateTime(
6600
0
                                       iField, nullptr)
6601
0
                                 : poSrcFeature->GetFieldAsString(iField));
6602
0
                }
6603
0
                else if (!bForgiving)
6604
0
                    return OGRERR_FAILURE;
6605
0
                break;
6606
0
            }
6607
6608
0
            default:
6609
0
            {
6610
0
                if (eSrcType == eDstType)
6611
0
                {
6612
0
                    SetField(iDstField, poSrcFeature->GetRawFieldRef(iField));
6613
0
                }
6614
0
                else if (eDstType == OFTString || eDstType == OFTStringList)
6615
0
                {
6616
0
                    SetField(iDstField, poSrcFeature->GetFieldAsString(iField));
6617
0
                }
6618
0
                else if (!bForgiving)
6619
0
                    return OGRERR_FAILURE;
6620
0
                break;
6621
0
            }
6622
0
        }
6623
0
    }
6624
6625
0
    return OGRERR_NONE;
6626
0
}
6627
6628
/************************************************************************/
6629
/*                           GetStyleString()                           */
6630
/************************************************************************/
6631
6632
/**
6633
 * \brief Fetch style string for this feature.
6634
 *
6635
 * Set the OGR Feature Style Specification for details on the format of
6636
 * this string, and ogr_featurestyle.h for services available to parse it.
6637
 *
6638
 * This method is the same as the C function OGR_F_GetStyleString().
6639
 *
6640
 * @return a reference to a representation in string format, or NULL if
6641
 * there isn't one.
6642
 */
6643
6644
const char *OGRFeature::GetStyleString() const
6645
0
{
6646
0
    if (m_pszStyleString)
6647
0
        return m_pszStyleString;
6648
6649
0
    const int iStyleFieldIndex = GetFieldIndex("OGR_STYLE");
6650
0
    if (iStyleFieldIndex >= 0)
6651
0
        return GetFieldAsString(iStyleFieldIndex);
6652
6653
0
    return nullptr;
6654
0
}
6655
6656
/************************************************************************/
6657
/*                        OGR_F_GetStyleString()                        */
6658
/************************************************************************/
6659
6660
/**
6661
 * \brief Fetch style string for this feature.
6662
 *
6663
 * Set the OGR Feature Style Specification for details on the format of
6664
 * this string, and ogr_featurestyle.h for services available to parse it.
6665
 *
6666
 * This function is the same as the C++ method OGRFeature::GetStyleString().
6667
 *
6668
 * @param hFeat handle to the feature to get the style from.
6669
 * @return a reference to a representation in string format, or NULL if
6670
 * there isn't one.
6671
 */
6672
6673
const char *OGR_F_GetStyleString(OGRFeatureH hFeat)
6674
0
{
6675
0
    VALIDATE_POINTER1(hFeat, "OGR_F_GetStyleString", nullptr);
6676
6677
0
    return OGRFeature::FromHandle(hFeat)->GetStyleString();
6678
0
}
6679
6680
/************************************************************************/
6681
/*                           SetStyleString()                           */
6682
/************************************************************************/
6683
6684
/**
6685
 * \brief Set feature style string.
6686
 *
6687
 * This method operates the same as OGRFeature::SetStyleStringDirectly() except
6688
 * that it does not assume ownership of the passed string, but instead makes a
6689
 * copy of it.
6690
 *
6691
 * This method is the same as the C function OGR_F_SetStyleString().
6692
 *
6693
 * @param pszString the style string to apply to this feature, cannot be NULL.
6694
 */
6695
6696
void OGRFeature::SetStyleString(const char *pszString)
6697
0
{
6698
0
    if (m_pszStyleString)
6699
0
    {
6700
0
        CPLFree(m_pszStyleString);
6701
0
        m_pszStyleString = nullptr;
6702
0
    }
6703
6704
0
    if (pszString)
6705
0
        m_pszStyleString = VSI_STRDUP_VERBOSE(pszString);
6706
0
}
6707
6708
/************************************************************************/
6709
/*                        OGR_F_SetStyleString()                        */
6710
/************************************************************************/
6711
6712
/**
6713
 * \brief Set feature style string.
6714
 *
6715
 * This method operates the same as OGR_F_SetStyleStringDirectly() except that it
6716
 * does not assume ownership of the passed string, but instead makes a copy of
6717
 * it.
6718
 *
6719
 * This function is the same as the C++ method OGRFeature::SetStyleString().
6720
 *
6721
 * @param hFeat handle to the feature to set style to.
6722
 * @param pszStyle the style string to apply to this feature, cannot be NULL.
6723
 */
6724
6725
void OGR_F_SetStyleString(OGRFeatureH hFeat, const char *pszStyle)
6726
6727
0
{
6728
0
    VALIDATE_POINTER0(hFeat, "OGR_F_SetStyleString");
6729
6730
0
    OGRFeature::FromHandle(hFeat)->SetStyleString(pszStyle);
6731
0
}
6732
6733
/************************************************************************/
6734
/*                       SetStyleStringDirectly()                       */
6735
/************************************************************************/
6736
6737
/**
6738
 * \brief Set feature style string.
6739
 *
6740
 * This method operates the same as OGRFeature::SetStyleString() except that it
6741
 * assumes ownership of the passed string.
6742
 *
6743
 * This method is the same as the C function OGR_F_SetStyleStringDirectly().
6744
 *
6745
 * @param pszString the style string to apply to this feature, cannot be NULL.
6746
 */
6747
6748
void OGRFeature::SetStyleStringDirectly(char *pszString)
6749
0
{
6750
0
    CPLFree(m_pszStyleString);
6751
0
    m_pszStyleString = pszString;
6752
0
}
6753
6754
/************************************************************************/
6755
/*                    OGR_F_SetStyleStringDirectly()                    */
6756
/************************************************************************/
6757
6758
/**
6759
 * \brief Set feature style string.
6760
 *
6761
 * This method operates the same as OGR_F_SetStyleString() except that it assumes
6762
 * ownership of the passed string.
6763
 *
6764
 * This function is the same as the C++ method
6765
 * OGRFeature::SetStyleStringDirectly().
6766
 *
6767
 * @param hFeat handle to the feature to set style to.
6768
 * @param pszStyle the style string to apply to this feature, cannot be NULL.
6769
 */
6770
6771
void OGR_F_SetStyleStringDirectly(OGRFeatureH hFeat, char *pszStyle)
6772
6773
0
{
6774
0
    VALIDATE_POINTER0(hFeat, "OGR_F_SetStyleStringDirectly");
6775
6776
0
    OGRFeature::FromHandle(hFeat)->SetStyleStringDirectly(pszStyle);
6777
0
}
6778
6779
//************************************************************************/
6780
/*                           SetStyleTable()                            */
6781
/************************************************************************/
6782
6783
/** Set style table.
6784
 * @param poStyleTable new style table (will be cloned)
6785
 */
6786
void OGRFeature::SetStyleTable(OGRStyleTable *poStyleTable)
6787
0
{
6788
0
    if (m_poStyleTable)
6789
0
        delete m_poStyleTable;
6790
0
    m_poStyleTable = poStyleTable ? poStyleTable->Clone() : nullptr;
6791
0
}
6792
6793
//************************************************************************/
6794
/*                       SetStyleTableDirectly()                        */
6795
/************************************************************************/
6796
6797
/** Set style table.
6798
 * @param poStyleTable new style table (ownership transferred to the object)
6799
 */
6800
void OGRFeature::SetStyleTableDirectly(OGRStyleTable *poStyleTable)
6801
0
{
6802
0
    if (m_poStyleTable)
6803
0
        delete m_poStyleTable;
6804
0
    m_poStyleTable = poStyleTable;
6805
0
}
6806
6807
//! @cond Doxygen_Suppress
6808
6809
/************************************************************************/
6810
/*                            RemapFields()                             */
6811
/*                                                                      */
6812
/*      This is used to transform a feature "in place" from one         */
6813
/*      feature defn to another with minimum work.                      */
6814
/************************************************************************/
6815
6816
OGRErr OGRFeature::RemapFields(const OGRFeatureDefn *poNewDefn,
6817
                               const int *panRemapSource)
6818
6819
0
{
6820
0
    if (poNewDefn == nullptr)
6821
0
        poNewDefn = poDefn;
6822
6823
0
    const int nNewFieldCount = poNewDefn->GetFieldCount();
6824
0
    OGRField *pauNewFields =
6825
0
        static_cast<OGRField *>(CPLCalloc(nNewFieldCount, sizeof(OGRField)));
6826
6827
0
    const int nFieldCount = poDefn->GetFieldCount();
6828
0
    for (int iDstField = 0; iDstField < nFieldCount; iDstField++)
6829
0
    {
6830
0
        if (panRemapSource[iDstField] == -1)
6831
0
        {
6832
0
            OGR_RawField_SetUnset(&pauNewFields[iDstField]);
6833
0
        }
6834
0
        else
6835
0
        {
6836
0
            memcpy(pauNewFields + iDstField,
6837
0
                   pauFields + panRemapSource[iDstField], sizeof(OGRField));
6838
0
        }
6839
0
    }
6840
6841
    // We really should be freeing memory for old columns that
6842
    // are no longer present.  We don't for now because it is a bit messy
6843
    // and would take too long to test.
6844
6845
    /* -------------------------------------------------------------------- */
6846
    /*      Apply new definition and fields.                                */
6847
    /* -------------------------------------------------------------------- */
6848
0
    CPLFree(pauFields);
6849
0
    pauFields = pauNewFields;
6850
6851
0
    poDefn = poNewDefn;
6852
6853
0
    return OGRERR_NONE;
6854
0
}
6855
6856
/************************************************************************/
6857
/*                            AppendField()                             */
6858
/*                                                                      */
6859
/*      This is used to transform a feature "in place" by appending     */
6860
/*      an unset field.                                                 */
6861
/************************************************************************/
6862
6863
void OGRFeature::AppendField()
6864
0
{
6865
0
    const int nFieldCount = poDefn->GetFieldCountUnsafe();
6866
0
    pauFields = static_cast<OGRField *>(
6867
0
        CPLRealloc(pauFields, nFieldCount * sizeof(OGRField)));
6868
0
    OGR_RawField_SetUnset(&pauFields[nFieldCount - 1]);
6869
0
}
6870
6871
/************************************************************************/
6872
/*                        RemapGeomFields()                             */
6873
/*                                                                      */
6874
/*      This is used to transform a feature "in place" from one         */
6875
/*      feature defn to another with minimum work.                      */
6876
/************************************************************************/
6877
6878
OGRErr OGRFeature::RemapGeomFields(const OGRFeatureDefn *poNewDefn,
6879
                                   const int *panRemapSource)
6880
6881
0
{
6882
0
    if (poNewDefn == nullptr)
6883
0
        poNewDefn = poDefn;
6884
6885
0
    OGRGeometry **papoNewGeomFields = static_cast<OGRGeometry **>(
6886
0
        CPLCalloc(poNewDefn->GetGeomFieldCount(), sizeof(OGRGeometry *)));
6887
6888
0
    for (int iDstField = 0; iDstField < poDefn->GetGeomFieldCount();
6889
0
         iDstField++)
6890
0
    {
6891
0
        if (panRemapSource[iDstField] == -1)
6892
0
        {
6893
0
            papoNewGeomFields[iDstField] = nullptr;
6894
0
        }
6895
0
        else
6896
0
        {
6897
0
            papoNewGeomFields[iDstField] =
6898
0
                papoGeometries[panRemapSource[iDstField]];
6899
0
        }
6900
0
    }
6901
6902
    // We really should be freeing memory for old columns that
6903
    // are no longer present.  We don't for now because it is a bit messy
6904
    // and would take too long to test.
6905
6906
    /* -------------------------------------------------------------------- */
6907
    /*      Apply new definition and fields.                                */
6908
    /* -------------------------------------------------------------------- */
6909
0
    CPLFree(papoGeometries);
6910
0
    papoGeometries = papoNewGeomFields;
6911
6912
0
    poDefn = poNewDefn;
6913
6914
0
    return OGRERR_NONE;
6915
0
}
6916
6917
//! @endcond
6918
6919
/************************************************************************/
6920
/*                        OGR_F_GetStyleTable()                         */
6921
/************************************************************************/
6922
6923
OGRStyleTableH OGR_F_GetStyleTable(OGRFeatureH hFeat)
6924
6925
0
{
6926
0
    VALIDATE_POINTER1(hFeat, "OGR_F_GetStyleTable", nullptr);
6927
6928
0
    return reinterpret_cast<OGRStyleTableH>(
6929
0
        OGRFeature::FromHandle(hFeat)->GetStyleTable());
6930
0
}
6931
6932
/************************************************************************/
6933
/*                    OGR_F_SetStyleTableDirectly()                     */
6934
/************************************************************************/
6935
6936
void OGR_F_SetStyleTableDirectly(OGRFeatureH hFeat, OGRStyleTableH hStyleTable)
6937
6938
0
{
6939
0
    VALIDATE_POINTER0(hFeat, "OGR_F_SetStyleTableDirectly");
6940
6941
0
    OGRFeature::FromHandle(hFeat)->SetStyleTableDirectly(
6942
0
        reinterpret_cast<OGRStyleTable *>(hStyleTable));
6943
0
}
6944
6945
/************************************************************************/
6946
/*                        OGR_F_SetStyleTable()                         */
6947
/************************************************************************/
6948
6949
void OGR_F_SetStyleTable(OGRFeatureH hFeat, OGRStyleTableH hStyleTable)
6950
6951
0
{
6952
0
    VALIDATE_POINTER0(hFeat, "OGR_F_SetStyleTable");
6953
0
    VALIDATE_POINTER0(hStyleTable, "OGR_F_SetStyleTable");
6954
6955
0
    OGRFeature::FromHandle(hFeat)->SetStyleTable(
6956
0
        reinterpret_cast<OGRStyleTable *>(hStyleTable));
6957
0
}
6958
6959
/************************************************************************/
6960
/*                        FillUnsetWithDefault()                        */
6961
/************************************************************************/
6962
6963
/**
6964
 * \brief Fill unset fields with default values that might be defined.
6965
 *
6966
 * This method is the same as the C function OGR_F_FillUnsetWithDefault().
6967
 *
6968
 * @param bNotNullableOnly if we should fill only unset fields with a not-null
6969
 *                     constraint.
6970
 * @param papszOptions unused currently. Must be set to NULL.
6971
 */
6972
6973
void OGRFeature::FillUnsetWithDefault(int bNotNullableOnly,
6974
                                      CPL_UNUSED CSLConstList papszOptions)
6975
0
{
6976
0
    const int nFieldCount = poDefn->GetFieldCount();
6977
0
    for (int i = 0; i < nFieldCount; i++)
6978
0
    {
6979
0
        if (IsFieldSetUnsafe(i))
6980
0
            continue;
6981
0
        const auto poFieldDefn = poDefn->GetFieldDefn(i);
6982
0
        if (bNotNullableOnly && poFieldDefn->IsNullable())
6983
0
            continue;
6984
0
        const char *pszDefault = poFieldDefn->GetDefault();
6985
0
        OGRFieldType eType = poFieldDefn->GetType();
6986
0
        if (pszDefault != nullptr)
6987
0
        {
6988
0
            if (eType == OFTDate || eType == OFTTime || eType == OFTDateTime)
6989
0
            {
6990
0
                if (STARTS_WITH_CI(pszDefault, "CURRENT"))
6991
0
                {
6992
0
                    time_t t = time(nullptr);
6993
0
                    struct tm brokendown;
6994
0
                    CPLUnixTimeToYMDHMS(t, &brokendown);
6995
0
                    SetField(i, brokendown.tm_year + 1900,
6996
0
                             brokendown.tm_mon + 1, brokendown.tm_mday,
6997
0
                             brokendown.tm_hour, brokendown.tm_min,
6998
0
                             static_cast<float>(brokendown.tm_sec), 100);
6999
0
                }
7000
0
                else
7001
0
                {
7002
0
                    int nYear = 0;
7003
0
                    int nMonth = 0;
7004
0
                    int nDay = 0;
7005
0
                    int nHour = 0;
7006
0
                    int nMinute = 0;
7007
0
                    float fSecond = 0.0f;
7008
0
                    if (sscanf(pszDefault, "'%d/%d/%d %d:%d:%f'", &nYear,
7009
0
                               &nMonth, &nDay, &nHour, &nMinute, &fSecond) == 6)
7010
0
                    {
7011
0
                        SetField(i, nYear, nMonth, nDay, nHour, nMinute,
7012
0
                                 fSecond, 100);
7013
0
                    }
7014
0
                }
7015
0
            }
7016
0
            else if (eType == OFTString && pszDefault[0] == '\'' &&
7017
0
                     pszDefault[strlen(pszDefault) - 1] == '\'')
7018
0
            {
7019
0
                CPLString osDefault(pszDefault + 1);
7020
0
                osDefault.pop_back();
7021
0
                char *pszTmp = CPLUnescapeString(osDefault, nullptr, CPLES_SQL);
7022
0
                SetField(i, pszTmp);
7023
0
                CPLFree(pszTmp);
7024
0
            }
7025
0
            else if (!poFieldDefn->IsDefaultDriverSpecific())
7026
0
                SetField(i, pszDefault);
7027
0
        }
7028
0
    }
7029
0
}
7030
7031
/************************************************************************/
7032
/*                     OGR_F_FillUnsetWithDefault()                     */
7033
/************************************************************************/
7034
7035
/**
7036
 * \brief Fill unset fields with default values that might be defined.
7037
 *
7038
 * This function is the same as the C++ method
7039
 * OGRFeature::FillUnsetWithDefault().
7040
 *
7041
 * @param hFeat handle to the feature.
7042
 * @param bNotNullableOnly if we should fill only unset fields with a not-null
7043
 *                     constraint.
7044
 * @param papszOptions unused currently. Must be set to NULL.
7045
 */
7046
7047
void OGR_F_FillUnsetWithDefault(OGRFeatureH hFeat, int bNotNullableOnly,
7048
                                CSLConstList papszOptions)
7049
7050
0
{
7051
0
    VALIDATE_POINTER0(hFeat, "OGR_F_FillUnsetWithDefault");
7052
7053
0
    OGRFeature::FromHandle(hFeat)->FillUnsetWithDefault(bNotNullableOnly,
7054
0
                                                        papszOptions);
7055
0
}
7056
7057
/************************************************************************/
7058
/*                              Validate()                              */
7059
/************************************************************************/
7060
7061
/**
7062
 * \brief Validate that a feature meets constraints of its schema.
7063
 *
7064
 * The scope of test is specified with the nValidateFlags parameter.
7065
 *
7066
 * Regarding OGR_F_VAL_WIDTH, the test is done assuming the string width must be
7067
 * interpreted as the number of UTF-8 characters. Some drivers might interpret
7068
 * the width as the number of bytes instead. So this test is rather conservative
7069
 * (if it fails, then it will fail for all interpretations).
7070
 *
7071
 * This method is the same as the C function OGR_F_Validate().
7072
 *
7073
 * @param nValidateFlags OGR_F_VAL_ALL or combination of OGR_F_VAL_NULL,
7074
 *                       OGR_F_VAL_GEOM_TYPE, OGR_F_VAL_WIDTH and
7075
 *                       OGR_F_VAL_ALLOW_NULL_WHEN_DEFAULT,
7076
 *                       OGR_F_VAL_ALLOW_DIFFERENT_GEOM_DIM with '|' operator
7077
 * @param bEmitError TRUE if a CPLError() must be emitted when a check fails
7078
 * @return TRUE if all enabled validation tests pass.
7079
 */
7080
7081
int OGRFeature::Validate(int nValidateFlags, int bEmitError) const
7082
7083
0
{
7084
0
    bool bRet = true;
7085
7086
0
    const int nGeomFieldCount = poDefn->GetGeomFieldCount();
7087
0
    for (int i = 0; i < nGeomFieldCount; i++)
7088
0
    {
7089
0
        if ((nValidateFlags & OGR_F_VAL_NULL) &&
7090
0
            !poDefn->GetGeomFieldDefn(i)->IsNullable() &&
7091
0
            GetGeomFieldRef(i) == nullptr)
7092
0
        {
7093
0
            bRet = false;
7094
0
            if (bEmitError)
7095
0
            {
7096
0
                CPLError(
7097
0
                    CE_Failure, CPLE_AppDefined,
7098
0
                    "Geometry field %s has a NULL content which is not allowed",
7099
0
                    poDefn->GetGeomFieldDefn(i)->GetNameRef());
7100
0
            }
7101
0
        }
7102
0
        if ((nValidateFlags & OGR_F_VAL_GEOM_TYPE) &&
7103
0
            poDefn->GetGeomFieldDefn(i)->GetType() != wkbUnknown)
7104
0
        {
7105
0
            const OGRGeometry *poGeom = GetGeomFieldRef(i);
7106
0
            if (poGeom != nullptr)
7107
0
            {
7108
0
                const OGRwkbGeometryType eType =
7109
0
                    poDefn->GetGeomFieldDefn(i)->GetType();
7110
0
                const OGRwkbGeometryType eFType = poGeom->getGeometryType();
7111
0
                if ((nValidateFlags & OGR_F_VAL_ALLOW_DIFFERENT_GEOM_DIM) &&
7112
0
                    (wkbFlatten(eFType) == wkbFlatten(eType) ||
7113
0
                     wkbFlatten(eType) == wkbUnknown))
7114
0
                {
7115
                    // Ok.
7116
0
                }
7117
0
                else if ((eType == wkbSetZ(wkbUnknown) && !wkbHasZ(eFType)) ||
7118
0
                         (eType != wkbSetZ(wkbUnknown) && eFType != eType))
7119
0
                {
7120
0
                    bRet = false;
7121
0
                    if (bEmitError)
7122
0
                    {
7123
0
                        CPLError(CE_Failure, CPLE_AppDefined,
7124
0
                                 "Geometry field %s has a %s geometry whereas "
7125
0
                                 "%s is expected",
7126
0
                                 poDefn->GetGeomFieldDefn(i)->GetNameRef(),
7127
0
                                 OGRGeometryTypeToName(eFType),
7128
0
                                 OGRGeometryTypeToName(eType));
7129
0
                    }
7130
0
                }
7131
0
            }
7132
0
        }
7133
0
    }
7134
0
    const int nFieldCount = poDefn->GetFieldCount();
7135
0
    for (int i = 0; i < nFieldCount; i++)
7136
0
    {
7137
0
        if ((nValidateFlags & OGR_F_VAL_NULL) &&
7138
0
            !poDefn->GetFieldDefn(i)->IsNullable() && !IsFieldSet(i) &&
7139
0
            (!(nValidateFlags & OGR_F_VAL_ALLOW_NULL_WHEN_DEFAULT) ||
7140
0
             poDefn->GetFieldDefn(i)->GetDefault() == nullptr))
7141
0
        {
7142
0
            bRet = false;
7143
0
            if (bEmitError)
7144
0
            {
7145
0
                CPLError(CE_Failure, CPLE_AppDefined,
7146
0
                         "Field %s.%s has a NULL content which is not allowed",
7147
0
                         poDefn->GetName(),
7148
0
                         poDefn->GetFieldDefn(i)->GetNameRef());
7149
0
            }
7150
0
        }
7151
0
        if ((nValidateFlags & OGR_F_VAL_WIDTH) &&
7152
0
            poDefn->GetFieldDefn(i)->GetWidth() > 0 &&
7153
0
            poDefn->GetFieldDefn(i)->GetType() == OFTString && IsFieldSet(i) &&
7154
0
            CPLIsUTF8(GetFieldAsString(i), -1) &&
7155
0
            CPLStrlenUTF8Ex(GetFieldAsString(i)) >
7156
0
                static_cast<size_t>(poDefn->GetFieldDefn(i)->GetWidth()))
7157
0
        {
7158
0
            bRet = false;
7159
0
            if (bEmitError)
7160
0
            {
7161
0
                CPLError(
7162
0
                    CE_Failure, CPLE_AppDefined,
7163
0
                    "Field %s.%s has %" PRIu64 " UTF-8 characters whereas "
7164
0
                    "a maximum of %d is allowed",
7165
0
                    poDefn->GetName(), poDefn->GetFieldDefn(i)->GetNameRef(),
7166
0
                    static_cast<uint64_t>(CPLStrlenUTF8Ex(GetFieldAsString(i))),
7167
0
                    poDefn->GetFieldDefn(i)->GetWidth());
7168
0
            }
7169
0
        }
7170
0
    }
7171
7172
0
    return bRet;
7173
0
}
7174
7175
/************************************************************************/
7176
/*                           OGR_F_Validate()                           */
7177
/************************************************************************/
7178
7179
/**
7180
 * \brief Validate that a feature meets constraints of its schema.
7181
 *
7182
 * The scope of test is specified with the nValidateFlags parameter.
7183
 *
7184
 * Regarding OGR_F_VAL_WIDTH, the test is done assuming the string width must be
7185
 * interpreted as the number of UTF-8 characters. Some drivers might interpret
7186
 * the width as the number of bytes instead. So this test is rather conservative
7187
 * (if it fails, then it will fail for all interpretations).
7188
 *
7189
 * This function is the same as the C++ method
7190
 * OGRFeature::Validate().
7191
 *
7192
 * @param hFeat handle to the feature to validate.
7193
 * @param nValidateFlags OGR_F_VAL_ALL or combination of OGR_F_VAL_NULL,
7194
 *                       OGR_F_VAL_GEOM_TYPE, OGR_F_VAL_WIDTH and
7195
 *                       OGR_F_VAL_ALLOW_NULL_WHEN_DEFAULT with '|' operator
7196
 * @param bEmitError TRUE if a CPLError() must be emitted when a check fails
7197
 * @return TRUE if all enabled validation tests pass.
7198
 */
7199
7200
int OGR_F_Validate(OGRFeatureH hFeat, int nValidateFlags, int bEmitError)
7201
7202
0
{
7203
0
    VALIDATE_POINTER1(hFeat, "OGR_F_Validate", FALSE);
7204
7205
0
    return OGRFeature::FromHandle(hFeat)->Validate(nValidateFlags, bEmitError);
7206
0
}
7207
7208
/************************************************************************/
7209
/*                           GetNativeData()                            */
7210
/************************************************************************/
7211
7212
/**
7213
 * \fn const char* OGRFeature::GetNativeData() const;
7214
 *
7215
 * \brief Returns the native data for the feature.
7216
 *
7217
 * The native data is the representation in a "natural" form that comes from
7218
 * the driver that created this feature, or that is aimed at an output driver.
7219
 * The native data may be in different format, which is indicated by
7220
 * GetNativeMediaType().
7221
 *
7222
 * Note that most drivers do not support storing the native data in the feature
7223
 * object, and if they do, generally the NATIVE_DATA open option must be passed
7224
 * at dataset opening.
7225
 *
7226
 * The "native data" does not imply it is something more performant or powerful
7227
 * than what can be obtained with the rest of the API, but it may be useful in
7228
 * round-tripping scenarios where some characteristics of the underlying format
7229
 * are not captured otherwise by the OGR abstraction.
7230
 *
7231
 * This function is the same as the C function
7232
 * OGR_F_GetNativeData().
7233
 *
7234
 * @return a string with the native data, or NULL if there is none.
7235
 *
7236
 * @see https://trac.osgeo.org/gdal/wiki/rfc60_improved_roundtripping_in_ogr
7237
 */
7238
7239
/************************************************************************/
7240
/*                        OGR_F_GetNativeData()                         */
7241
/************************************************************************/
7242
7243
/**
7244
 * \brief Returns the native data for the feature.
7245
 *
7246
 * The native data is the representation in a "natural" form that comes from
7247
 * the driver that created this feature, or that is aimed at an output driver.
7248
 * The native data may be in different format, which is indicated by
7249
 * OGR_F_GetNativeMediaType().
7250
 *
7251
 * Note that most drivers do not support storing the native data in the feature
7252
 * object, and if they do, generally the NATIVE_DATA open option must be passed
7253
 * at dataset opening.
7254
 *
7255
 * The "native data" does not imply it is something more performant or powerful
7256
 * than what can be obtained with the rest of the API, but it may be useful in
7257
 * round-tripping scenarios where some characteristics of the underlying format
7258
 * are not captured otherwise by the OGR abstraction.
7259
 *
7260
 * This function is the same as the C++ method
7261
 * OGRFeature::GetNativeData().
7262
 *
7263
 * @param hFeat handle to the feature.
7264
 * @return a string with the native data, or NULL if there is none.
7265
 *
7266
 * @see https://trac.osgeo.org/gdal/wiki/rfc60_improved_roundtripping_in_ogr
7267
 */
7268
7269
const char *OGR_F_GetNativeData(OGRFeatureH hFeat)
7270
7271
0
{
7272
0
    VALIDATE_POINTER1(hFeat, "OGR_F_GetNativeData", nullptr);
7273
7274
0
    return OGRFeature::FromHandle(hFeat)->GetNativeData();
7275
0
}
7276
7277
/************************************************************************/
7278
/*                         GetNativeMediaType()                         */
7279
/************************************************************************/
7280
7281
/**
7282
 * \fn const char* OGRFeature::GetNativeMediaType() const;
7283
 *
7284
 * \brief Returns the native media type for the feature.
7285
 *
7286
 * The native media type is the identifier for the format of the native data.
7287
 * It follows the IANA RFC 2045 (see https://en.wikipedia.org/wiki/Media_type),
7288
 * e.g. "application/vnd.geo+json" for JSon.
7289
 *
7290
 * This function is the same as the C function
7291
 * OGR_F_GetNativeMediaType().
7292
 *
7293
 * @return a string with the native media type, or NULL if there is none.
7294
 *
7295
 * @see https://trac.osgeo.org/gdal/wiki/rfc60_improved_roundtripping_in_ogr
7296
 */
7297
7298
/************************************************************************/
7299
/*                      OGR_F_GetNativeMediaType()                      */
7300
/************************************************************************/
7301
7302
/**
7303
 * \brief Returns the native media type for the feature.
7304
 *
7305
 * The native media type is the identifier for the format of the native data.
7306
 * It follows the IANA RFC 2045 (see https://en.wikipedia.org/wiki/Media_type),
7307
 * e.g. "application/vnd.geo+json" for JSon.
7308
 *
7309
 * This function is the same as the C function
7310
 * OGR_F_GetNativeMediaType().
7311
 *
7312
 * @param hFeat handle to the feature.
7313
 * @return a string with the native media type, or NULL if there is none.
7314
 *
7315
 * @see https://trac.osgeo.org/gdal/wiki/rfc60_improved_roundtripping_in_ogr
7316
 */
7317
7318
const char *OGR_F_GetNativeMediaType(OGRFeatureH hFeat)
7319
0
{
7320
0
    VALIDATE_POINTER1(hFeat, "OGR_F_GetNativeMediaType", nullptr);
7321
7322
0
    return OGRFeature::FromHandle(hFeat)->GetNativeMediaType();
7323
0
}
7324
7325
/************************************************************************/
7326
/*                           SetNativeData()                            */
7327
/************************************************************************/
7328
7329
/**
7330
 * \brief Sets the native data for the feature.
7331
 *
7332
 * The native data is the representation in a "natural" form that comes from
7333
 * the driver that created this feature, or that is aimed at an output driver.
7334
 * The native data may be in different format, which is indicated by
7335
 * GetNativeMediaType().
7336
 *
7337
 * This function is the same as the C function
7338
 * OGR_F_SetNativeData().
7339
 *
7340
 * @param pszNativeData a string with the native data, or NULL if there is none.
7341
 *
7342
 * @see https://trac.osgeo.org/gdal/wiki/rfc60_improved_roundtripping_in_ogr
7343
 */
7344
7345
void OGRFeature::SetNativeData(const char *pszNativeData)
7346
0
{
7347
0
    CPLFree(m_pszNativeData);
7348
0
    m_pszNativeData =
7349
0
        pszNativeData ? VSI_STRDUP_VERBOSE(pszNativeData) : nullptr;
7350
0
}
7351
7352
/************************************************************************/
7353
/*                        OGR_F_SetNativeData()                         */
7354
/************************************************************************/
7355
7356
/**
7357
 * \brief Sets the native data for the feature.
7358
 *
7359
 * The native data is the representation in a "natural" form that comes from
7360
 * the driver that created this feature, or that is aimed at an output driver.
7361
 * The native data may be in different format, which is indicated by
7362
 * OGR_F_GetNativeMediaType().
7363
 *
7364
 * This function is the same as the C++ method
7365
 * OGRFeature::SetNativeData().
7366
 *
7367
 * @param hFeat handle to the feature.
7368
 * @param pszNativeData a string with the native data, or NULL if there is none.
7369
 *
7370
 * @see https://trac.osgeo.org/gdal/wiki/rfc60_improved_roundtripping_in_ogr
7371
 */
7372
7373
void OGR_F_SetNativeData(OGRFeatureH hFeat, const char *pszNativeData)
7374
0
{
7375
0
    VALIDATE_POINTER0(hFeat, "OGR_F_SetNativeData");
7376
7377
0
    OGRFeature::FromHandle(hFeat)->SetNativeData(pszNativeData);
7378
0
}
7379
7380
/************************************************************************/
7381
/*                         SetNativeMediaType()                         */
7382
/************************************************************************/
7383
7384
/**
7385
 * \brief Sets the native media type for the feature.
7386
 *
7387
 * The native media type is the identifier for the format of the native data.
7388
 * It follows the IANA RFC 2045 (see https://en.wikipedia.org/wiki/Media_type),
7389
 * e.g. "application/vnd.geo+json" for JSon.
7390
 *
7391
 * This function is the same as the C function
7392
 * OGR_F_SetNativeMediaType().
7393
 *
7394
 * @param pszNativeMediaType a string with the native media type, or NULL if
7395
 * there is none.
7396
 *
7397
 * @see https://trac.osgeo.org/gdal/wiki/rfc60_improved_roundtripping_in_ogr
7398
 */
7399
7400
void OGRFeature::SetNativeMediaType(const char *pszNativeMediaType)
7401
0
{
7402
0
    CPLFree(m_pszNativeMediaType);
7403
0
    m_pszNativeMediaType =
7404
0
        pszNativeMediaType ? VSI_STRDUP_VERBOSE(pszNativeMediaType) : nullptr;
7405
0
}
7406
7407
/************************************************************************/
7408
/*                      OGR_F_SetNativeMediaType()                      */
7409
/************************************************************************/
7410
7411
/**
7412
 * \brief Sets the native media type for the feature.
7413
 *
7414
 * The native media type is the identifier for the format of the native data.
7415
 * It follows the IANA RFC 2045 (see https://en.wikipedia.org/wiki/Media_type),
7416
 * e.g. "application/vnd.geo+json" for JSon.
7417
 *
7418
 * This function is the same as the C++ method
7419
 * OGRFeature::SetNativeMediaType().
7420
 *
7421
 * @param hFeat handle to the feature.
7422
 * @param pszNativeMediaType a string with the native media type, or NULL if
7423
 * there is none.
7424
 *
7425
 * @see https://trac.osgeo.org/gdal/wiki/rfc60_improved_roundtripping_in_ogr
7426
 */
7427
7428
void OGR_F_SetNativeMediaType(OGRFeatureH hFeat, const char *pszNativeMediaType)
7429
0
{
7430
0
    VALIDATE_POINTER0(hFeat, "OGR_F_SetNativeMediaType");
7431
7432
0
    OGRFeature::FromHandle(hFeat)->SetNativeMediaType(pszNativeMediaType);
7433
0
}
7434
7435
/************************************************************************/
7436
/*                        OGR_RawField_IsUnset()                        */
7437
/************************************************************************/
7438
7439
/**
7440
 * \brief Returns whether a raw field is unset.
7441
 *
7442
 * Note: this function is rather low-level and should be rarely used in client
7443
 * code. Use instead OGR_F_IsFieldSet().
7444
 *
7445
 * @param puField pointer to raw field.
7446
 */
7447
7448
int OGR_RawField_IsUnset(const OGRField *puField)
7449
0
{
7450
0
    return puField->Set.nMarker1 == OGRUnsetMarker &&
7451
0
           puField->Set.nMarker2 == OGRUnsetMarker &&
7452
0
           puField->Set.nMarker3 == OGRUnsetMarker;
7453
0
}
7454
7455
/************************************************************************/
7456
/*                        OGR_RawField_IsNull()                         */
7457
/************************************************************************/
7458
7459
/**
7460
 * \brief Returns whether a raw field is null.
7461
 *
7462
 * Note: this function is rather low-level and should be rarely used in client
7463
 * code. Use instead OGR_F_IsFieldNull().
7464
 *
7465
 * @param puField pointer to raw field.
7466
 */
7467
7468
int OGR_RawField_IsNull(const OGRField *puField)
7469
0
{
7470
0
    return puField->Set.nMarker1 == OGRNullMarker &&
7471
0
           puField->Set.nMarker2 == OGRNullMarker &&
7472
0
           puField->Set.nMarker3 == OGRNullMarker;
7473
0
}
7474
7475
/************************************************************************/
7476
/*                       OGR_RawField_SetUnset()                        */
7477
/************************************************************************/
7478
7479
/**
7480
 * \brief Mark a raw field as unset.
7481
 *
7482
 * This should be called on a un-initialized field. In particular this will not
7483
 * free any memory dynamically allocated.
7484
 *
7485
 * Note: this function is rather low-level and should be rarely used in client
7486
 * code. Use instead OGR_F_UnsetField().
7487
 *
7488
 * @param puField pointer to raw field.
7489
 */
7490
7491
void OGR_RawField_SetUnset(OGRField *puField)
7492
0
{
7493
0
    puField->Set.nMarker1 = OGRUnsetMarker;
7494
0
    puField->Set.nMarker2 = OGRUnsetMarker;
7495
0
    puField->Set.nMarker3 = OGRUnsetMarker;
7496
0
}
7497
7498
/************************************************************************/
7499
/*                        OGR_RawField_SetNull()                        */
7500
/************************************************************************/
7501
7502
/**
7503
 * \brief Mark a raw field as null.
7504
 *
7505
 * This should be called on a un-initialized field. In particular this will not
7506
 * free any memory dynamically allocated.
7507
 *
7508
 * Note: this function is rather low-level and should be rarely used in client
7509
 * code. Use instead OGR_F_SetFieldNull().
7510
 *
7511
 * @param puField pointer to raw field.
7512
 */
7513
7514
void OGR_RawField_SetNull(OGRField *puField)
7515
0
{
7516
0
    puField->Set.nMarker1 = OGRNullMarker;
7517
0
    puField->Set.nMarker2 = OGRNullMarker;
7518
0
    puField->Set.nMarker3 = OGRNullMarker;
7519
0
}
7520
7521
/************************************************************************/
7522
/*                      OGRFeatureUniquePtrDeleter                      */
7523
/************************************************************************/
7524
7525
//! @cond Doxygen_Suppress
7526
void OGRFeatureUniquePtrDeleter::operator()(OGRFeature *poFeature) const
7527
0
{
7528
0
    delete poFeature;
7529
0
}
7530
7531
//! @endcond
7532
7533
namespace
7534
{
7535
// Implementation borrowed to OpenFileGDB
7536
7537
/************************************************************************/
7538
/*                             WriteUInt8()                             */
7539
/************************************************************************/
7540
7541
inline void WriteUInt8(std::vector<GByte> &abyBuffer, uint8_t nVal)
7542
0
{
7543
0
    abyBuffer.push_back(nVal);
7544
0
}
7545
7546
/************************************************************************/
7547
/*                            WriteVarUInt()                            */
7548
/************************************************************************/
7549
7550
inline void WriteVarUInt(std::vector<GByte> &abyBuffer, uint64_t nVal)
7551
0
{
7552
0
    while (true)
7553
0
    {
7554
0
        if (nVal >= 0x80)
7555
0
        {
7556
0
            WriteUInt8(abyBuffer, static_cast<uint8_t>(0x80 | (nVal & 0x7F)));
7557
0
            nVal >>= 7;
7558
0
        }
7559
0
        else
7560
0
        {
7561
0
            WriteUInt8(abyBuffer, static_cast<uint8_t>(nVal));
7562
0
            break;
7563
0
        }
7564
0
    }
7565
0
}
7566
7567
/************************************************************************/
7568
/*                            WriteVarInt()                             */
7569
/************************************************************************/
7570
7571
inline void WriteVarInt(std::vector<GByte> &abyBuffer, int64_t nVal)
7572
0
{
7573
0
    uint64_t nUVal;
7574
0
    if (nVal < 0)
7575
0
    {
7576
0
        if (nVal == std::numeric_limits<int64_t>::min())
7577
0
            nUVal = static_cast<uint64_t>(1) << 63;
7578
0
        else
7579
0
            nUVal = -nVal;
7580
0
        if (nUVal >= 0x40)
7581
0
        {
7582
0
            WriteUInt8(abyBuffer,
7583
0
                       static_cast<uint8_t>(0x80 | 0x40 | (nUVal & 0x3F)));
7584
0
            nUVal >>= 6;
7585
0
        }
7586
0
        else
7587
0
        {
7588
0
            WriteUInt8(abyBuffer, static_cast<uint8_t>(0x40 | (nUVal & 0x3F)));
7589
0
            return;
7590
0
        }
7591
0
    }
7592
0
    else
7593
0
    {
7594
0
        nUVal = nVal;
7595
0
        if (nUVal >= 0x40)
7596
0
        {
7597
0
            WriteUInt8(abyBuffer, static_cast<uint8_t>(0x80 | (nUVal & 0x3F)));
7598
0
            nUVal >>= 6;
7599
0
        }
7600
0
        else
7601
0
        {
7602
0
            WriteUInt8(abyBuffer, static_cast<uint8_t>((nUVal & 0x3F)));
7603
0
            return;
7604
0
        }
7605
0
    }
7606
7607
0
    WriteVarUInt(abyBuffer, nUVal);
7608
0
}
7609
7610
/************************************************************************/
7611
/*                            WriteFloat32()                            */
7612
/************************************************************************/
7613
7614
inline void WriteFloat32(std::vector<GByte> &abyBuffer, float fVal)
7615
0
{
7616
0
    CPL_LSBPTR32(&fVal);
7617
0
    const GByte *pabyInput = reinterpret_cast<const GByte *>(&fVal);
7618
0
    abyBuffer.insert(abyBuffer.end(), pabyInput, pabyInput + sizeof(fVal));
7619
0
}
7620
7621
/************************************************************************/
7622
/*                            WriteFloat64()                            */
7623
/************************************************************************/
7624
7625
inline void WriteFloat64(std::vector<GByte> &abyBuffer, double dfVal)
7626
0
{
7627
0
    CPL_LSBPTR64(&dfVal);
7628
0
    const GByte *pabyInput = reinterpret_cast<const GByte *>(&dfVal);
7629
0
    abyBuffer.insert(abyBuffer.end(), pabyInput, pabyInput + sizeof(dfVal));
7630
0
}
7631
7632
}  // namespace
7633
7634
/************************************************************************/
7635
/*                   OGRFeature::SerializeToBinary()                    */
7636
/************************************************************************/
7637
7638
/** Serialize the feature to a binary encoding.
7639
 *
7640
 * This saves the feature ID, attribute fields content and geometry fields
7641
 * content.
7642
 *
7643
 * This method is aimed at being paired with DeserializeFromBinary().
7644
 *
7645
 * The format of that encoding may vary across GDAL versions.
7646
 *
7647
 * Note that abyBuffer is cleared at the beginning of this function.
7648
 *
7649
 * @since 3.9
7650
 */
7651
bool OGRFeature::SerializeToBinary(std::vector<GByte> &abyBuffer) const
7652
0
{
7653
0
    const int nFieldCount = poDefn->GetFieldCount();
7654
0
    const int nGeomFieldCount = poDefn->GetGeomFieldCount();
7655
0
    try
7656
0
    {
7657
0
        abyBuffer.clear();
7658
        // Set field flags
7659
        // For attribute fields, we have 2 bits
7660
        // - first one set if the field is unset
7661
        // - second one set if the field is null
7662
        // For geometry fields, we have one bit set to indicate if the geometry
7663
        // is non-null.
7664
0
        const size_t nPresenceFlagsSize =
7665
0
            ((2 * nFieldCount + nGeomFieldCount) + 7) / 8;
7666
0
        abyBuffer.resize(nPresenceFlagsSize);
7667
7668
0
        WriteVarInt(abyBuffer, GetFID());
7669
7670
0
        const auto SetFlagBit = [&abyBuffer](int iBit)
7671
0
        { abyBuffer[iBit / 8] |= (1 << (iBit % 8)); };
7672
7673
0
        for (int i = 0; i < nFieldCount; ++i)
7674
0
        {
7675
0
            const OGRField &uField = pauFields[i];
7676
0
            if (OGR_RawField_IsUnset(&uField))
7677
0
            {
7678
0
                const int iBit = 2 * i;
7679
0
                SetFlagBit(iBit);
7680
0
                continue;
7681
0
            }
7682
0
            if (OGR_RawField_IsNull(&uField))
7683
0
            {
7684
0
                const int iBit = 2 * i + 1;
7685
0
                SetFlagBit(iBit);
7686
0
                continue;
7687
0
            }
7688
0
            const auto poFDefn = poDefn->GetFieldDefn(i);
7689
0
            switch (poFDefn->GetType())
7690
0
            {
7691
0
                case OFTInteger:
7692
0
                {
7693
0
                    WriteVarInt(abyBuffer, uField.Integer);
7694
0
                    break;
7695
0
                }
7696
0
                case OFTInteger64:
7697
0
                {
7698
0
                    WriteVarInt(abyBuffer, uField.Integer64);
7699
0
                    break;
7700
0
                }
7701
0
                case OFTReal:
7702
0
                {
7703
0
                    WriteFloat64(abyBuffer, uField.Real);
7704
0
                    break;
7705
0
                }
7706
0
                case OFTString:
7707
0
                {
7708
0
                    const size_t nStrSize = strlen(uField.String);
7709
0
                    WriteVarUInt(abyBuffer, nStrSize);
7710
0
                    const GByte *pabyStr =
7711
0
                        reinterpret_cast<const GByte *>(uField.String);
7712
0
                    abyBuffer.insert(abyBuffer.end(), pabyStr,
7713
0
                                     pabyStr + nStrSize);
7714
0
                    break;
7715
0
                }
7716
0
                case OFTIntegerList:
7717
0
                {
7718
0
                    WriteVarInt(abyBuffer, uField.IntegerList.nCount);
7719
0
                    for (int j = 0; j < uField.IntegerList.nCount; ++j)
7720
0
                        WriteVarInt(abyBuffer, uField.IntegerList.paList[j]);
7721
0
                    break;
7722
0
                }
7723
0
                case OFTInteger64List:
7724
0
                {
7725
0
                    WriteVarInt(abyBuffer, uField.Integer64List.nCount);
7726
0
                    for (int j = 0; j < uField.Integer64List.nCount; ++j)
7727
0
                        WriteVarInt(abyBuffer, uField.Integer64List.paList[j]);
7728
0
                    break;
7729
0
                }
7730
0
                case OFTRealList:
7731
0
                {
7732
0
                    WriteVarInt(abyBuffer, uField.RealList.nCount);
7733
0
                    for (int j = 0; j < uField.RealList.nCount; ++j)
7734
0
                        WriteFloat64(abyBuffer, uField.RealList.paList[j]);
7735
0
                    break;
7736
0
                }
7737
0
                case OFTStringList:
7738
0
                {
7739
0
                    WriteVarInt(abyBuffer, uField.StringList.nCount);
7740
0
                    for (int j = 0; j < uField.StringList.nCount; ++j)
7741
0
                    {
7742
0
                        const char *pszStr = uField.StringList.paList[j];
7743
0
                        const size_t nStrSize = strlen(pszStr);
7744
0
                        WriteVarUInt(abyBuffer, nStrSize);
7745
0
                        const GByte *pabyStr =
7746
0
                            reinterpret_cast<const GByte *>(pszStr);
7747
0
                        abyBuffer.insert(abyBuffer.end(), pabyStr,
7748
0
                                         pabyStr + nStrSize);
7749
0
                    }
7750
0
                    break;
7751
0
                }
7752
0
                case OFTBinary:
7753
0
                {
7754
0
                    WriteVarInt(abyBuffer, uField.Binary.nCount);
7755
0
                    abyBuffer.insert(abyBuffer.end(), uField.Binary.paData,
7756
0
                                     uField.Binary.paData +
7757
0
                                         uField.Binary.nCount);
7758
0
                    break;
7759
0
                }
7760
0
                case OFTWideString:
7761
0
                case OFTWideStringList:
7762
0
                    break;
7763
0
                case OFTDate:
7764
0
                {
7765
0
                    WriteVarInt(abyBuffer, uField.Date.Year);
7766
0
                    WriteUInt8(abyBuffer, uField.Date.Month);
7767
0
                    WriteUInt8(abyBuffer, uField.Date.Day);
7768
0
                    break;
7769
0
                }
7770
0
                case OFTTime:
7771
0
                {
7772
0
                    WriteUInt8(abyBuffer, uField.Date.Hour);
7773
0
                    WriteUInt8(abyBuffer, uField.Date.Minute);
7774
0
                    WriteFloat32(abyBuffer, uField.Date.Second);
7775
0
                    WriteUInt8(abyBuffer, uField.Date.TZFlag);
7776
0
                    break;
7777
0
                }
7778
0
                case OFTDateTime:
7779
0
                {
7780
0
                    WriteVarInt(abyBuffer, uField.Date.Year);
7781
0
                    WriteUInt8(abyBuffer, uField.Date.Month);
7782
0
                    WriteUInt8(abyBuffer, uField.Date.Day);
7783
0
                    WriteUInt8(abyBuffer, uField.Date.Hour);
7784
0
                    WriteUInt8(abyBuffer, uField.Date.Minute);
7785
0
                    WriteFloat32(abyBuffer, uField.Date.Second);
7786
0
                    WriteUInt8(abyBuffer, uField.Date.TZFlag);
7787
0
                    break;
7788
0
                }
7789
0
            }
7790
0
        }
7791
0
        for (int i = 0; i < nGeomFieldCount; ++i)
7792
0
        {
7793
0
            if (!papoGeometries[i])
7794
0
            {
7795
0
                const int iBit = 2 * nFieldCount + i;
7796
0
                SetFlagBit(iBit);
7797
0
                continue;
7798
0
            }
7799
0
            const size_t nSize = papoGeometries[i]->WkbSize();
7800
0
            WriteVarUInt(abyBuffer, nSize);
7801
0
            const size_t nBufSizeBefore = abyBuffer.size();
7802
0
            abyBuffer.resize(nBufSizeBefore + nSize);
7803
0
            papoGeometries[i]->exportToWkb(
7804
0
                wkbNDR, abyBuffer.data() + nBufSizeBefore, wkbVariantIso);
7805
0
        }
7806
0
        return true;
7807
0
    }
7808
0
    catch (const std::bad_alloc &)
7809
0
    {
7810
0
        CPLError(CE_Failure, CPLE_OutOfMemory, "Out of memory");
7811
0
        return false;
7812
0
    }
7813
0
}
7814
7815
namespace
7816
{
7817
// Implementation borrowed to OpenFileGDB
7818
7819
/************************************************************************/
7820
/*                            ReadVarUInt()                             */
7821
/************************************************************************/
7822
7823
template <class OutType>
7824
static bool ReadVarUInt(const GByte *&pabyIter, const GByte *pabyEnd,
7825
                        OutType &nOutVal)
7826
0
{
7827
0
    if (pabyIter >= pabyEnd)
7828
0
        return false;
7829
0
    OutType b = *pabyIter;
7830
0
    if ((b & 0x80) == 0)
7831
0
    {
7832
0
        pabyIter++;
7833
0
        nOutVal = b;
7834
0
        return true;
7835
0
    }
7836
0
    const GByte *pabyLocalIter = pabyIter + 1;
7837
0
    int nShift = 7;
7838
0
    OutType nVal = (b & 0x7F);
7839
0
    while (true)
7840
0
    {
7841
0
        if (pabyLocalIter >= pabyEnd)
7842
0
            return false;
7843
0
        b = *pabyLocalIter;
7844
0
        pabyLocalIter++;
7845
0
        nVal |= (b & 0x7F) << nShift;
7846
0
        if ((b & 0x80) == 0)
7847
0
        {
7848
0
            pabyIter = pabyLocalIter;
7849
0
            nOutVal = nVal;
7850
0
            return true;
7851
0
        }
7852
0
        nShift += 7;
7853
        // To avoid undefined behavior later when doing << nShift
7854
0
        if (nShift >= static_cast<int>(sizeof(OutType)) * 8)
7855
0
        {
7856
0
            return false;
7857
0
        }
7858
0
    }
7859
0
}
7860
7861
/************************************************************************/
7862
/*                             ReadVarInt()                             */
7863
/************************************************************************/
7864
7865
template <class OutType>
7866
CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW static bool
7867
ReadVarInt(const GByte *&pabyIter, const GByte *pabyEnd, OutType &nOutVal)
7868
0
{
7869
0
    GUInt32 b;
7870
7871
0
    if (pabyIter >= pabyEnd)
7872
0
        return false;
7873
0
    b = *pabyIter;
7874
0
    GUIntBig nVal = (b & 0x3F);
7875
0
    bool bNegative = (b & 0x40) != 0;
7876
0
    if ((b & 0x80) == 0)
7877
0
    {
7878
0
        pabyIter++;
7879
0
        if (bNegative)
7880
0
            nOutVal = -static_cast<OutType>(nVal);
7881
0
        else
7882
0
            nOutVal = static_cast<OutType>(nVal);
7883
0
        return true;
7884
0
    }
7885
7886
0
    const GByte *pabyLocalIter = pabyIter + 1;
7887
0
    int nShift = 6;
7888
0
    while (true)
7889
0
    {
7890
0
        if (pabyLocalIter >= pabyEnd)
7891
0
            return false;
7892
0
        GUIntBig b64 = *pabyLocalIter;
7893
0
        pabyLocalIter++;
7894
0
        nVal |= (b64 & 0x7F) << nShift;
7895
0
        if ((b64 & 0x80) == 0)
7896
0
        {
7897
0
            pabyIter = pabyLocalIter;
7898
0
            if (bNegative)
7899
0
                nOutVal = -static_cast<OutType>(nVal);
7900
0
            else
7901
0
                nOutVal = static_cast<OutType>(nVal);
7902
0
            return true;
7903
0
        }
7904
0
        nShift += 7;
7905
        // To avoid undefined behavior later when doing << nShift
7906
0
        if (nShift >= static_cast<int>(sizeof(GIntBig)) * 8)
7907
0
        {
7908
0
            return false;
7909
0
        }
7910
0
    }
7911
0
}
Unexecuted instantiation: ogrfeature.cpp:bool (anonymous namespace)::ReadVarInt<long long>(unsigned char const*&, unsigned char const*, long long&)
Unexecuted instantiation: ogrfeature.cpp:bool (anonymous namespace)::ReadVarInt<int>(unsigned char const*&, unsigned char const*, int&)
Unexecuted instantiation: ogrfeature.cpp:bool (anonymous namespace)::ReadVarInt<short>(unsigned char const*&, unsigned char const*, short&)
7912
7913
/************************************************************************/
7914
/*                             ReadUInt8()                              */
7915
/************************************************************************/
7916
7917
inline bool ReadUInt8(const GByte *&pabyIter, const GByte *pabyEnd, GByte &nVal)
7918
0
{
7919
0
    if (pabyIter + sizeof(nVal) > pabyEnd)
7920
0
        return false;
7921
0
    nVal = *pabyIter;
7922
0
    pabyIter += sizeof(nVal);
7923
0
    return true;
7924
0
}
7925
7926
/************************************************************************/
7927
/*                            ReadFloat32()                             */
7928
/************************************************************************/
7929
7930
inline bool ReadFloat32(const GByte *&pabyIter, const GByte *pabyEnd,
7931
                        float &fVal)
7932
0
{
7933
0
    if (pabyIter + sizeof(fVal) > pabyEnd)
7934
0
        return false;
7935
    // cppcheck-suppress bufferAccessOutOfBounds
7936
0
    memcpy(&fVal, pabyIter, sizeof(fVal));
7937
0
    CPL_LSBPTR32(&fVal);
7938
0
    pabyIter += sizeof(fVal);
7939
0
    return true;
7940
0
}
7941
7942
/************************************************************************/
7943
/*                            ReadFloat64()                             */
7944
/************************************************************************/
7945
7946
inline bool ReadFloat64(const GByte *&pabyIter, const GByte *pabyEnd,
7947
                        double &dfVal)
7948
0
{
7949
0
    if (pabyIter + sizeof(dfVal) > pabyEnd)
7950
0
        return false;
7951
0
    memcpy(&dfVal, pabyIter, sizeof(dfVal));
7952
0
    CPL_LSBPTR64(&dfVal);
7953
0
    pabyIter += sizeof(dfVal);
7954
0
    return true;
7955
0
}
7956
7957
}  // namespace
7958
7959
/************************************************************************/
7960
/*                 OGRFeature::DeserializeFromBinary()                  */
7961
/************************************************************************/
7962
7963
/** Instantiate a feature from a binary encoding produces by SerializeToBinary()
7964
 *
7965
 * This sets the feature ID, attribute fields content and geometry fields
7966
 * content.
7967
 *
7968
 * DeserializeFromBinary() should be called on a feature whose feature definition
7969
 * is exactly the same as the one on which SerializeToBinary() was called.
7970
 * (but there is no security issue if not doing that, or if feeding a "random"
7971
 * buffer to that method).
7972
 *
7973
 * The format of that encoding may vary across GDAL versions.
7974
 *
7975
 * @since 3.9
7976
 */
7977
bool OGRFeature::DeserializeFromBinary(const GByte *pabyBuffer, size_t nSize)
7978
0
{
7979
0
    Reset();
7980
7981
0
    const GByte *const pabyFlags = pabyBuffer;
7982
0
    const GByte *const pabyEnd = pabyBuffer + nSize;
7983
0
    const int nFieldCount = poDefn->GetFieldCount();
7984
0
    const int nGeomFieldCount = poDefn->GetGeomFieldCount();
7985
0
    const size_t nPresenceFlagsSize =
7986
0
        ((2 * nFieldCount + nGeomFieldCount) + 7) / 8;
7987
0
    if (nSize < nPresenceFlagsSize)
7988
0
        return false;
7989
0
    pabyBuffer += nPresenceFlagsSize;
7990
7991
0
    if (!ReadVarInt(pabyBuffer, pabyEnd, nFID))
7992
0
        return false;
7993
7994
0
    const auto IsFlagBitSet = [pabyFlags](int iBit) -> bool
7995
0
    { return (pabyFlags[iBit / 8] & (1 << (iBit % 8))) != 0; };
7996
7997
0
    for (int i = 0; i < nFieldCount; ++i)
7998
0
    {
7999
0
        OGRField &uField = pauFields[i];
8000
0
        {
8001
0
            const int iBit = 2 * i;
8002
0
            if (IsFlagBitSet(iBit))
8003
0
            {
8004
                // OGR_RawField_SetUnset(&uField);
8005
0
                continue;
8006
0
            }
8007
0
        }
8008
0
        {
8009
0
            const int iBit = 2 * i + 1;
8010
0
            if (IsFlagBitSet(iBit))
8011
0
            {
8012
0
                OGR_RawField_SetNull(&uField);
8013
0
                continue;
8014
0
            }
8015
0
        }
8016
0
        const auto poFDefn = poDefn->GetFieldDefn(i);
8017
0
        switch (poFDefn->GetType())
8018
0
        {
8019
0
            case OFTInteger:
8020
0
            {
8021
0
                uField.Set.nMarker2 = 0;
8022
0
                uField.Set.nMarker3 = 0;
8023
0
                if (!ReadVarInt(pabyBuffer, pabyEnd, uField.Integer))
8024
0
                    return false;
8025
0
                break;
8026
0
            }
8027
0
            case OFTInteger64:
8028
0
            {
8029
0
                uField.Set.nMarker2 = 0;
8030
0
                uField.Set.nMarker3 = 0;
8031
0
                if (!ReadVarInt(pabyBuffer, pabyEnd, uField.Integer64))
8032
0
                    return false;
8033
0
                break;
8034
0
            }
8035
0
            case OFTReal:
8036
0
            {
8037
0
                uField.Set.nMarker2 = 0;
8038
0
                uField.Set.nMarker3 = 0;
8039
0
                if (!ReadFloat64(pabyBuffer, pabyEnd, uField.Real))
8040
0
                    return false;
8041
0
                break;
8042
0
            }
8043
0
            case OFTString:
8044
0
            {
8045
0
                size_t nStrSize = 0;
8046
0
                if (!ReadVarUInt(pabyBuffer, pabyEnd, nStrSize) ||
8047
0
                    nStrSize > std::numeric_limits<size_t>::max() - 1)
8048
0
                {
8049
0
                    return false;
8050
0
                }
8051
0
                if (nStrSize > static_cast<size_t>(pabyEnd - pabyBuffer))
8052
0
                    return false;
8053
0
                auto ptr =
8054
0
                    static_cast<char *>(VSI_MALLOC_VERBOSE(nStrSize + 1));
8055
0
                if (!ptr)
8056
0
                    return false;
8057
0
                uField.Set.nMarker2 = 0;
8058
0
                uField.Set.nMarker3 = 0;
8059
0
                uField.String = ptr;
8060
0
                memcpy(uField.String, pabyBuffer, nStrSize);
8061
0
                uField.String[nStrSize] = 0;
8062
0
                pabyBuffer += nStrSize;
8063
0
                break;
8064
0
            }
8065
0
            case OFTIntegerList:
8066
0
            {
8067
0
                int nCount = 0;
8068
0
                if (!ReadVarInt(pabyBuffer, pabyEnd, nCount) || nCount < 0 ||
8069
0
                    nCount > pabyEnd - pabyBuffer)
8070
0
                {
8071
0
                    return false;
8072
0
                }
8073
0
                auto ptr = static_cast<int *>(
8074
0
                    VSI_MALLOC2_VERBOSE(nCount, sizeof(int)));
8075
0
                if (!ptr)
8076
0
                    return false;
8077
0
                uField.Set.nMarker2 = 0;
8078
0
                uField.Set.nMarker3 = 0;
8079
0
                uField.IntegerList.paList = ptr;
8080
0
                uField.IntegerList.nCount = nCount;
8081
0
                for (int j = 0; j < nCount; ++j)
8082
0
                {
8083
0
                    if (!ReadVarInt(pabyBuffer, pabyEnd,
8084
0
                                    uField.IntegerList.paList[j]))
8085
0
                        return false;
8086
0
                }
8087
0
                break;
8088
0
            }
8089
0
            case OFTInteger64List:
8090
0
            {
8091
0
                int nCount = 0;
8092
0
                if (!ReadVarInt(pabyBuffer, pabyEnd, nCount) || nCount < 0 ||
8093
0
                    nCount > pabyEnd - pabyBuffer)
8094
0
                {
8095
0
                    return false;
8096
0
                }
8097
0
                auto ptr = static_cast<GIntBig *>(
8098
0
                    VSI_MALLOC2_VERBOSE(nCount, sizeof(GIntBig)));
8099
0
                if (!ptr)
8100
0
                    return false;
8101
0
                uField.Set.nMarker2 = 0;
8102
0
                uField.Set.nMarker3 = 0;
8103
0
                uField.Integer64List.paList = ptr;
8104
0
                uField.Integer64List.nCount = nCount;
8105
0
                for (int j = 0; j < nCount; ++j)
8106
0
                {
8107
0
                    if (!ReadVarInt(pabyBuffer, pabyEnd,
8108
0
                                    uField.Integer64List.paList[j]))
8109
0
                        return false;
8110
0
                }
8111
0
                break;
8112
0
            }
8113
0
            case OFTRealList:
8114
0
            {
8115
0
                int nCount = 0;
8116
0
                if (!ReadVarInt(pabyBuffer, pabyEnd, nCount) || nCount < 0 ||
8117
0
                    nCount > pabyEnd - pabyBuffer)
8118
0
                {
8119
0
                    return false;
8120
0
                }
8121
0
                auto ptr = static_cast<double *>(
8122
0
                    VSI_MALLOC2_VERBOSE(nCount, sizeof(double)));
8123
0
                if (!ptr)
8124
0
                    return false;
8125
0
                uField.Set.nMarker2 = 0;
8126
0
                uField.Set.nMarker3 = 0;
8127
0
                uField.RealList.paList = ptr;
8128
0
                uField.RealList.nCount = nCount;
8129
0
                for (int j = 0; j < nCount; ++j)
8130
0
                {
8131
0
                    if (!ReadFloat64(pabyBuffer, pabyEnd,
8132
0
                                     uField.RealList.paList[j]))
8133
0
                        return false;
8134
0
                }
8135
0
                break;
8136
0
            }
8137
0
            case OFTStringList:
8138
0
            {
8139
0
                int nCount = 0;
8140
0
                if (!ReadVarInt(pabyBuffer, pabyEnd, nCount) || nCount < 0 ||
8141
0
                    nCount > std::numeric_limits<int>::max() - 1 ||
8142
0
                    nCount > pabyEnd - pabyBuffer)
8143
0
                {
8144
0
                    return false;
8145
0
                }
8146
0
                auto ptr = static_cast<char **>(
8147
0
                    VSI_CALLOC_VERBOSE(nCount + 1, sizeof(char *)));
8148
0
                if (!ptr)
8149
0
                    return false;
8150
0
                uField.Set.nMarker2 = 0;
8151
0
                uField.Set.nMarker3 = 0;
8152
0
                uField.StringList.paList = ptr;
8153
0
                uField.StringList.nCount = nCount;
8154
0
                for (int j = 0; j < nCount; ++j)
8155
0
                {
8156
0
                    size_t nStrSize = 0;
8157
0
                    if (!ReadVarUInt(pabyBuffer, pabyEnd, nStrSize) ||
8158
0
                        nStrSize > std::numeric_limits<size_t>::max() - 1)
8159
0
                    {
8160
0
                        return false;
8161
0
                    }
8162
0
                    if (nStrSize > static_cast<size_t>(pabyEnd - pabyBuffer))
8163
0
                        return false;
8164
0
                    uField.StringList.paList[j] =
8165
0
                        static_cast<char *>(VSI_MALLOC_VERBOSE(nStrSize + 1));
8166
0
                    if (!uField.StringList.paList[j])
8167
0
                        return false;
8168
0
                    memcpy(uField.StringList.paList[j], pabyBuffer, nStrSize);
8169
0
                    uField.StringList.paList[j][nStrSize] = 0;
8170
0
                    pabyBuffer += nStrSize;
8171
0
                }
8172
0
                break;
8173
0
            }
8174
0
            case OFTBinary:
8175
0
            {
8176
0
                int nBinSize = 0;
8177
0
                if (!ReadVarInt(pabyBuffer, pabyEnd, nBinSize) || nBinSize < 0)
8178
0
                {
8179
0
                    return false;
8180
0
                }
8181
0
                if (nBinSize > pabyEnd - pabyBuffer)
8182
0
                    return false;
8183
0
                auto ptr = static_cast<GByte *>(VSI_MALLOC_VERBOSE(nBinSize));
8184
0
                if (!ptr)
8185
0
                    return false;
8186
0
                uField.Set.nMarker2 = 0;
8187
0
                uField.Set.nMarker3 = 0;
8188
0
                uField.Binary.paData = ptr;
8189
0
                uField.Binary.nCount = nBinSize;
8190
0
                memcpy(uField.Binary.paData, pabyBuffer, nBinSize);
8191
0
                pabyBuffer += nBinSize;
8192
0
                break;
8193
0
            }
8194
0
            case OFTWideString:
8195
0
            case OFTWideStringList:
8196
0
                break;
8197
0
            case OFTDate:
8198
0
            {
8199
0
                memset(&uField, 0, sizeof(uField));
8200
0
                if (!ReadVarInt(pabyBuffer, pabyEnd, uField.Date.Year) ||
8201
0
                    !ReadUInt8(pabyBuffer, pabyEnd, uField.Date.Month) ||
8202
0
                    !ReadUInt8(pabyBuffer, pabyEnd, uField.Date.Day))
8203
0
                {
8204
0
                    return false;
8205
0
                }
8206
0
                break;
8207
0
            }
8208
0
            case OFTTime:
8209
0
            {
8210
0
                memset(&uField, 0, sizeof(uField));
8211
0
                if (!ReadUInt8(pabyBuffer, pabyEnd, uField.Date.Hour) ||
8212
0
                    !ReadUInt8(pabyBuffer, pabyEnd, uField.Date.Minute) ||
8213
0
                    !ReadFloat32(pabyBuffer, pabyEnd, uField.Date.Second) ||
8214
0
                    !ReadUInt8(pabyBuffer, pabyEnd, uField.Date.TZFlag))
8215
0
                {
8216
0
                    return false;
8217
0
                }
8218
0
                break;
8219
0
            }
8220
0
            case OFTDateTime:
8221
0
            {
8222
0
                memset(&uField, 0, sizeof(uField));
8223
0
                if (!ReadVarInt(pabyBuffer, pabyEnd, uField.Date.Year) ||
8224
0
                    !ReadUInt8(pabyBuffer, pabyEnd, uField.Date.Month) ||
8225
0
                    !ReadUInt8(pabyBuffer, pabyEnd, uField.Date.Day) ||
8226
0
                    !ReadUInt8(pabyBuffer, pabyEnd, uField.Date.Hour) ||
8227
0
                    !ReadUInt8(pabyBuffer, pabyEnd, uField.Date.Minute) ||
8228
0
                    !ReadFloat32(pabyBuffer, pabyEnd, uField.Date.Second) ||
8229
0
                    !ReadUInt8(pabyBuffer, pabyEnd, uField.Date.TZFlag))
8230
0
                {
8231
0
                    return false;
8232
0
                }
8233
0
                break;
8234
0
            }
8235
0
        }
8236
0
    }
8237
0
    for (int i = 0; i < nGeomFieldCount; ++i)
8238
0
    {
8239
0
        const int iBit = 2 * nFieldCount + i;
8240
0
        if (IsFlagBitSet(iBit))
8241
0
        {
8242
0
            continue;
8243
0
        }
8244
0
        size_t nWkbSize = 0;
8245
0
        if (!ReadVarUInt(pabyBuffer, pabyEnd, nWkbSize))
8246
0
        {
8247
0
            return false;
8248
0
        }
8249
0
        if (nWkbSize > static_cast<size_t>(pabyEnd - pabyBuffer))
8250
0
        {
8251
0
            return false;
8252
0
        }
8253
0
        OGRGeometry *poGeom = nullptr;
8254
0
        if (OGRGeometryFactory::createFromWkb(
8255
0
                pabyBuffer, poDefn->GetGeomFieldDefn(i)->GetSpatialRef(),
8256
0
                &poGeom, nWkbSize, wkbVariantIso) != OGRERR_NONE ||
8257
0
            !poGeom)
8258
0
        {
8259
0
            delete poGeom;
8260
0
            return false;
8261
0
        }
8262
0
        pabyBuffer += nWkbSize;
8263
0
        papoGeometries[i] = poGeom;
8264
0
    }
8265
0
    return true;
8266
0
}
8267
8268
/************************************************************************/
8269
/*                    OGRFeature::ConstFieldIterator                    */
8270
/************************************************************************/
8271
8272
struct OGRFeature::FieldValue::Private
8273
{
8274
    CPL_DISALLOW_COPY_ASSIGN(Private)
8275
8276
    OGRFeature *m_poSelf = nullptr;
8277
    int m_nPos = 0;
8278
    mutable std::vector<int> m_anList{};
8279
    mutable std::vector<GIntBig> m_anList64{};
8280
    mutable std::vector<double> m_adfList{};
8281
    mutable std::vector<std::string> m_aosList{};
8282
8283
    Private(const OGRFeature *poSelf, int iFieldIndex)
8284
0
        : m_poSelf(const_cast<OGRFeature *>(poSelf)), m_nPos(iFieldIndex)
8285
0
    {
8286
0
    }
8287
8288
    Private(OGRFeature *poSelf, int iFieldIndex)
8289
0
        : m_poSelf(poSelf), m_nPos(iFieldIndex)
8290
0
    {
8291
0
    }
8292
};
8293
8294
struct OGRFeature::ConstFieldIterator::Private
8295
{
8296
    CPL_DISALLOW_COPY_ASSIGN(Private)
8297
8298
    OGRFeature::FieldValue m_oValue;
8299
    int m_nPos = 0;
8300
8301
    Private(const OGRFeature *poSelf, int iFieldIndex)
8302
0
        : m_oValue(poSelf, iFieldIndex)
8303
0
    {
8304
0
    }
8305
};
8306
8307
//! @cond Doxygen_Suppress
8308
OGRFeature::ConstFieldIterator::ConstFieldIterator(const OGRFeature *poSelf,
8309
                                                   int nPos)
8310
0
    : m_poPrivate(new Private(poSelf, nPos))
8311
0
{
8312
0
    m_poPrivate->m_nPos = nPos;
8313
0
}
8314
8315
0
OGRFeature::ConstFieldIterator::~ConstFieldIterator() = default;
8316
8317
const OGRFeature::FieldValue &OGRFeature::ConstFieldIterator::operator*() const
8318
0
{
8319
0
    return m_poPrivate->m_oValue;
8320
0
}
8321
8322
OGRFeature::ConstFieldIterator &OGRFeature::ConstFieldIterator::operator++()
8323
0
{
8324
0
    ++m_poPrivate->m_nPos;
8325
0
    m_poPrivate->m_oValue.m_poPrivate->m_nPos = m_poPrivate->m_nPos;
8326
0
    return *this;
8327
0
}
8328
8329
bool OGRFeature::ConstFieldIterator::operator!=(
8330
    const ConstFieldIterator &it) const
8331
0
{
8332
0
    return m_poPrivate->m_nPos != it.m_poPrivate->m_nPos;
8333
0
}
8334
8335
//! @endcond
8336
8337
OGRFeature::ConstFieldIterator OGRFeature::begin() const
8338
0
{
8339
0
    return {this, 0};
8340
0
}
8341
8342
OGRFeature::ConstFieldIterator OGRFeature::end() const
8343
0
{
8344
0
    return {this, GetFieldCount()};
8345
0
}
8346
8347
/************************************************************************/
8348
/*                        OGRFeature::FieldValue                        */
8349
/************************************************************************/
8350
8351
OGRFeature::FieldValue::FieldValue(const OGRFeature *poFeature, int iFieldIndex)
8352
0
    : m_poPrivate(new Private(poFeature, iFieldIndex))
8353
0
{
8354
0
}
8355
8356
OGRFeature::FieldValue::FieldValue(OGRFeature *poFeature, int iFieldIndex)
8357
0
    : m_poPrivate(new Private(poFeature, iFieldIndex))
8358
0
{
8359
0
}
8360
8361
OGRFeature::FieldValue &OGRFeature::FieldValue::Assign(const FieldValue &oOther)
8362
0
{
8363
0
    if (&oOther != this &&
8364
0
        !(m_poPrivate->m_poSelf == oOther.m_poPrivate->m_poSelf &&
8365
0
          m_poPrivate->m_nPos == oOther.m_poPrivate->m_nPos))
8366
0
    {
8367
0
        OGRFieldType eOtherType(oOther.GetType());
8368
0
        if (oOther.IsNull())
8369
0
            SetNull();
8370
0
        else if (oOther.IsUnset())
8371
0
            Unset();
8372
0
        else if (eOtherType == OFTInteger)
8373
0
            m_poPrivate->m_poSelf->SetField(m_poPrivate->m_nPos,
8374
0
                                            oOther.GetInteger());
8375
0
        else if (eOtherType == OFTInteger64)
8376
0
            m_poPrivate->m_poSelf->SetField(m_poPrivate->m_nPos,
8377
0
                                            oOther.GetInteger64());
8378
0
        else if (eOtherType == OFTReal)
8379
0
            m_poPrivate->m_poSelf->SetField(m_poPrivate->m_nPos,
8380
0
                                            oOther.GetDouble());
8381
0
        else if (eOtherType == OFTString)
8382
0
            m_poPrivate->m_poSelf->SetField(m_poPrivate->m_nPos,
8383
0
                                            oOther.GetString());
8384
0
        else if (eOtherType == OFTDate || eOtherType == OFTDateTime ||
8385
0
                 eOtherType == OFTTime)
8386
0
        {
8387
0
            int nYear = 0;
8388
0
            int nMonth = 0;
8389
0
            int nDay = 0;
8390
0
            int nHour = 0;
8391
0
            int nMinute = 0;
8392
0
            float fSecond = 0.0f;
8393
0
            int nTZFlag = 0;
8394
0
            oOther.GetDateTime(&nYear, &nMonth, &nDay, &nHour, &nMinute,
8395
0
                               &fSecond, &nTZFlag);
8396
0
            m_poPrivate->m_poSelf->SetField(m_poPrivate->m_nPos, nYear, nMonth,
8397
0
                                            nDay, nHour, nMinute, fSecond,
8398
0
                                            nTZFlag);
8399
0
        }
8400
0
        else if (eOtherType == OFTStringList)
8401
0
        {
8402
0
            m_poPrivate->m_poSelf->SetField(
8403
0
                m_poPrivate->m_nPos,
8404
0
                oOther.m_poPrivate->m_poSelf->GetFieldAsStringList(
8405
0
                    oOther.m_poPrivate->m_nPos));
8406
0
        }
8407
0
        else if (eOtherType == OFTIntegerList)
8408
0
        {
8409
0
            operator=(oOther.GetAsIntegerList());
8410
0
        }
8411
0
        else if (eOtherType == OFTInteger64List)
8412
0
        {
8413
0
            operator=(oOther.GetAsInteger64List());
8414
0
        }
8415
0
        else if (eOtherType == OFTRealList)
8416
0
        {
8417
0
            operator=(oOther.GetAsDoubleList());
8418
0
        }
8419
0
    }
8420
0
    return *this;
8421
0
}
8422
8423
OGRFeature::FieldValue &
8424
OGRFeature::FieldValue::operator=(const FieldValue &oOther)
8425
0
{
8426
0
    return Assign(oOther);
8427
0
}
8428
8429
//! @cond Doxygen_Suppress
8430
OGRFeature::FieldValue &OGRFeature::FieldValue::operator=(FieldValue &&oOther)
8431
0
{
8432
0
    return Assign(oOther);
8433
0
}
8434
8435
//! @endcond
8436
8437
OGRFeature::FieldValue &OGRFeature::FieldValue::operator=(int nVal)
8438
0
{
8439
0
    m_poPrivate->m_poSelf->SetField(m_poPrivate->m_nPos, nVal);
8440
0
    return *this;
8441
0
}
8442
8443
OGRFeature::FieldValue &OGRFeature::FieldValue::operator=(GIntBig nVal)
8444
0
{
8445
0
    m_poPrivate->m_poSelf->SetField(m_poPrivate->m_nPos, nVal);
8446
0
    return *this;
8447
0
}
8448
8449
OGRFeature::FieldValue &OGRFeature::FieldValue::operator=(double dfVal)
8450
0
{
8451
0
    m_poPrivate->m_poSelf->SetField(m_poPrivate->m_nPos, dfVal);
8452
0
    return *this;
8453
0
}
8454
8455
OGRFeature::FieldValue &OGRFeature::FieldValue::operator=(const char *pszVal)
8456
0
{
8457
0
    m_poPrivate->m_poSelf->SetField(m_poPrivate->m_nPos, pszVal);
8458
0
    return *this;
8459
0
}
8460
8461
OGRFeature::FieldValue &
8462
OGRFeature::FieldValue::operator=(const std::string &osVal)
8463
0
{
8464
0
    m_poPrivate->m_poSelf->SetField(m_poPrivate->m_nPos, osVal.c_str());
8465
0
    return *this;
8466
0
}
8467
8468
OGRFeature::FieldValue &
8469
OGRFeature::FieldValue::operator=(const std::vector<int> &oArray)
8470
0
{
8471
0
    m_poPrivate->m_poSelf->SetField(m_poPrivate->m_nPos,
8472
0
                                    static_cast<int>(oArray.size()),
8473
0
                                    oArray.empty() ? nullptr : oArray.data());
8474
0
    return *this;
8475
0
}
8476
8477
OGRFeature::FieldValue &
8478
OGRFeature::FieldValue::operator=(const std::vector<GIntBig> &oArray)
8479
0
{
8480
0
    m_poPrivate->m_poSelf->SetField(m_poPrivate->m_nPos,
8481
0
                                    static_cast<int>(oArray.size()),
8482
0
                                    oArray.empty() ? nullptr : oArray.data());
8483
0
    return *this;
8484
0
}
8485
8486
OGRFeature::FieldValue &
8487
OGRFeature::FieldValue::operator=(const std::vector<double> &oArray)
8488
0
{
8489
0
    m_poPrivate->m_poSelf->SetField(m_poPrivate->m_nPos,
8490
0
                                    static_cast<int>(oArray.size()),
8491
0
                                    oArray.empty() ? nullptr : oArray.data());
8492
0
    return *this;
8493
0
}
8494
8495
OGRFeature::FieldValue &
8496
OGRFeature::FieldValue::operator=(const std::vector<std::string> &oArray)
8497
0
{
8498
0
    CPLStringList aosList;
8499
0
    for (auto &&oStr : oArray)
8500
0
        aosList.AddString(oStr.c_str());
8501
0
    m_poPrivate->m_poSelf->SetField(m_poPrivate->m_nPos, aosList.List());
8502
0
    return *this;
8503
0
}
8504
8505
OGRFeature::FieldValue &
8506
OGRFeature::FieldValue::operator=(CSLConstList papszValues)
8507
0
{
8508
0
    m_poPrivate->m_poSelf->SetField(m_poPrivate->m_nPos, papszValues);
8509
0
    return *this;
8510
0
}
8511
8512
void OGRFeature::FieldValue::SetDateTime(int nYear, int nMonth, int nDay,
8513
                                         int nHour, int nMinute, float fSecond,
8514
                                         int nTZFlag)
8515
0
{
8516
0
    m_poPrivate->m_poSelf->SetField(m_poPrivate->m_nPos, nYear, nMonth, nDay,
8517
0
                                    nHour, nMinute, fSecond, nTZFlag);
8518
0
}
8519
8520
void OGRFeature::FieldValue::SetNull()
8521
0
{
8522
0
    m_poPrivate->m_poSelf->SetFieldNull(m_poPrivate->m_nPos);
8523
0
}
8524
8525
void OGRFeature::FieldValue::clear()
8526
0
{
8527
0
    m_poPrivate->m_poSelf->UnsetField(m_poPrivate->m_nPos);
8528
0
}
8529
8530
//! @cond Doxygen_Suppress
8531
0
OGRFeature::FieldValue::~FieldValue() = default;
8532
8533
//! @endcond
8534
8535
int OGRFeature::FieldValue::GetIndex() const
8536
0
{
8537
0
    return m_poPrivate->m_nPos;
8538
0
}
8539
8540
const OGRFieldDefn *OGRFeature::FieldValue::GetDefn() const
8541
0
{
8542
0
    return m_poPrivate->m_poSelf->GetFieldDefnRef(GetIndex());
8543
0
}
8544
8545
const OGRField *OGRFeature::FieldValue::GetRawValue() const
8546
0
{
8547
0
    return &(m_poPrivate->m_poSelf->pauFields[GetIndex()]);
8548
0
}
8549
8550
bool OGRFeature::FieldValue::IsUnset() const
8551
0
{
8552
0
    return CPL_TO_BOOL(OGR_RawField_IsUnset(GetRawValue()));
8553
0
}
8554
8555
bool OGRFeature::FieldValue::IsNull() const
8556
0
{
8557
0
    return CPL_TO_BOOL(OGR_RawField_IsNull(GetRawValue()));
8558
0
}
8559
8560
int OGRFeature::FieldValue::GetAsInteger() const
8561
0
{
8562
0
    return const_cast<OGRFeature *>(m_poPrivate->m_poSelf)
8563
0
        ->GetFieldAsInteger(GetIndex());
8564
0
}
8565
8566
GIntBig OGRFeature::FieldValue::GetAsInteger64() const
8567
0
{
8568
0
    return const_cast<OGRFeature *>(m_poPrivate->m_poSelf)
8569
0
        ->GetFieldAsInteger64(GetIndex());
8570
0
}
8571
8572
double OGRFeature::FieldValue::GetAsDouble() const
8573
0
{
8574
0
    return const_cast<OGRFeature *>(m_poPrivate->m_poSelf)
8575
0
        ->GetFieldAsDouble(GetIndex());
8576
0
}
8577
8578
const char *OGRFeature::FieldValue::GetAsString() const
8579
0
{
8580
0
    return const_cast<OGRFeature *>(m_poPrivate->m_poSelf)
8581
0
        ->GetFieldAsString(GetIndex());
8582
0
}
8583
8584
bool OGRFeature::FieldValue::GetDateTime(int *pnYear, int *pnMonth, int *pnDay,
8585
                                         int *pnHour, int *pnMinute,
8586
                                         float *pfSecond, int *pnTZFlag) const
8587
0
{
8588
0
    return CPL_TO_BOOL(const_cast<OGRFeature *>(m_poPrivate->m_poSelf)
8589
0
                           ->GetFieldAsDateTime(GetIndex(), pnYear, pnMonth,
8590
0
                                                pnDay, pnHour, pnMinute,
8591
0
                                                pfSecond, pnTZFlag));
8592
0
}
8593
8594
const std::vector<int> &OGRFeature::FieldValue::GetAsIntegerList() const
8595
0
{
8596
0
    int nCount = 0;
8597
0
    auto &&panList = const_cast<OGRFeature *>(m_poPrivate->m_poSelf)
8598
0
                         ->GetFieldAsIntegerList(GetIndex(), &nCount);
8599
0
    m_poPrivate->m_anList.assign(panList, panList + nCount);
8600
0
    return m_poPrivate->m_anList;
8601
0
}
8602
8603
const std::vector<GIntBig> &OGRFeature::FieldValue::GetAsInteger64List() const
8604
0
{
8605
0
    int nCount = 0;
8606
0
    auto &&panList = const_cast<OGRFeature *>(m_poPrivate->m_poSelf)
8607
0
                         ->GetFieldAsInteger64List(GetIndex(), &nCount);
8608
0
    m_poPrivate->m_anList64.assign(panList, panList + nCount);
8609
0
    return m_poPrivate->m_anList64;
8610
0
}
8611
8612
const std::vector<double> &OGRFeature::FieldValue::GetAsDoubleList() const
8613
0
{
8614
0
    int nCount = 0;
8615
0
    auto &&panList = const_cast<OGRFeature *>(m_poPrivate->m_poSelf)
8616
0
                         ->GetFieldAsDoubleList(GetIndex(), &nCount);
8617
0
    m_poPrivate->m_adfList.assign(panList, panList + nCount);
8618
0
    return m_poPrivate->m_adfList;
8619
0
}
8620
8621
const std::vector<std::string> &OGRFeature::FieldValue::GetAsStringList() const
8622
0
{
8623
0
    auto &&papszList = const_cast<OGRFeature *>(m_poPrivate->m_poSelf)
8624
0
                           ->GetFieldAsStringList(GetIndex());
8625
0
    m_poPrivate->m_aosList.clear();
8626
0
    if (papszList)
8627
0
    {
8628
0
        for (char **papszIter = papszList; *papszIter; ++papszIter)
8629
0
        {
8630
0
            m_poPrivate->m_aosList.emplace_back(*papszIter);
8631
0
        }
8632
0
    }
8633
0
    return m_poPrivate->m_aosList;
8634
0
}
8635
8636
OGRFeature::FieldValue::operator CSLConstList() const
8637
0
{
8638
0
    return const_cast<CSLConstList>(
8639
0
        const_cast<OGRFeature *>(m_poPrivate->m_poSelf)
8640
0
            ->GetFieldAsStringList(GetIndex()));
8641
0
}
8642
8643
OGRRangeFieldDomain *OGRRangeFieldDomain::Clone() const
8644
0
{
8645
0
    auto poDomain = new OGRRangeFieldDomain(
8646
0
        m_osName, m_osDescription, m_eFieldType, m_eFieldSubType, m_sMin,
8647
0
        m_bMinIsInclusive, m_sMax, m_bMaxIsInclusive);
8648
0
    poDomain->SetMergePolicy(m_eMergePolicy);
8649
0
    poDomain->SetSplitPolicy(m_eSplitPolicy);
8650
0
    return poDomain;
8651
0
}
8652
8653
OGRGlobFieldDomain *OGRGlobFieldDomain::Clone() const
8654
0
{
8655
0
    auto poDomain = new OGRGlobFieldDomain(
8656
0
        m_osName, m_osDescription, m_eFieldType, m_eFieldSubType, m_osGlob);
8657
0
    poDomain->SetMergePolicy(m_eMergePolicy);
8658
0
    poDomain->SetSplitPolicy(m_eSplitPolicy);
8659
0
    return poDomain;
8660
0
}
8661
#if defined(__GNUC__)
8662
#pragma GCC diagnostic pop
8663
#endif