Coverage Report

Created: 2025-08-28 06:57

/src/gdal/ogr/ogrsf_frmts/shape/ogrshapelayer.cpp
Line
Count
Source (jump to first uncovered line)
1
/******************************************************************************
2
 *
3
 * Project:  OpenGIS Simple Features Reference Implementation
4
 * Purpose:  Implements OGRShapeLayer class.
5
 * Author:   Frank Warmerdam, warmerdam@pobox.com
6
 *
7
 ******************************************************************************
8
 * Copyright (c) 1999,  Les Technologies SoftMap Inc.
9
 * Copyright (c) 2007-2013, Even Rouault <even dot rouault at spatialys.com>
10
 *
11
 * SPDX-License-Identifier: MIT
12
 ****************************************************************************/
13
14
#include "ogrshape.h"
15
16
#include <cerrno>
17
#include <limits>
18
#include <cmath>
19
#include <cstddef>
20
#include <cstdio>
21
#include <cstdlib>
22
#include <cstring>
23
#include <ctime>
24
#include <algorithm>
25
#include <string>
26
27
#include "cpl_conv.h"
28
#include "cpl_error.h"
29
#include "cpl_multiproc.h"
30
#include "cpl_port.h"
31
#include "cpl_string.h"
32
#include "cpl_time.h"
33
#include "cpl_vsi.h"
34
#include "ogr_core.h"
35
#include "ogr_feature.h"
36
#include "ogr_geometry.h"
37
#include "ogr_p.h"
38
#include "ogr_spatialref.h"
39
#include "ogr_srs_api.h"
40
#include "ogrlayerpool.h"
41
#include "ograrrowarrayhelper.h"
42
#include "ogrsf_frmts.h"
43
#include "shapefil.h"
44
#include "shp_vsi.h"
45
46
/************************************************************************/
47
/*                           OGRShapeLayer()                            */
48
/************************************************************************/
49
50
OGRShapeLayer::OGRShapeLayer(OGRShapeDataSource *poDSIn,
51
                             const char *pszFullNameIn, SHPHandle hSHPIn,
52
                             DBFHandle hDBFIn,
53
                             const OGRSpatialReference *poSRSIn, bool bSRSSetIn,
54
                             const std::string &osPrjFilename, bool bUpdate,
55
                             OGRwkbGeometryType eReqType,
56
                             CSLConstList papszCreateOptions)
57
0
    : OGRAbstractProxiedLayer(poDSIn->GetPool()), m_poDS(poDSIn),
58
0
      m_osFullName(pszFullNameIn), m_hSHP(hSHPIn), m_hDBF(hDBFIn),
59
0
      m_bUpdateAccess(bUpdate), m_eRequestedGeomType(eReqType),
60
0
      m_bHSHPWasNonNULL(hSHPIn != nullptr), m_bHDBFWasNonNULL(hDBFIn != nullptr)
61
0
{
62
0
    if (m_hSHP != nullptr)
63
0
    {
64
0
        m_nTotalShapeCount = m_hSHP->nRecords;
65
0
        if (m_hDBF != nullptr && m_hDBF->nRecords != m_nTotalShapeCount)
66
0
        {
67
0
            CPLDebug("Shape",
68
0
                     "Inconsistent record number in .shp (%d) and in .dbf (%d)",
69
0
                     m_hSHP->nRecords, m_hDBF->nRecords);
70
0
        }
71
0
    }
72
0
    else if (m_hDBF != nullptr)
73
0
    {
74
0
        m_nTotalShapeCount = m_hDBF->nRecords;
75
0
    }
76
0
#ifdef DEBUG
77
0
    else
78
0
    {
79
0
        CPLError(CE_Fatal, CPLE_AssertionFailed,
80
0
                 "Should not happen: Both m_hSHP and m_hDBF are nullptrs");
81
0
    }
82
0
#endif
83
84
0
    if (!TouchLayer())
85
0
    {
86
0
        CPLDebug("Shape", "TouchLayer in shape ctor failed. ");
87
0
    }
88
89
0
    if (m_hDBF != nullptr && m_hDBF->pszCodePage != nullptr)
90
0
    {
91
0
        CPLDebug("Shape", "DBF Codepage = %s for %s", m_hDBF->pszCodePage,
92
0
                 m_osFullName.c_str());
93
94
        // Not too sure about this, but it seems like better than nothing.
95
0
        m_osEncoding = ConvertCodePage(m_hDBF->pszCodePage);
96
0
    }
97
98
0
    if (m_hDBF != nullptr)
99
0
    {
100
0
        if (!(m_hDBF->nUpdateYearSince1900 == 95 && m_hDBF->nUpdateMonth == 7 &&
101
0
              m_hDBF->nUpdateDay == 26))
102
0
        {
103
0
            SetMetadataItem("DBF_DATE_LAST_UPDATE",
104
0
                            CPLSPrintf("%04d-%02d-%02d",
105
0
                                       m_hDBF->nUpdateYearSince1900 + 1900,
106
0
                                       m_hDBF->nUpdateMonth,
107
0
                                       m_hDBF->nUpdateDay));
108
0
        }
109
0
        struct tm tm;
110
0
        CPLUnixTimeToYMDHMS(time(nullptr), &tm);
111
0
        DBFSetLastModifiedDate(m_hDBF, tm.tm_year, tm.tm_mon + 1, tm.tm_mday);
112
0
    }
113
114
0
    const char *pszShapeEncoding =
115
0
        CSLFetchNameValue(m_poDS->GetOpenOptions(), "ENCODING");
116
0
    if (pszShapeEncoding == nullptr && m_osEncoding == "")
117
0
        pszShapeEncoding = CSLFetchNameValue(papszCreateOptions, "ENCODING");
118
0
    if (pszShapeEncoding == nullptr)
119
0
        pszShapeEncoding = CPLGetConfigOption("SHAPE_ENCODING", nullptr);
120
0
    if (pszShapeEncoding != nullptr)
121
0
        m_osEncoding = pszShapeEncoding;
122
123
0
    if (m_osEncoding != "")
124
0
    {
125
0
        CPLDebug("Shape", "Treating as encoding '%s'.", m_osEncoding.c_str());
126
127
0
        if (!OGRShapeLayer::TestCapability(OLCStringsAsUTF8))
128
0
        {
129
0
            CPLDebug("Shape", "Cannot recode from '%s'. Disabling recoding",
130
0
                     m_osEncoding.c_str());
131
0
            m_osEncoding = "";
132
0
        }
133
0
    }
134
0
    SetMetadataItem("SOURCE_ENCODING", m_osEncoding, "SHAPEFILE");
135
136
0
    m_poFeatureDefn = SHPReadOGRFeatureDefn(
137
0
        CPLGetBasenameSafe(m_osFullName.c_str()).c_str(), m_hSHP, m_hDBF,
138
0
        m_osEncoding,
139
0
        CPLFetchBool(m_poDS->GetOpenOptions(), "ADJUST_TYPE", false));
140
141
    // To make sure that
142
    //  GetLayerDefn()->GetGeomFieldDefn(0)->GetSpatialRef() == GetSpatialRef()
143
0
    OGRwkbGeometryType eGeomType = m_poFeatureDefn->GetGeomType();
144
0
    if (eGeomType != wkbNone)
145
0
    {
146
0
        OGRwkbGeometryType eType = wkbUnknown;
147
148
0
        if (m_eRequestedGeomType == wkbNone)
149
0
        {
150
0
            eType = eGeomType;
151
152
0
            const char *pszAdjustGeomType = CSLFetchNameValueDef(
153
0
                m_poDS->GetOpenOptions(), "ADJUST_GEOM_TYPE", "FIRST_SHAPE");
154
0
            const bool bFirstShape = EQUAL(pszAdjustGeomType, "FIRST_SHAPE");
155
0
            const bool bAllShapes = EQUAL(pszAdjustGeomType, "ALL_SHAPES");
156
0
            if ((m_hSHP != nullptr) && (m_hSHP->nRecords > 0) &&
157
0
                wkbHasM(eType) && (bFirstShape || bAllShapes))
158
0
            {
159
0
                bool bMIsUsed = false;
160
0
                for (int iShape = 0; iShape < m_hSHP->nRecords; iShape++)
161
0
                {
162
0
                    SHPObject *psShape = SHPReadObject(m_hSHP, iShape);
163
0
                    if (psShape)
164
0
                    {
165
0
                        if (psShape->bMeasureIsUsed && psShape->nVertices > 0 &&
166
0
                            psShape->padfM != nullptr)
167
0
                        {
168
0
                            for (int i = 0; i < psShape->nVertices; i++)
169
0
                            {
170
                                // Per the spec, if the M value is smaller than
171
                                // -1e38, it is a nodata value.
172
0
                                if (psShape->padfM[i] > -1e38)
173
0
                                {
174
0
                                    bMIsUsed = true;
175
0
                                    break;
176
0
                                }
177
0
                            }
178
0
                        }
179
180
0
                        SHPDestroyObject(psShape);
181
0
                    }
182
0
                    if (bFirstShape || bMIsUsed)
183
0
                        break;
184
0
                }
185
0
                if (!bMIsUsed)
186
0
                    eType = OGR_GT_SetModifier(eType, wkbHasZ(eType), FALSE);
187
0
            }
188
0
        }
189
0
        else
190
0
        {
191
0
            eType = m_eRequestedGeomType;
192
0
        }
193
194
0
        OGRSpatialReference *poSRSClone = poSRSIn ? poSRSIn->Clone() : nullptr;
195
0
        if (poSRSClone)
196
0
        {
197
0
            poSRSClone->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
198
0
        }
199
0
        auto poGeomFieldDefn = std::make_unique<OGRShapeGeomFieldDefn>(
200
0
            m_osFullName.c_str(), eType, bSRSSetIn, poSRSClone);
201
0
        if (!osPrjFilename.empty())
202
0
            poGeomFieldDefn->SetPrjFilename(osPrjFilename);
203
0
        if (poSRSClone)
204
0
            poSRSClone->Release();
205
0
        m_poFeatureDefn->SetGeomType(wkbNone);
206
0
        m_poFeatureDefn->AddGeomFieldDefn(std::move(poGeomFieldDefn));
207
0
    }
208
209
0
    SetDescription(m_poFeatureDefn->GetName());
210
0
    m_bRewindOnWrite = CPLTestBool(CPLGetConfigOption(
211
0
        "SHAPE_REWIND_ON_WRITE",
212
0
        m_hSHP != nullptr && m_hSHP->nShapeType != SHPT_MULTIPATCH ? "NO"
213
0
                                                                   : "YES"));
214
215
0
    m_poFeatureDefn->Seal(/* bSealFields = */ true);
216
0
}
217
218
/************************************************************************/
219
/*                           ~OGRShapeLayer()                           */
220
/************************************************************************/
221
222
OGRShapeLayer::~OGRShapeLayer()
223
224
0
{
225
0
    if (m_eNeedRepack == YES && m_bAutoRepack)
226
0
        Repack();
227
228
0
    if (m_bResizeAtClose && m_hDBF != nullptr)
229
0
    {
230
0
        ResizeDBF();
231
0
    }
232
0
    if (m_bCreateSpatialIndexAtClose && m_hSHP != nullptr)
233
0
    {
234
0
        CreateSpatialIndex(0);
235
0
    }
236
237
0
    if (m_nFeaturesRead > 0 && m_poFeatureDefn != nullptr)
238
0
    {
239
0
        CPLDebug("Shape", "%d features read on layer '%s'.",
240
0
                 static_cast<int>(m_nFeaturesRead), m_poFeatureDefn->GetName());
241
0
    }
242
243
0
    ClearMatchingFIDs();
244
0
    ClearSpatialFIDs();
245
246
0
    if (m_poFeatureDefn != nullptr)
247
0
        m_poFeatureDefn->Release();
248
249
0
    if (m_hDBF != nullptr)
250
0
        DBFClose(m_hDBF);
251
252
0
    if (m_hSHP != nullptr)
253
0
        SHPClose(m_hSHP);
254
255
0
    if (m_hQIX != nullptr)
256
0
        SHPCloseDiskTree(m_hQIX);
257
258
0
    if (m_hSBN != nullptr)
259
0
        SBNCloseDiskTree(m_hSBN);
260
0
}
261
262
/************************************************************************/
263
/*                       SetModificationDate()                          */
264
/************************************************************************/
265
266
void OGRShapeLayer::SetModificationDate(const char *pszStr)
267
0
{
268
0
    if (m_hDBF && pszStr)
269
0
    {
270
0
        int year = 0;
271
0
        int month = 0;
272
0
        int day = 0;
273
0
        if ((sscanf(pszStr, "%04d-%02d-%02d", &year, &month, &day) == 3 ||
274
0
             sscanf(pszStr, "%04d/%02d/%02d", &year, &month, &day) == 3) &&
275
0
            (year >= 1900 && year <= 1900 + 255 && month >= 1 && month <= 12 &&
276
0
             day >= 1 && day <= 31))
277
0
        {
278
0
            DBFSetLastModifiedDate(m_hDBF, year - 1900, month, day);
279
0
        }
280
0
    }
281
0
}
282
283
/************************************************************************/
284
/*                       SetWriteDBFEOFChar()                           */
285
/************************************************************************/
286
287
void OGRShapeLayer::SetWriteDBFEOFChar(bool b)
288
0
{
289
0
    if (m_hDBF)
290
0
    {
291
0
        DBFSetWriteEndOfFileChar(m_hDBF, b);
292
0
    }
293
0
}
294
295
/************************************************************************/
296
/*                          ConvertCodePage()                           */
297
/************************************************************************/
298
299
static CPLString GetEncodingFromLDIDNumber(int nLDID)
300
0
{
301
0
    int nCP = -1;  // Windows code page.
302
303
    // http://www.autopark.ru/ASBProgrammerGuide/DBFSTRUC.HTM
304
0
    switch (nLDID)
305
0
    {
306
0
        case 1:
307
0
            nCP = 437;
308
0
            break;
309
0
        case 2:
310
0
            nCP = 850;
311
0
            break;
312
0
        case 3:
313
0
            nCP = 1252;
314
0
            break;
315
0
        case 4:
316
0
            nCP = 10000;
317
0
            break;
318
0
        case 8:
319
0
            nCP = 865;
320
0
            break;
321
0
        case 10:
322
0
            nCP = 850;
323
0
            break;
324
0
        case 11:
325
0
            nCP = 437;
326
0
            break;
327
0
        case 13:
328
0
            nCP = 437;
329
0
            break;
330
0
        case 14:
331
0
            nCP = 850;
332
0
            break;
333
0
        case 15:
334
0
            nCP = 437;
335
0
            break;
336
0
        case 16:
337
0
            nCP = 850;
338
0
            break;
339
0
        case 17:
340
0
            nCP = 437;
341
0
            break;
342
0
        case 18:
343
0
            nCP = 850;
344
0
            break;
345
0
        case 19:
346
0
            nCP = 932;
347
0
            break;
348
0
        case 20:
349
0
            nCP = 850;
350
0
            break;
351
0
        case 21:
352
0
            nCP = 437;
353
0
            break;
354
0
        case 22:
355
0
            nCP = 850;
356
0
            break;
357
0
        case 23:
358
0
            nCP = 865;
359
0
            break;
360
0
        case 24:
361
0
            nCP = 437;
362
0
            break;
363
0
        case 25:
364
0
            nCP = 437;
365
0
            break;
366
0
        case 26:
367
0
            nCP = 850;
368
0
            break;
369
0
        case 27:
370
0
            nCP = 437;
371
0
            break;
372
0
        case 28:
373
0
            nCP = 863;
374
0
            break;
375
0
        case 29:
376
0
            nCP = 850;
377
0
            break;
378
0
        case 31:
379
0
            nCP = 852;
380
0
            break;
381
0
        case 34:
382
0
            nCP = 852;
383
0
            break;
384
0
        case 35:
385
0
            nCP = 852;
386
0
            break;
387
0
        case 36:
388
0
            nCP = 860;
389
0
            break;
390
0
        case 37:
391
0
            nCP = 850;
392
0
            break;
393
0
        case 38:
394
0
            nCP = 866;
395
0
            break;
396
0
        case 55:
397
0
            nCP = 850;
398
0
            break;
399
0
        case 64:
400
0
            nCP = 852;
401
0
            break;
402
0
        case 77:
403
0
            nCP = 936;
404
0
            break;
405
0
        case 78:
406
0
            nCP = 949;
407
0
            break;
408
0
        case 79:
409
0
            nCP = 950;
410
0
            break;
411
0
        case 80:
412
0
            nCP = 874;
413
0
            break;
414
0
        case 87:
415
0
            return CPL_ENC_ISO8859_1;
416
0
        case 88:
417
0
            nCP = 1252;
418
0
            break;
419
0
        case 89:
420
0
            nCP = 1252;
421
0
            break;
422
0
        case 100:
423
0
            nCP = 852;
424
0
            break;
425
0
        case 101:
426
0
            nCP = 866;
427
0
            break;
428
0
        case 102:
429
0
            nCP = 865;
430
0
            break;
431
0
        case 103:
432
0
            nCP = 861;
433
0
            break;
434
0
        case 104:
435
0
            nCP = 895;
436
0
            break;
437
0
        case 105:
438
0
            nCP = 620;
439
0
            break;
440
0
        case 106:
441
0
            nCP = 737;
442
0
            break;
443
0
        case 107:
444
0
            nCP = 857;
445
0
            break;
446
0
        case 108:
447
0
            nCP = 863;
448
0
            break;
449
0
        case 120:
450
0
            nCP = 950;
451
0
            break;
452
0
        case 121:
453
0
            nCP = 949;
454
0
            break;
455
0
        case 122:
456
0
            nCP = 936;
457
0
            break;
458
0
        case 123:
459
0
            nCP = 932;
460
0
            break;
461
0
        case 124:
462
0
            nCP = 874;
463
0
            break;
464
0
        case 134:
465
0
            nCP = 737;
466
0
            break;
467
0
        case 135:
468
0
            nCP = 852;
469
0
            break;
470
0
        case 136:
471
0
            nCP = 857;
472
0
            break;
473
0
        case 150:
474
0
            nCP = 10007;
475
0
            break;
476
0
        case 151:
477
0
            nCP = 10029;
478
0
            break;
479
0
        case 200:
480
0
            nCP = 1250;
481
0
            break;
482
0
        case 201:
483
0
            nCP = 1251;
484
0
            break;
485
0
        case 202:
486
0
            nCP = 1254;
487
0
            break;
488
0
        case 203:
489
0
            nCP = 1253;
490
0
            break;
491
0
        case 204:
492
0
            nCP = 1257;
493
0
            break;
494
0
        default:
495
0
            break;
496
0
    }
497
498
0
    if (nCP < 0)
499
0
        return CPLString();
500
0
    return CPLString().Printf("CP%d", nCP);
501
0
}
502
503
static CPLString GetEncodingFromCPG(const char *pszCPG)
504
0
{
505
    // see https://support.esri.com/en/technical-article/000013192
506
0
    CPLString m_osEncodingFromCPG;
507
0
    const int nCPG = atoi(pszCPG);
508
0
    if ((nCPG >= 437 && nCPG <= 950) || (nCPG >= 1250 && nCPG <= 1258))
509
0
    {
510
0
        m_osEncodingFromCPG.Printf("CP%d", nCPG);
511
0
    }
512
0
    else if (STARTS_WITH_CI(pszCPG, "8859"))
513
0
    {
514
0
        if (pszCPG[4] == '-')
515
0
            m_osEncodingFromCPG.Printf("ISO-8859-%s", pszCPG + 5);
516
0
        else
517
0
            m_osEncodingFromCPG.Printf("ISO-8859-%s", pszCPG + 4);
518
0
    }
519
0
    else if (STARTS_WITH_CI(pszCPG, "UTF-8") || STARTS_WITH_CI(pszCPG, "UTF8"))
520
0
        m_osEncodingFromCPG = CPL_ENC_UTF8;
521
0
    else if (STARTS_WITH_CI(pszCPG, "ANSI 1251"))
522
0
        m_osEncodingFromCPG = "CP1251";
523
0
    else
524
0
    {
525
        // Try just using the CPG value directly.  Works for stuff like Big5.
526
0
        m_osEncodingFromCPG = pszCPG;
527
0
    }
528
0
    return m_osEncodingFromCPG;
529
0
}
530
531
CPLString OGRShapeLayer::ConvertCodePage(const char *pszCodePage)
532
533
0
{
534
0
    CPLString l_m_osEncoding;
535
536
0
    if (pszCodePage == nullptr)
537
0
        return l_m_osEncoding;
538
539
0
    std::string m_osEncodingFromLDID;
540
0
    if (m_hDBF->iLanguageDriver != 0)
541
0
    {
542
0
        SetMetadataItem("LDID_VALUE", CPLSPrintf("%d", m_hDBF->iLanguageDriver),
543
0
                        "SHAPEFILE");
544
545
0
        m_osEncodingFromLDID =
546
0
            GetEncodingFromLDIDNumber(m_hDBF->iLanguageDriver);
547
0
    }
548
0
    if (!m_osEncodingFromLDID.empty())
549
0
    {
550
0
        SetMetadataItem("ENCODING_FROM_LDID", m_osEncodingFromLDID.c_str(),
551
0
                        "SHAPEFILE");
552
0
    }
553
554
0
    std::string m_osEncodingFromCPG;
555
0
    if (!STARTS_WITH_CI(pszCodePage, "LDID/"))
556
0
    {
557
0
        SetMetadataItem("CPG_VALUE", pszCodePage, "SHAPEFILE");
558
559
0
        m_osEncodingFromCPG = GetEncodingFromCPG(pszCodePage);
560
561
0
        if (!m_osEncodingFromCPG.empty())
562
0
            SetMetadataItem("ENCODING_FROM_CPG", m_osEncodingFromCPG.c_str(),
563
0
                            "SHAPEFILE");
564
565
0
        l_m_osEncoding = std::move(m_osEncodingFromCPG);
566
0
    }
567
0
    else if (!m_osEncodingFromLDID.empty())
568
0
    {
569
0
        l_m_osEncoding = std::move(m_osEncodingFromLDID);
570
0
    }
571
572
0
    return l_m_osEncoding;
573
0
}
574
575
/************************************************************************/
576
/*                            CheckForQIX()                             */
577
/************************************************************************/
578
579
bool OGRShapeLayer::CheckForQIX()
580
581
0
{
582
0
    if (m_bCheckedForQIX)
583
0
        return m_hQIX != nullptr;
584
585
0
    const std::string osQIXFilename =
586
0
        CPLResetExtensionSafe(m_osFullName.c_str(), "qix");
587
588
0
    m_hQIX = SHPOpenDiskTree(osQIXFilename.c_str(), nullptr);
589
590
0
    m_bCheckedForQIX = true;
591
592
0
    return m_hQIX != nullptr;
593
0
}
594
595
/************************************************************************/
596
/*                            CheckForSBN()                             */
597
/************************************************************************/
598
599
bool OGRShapeLayer::CheckForSBN()
600
601
0
{
602
0
    if (m_bCheckedForSBN)
603
0
        return m_hSBN != nullptr;
604
605
0
    const std::string osSBNFilename =
606
0
        CPLResetExtensionSafe(m_osFullName.c_str(), "sbn");
607
608
0
    m_hSBN = SBNOpenDiskTree(osSBNFilename.c_str(), nullptr);
609
610
0
    m_bCheckedForSBN = true;
611
612
0
    return m_hSBN != nullptr;
613
0
}
614
615
/************************************************************************/
616
/*                            ScanIndices()                             */
617
/*                                                                      */
618
/*      Utilize optional spatial and attribute indices if they are      */
619
/*      available.                                                      */
620
/************************************************************************/
621
622
bool OGRShapeLayer::ScanIndices()
623
624
0
{
625
0
    m_iMatchingFID = 0;
626
627
    /* -------------------------------------------------------------------- */
628
    /*      Utilize attribute index if appropriate.                         */
629
    /* -------------------------------------------------------------------- */
630
0
    if (m_poAttrQuery != nullptr)
631
0
    {
632
0
        CPLAssert(m_panMatchingFIDs == nullptr);
633
634
0
        InitializeIndexSupport(m_osFullName.c_str());
635
636
0
        m_panMatchingFIDs =
637
0
            m_poAttrQuery->EvaluateAgainstIndices(this, nullptr);
638
0
    }
639
640
    /* -------------------------------------------------------------------- */
641
    /*      Check for spatial index if we have a spatial query.             */
642
    /* -------------------------------------------------------------------- */
643
644
0
    if (m_poFilterGeom == nullptr || m_hSHP == nullptr)
645
0
        return true;
646
647
0
    OGREnvelope oSpatialFilterEnvelope;
648
0
    bool bTryQIXorSBN = true;
649
650
0
    m_poFilterGeom->getEnvelope(&oSpatialFilterEnvelope);
651
652
0
    OGREnvelope oLayerExtent;
653
0
    if (GetExtent(&oLayerExtent, TRUE) == OGRERR_NONE)
654
0
    {
655
0
        if (oSpatialFilterEnvelope.Contains(oLayerExtent))
656
0
        {
657
            // The spatial filter is larger than the layer extent. No use of
658
            // .qix file for now.
659
0
            return true;
660
0
        }
661
0
        else if (!oSpatialFilterEnvelope.Intersects(oLayerExtent))
662
0
        {
663
            // No intersection : no need to check for .qix or .sbn.
664
0
            bTryQIXorSBN = false;
665
666
            // Set an empty result for spatial FIDs.
667
0
            free(m_panSpatialFIDs);
668
0
            m_panSpatialFIDs = static_cast<int *>(calloc(1, sizeof(int)));
669
0
            m_nSpatialFIDCount = 0;
670
671
0
            delete m_poFilterGeomLastValid;
672
0
            m_poFilterGeomLastValid = m_poFilterGeom->clone();
673
0
        }
674
0
    }
675
676
0
    if (bTryQIXorSBN)
677
0
    {
678
0
        if (!m_bCheckedForQIX)
679
0
            CPL_IGNORE_RET_VAL(CheckForQIX());
680
0
        if (m_hQIX == nullptr && !m_bCheckedForSBN)
681
0
            CPL_IGNORE_RET_VAL(CheckForSBN());
682
0
    }
683
684
    /* -------------------------------------------------------------------- */
685
    /*      Compute spatial index if appropriate.                           */
686
    /* -------------------------------------------------------------------- */
687
0
    if (bTryQIXorSBN && (m_hQIX != nullptr || m_hSBN != nullptr) &&
688
0
        m_panSpatialFIDs == nullptr)
689
0
    {
690
0
        double adfBoundsMin[4] = {oSpatialFilterEnvelope.MinX,
691
0
                                  oSpatialFilterEnvelope.MinY, 0.0, 0.0};
692
0
        double adfBoundsMax[4] = {oSpatialFilterEnvelope.MaxX,
693
0
                                  oSpatialFilterEnvelope.MaxY, 0.0, 0.0};
694
695
0
        if (m_hQIX != nullptr)
696
0
            m_panSpatialFIDs = SHPSearchDiskTreeEx(
697
0
                m_hQIX, adfBoundsMin, adfBoundsMax, &m_nSpatialFIDCount);
698
0
        else
699
0
            m_panSpatialFIDs = SBNSearchDiskTree(
700
0
                m_hSBN, adfBoundsMin, adfBoundsMax, &m_nSpatialFIDCount);
701
702
0
        CPLDebug("SHAPE", "Used spatial index, got %d matches.",
703
0
                 m_nSpatialFIDCount);
704
705
0
        delete m_poFilterGeomLastValid;
706
0
        m_poFilterGeomLastValid = m_poFilterGeom->clone();
707
0
    }
708
709
    /* -------------------------------------------------------------------- */
710
    /*      Use spatial index if appropriate.                               */
711
    /* -------------------------------------------------------------------- */
712
0
    if (m_panSpatialFIDs != nullptr)
713
0
    {
714
        // Use resulting list as matching FID list (but reallocate and
715
        // terminate with OGRNullFID).
716
0
        if (m_panMatchingFIDs == nullptr)
717
0
        {
718
0
            m_panMatchingFIDs = static_cast<GIntBig *>(
719
0
                CPLMalloc(sizeof(GIntBig) * (m_nSpatialFIDCount + 1)));
720
0
            for (int i = 0; i < m_nSpatialFIDCount; i++)
721
0
                m_panMatchingFIDs[i] =
722
0
                    static_cast<GIntBig>(m_panSpatialFIDs[i]);
723
0
            m_panMatchingFIDs[m_nSpatialFIDCount] = OGRNullFID;
724
0
        }
725
        // Cull attribute index matches based on those in the spatial index
726
        // result set.  We assume that the attribute results are in sorted
727
        // order.
728
0
        else
729
0
        {
730
0
            int iWrite = 0;
731
0
            int iSpatial = 0;
732
733
0
            for (int iRead = 0; m_panMatchingFIDs[iRead] != OGRNullFID; iRead++)
734
0
            {
735
0
                while (iSpatial < m_nSpatialFIDCount &&
736
0
                       m_panSpatialFIDs[iSpatial] < m_panMatchingFIDs[iRead])
737
0
                    iSpatial++;
738
739
0
                if (iSpatial == m_nSpatialFIDCount)
740
0
                    continue;
741
742
0
                if (m_panSpatialFIDs[iSpatial] == m_panMatchingFIDs[iRead])
743
0
                    m_panMatchingFIDs[iWrite++] = m_panMatchingFIDs[iRead];
744
0
            }
745
0
            m_panMatchingFIDs[iWrite] = OGRNullFID;
746
0
        }
747
748
0
        if (m_nSpatialFIDCount > 100000)
749
0
        {
750
0
            ClearSpatialFIDs();
751
0
        }
752
0
    }
753
754
0
    return true;
755
0
}
756
757
/************************************************************************/
758
/*                            ResetReading()                            */
759
/************************************************************************/
760
761
void OGRShapeLayer::ResetReading()
762
763
0
{
764
0
    if (!TouchLayer())
765
0
        return;
766
767
0
    m_iMatchingFID = 0;
768
769
0
    m_iNextShapeId = 0;
770
771
0
    if (m_bHeaderDirty && m_bUpdateAccess)
772
0
        SyncToDisk();
773
774
0
    if (m_hDBF)
775
0
        VSIFClearErrL(VSI_SHP_GetVSIL(m_hDBF->fp));
776
0
}
777
778
/************************************************************************/
779
/*                        ClearMatchingFIDs()                           */
780
/************************************************************************/
781
782
void OGRShapeLayer::ClearMatchingFIDs()
783
0
{
784
    /* -------------------------------------------------------------------- */
785
    /*      Clear previous index search result, if any.                     */
786
    /* -------------------------------------------------------------------- */
787
0
    CPLFree(m_panMatchingFIDs);
788
0
    m_panMatchingFIDs = nullptr;
789
0
}
790
791
/************************************************************************/
792
/*                        ClearSpatialFIDs()                           */
793
/************************************************************************/
794
795
void OGRShapeLayer::ClearSpatialFIDs()
796
0
{
797
0
    if (m_panSpatialFIDs != nullptr)
798
0
    {
799
0
        CPLDebug("SHAPE", "Clear m_panSpatialFIDs");
800
0
        free(m_panSpatialFIDs);
801
0
    }
802
0
    m_panSpatialFIDs = nullptr;
803
0
    m_nSpatialFIDCount = 0;
804
805
0
    delete m_poFilterGeomLastValid;
806
0
    m_poFilterGeomLastValid = nullptr;
807
0
}
808
809
/************************************************************************/
810
/*                         ISetSpatialFilter()                          */
811
/************************************************************************/
812
813
OGRErr OGRShapeLayer::ISetSpatialFilter(int iGeomField,
814
                                        const OGRGeometry *poGeomIn)
815
0
{
816
0
    ClearMatchingFIDs();
817
818
0
    if (poGeomIn == nullptr)
819
0
    {
820
        // Do nothing.
821
0
    }
822
0
    else if (m_poFilterGeomLastValid != nullptr &&
823
0
             m_poFilterGeomLastValid->Equals(poGeomIn))
824
0
    {
825
        // Do nothing.
826
0
    }
827
0
    else if (m_panSpatialFIDs != nullptr)
828
0
    {
829
        // We clear the spatialFIDs only if we have a new non-NULL spatial
830
        // filter, otherwise we keep the previous result cached. This can be
831
        // useful when several SQL layers rely on the same table layer, and use
832
        // the same spatial filters. But as there is in the destructor of
833
        // OGRGenSQLResultsLayer a clearing of the spatial filter of the table
834
        // layer, we need this trick.
835
0
        ClearSpatialFIDs();
836
0
    }
837
838
0
    return OGRLayer::ISetSpatialFilter(iGeomField, poGeomIn);
839
0
}
840
841
/************************************************************************/
842
/*                         SetAttributeFilter()                         */
843
/************************************************************************/
844
845
OGRErr OGRShapeLayer::SetAttributeFilter(const char *pszAttributeFilter)
846
0
{
847
0
    ClearMatchingFIDs();
848
849
0
    return OGRLayer::SetAttributeFilter(pszAttributeFilter);
850
0
}
851
852
/************************************************************************/
853
/*                           SetNextByIndex()                           */
854
/*                                                                      */
855
/*      If we already have an FID list, we can easily reposition        */
856
/*      ourselves in it.                                                */
857
/************************************************************************/
858
859
OGRErr OGRShapeLayer::SetNextByIndex(GIntBig nIndex)
860
861
0
{
862
0
    if (!TouchLayer())
863
0
        return OGRERR_FAILURE;
864
865
0
    if (nIndex < 0 || nIndex >= m_nTotalShapeCount)
866
0
    {
867
0
        m_iNextShapeId = m_nTotalShapeCount;
868
0
        return OGRERR_NON_EXISTING_FEATURE;
869
0
    }
870
871
    // Eventually we should try to use m_panMatchingFIDs list
872
    // if available and appropriate.
873
0
    if (m_poFilterGeom != nullptr || m_poAttrQuery != nullptr)
874
0
        return OGRLayer::SetNextByIndex(nIndex);
875
876
0
    m_iNextShapeId = static_cast<int>(nIndex);
877
878
0
    return OGRERR_NONE;
879
0
}
880
881
/************************************************************************/
882
/*                             FetchShape()                             */
883
/*                                                                      */
884
/*      Take a shape id, a geometry, and a feature, and set the feature */
885
/*      if the shapeid bbox intersects the geometry.                    */
886
/************************************************************************/
887
888
OGRFeature *OGRShapeLayer::FetchShape(int iShapeId)
889
890
0
{
891
0
    OGRFeature *poFeature = nullptr;
892
893
0
    if (m_poFilterGeom != nullptr && m_hSHP != nullptr)
894
0
    {
895
0
        SHPObject *psShape = SHPReadObject(m_hSHP, iShapeId);
896
897
        // do not trust degenerate bounds on non-point geometries
898
        // or bounds on null shapes.
899
0
        if (psShape == nullptr ||
900
0
            (psShape->nSHPType != SHPT_POINT &&
901
0
             psShape->nSHPType != SHPT_POINTZ &&
902
0
             psShape->nSHPType != SHPT_POINTM &&
903
0
             (psShape->dfXMin == psShape->dfXMax ||
904
0
              psShape->dfYMin == psShape->dfYMax)) ||
905
0
            psShape->nSHPType == SHPT_NULL)
906
0
        {
907
0
            poFeature = SHPReadOGRFeature(m_hSHP, m_hDBF, m_poFeatureDefn,
908
0
                                          iShapeId, psShape, m_osEncoding,
909
0
                                          m_bHasWarnedWrongWindingOrder);
910
0
        }
911
0
        else if (m_sFilterEnvelope.MaxX < psShape->dfXMin ||
912
0
                 m_sFilterEnvelope.MaxY < psShape->dfYMin ||
913
0
                 psShape->dfXMax < m_sFilterEnvelope.MinX ||
914
0
                 psShape->dfYMax < m_sFilterEnvelope.MinY)
915
0
        {
916
0
            SHPDestroyObject(psShape);
917
0
            poFeature = nullptr;
918
0
        }
919
0
        else
920
0
        {
921
0
            poFeature = SHPReadOGRFeature(m_hSHP, m_hDBF, m_poFeatureDefn,
922
0
                                          iShapeId, psShape, m_osEncoding,
923
0
                                          m_bHasWarnedWrongWindingOrder);
924
0
        }
925
0
    }
926
0
    else
927
0
    {
928
0
        poFeature = SHPReadOGRFeature(m_hSHP, m_hDBF, m_poFeatureDefn, iShapeId,
929
0
                                      nullptr, m_osEncoding,
930
0
                                      m_bHasWarnedWrongWindingOrder);
931
0
    }
932
933
0
    return poFeature;
934
0
}
935
936
/************************************************************************/
937
/*                           GetNextFeature()                           */
938
/************************************************************************/
939
940
OGRFeature *OGRShapeLayer::GetNextFeature()
941
942
0
{
943
0
    if (!TouchLayer())
944
0
        return nullptr;
945
946
    /* -------------------------------------------------------------------- */
947
    /*      Collect a matching list if we have attribute or spatial         */
948
    /*      indices.  Only do this on the first request for a given pass    */
949
    /*      of course.                                                      */
950
    /* -------------------------------------------------------------------- */
951
0
    if ((m_poAttrQuery != nullptr || m_poFilterGeom != nullptr) &&
952
0
        m_iNextShapeId == 0 && m_panMatchingFIDs == nullptr)
953
0
    {
954
0
        ScanIndices();
955
0
    }
956
957
    /* -------------------------------------------------------------------- */
958
    /*      Loop till we find a feature matching our criteria.              */
959
    /* -------------------------------------------------------------------- */
960
0
    OGRFeature *poFeature = nullptr;
961
962
0
    while (true)
963
0
    {
964
0
        if (m_panMatchingFIDs != nullptr)
965
0
        {
966
0
            if (m_panMatchingFIDs[m_iMatchingFID] == OGRNullFID)
967
0
            {
968
0
                return nullptr;
969
0
            }
970
971
            // Check the shape object's geometry, and if it matches
972
            // any spatial filter, return it.
973
0
            poFeature =
974
0
                FetchShape(static_cast<int>(m_panMatchingFIDs[m_iMatchingFID]));
975
976
0
            m_iMatchingFID++;
977
0
        }
978
0
        else
979
0
        {
980
0
            if (m_iNextShapeId >= m_nTotalShapeCount)
981
0
            {
982
0
                return nullptr;
983
0
            }
984
985
0
            if (m_hDBF)
986
0
            {
987
0
                if (DBFIsRecordDeleted(m_hDBF, m_iNextShapeId))
988
0
                    poFeature = nullptr;
989
0
                else if (VSIFEofL(VSI_SHP_GetVSIL(m_hDBF->fp)) ||
990
0
                         VSIFErrorL(VSI_SHP_GetVSIL(m_hDBF->fp)))
991
0
                    return nullptr;  //* I/O error.
992
0
                else
993
0
                    poFeature = FetchShape(m_iNextShapeId);
994
0
            }
995
0
            else
996
0
                poFeature = FetchShape(m_iNextShapeId);
997
998
0
            m_iNextShapeId++;
999
0
        }
1000
1001
0
        if (poFeature != nullptr)
1002
0
        {
1003
0
            OGRGeometry *poGeom = poFeature->GetGeometryRef();
1004
0
            if (poGeom != nullptr)
1005
0
            {
1006
0
                poGeom->assignSpatialReference(GetSpatialRef());
1007
0
            }
1008
1009
0
            m_nFeaturesRead++;
1010
1011
0
            if ((m_poFilterGeom == nullptr || FilterGeometry(poGeom)) &&
1012
0
                (m_poAttrQuery == nullptr ||
1013
0
                 m_poAttrQuery->Evaluate(poFeature)))
1014
0
            {
1015
0
                return poFeature;
1016
0
            }
1017
1018
0
            delete poFeature;
1019
0
        }
1020
0
    }
1021
0
}
1022
1023
/************************************************************************/
1024
/*                             GetFeature()                             */
1025
/************************************************************************/
1026
1027
OGRFeature *OGRShapeLayer::GetFeature(GIntBig nFeatureId)
1028
1029
0
{
1030
0
    if (!TouchLayer() || nFeatureId > INT_MAX)
1031
0
        return nullptr;
1032
1033
0
    OGRFeature *poFeature = SHPReadOGRFeature(
1034
0
        m_hSHP, m_hDBF, m_poFeatureDefn, static_cast<int>(nFeatureId), nullptr,
1035
0
        m_osEncoding, m_bHasWarnedWrongWindingOrder);
1036
1037
0
    if (poFeature == nullptr)
1038
0
    {
1039
        // Reading shape feature failed.
1040
0
        return nullptr;
1041
0
    }
1042
1043
0
    if (poFeature->GetGeometryRef() != nullptr)
1044
0
    {
1045
0
        poFeature->GetGeometryRef()->assignSpatialReference(GetSpatialRef());
1046
0
    }
1047
1048
0
    m_nFeaturesRead++;
1049
1050
0
    return poFeature;
1051
0
}
1052
1053
/************************************************************************/
1054
/*                             StartUpdate()                            */
1055
/************************************************************************/
1056
1057
bool OGRShapeLayer::StartUpdate(const char *pszOperation)
1058
0
{
1059
0
    if (!m_poDS->UncompressIfNeeded())
1060
0
        return false;
1061
1062
0
    if (!TouchLayer())
1063
0
        return false;
1064
1065
0
    if (!m_bUpdateAccess)
1066
0
    {
1067
0
        CPLError(CE_Failure, CPLE_NotSupported,
1068
0
                 "%s : unsupported operation on a read-only datasource.",
1069
0
                 pszOperation);
1070
0
        return false;
1071
0
    }
1072
1073
0
    return true;
1074
0
}
1075
1076
/************************************************************************/
1077
/*                             ISetFeature()                             */
1078
/************************************************************************/
1079
1080
OGRErr OGRShapeLayer::ISetFeature(OGRFeature *poFeature)
1081
1082
0
{
1083
0
    if (!StartUpdate("SetFeature"))
1084
0
        return OGRERR_FAILURE;
1085
1086
0
    GIntBig nFID = poFeature->GetFID();
1087
0
    if (nFID < 0 || (m_hSHP != nullptr && nFID >= m_hSHP->nRecords) ||
1088
0
        (m_hDBF != nullptr && nFID >= m_hDBF->nRecords))
1089
0
    {
1090
0
        return OGRERR_NON_EXISTING_FEATURE;
1091
0
    }
1092
1093
0
    m_bHeaderDirty = true;
1094
0
    if (CheckForQIX() || CheckForSBN())
1095
0
        DropSpatialIndex();
1096
1097
0
    unsigned int nOffset = 0;
1098
0
    unsigned int nSize = 0;
1099
0
    bool bIsLastRecord = false;
1100
0
    if (m_hSHP != nullptr)
1101
0
    {
1102
0
        nOffset = m_hSHP->panRecOffset[nFID];
1103
0
        nSize = m_hSHP->panRecSize[nFID];
1104
0
        bIsLastRecord = (nOffset + nSize + 8 == m_hSHP->nFileSize);
1105
0
    }
1106
1107
0
    OGRErr eErr = SHPWriteOGRFeature(m_hSHP, m_hDBF, m_poFeatureDefn, poFeature,
1108
0
                                     m_osEncoding, &m_bTruncationWarningEmitted,
1109
0
                                     m_bRewindOnWrite);
1110
1111
0
    if (m_hSHP != nullptr)
1112
0
    {
1113
0
        if (bIsLastRecord)
1114
0
        {
1115
            // Optimization: we don't need repacking if this is the last
1116
            // record of the file. Just potential truncation
1117
0
            CPLAssert(nOffset == m_hSHP->panRecOffset[nFID]);
1118
0
            CPLAssert(m_hSHP->panRecOffset[nFID] + m_hSHP->panRecSize[nFID] +
1119
0
                          8 ==
1120
0
                      m_hSHP->nFileSize);
1121
0
            if (m_hSHP->panRecSize[nFID] < nSize)
1122
0
            {
1123
0
                VSIFTruncateL(VSI_SHP_GetVSIL(m_hSHP->fpSHP),
1124
0
                              m_hSHP->nFileSize);
1125
0
            }
1126
0
        }
1127
0
        else if (nOffset != m_hSHP->panRecOffset[nFID] ||
1128
0
                 nSize != m_hSHP->panRecSize[nFID])
1129
0
        {
1130
0
            m_bSHPNeedsRepack = true;
1131
0
            m_eNeedRepack = YES;
1132
0
        }
1133
0
    }
1134
1135
0
    return eErr;
1136
0
}
1137
1138
/************************************************************************/
1139
/*                           DeleteFeature()                            */
1140
/************************************************************************/
1141
1142
OGRErr OGRShapeLayer::DeleteFeature(GIntBig nFID)
1143
1144
0
{
1145
0
    if (!StartUpdate("DeleteFeature"))
1146
0
        return OGRERR_FAILURE;
1147
1148
0
    if (nFID < 0 || (m_hSHP != nullptr && nFID >= m_hSHP->nRecords) ||
1149
0
        (m_hDBF != nullptr && nFID >= m_hDBF->nRecords))
1150
0
    {
1151
0
        return OGRERR_NON_EXISTING_FEATURE;
1152
0
    }
1153
1154
0
    if (!m_hDBF)
1155
0
    {
1156
0
        CPLError(CE_Failure, CPLE_AppDefined,
1157
0
                 "Attempt to delete shape in shapefile with no .dbf file.  "
1158
0
                 "Deletion is done by marking record deleted in dbf "
1159
0
                 "and is not supported without a .dbf file.");
1160
0
        return OGRERR_FAILURE;
1161
0
    }
1162
1163
0
    if (DBFIsRecordDeleted(m_hDBF, static_cast<int>(nFID)))
1164
0
    {
1165
0
        return OGRERR_NON_EXISTING_FEATURE;
1166
0
    }
1167
1168
0
    if (!DBFMarkRecordDeleted(m_hDBF, static_cast<int>(nFID), TRUE))
1169
0
        return OGRERR_FAILURE;
1170
1171
0
    m_bHeaderDirty = true;
1172
0
    if (CheckForQIX() || CheckForSBN())
1173
0
        DropSpatialIndex();
1174
0
    m_eNeedRepack = YES;
1175
1176
0
    return OGRERR_NONE;
1177
0
}
1178
1179
/************************************************************************/
1180
/*                           ICreateFeature()                            */
1181
/************************************************************************/
1182
1183
OGRErr OGRShapeLayer::ICreateFeature(OGRFeature *poFeature)
1184
1185
0
{
1186
0
    if (!StartUpdate("CreateFeature"))
1187
0
        return OGRERR_FAILURE;
1188
1189
0
    if (m_hDBF != nullptr &&
1190
0
        !VSI_SHP_WriteMoreDataOK(m_hDBF->fp, m_hDBF->nRecordLength))
1191
0
    {
1192
0
        return OGRERR_FAILURE;
1193
0
    }
1194
1195
0
    m_bHeaderDirty = true;
1196
0
    if (CheckForQIX() || CheckForSBN())
1197
0
        DropSpatialIndex();
1198
1199
0
    poFeature->SetFID(OGRNullFID);
1200
1201
0
    if (m_nTotalShapeCount == 0 &&
1202
0
        wkbFlatten(m_eRequestedGeomType) == wkbUnknown && m_hSHP != nullptr &&
1203
0
        m_hSHP->nShapeType != SHPT_MULTIPATCH &&
1204
0
        poFeature->GetGeometryRef() != nullptr)
1205
0
    {
1206
0
        OGRGeometry *poGeom = poFeature->GetGeometryRef();
1207
0
        int nShapeType = -1;
1208
1209
0
        switch (poGeom->getGeometryType())
1210
0
        {
1211
0
            case wkbPoint:
1212
0
                nShapeType = SHPT_POINT;
1213
0
                m_eRequestedGeomType = wkbPoint;
1214
0
                break;
1215
1216
0
            case wkbPoint25D:
1217
0
                nShapeType = SHPT_POINTZ;
1218
0
                m_eRequestedGeomType = wkbPoint25D;
1219
0
                break;
1220
1221
0
            case wkbPointM:
1222
0
                nShapeType = SHPT_POINTM;
1223
0
                m_eRequestedGeomType = wkbPointM;
1224
0
                break;
1225
1226
0
            case wkbPointZM:
1227
0
                nShapeType = SHPT_POINTZ;
1228
0
                m_eRequestedGeomType = wkbPointZM;
1229
0
                break;
1230
1231
0
            case wkbMultiPoint:
1232
0
                nShapeType = SHPT_MULTIPOINT;
1233
0
                m_eRequestedGeomType = wkbMultiPoint;
1234
0
                break;
1235
1236
0
            case wkbMultiPoint25D:
1237
0
                nShapeType = SHPT_MULTIPOINTZ;
1238
0
                m_eRequestedGeomType = wkbMultiPoint25D;
1239
0
                break;
1240
1241
0
            case wkbMultiPointM:
1242
0
                nShapeType = SHPT_MULTIPOINTM;
1243
0
                m_eRequestedGeomType = wkbMultiPointM;
1244
0
                break;
1245
1246
0
            case wkbMultiPointZM:
1247
0
                nShapeType = SHPT_MULTIPOINTZ;
1248
0
                m_eRequestedGeomType = wkbMultiPointM;
1249
0
                break;
1250
1251
0
            case wkbLineString:
1252
0
            case wkbMultiLineString:
1253
0
                nShapeType = SHPT_ARC;
1254
0
                m_eRequestedGeomType = wkbLineString;
1255
0
                break;
1256
1257
0
            case wkbLineString25D:
1258
0
            case wkbMultiLineString25D:
1259
0
                nShapeType = SHPT_ARCZ;
1260
0
                m_eRequestedGeomType = wkbLineString25D;
1261
0
                break;
1262
1263
0
            case wkbLineStringM:
1264
0
            case wkbMultiLineStringM:
1265
0
                nShapeType = SHPT_ARCM;
1266
0
                m_eRequestedGeomType = wkbLineStringM;
1267
0
                break;
1268
1269
0
            case wkbLineStringZM:
1270
0
            case wkbMultiLineStringZM:
1271
0
                nShapeType = SHPT_ARCZ;
1272
0
                m_eRequestedGeomType = wkbLineStringZM;
1273
0
                break;
1274
1275
0
            case wkbPolygon:
1276
0
            case wkbMultiPolygon:
1277
0
            case wkbTriangle:
1278
0
                nShapeType = SHPT_POLYGON;
1279
0
                m_eRequestedGeomType = wkbPolygon;
1280
0
                break;
1281
1282
0
            case wkbPolygon25D:
1283
0
            case wkbMultiPolygon25D:
1284
0
            case wkbTriangleZ:
1285
0
                nShapeType = SHPT_POLYGONZ;
1286
0
                m_eRequestedGeomType = wkbPolygon25D;
1287
0
                break;
1288
1289
0
            case wkbPolygonM:
1290
0
            case wkbMultiPolygonM:
1291
0
            case wkbTriangleM:
1292
0
                nShapeType = SHPT_POLYGONM;
1293
0
                m_eRequestedGeomType = wkbPolygonM;
1294
0
                break;
1295
1296
0
            case wkbPolygonZM:
1297
0
            case wkbMultiPolygonZM:
1298
0
            case wkbTriangleZM:
1299
0
                nShapeType = SHPT_POLYGONZ;
1300
0
                m_eRequestedGeomType = wkbPolygonZM;
1301
0
                break;
1302
1303
0
            default:
1304
0
                nShapeType = -1;
1305
0
                break;
1306
0
        }
1307
1308
0
        if (wkbFlatten(poGeom->getGeometryType()) == wkbTIN ||
1309
0
            wkbFlatten(poGeom->getGeometryType()) == wkbPolyhedralSurface)
1310
0
        {
1311
0
            nShapeType = SHPT_MULTIPATCH;
1312
0
            m_eRequestedGeomType = wkbUnknown;
1313
0
        }
1314
1315
0
        if (wkbFlatten(poGeom->getGeometryType()) == wkbGeometryCollection)
1316
0
        {
1317
0
            const OGRGeometryCollection *poGC = poGeom->toGeometryCollection();
1318
0
            bool bIsMultiPatchCompatible = false;
1319
0
            for (int iGeom = 0; iGeom < poGC->getNumGeometries(); iGeom++)
1320
0
            {
1321
0
                OGRwkbGeometryType eSubGeomType =
1322
0
                    wkbFlatten(poGC->getGeometryRef(iGeom)->getGeometryType());
1323
0
                if (eSubGeomType == wkbTIN ||
1324
0
                    eSubGeomType == wkbPolyhedralSurface)
1325
0
                {
1326
0
                    bIsMultiPatchCompatible = true;
1327
0
                }
1328
0
                else if (eSubGeomType != wkbMultiPolygon)
1329
0
                {
1330
0
                    bIsMultiPatchCompatible = false;
1331
0
                    break;
1332
0
                }
1333
0
            }
1334
0
            if (bIsMultiPatchCompatible)
1335
0
            {
1336
0
                nShapeType = SHPT_MULTIPATCH;
1337
0
                m_eRequestedGeomType = wkbUnknown;
1338
0
            }
1339
0
        }
1340
1341
0
        if (nShapeType != -1)
1342
0
        {
1343
0
            whileUnsealing(m_poFeatureDefn)->SetGeomType(m_eRequestedGeomType);
1344
0
            ResetGeomType(nShapeType);
1345
0
        }
1346
0
    }
1347
1348
0
    const OGRErr eErr = SHPWriteOGRFeature(
1349
0
        m_hSHP, m_hDBF, m_poFeatureDefn, poFeature, m_osEncoding,
1350
0
        &m_bTruncationWarningEmitted, m_bRewindOnWrite);
1351
1352
0
    if (m_hSHP != nullptr)
1353
0
        m_nTotalShapeCount = m_hSHP->nRecords;
1354
0
    else if (m_hDBF != nullptr)
1355
0
        m_nTotalShapeCount = m_hDBF->nRecords;
1356
0
#ifdef DEBUG
1357
0
    else  // Silence coverity.
1358
0
        CPLError(CE_Fatal, CPLE_AssertionFailed,
1359
0
                 "Should not happen: Both m_hSHP and m_hDBF are nullptrs");
1360
0
#endif
1361
1362
0
    return eErr;
1363
0
}
1364
1365
/************************************************************************/
1366
/*               GetFeatureCountWithSpatialFilterOnly()                 */
1367
/*                                                                      */
1368
/* Specialized implementation of GetFeatureCount() when there is *only* */
1369
/* a spatial filter and no attribute filter.                            */
1370
/************************************************************************/
1371
1372
int OGRShapeLayer::GetFeatureCountWithSpatialFilterOnly()
1373
1374
0
{
1375
    /* -------------------------------------------------------------------- */
1376
    /*      Collect a matching list if we have attribute or spatial         */
1377
    /*      indices.  Only do this on the first request for a given pass    */
1378
    /*      of course.                                                      */
1379
    /* -------------------------------------------------------------------- */
1380
0
    if (m_panMatchingFIDs == nullptr)
1381
0
    {
1382
0
        ScanIndices();
1383
0
    }
1384
1385
0
    int nFeatureCount = 0;
1386
0
    int iLocalMatchingFID = 0;
1387
0
    int iLocalNextShapeId = 0;
1388
0
    bool bExpectPoints = false;
1389
1390
0
    if (wkbFlatten(m_poFeatureDefn->GetGeomType()) == wkbPoint)
1391
0
        bExpectPoints = true;
1392
1393
    /* -------------------------------------------------------------------- */
1394
    /*      Loop till we find a feature matching our criteria.              */
1395
    /* -------------------------------------------------------------------- */
1396
1397
0
    SHPObject sShape;
1398
0
    memset(&sShape, 0, sizeof(sShape));
1399
1400
0
    while (true)
1401
0
    {
1402
0
        int iShape = -1;
1403
1404
0
        if (m_panMatchingFIDs != nullptr)
1405
0
        {
1406
0
            iShape = static_cast<int>(m_panMatchingFIDs[iLocalMatchingFID]);
1407
0
            if (iShape == OGRNullFID)
1408
0
                break;
1409
0
            iLocalMatchingFID++;
1410
0
        }
1411
0
        else
1412
0
        {
1413
0
            if (iLocalNextShapeId >= m_nTotalShapeCount)
1414
0
                break;
1415
0
            iShape = iLocalNextShapeId++;
1416
1417
0
            if (m_hDBF)
1418
0
            {
1419
0
                if (DBFIsRecordDeleted(m_hDBF, iShape))
1420
0
                    continue;
1421
1422
0
                if (VSIFEofL(VSI_SHP_GetVSIL(m_hDBF->fp)) ||
1423
0
                    VSIFErrorL(VSI_SHP_GetVSIL(m_hDBF->fp)))
1424
0
                    break;
1425
0
            }
1426
0
        }
1427
1428
        // Read full shape for point layers.
1429
0
        SHPObject *psShape = nullptr;
1430
0
        if (bExpectPoints ||
1431
0
            m_hSHP->panRecOffset[iShape] == 0 /* lazy shx loading case */)
1432
0
            psShape = SHPReadObject(m_hSHP, iShape);
1433
1434
        /* --------------------------------------------------------------------
1435
         */
1436
        /*      Only read feature type and bounding box for now. In case of */
1437
        /*      inconclusive tests on bounding box only, we will read the full
1438
         */
1439
        /*      shape later. */
1440
        /* --------------------------------------------------------------------
1441
         */
1442
0
        else if (iShape >= 0 && iShape < m_hSHP->nRecords &&
1443
0
                 m_hSHP->panRecSize[iShape] > 4 + 8 * 4)
1444
0
        {
1445
0
            GByte abyBuf[4 + 8 * 4] = {};
1446
0
            if (m_hSHP->sHooks.FSeek(
1447
0
                    m_hSHP->fpSHP, m_hSHP->panRecOffset[iShape] + 8, 0) == 0 &&
1448
0
                m_hSHP->sHooks.FRead(abyBuf, sizeof(abyBuf), 1,
1449
0
                                     m_hSHP->fpSHP) == 1)
1450
0
            {
1451
0
                memcpy(&(sShape.nSHPType), abyBuf, 4);
1452
0
                CPL_LSBPTR32(&(sShape.nSHPType));
1453
0
                if (sShape.nSHPType != SHPT_NULL &&
1454
0
                    sShape.nSHPType != SHPT_POINT &&
1455
0
                    sShape.nSHPType != SHPT_POINTM &&
1456
0
                    sShape.nSHPType != SHPT_POINTZ)
1457
0
                {
1458
0
                    psShape = &sShape;
1459
0
                    memcpy(&(sShape.dfXMin), abyBuf + 4, 8);
1460
0
                    memcpy(&(sShape.dfYMin), abyBuf + 12, 8);
1461
0
                    memcpy(&(sShape.dfXMax), abyBuf + 20, 8);
1462
0
                    memcpy(&(sShape.dfYMax), abyBuf + 28, 8);
1463
0
                    CPL_LSBPTR64(&(sShape.dfXMin));
1464
0
                    CPL_LSBPTR64(&(sShape.dfYMin));
1465
0
                    CPL_LSBPTR64(&(sShape.dfXMax));
1466
0
                    CPL_LSBPTR64(&(sShape.dfYMax));
1467
0
                }
1468
0
            }
1469
0
            else
1470
0
            {
1471
0
                break;
1472
0
            }
1473
0
        }
1474
1475
0
        if (psShape != nullptr && psShape->nSHPType != SHPT_NULL)
1476
0
        {
1477
0
            OGRGeometry *poGeometry = nullptr;
1478
0
            OGREnvelope sGeomEnv;
1479
            // Test if we have a degenerated bounding box.
1480
0
            if (psShape->nSHPType != SHPT_POINT &&
1481
0
                psShape->nSHPType != SHPT_POINTZ &&
1482
0
                psShape->nSHPType != SHPT_POINTM &&
1483
0
                (psShape->dfXMin == psShape->dfXMax ||
1484
0
                 psShape->dfYMin == psShape->dfYMax))
1485
0
            {
1486
                // Need to read the full geometry to compute the envelope.
1487
0
                if (psShape == &sShape)
1488
0
                    psShape = SHPReadObject(m_hSHP, iShape);
1489
1490
0
                if (psShape)
1491
0
                {
1492
0
                    poGeometry = SHPReadOGRObject(
1493
0
                        m_hSHP, iShape, psShape, m_bHasWarnedWrongWindingOrder);
1494
0
                    if (poGeometry)
1495
0
                        poGeometry->getEnvelope(&sGeomEnv);
1496
0
                    psShape = nullptr;
1497
0
                }
1498
0
            }
1499
0
            else
1500
0
            {
1501
                // Trust the shape bounding box as the shape envelope.
1502
0
                sGeomEnv.MinX = psShape->dfXMin;
1503
0
                sGeomEnv.MinY = psShape->dfYMin;
1504
0
                sGeomEnv.MaxX = psShape->dfXMax;
1505
0
                sGeomEnv.MaxY = psShape->dfYMax;
1506
0
            }
1507
1508
            /* --------------------------------------------------------------------
1509
             */
1510
            /*      If there is no */
1511
            /*      intersection between the envelopes we are sure not to have
1512
             */
1513
            /*      any intersection. */
1514
            /* --------------------------------------------------------------------
1515
             */
1516
0
            if (sGeomEnv.MaxX < m_sFilterEnvelope.MinX ||
1517
0
                sGeomEnv.MaxY < m_sFilterEnvelope.MinY ||
1518
0
                m_sFilterEnvelope.MaxX < sGeomEnv.MinX ||
1519
0
                m_sFilterEnvelope.MaxY < sGeomEnv.MinY)
1520
0
            {
1521
0
            }
1522
            /* --------------------------------------------------------------------
1523
             */
1524
            /*      If the filter geometry is its own envelope and if the */
1525
            /*      envelope of the geometry is inside the filter geometry, */
1526
            /*      the geometry itself is inside the filter geometry */
1527
            /* --------------------------------------------------------------------
1528
             */
1529
0
            else if (m_bFilterIsEnvelope &&
1530
0
                     sGeomEnv.MinX >= m_sFilterEnvelope.MinX &&
1531
0
                     sGeomEnv.MinY >= m_sFilterEnvelope.MinY &&
1532
0
                     sGeomEnv.MaxX <= m_sFilterEnvelope.MaxX &&
1533
0
                     sGeomEnv.MaxY <= m_sFilterEnvelope.MaxY)
1534
0
            {
1535
0
                nFeatureCount++;
1536
0
            }
1537
0
            else
1538
0
            {
1539
                /* --------------------------------------------------------------------
1540
                 */
1541
                /*      Fallback to full intersect test (using GEOS) if we still
1542
                 */
1543
                /*      don't know for sure. */
1544
                /* --------------------------------------------------------------------
1545
                 */
1546
0
                if (OGRGeometryFactory::haveGEOS())
1547
0
                {
1548
                    // Read the full geometry.
1549
0
                    if (poGeometry == nullptr)
1550
0
                    {
1551
0
                        if (psShape == &sShape)
1552
0
                            psShape = SHPReadObject(m_hSHP, iShape);
1553
0
                        if (psShape)
1554
0
                        {
1555
0
                            poGeometry =
1556
0
                                SHPReadOGRObject(m_hSHP, iShape, psShape,
1557
0
                                                 m_bHasWarnedWrongWindingOrder);
1558
0
                            psShape = nullptr;
1559
0
                        }
1560
0
                    }
1561
0
                    if (poGeometry == nullptr)
1562
0
                    {
1563
0
                        nFeatureCount++;
1564
0
                    }
1565
0
                    else if (m_pPreparedFilterGeom != nullptr)
1566
0
                    {
1567
0
                        if (OGRPreparedGeometryIntersects(
1568
0
                                m_pPreparedFilterGeom,
1569
0
                                OGRGeometry::ToHandle(poGeometry)))
1570
0
                        {
1571
0
                            nFeatureCount++;
1572
0
                        }
1573
0
                    }
1574
0
                    else if (m_poFilterGeom->Intersects(poGeometry))
1575
0
                        nFeatureCount++;
1576
0
                }
1577
0
                else
1578
0
                {
1579
0
                    nFeatureCount++;
1580
0
                }
1581
0
            }
1582
1583
0
            delete poGeometry;
1584
0
        }
1585
0
        else
1586
0
        {
1587
0
            nFeatureCount++;
1588
0
        }
1589
1590
0
        if (psShape && psShape != &sShape)
1591
0
            SHPDestroyObject(psShape);
1592
0
    }
1593
1594
0
    return nFeatureCount;
1595
0
}
1596
1597
/************************************************************************/
1598
/*                          GetFeatureCount()                           */
1599
/************************************************************************/
1600
1601
GIntBig OGRShapeLayer::GetFeatureCount(int bForce)
1602
1603
0
{
1604
    // Check if the spatial filter is non-trivial.
1605
0
    bool bHasTrivialSpatialFilter = false;
1606
0
    if (m_poFilterGeom != nullptr)
1607
0
    {
1608
0
        OGREnvelope oSpatialFilterEnvelope;
1609
0
        m_poFilterGeom->getEnvelope(&oSpatialFilterEnvelope);
1610
1611
0
        OGREnvelope oLayerExtent;
1612
0
        if (GetExtent(&oLayerExtent, TRUE) == OGRERR_NONE)
1613
0
        {
1614
0
            if (oSpatialFilterEnvelope.Contains(oLayerExtent))
1615
0
            {
1616
0
                bHasTrivialSpatialFilter = true;
1617
0
            }
1618
0
            else
1619
0
            {
1620
0
                bHasTrivialSpatialFilter = false;
1621
0
            }
1622
0
        }
1623
0
        else
1624
0
        {
1625
0
            bHasTrivialSpatialFilter = false;
1626
0
        }
1627
0
    }
1628
0
    else
1629
0
    {
1630
0
        bHasTrivialSpatialFilter = true;
1631
0
    }
1632
1633
0
    if (bHasTrivialSpatialFilter && m_poAttrQuery == nullptr)
1634
0
        return m_nTotalShapeCount;
1635
1636
0
    if (!TouchLayer())
1637
0
        return 0;
1638
1639
    // Spatial filter only.
1640
0
    if (m_poAttrQuery == nullptr && m_hSHP != nullptr)
1641
0
    {
1642
0
        return GetFeatureCountWithSpatialFilterOnly();
1643
0
    }
1644
1645
    // Attribute filter only.
1646
0
    if (m_poAttrQuery != nullptr && m_poFilterGeom == nullptr)
1647
0
    {
1648
        // See if we can ignore reading geometries.
1649
0
        const bool bSaveGeometryIgnored =
1650
0
            CPL_TO_BOOL(m_poFeatureDefn->IsGeometryIgnored());
1651
0
        if (!AttributeFilterEvaluationNeedsGeometry())
1652
0
            m_poFeatureDefn->SetGeometryIgnored(TRUE);
1653
1654
0
        GIntBig nRet = OGRLayer::GetFeatureCount(bForce);
1655
1656
0
        m_poFeatureDefn->SetGeometryIgnored(bSaveGeometryIgnored);
1657
0
        return nRet;
1658
0
    }
1659
1660
0
    return OGRLayer::GetFeatureCount(bForce);
1661
0
}
1662
1663
/************************************************************************/
1664
/*                            IGetExtent()                              */
1665
/*                                                                      */
1666
/*      Fetch extent of the data currently stored in the dataset.       */
1667
/*      The bForce flag has no effect on SHP files since that value     */
1668
/*      is always in the header.                                        */
1669
/*                                                                      */
1670
/*      Returns OGRERR_NONE/OGRRERR_FAILURE.                            */
1671
/************************************************************************/
1672
1673
OGRErr OGRShapeLayer::IGetExtent(int iGeomField, OGREnvelope *psExtent,
1674
                                 bool bForce)
1675
1676
0
{
1677
0
    if (!TouchLayer())
1678
0
        return OGRERR_FAILURE;
1679
1680
0
    if (m_hSHP == nullptr)
1681
0
        return OGRERR_FAILURE;
1682
1683
0
    double adMin[4] = {0.0, 0.0, 0.0, 0.0};
1684
0
    double adMax[4] = {0.0, 0.0, 0.0, 0.0};
1685
1686
0
    SHPGetInfo(m_hSHP, nullptr, nullptr, adMin, adMax);
1687
1688
0
    psExtent->MinX = adMin[0];
1689
0
    psExtent->MinY = adMin[1];
1690
0
    psExtent->MaxX = adMax[0];
1691
0
    psExtent->MaxY = adMax[1];
1692
1693
0
    if (std::isnan(adMin[0]) || std::isnan(adMin[1]) || std::isnan(adMax[0]) ||
1694
0
        std::isnan(adMax[1]))
1695
0
    {
1696
0
        CPLDebug("SHAPE", "Invalid extent in shape header");
1697
1698
        // Disable filters to avoid infinite recursion in GetNextFeature()
1699
        // that calls ScanIndices() that call GetExtent.
1700
0
        OGRFeatureQuery *poAttrQuery = m_poAttrQuery;
1701
0
        m_poAttrQuery = nullptr;
1702
0
        OGRGeometry *poFilterGeom = m_poFilterGeom;
1703
0
        m_poFilterGeom = nullptr;
1704
1705
0
        psExtent->MinX = 0;
1706
0
        psExtent->MinY = 0;
1707
0
        psExtent->MaxX = 0;
1708
0
        psExtent->MaxY = 0;
1709
1710
0
        const OGRErr eErr = OGRLayer::IGetExtent(iGeomField, psExtent, bForce);
1711
1712
0
        m_poAttrQuery = poAttrQuery;
1713
0
        m_poFilterGeom = poFilterGeom;
1714
0
        return eErr;
1715
0
    }
1716
1717
0
    return OGRERR_NONE;
1718
0
}
1719
1720
OGRErr OGRShapeLayer::IGetExtent3D(int iGeomField, OGREnvelope3D *psExtent3D,
1721
                                   bool bForce)
1722
0
{
1723
0
    if (m_poFilterGeom || m_poAttrQuery)
1724
0
        return OGRLayer::IGetExtent3D(iGeomField, psExtent3D, bForce);
1725
1726
0
    if (!TouchLayer())
1727
0
        return OGRERR_FAILURE;
1728
1729
0
    if (m_hSHP == nullptr)
1730
0
        return OGRERR_FAILURE;
1731
1732
0
    double adMin[4] = {0.0, 0.0, 0.0, 0.0};
1733
0
    double adMax[4] = {0.0, 0.0, 0.0, 0.0};
1734
1735
0
    SHPGetInfo(m_hSHP, nullptr, nullptr, adMin, adMax);
1736
1737
0
    psExtent3D->MinX = adMin[0];
1738
0
    psExtent3D->MinY = adMin[1];
1739
0
    psExtent3D->MaxX = adMax[0];
1740
0
    psExtent3D->MaxY = adMax[1];
1741
1742
0
    if (OGR_GT_HasZ(m_poFeatureDefn->GetGeomType()))
1743
0
    {
1744
0
        psExtent3D->MinZ = adMin[2];
1745
0
        psExtent3D->MaxZ = adMax[2];
1746
0
    }
1747
0
    else
1748
0
    {
1749
0
        psExtent3D->MinZ = std::numeric_limits<double>::infinity();
1750
0
        psExtent3D->MaxZ = -std::numeric_limits<double>::infinity();
1751
0
    }
1752
1753
0
    if (std::isnan(adMin[0]) || std::isnan(adMin[1]) || std::isnan(adMax[0]) ||
1754
0
        std::isnan(adMax[1]))
1755
0
    {
1756
0
        CPLDebug("SHAPE", "Invalid extent in shape header");
1757
1758
        // Disable filters to avoid infinite recursion in GetNextFeature()
1759
        // that calls ScanIndices() that call GetExtent.
1760
0
        OGRFeatureQuery *poAttrQuery = m_poAttrQuery;
1761
0
        m_poAttrQuery = nullptr;
1762
0
        OGRGeometry *poFilterGeom = m_poFilterGeom;
1763
0
        m_poFilterGeom = nullptr;
1764
1765
0
        const OGRErr eErr =
1766
0
            OGRLayer::IGetExtent3D(iGeomField, psExtent3D, bForce);
1767
1768
0
        m_poAttrQuery = poAttrQuery;
1769
0
        m_poFilterGeom = poFilterGeom;
1770
0
        return eErr;
1771
0
    }
1772
1773
0
    return OGRERR_NONE;
1774
0
}
1775
1776
/************************************************************************/
1777
/*                           TestCapability()                           */
1778
/************************************************************************/
1779
1780
int OGRShapeLayer::TestCapability(const char *pszCap)
1781
1782
0
{
1783
0
    if (!TouchLayer())
1784
0
        return FALSE;
1785
1786
0
    if (EQUAL(pszCap, OLCRandomRead))
1787
0
        return TRUE;
1788
1789
0
    if (EQUAL(pszCap, OLCSequentialWrite) || EQUAL(pszCap, OLCRandomWrite))
1790
0
        return m_bUpdateAccess;
1791
1792
0
    if (EQUAL(pszCap, OLCFastFeatureCount))
1793
0
    {
1794
0
        if (!(m_poFilterGeom == nullptr || CheckForQIX() || CheckForSBN()))
1795
0
            return FALSE;
1796
1797
0
        if (m_poAttrQuery != nullptr)
1798
0
        {
1799
0
            InitializeIndexSupport(m_osFullName.c_str());
1800
0
            return m_poAttrQuery->CanUseIndex(this);
1801
0
        }
1802
0
        return TRUE;
1803
0
    }
1804
1805
0
    if (EQUAL(pszCap, OLCDeleteFeature))
1806
0
        return m_bUpdateAccess;
1807
1808
0
    if (EQUAL(pszCap, OLCFastSpatialFilter))
1809
0
        return CheckForQIX() || CheckForSBN();
1810
1811
0
    if (EQUAL(pszCap, OLCFastGetExtent))
1812
0
        return TRUE;
1813
1814
0
    if (EQUAL(pszCap, OLCFastGetExtent3D))
1815
0
        return m_poFilterGeom == nullptr && m_poAttrQuery == nullptr;
1816
1817
0
    if (EQUAL(pszCap, OLCFastSetNextByIndex))
1818
0
        return m_poFilterGeom == nullptr && m_poAttrQuery == nullptr;
1819
1820
0
    if (EQUAL(pszCap, OLCCreateField))
1821
0
        return m_bUpdateAccess;
1822
1823
0
    if (EQUAL(pszCap, OLCDeleteField))
1824
0
        return m_bUpdateAccess;
1825
1826
0
    if (EQUAL(pszCap, OLCReorderFields))
1827
0
        return m_bUpdateAccess;
1828
1829
0
    if (EQUAL(pszCap, OLCAlterFieldDefn) ||
1830
0
        EQUAL(pszCap, OLCAlterGeomFieldDefn))
1831
0
        return m_bUpdateAccess;
1832
1833
0
    if (EQUAL(pszCap, OLCRename))
1834
0
        return m_bUpdateAccess;
1835
1836
0
    if (EQUAL(pszCap, OLCIgnoreFields))
1837
0
        return TRUE;
1838
1839
0
    if (EQUAL(pszCap, OLCStringsAsUTF8))
1840
0
    {
1841
        // No encoding defined: we don't know.
1842
0
        if (m_osEncoding.empty())
1843
0
            return FALSE;
1844
1845
0
        if (m_hDBF == nullptr || DBFGetFieldCount(m_hDBF) == 0)
1846
0
            return TRUE;
1847
1848
        // Otherwise test that we can re-encode field names to UTF-8.
1849
0
        const int nFieldCount = DBFGetFieldCount(m_hDBF);
1850
0
        for (int i = 0; i < nFieldCount; i++)
1851
0
        {
1852
0
            char szFieldName[XBASE_FLDNAME_LEN_READ + 1] = {};
1853
0
            int nWidth = 0;
1854
0
            int nPrecision = 0;
1855
1856
0
            DBFGetFieldInfo(m_hDBF, i, szFieldName, &nWidth, &nPrecision);
1857
1858
0
            if (!CPLCanRecode(szFieldName, m_osEncoding, CPL_ENC_UTF8))
1859
0
            {
1860
0
                return FALSE;
1861
0
            }
1862
0
        }
1863
1864
0
        return TRUE;
1865
0
    }
1866
1867
0
    if (EQUAL(pszCap, OLCMeasuredGeometries))
1868
0
        return TRUE;
1869
1870
0
    if (EQUAL(pszCap, OLCZGeometries))
1871
0
        return TRUE;
1872
1873
0
    return FALSE;
1874
0
}
1875
1876
/************************************************************************/
1877
/*                            CreateField()                             */
1878
/************************************************************************/
1879
1880
OGRErr OGRShapeLayer::CreateField(const OGRFieldDefn *poFieldDefn,
1881
                                  int bApproxOK)
1882
1883
0
{
1884
0
    if (!StartUpdate("CreateField"))
1885
0
        return OGRERR_FAILURE;
1886
1887
0
    CPLAssert(nullptr != poFieldDefn);
1888
1889
0
    bool bDBFJustCreated = false;
1890
0
    if (m_hDBF == nullptr)
1891
0
    {
1892
0
        const CPLString osFilename =
1893
0
            CPLResetExtensionSafe(m_osFullName.c_str(), "dbf");
1894
0
        m_hDBF = DBFCreate(osFilename);
1895
1896
0
        if (m_hDBF == nullptr)
1897
0
        {
1898
0
            CPLError(CE_Failure, CPLE_OpenFailed,
1899
0
                     "Failed to create DBF file `%s'.", osFilename.c_str());
1900
0
            return OGRERR_FAILURE;
1901
0
        }
1902
1903
0
        bDBFJustCreated = true;
1904
0
    }
1905
1906
0
    if (m_hDBF->nHeaderLength + XBASE_FLDHDR_SZ > 65535)
1907
0
    {
1908
0
        CPLError(CE_Failure, CPLE_NotSupported,
1909
0
                 "Cannot add field %s. Header length limit reached "
1910
0
                 "(max 65535 bytes, 2046 fields).",
1911
0
                 poFieldDefn->GetNameRef());
1912
0
        return OGRERR_FAILURE;
1913
0
    }
1914
1915
0
    CPLErrorReset();
1916
1917
0
    if (m_poFeatureDefn->GetFieldCount() == 255)
1918
0
    {
1919
0
        CPLError(CE_Warning, CPLE_AppDefined,
1920
0
                 "Creating a 256th field, "
1921
0
                 "but some DBF readers might only support 255 fields");
1922
0
    }
1923
1924
    /* -------------------------------------------------------------------- */
1925
    /*      Normalize field name                                            */
1926
    /* -------------------------------------------------------------------- */
1927
0
    CPLString osFieldName;
1928
0
    if (!m_osEncoding.empty())
1929
0
    {
1930
0
        CPLClearRecodeWarningFlags();
1931
0
        CPLPushErrorHandler(CPLQuietErrorHandler);
1932
0
        CPLErr eLastErr = CPLGetLastErrorType();
1933
0
        char *const pszRecoded =
1934
0
            CPLRecode(poFieldDefn->GetNameRef(), CPL_ENC_UTF8, m_osEncoding);
1935
0
        CPLPopErrorHandler();
1936
0
        osFieldName = pszRecoded;
1937
0
        CPLFree(pszRecoded);
1938
0
        if (CPLGetLastErrorType() != eLastErr)
1939
0
        {
1940
0
            CPLError(CE_Failure, CPLE_AppDefined,
1941
0
                     "Failed to create field name '%s': cannot convert to %s",
1942
0
                     poFieldDefn->GetNameRef(), m_osEncoding.c_str());
1943
0
            return OGRERR_FAILURE;
1944
0
        }
1945
0
    }
1946
0
    else
1947
0
    {
1948
0
        osFieldName = poFieldDefn->GetNameRef();
1949
0
    }
1950
1951
0
    const int nNameSize = static_cast<int>(osFieldName.size());
1952
0
    char szNewFieldName[XBASE_FLDNAME_LEN_WRITE + 1];
1953
0
    CPLString osRadixFieldName;
1954
0
    CPLString osRadixFieldNameUC;
1955
0
    {
1956
0
        char *pszTmp = CPLScanString(
1957
0
            osFieldName, std::min(nNameSize, XBASE_FLDNAME_LEN_WRITE), TRUE,
1958
0
            TRUE);
1959
0
        strncpy(szNewFieldName, pszTmp, sizeof(szNewFieldName) - 1);
1960
0
        szNewFieldName[sizeof(szNewFieldName) - 1] = '\0';
1961
0
        osRadixFieldName = pszTmp;
1962
0
        osRadixFieldNameUC = CPLString(osRadixFieldName).toupper();
1963
0
        CPLFree(pszTmp);
1964
0
    }
1965
1966
0
    CPLString osNewFieldNameUC(szNewFieldName);
1967
0
    osNewFieldNameUC.toupper();
1968
1969
0
    if (m_oSetUCFieldName.empty())
1970
0
    {
1971
0
        for (int i = 0; i < m_poFeatureDefn->GetFieldCount(); i++)
1972
0
        {
1973
0
            m_oSetUCFieldName.insert(
1974
0
                CPLString(m_poFeatureDefn->GetFieldDefn(i)->GetNameRef())
1975
0
                    .toupper());
1976
0
        }
1977
0
    }
1978
1979
0
    bool bFoundFieldName =
1980
0
        m_oSetUCFieldName.find(osNewFieldNameUC) != m_oSetUCFieldName.end();
1981
1982
0
    if (!bApproxOK && (bFoundFieldName || !EQUAL(osFieldName, szNewFieldName)))
1983
0
    {
1984
0
        CPLError(CE_Failure, CPLE_NotSupported,
1985
0
                 "Failed to add field named '%s'", poFieldDefn->GetNameRef());
1986
1987
0
        return OGRERR_FAILURE;
1988
0
    }
1989
1990
0
    if (bFoundFieldName)
1991
0
    {
1992
0
        int nRenameNum = 1;
1993
0
        while (bFoundFieldName && nRenameNum < 10)
1994
0
        {
1995
0
            CPLsnprintf(szNewFieldName, sizeof(szNewFieldName), "%.8s_%.1d",
1996
0
                        osRadixFieldName.c_str(), nRenameNum);
1997
0
            osNewFieldNameUC.Printf("%.8s_%.1d", osRadixFieldNameUC.c_str(),
1998
0
                                    nRenameNum);
1999
0
            bFoundFieldName = m_oSetUCFieldName.find(osNewFieldNameUC) !=
2000
0
                              m_oSetUCFieldName.end();
2001
0
            nRenameNum++;
2002
0
        }
2003
2004
0
        while (bFoundFieldName && nRenameNum < 100)
2005
0
        {
2006
0
            CPLsnprintf(szNewFieldName, sizeof(szNewFieldName), "%.8s%.2d",
2007
0
                        osRadixFieldName.c_str(), nRenameNum);
2008
0
            osNewFieldNameUC.Printf("%.8s%.2d", osRadixFieldNameUC.c_str(),
2009
0
                                    nRenameNum);
2010
0
            bFoundFieldName = m_oSetUCFieldName.find(osNewFieldNameUC) !=
2011
0
                              m_oSetUCFieldName.end();
2012
0
            nRenameNum++;
2013
0
        }
2014
2015
0
        if (bFoundFieldName)
2016
0
        {
2017
            // One hundred similar field names!!?
2018
0
            CPLError(
2019
0
                CE_Failure, CPLE_NotSupported,
2020
0
                "Too many field names like '%s' when truncated to %d letters "
2021
0
                "for Shapefile format.",
2022
0
                poFieldDefn->GetNameRef(), XBASE_FLDNAME_LEN_WRITE);
2023
0
            return OGRERR_FAILURE;
2024
0
        }
2025
0
    }
2026
2027
0
    OGRFieldDefn oModFieldDefn(poFieldDefn);
2028
2029
0
    if (!EQUAL(osFieldName, szNewFieldName))
2030
0
    {
2031
0
        CPLError(CE_Warning, CPLE_NotSupported,
2032
0
                 "Normalized/laundered field name: '%s' to '%s'",
2033
0
                 poFieldDefn->GetNameRef(), szNewFieldName);
2034
2035
        // Set field name with normalized value.
2036
0
        oModFieldDefn.SetName(szNewFieldName);
2037
0
    }
2038
2039
    /* -------------------------------------------------------------------- */
2040
    /*      Add field to layer                                              */
2041
    /* -------------------------------------------------------------------- */
2042
0
    char chType = 'C';
2043
0
    int nWidth = 0;
2044
0
    int nDecimals = 0;
2045
2046
0
    switch (oModFieldDefn.GetType())
2047
0
    {
2048
0
        case OFTInteger:
2049
0
            if (oModFieldDefn.GetSubType() == OFSTBoolean)
2050
0
            {
2051
0
                chType = 'L';
2052
0
                nWidth = 1;
2053
0
            }
2054
0
            else
2055
0
            {
2056
0
                chType = 'N';
2057
0
                nWidth = oModFieldDefn.GetWidth();
2058
0
                if (nWidth == 0)
2059
0
                    nWidth = 9;
2060
0
            }
2061
0
            break;
2062
2063
0
        case OFTInteger64:
2064
0
            chType = 'N';
2065
0
            nWidth = oModFieldDefn.GetWidth();
2066
0
            if (nWidth == 0)
2067
0
                nWidth = 18;
2068
0
            break;
2069
2070
0
        case OFTReal:
2071
0
            chType = 'N';
2072
0
            nWidth = oModFieldDefn.GetWidth();
2073
0
            nDecimals = oModFieldDefn.GetPrecision();
2074
0
            if (nWidth == 0)
2075
0
            {
2076
0
                nWidth = 24;
2077
0
                nDecimals = 15;
2078
0
            }
2079
0
            break;
2080
2081
0
        case OFTString:
2082
0
            chType = 'C';
2083
0
            nWidth = oModFieldDefn.GetWidth();
2084
0
            if (nWidth == 0)
2085
0
                nWidth = 80;
2086
0
            else if (nWidth > OGR_DBF_MAX_FIELD_WIDTH)
2087
0
            {
2088
0
                CPLError(CE_Warning, CPLE_AppDefined,
2089
0
                         "Field %s of width %d truncated to %d.",
2090
0
                         szNewFieldName, nWidth, OGR_DBF_MAX_FIELD_WIDTH);
2091
0
                nWidth = OGR_DBF_MAX_FIELD_WIDTH;
2092
0
            }
2093
0
            break;
2094
2095
0
        case OFTDate:
2096
0
            chType = 'D';
2097
0
            nWidth = 8;
2098
0
            break;
2099
2100
0
        case OFTDateTime:
2101
0
            CPLError(
2102
0
                CE_Warning, CPLE_NotSupported,
2103
0
                "Field %s created as String field, though DateTime requested.",
2104
0
                szNewFieldName);
2105
0
            chType = 'C';
2106
0
            nWidth = static_cast<int>(strlen("YYYY-MM-DDTHH:MM:SS.sss+HH:MM"));
2107
0
            oModFieldDefn.SetType(OFTString);
2108
0
            break;
2109
2110
0
        default:
2111
0
            CPLError(CE_Failure, CPLE_NotSupported,
2112
0
                     "Can't create fields of type %s on shapefile layers.",
2113
0
                     OGRFieldDefn::GetFieldTypeName(oModFieldDefn.GetType()));
2114
2115
0
            return OGRERR_FAILURE;
2116
0
            break;
2117
0
    }
2118
2119
0
    oModFieldDefn.SetWidth(nWidth);
2120
0
    oModFieldDefn.SetPrecision(nDecimals);
2121
2122
    // Suppress the dummy FID field if we have created it just before.
2123
0
    if (DBFGetFieldCount(m_hDBF) == 1 && m_poFeatureDefn->GetFieldCount() == 0)
2124
0
    {
2125
0
        DBFDeleteField(m_hDBF, 0);
2126
0
    }
2127
2128
0
    const int iNewField = DBFAddNativeFieldType(m_hDBF, szNewFieldName, chType,
2129
0
                                                nWidth, nDecimals);
2130
2131
0
    if (iNewField != -1)
2132
0
    {
2133
0
        m_oSetUCFieldName.insert(std::move(osNewFieldNameUC));
2134
2135
0
        whileUnsealing(m_poFeatureDefn)->AddFieldDefn(&oModFieldDefn);
2136
2137
0
        if (bDBFJustCreated)
2138
0
        {
2139
0
            for (int i = 0; i < m_nTotalShapeCount; i++)
2140
0
            {
2141
0
                DBFWriteNULLAttribute(m_hDBF, i, 0);
2142
0
            }
2143
0
        }
2144
2145
0
        return OGRERR_NONE;
2146
0
    }
2147
2148
0
    CPLError(CE_Failure, CPLE_AppDefined,
2149
0
             "Can't create field %s in Shape DBF file, reason unknown.",
2150
0
             szNewFieldName);
2151
2152
0
    return OGRERR_FAILURE;
2153
0
}
2154
2155
/************************************************************************/
2156
/*                            DeleteField()                             */
2157
/************************************************************************/
2158
2159
OGRErr OGRShapeLayer::DeleteField(int iField)
2160
0
{
2161
0
    if (!StartUpdate("DeleteField"))
2162
0
        return OGRERR_FAILURE;
2163
2164
0
    if (iField < 0 || iField >= m_poFeatureDefn->GetFieldCount())
2165
0
    {
2166
0
        CPLError(CE_Failure, CPLE_NotSupported, "Invalid field index");
2167
0
        return OGRERR_FAILURE;
2168
0
    }
2169
2170
0
    m_oSetUCFieldName.clear();
2171
2172
0
    if (DBFDeleteField(m_hDBF, iField))
2173
0
    {
2174
0
        TruncateDBF();
2175
2176
0
        return whileUnsealing(m_poFeatureDefn)->DeleteFieldDefn(iField);
2177
0
    }
2178
2179
0
    return OGRERR_FAILURE;
2180
0
}
2181
2182
/************************************************************************/
2183
/*                           ReorderFields()                            */
2184
/************************************************************************/
2185
2186
OGRErr OGRShapeLayer::ReorderFields(int *panMap)
2187
0
{
2188
0
    if (!StartUpdate("ReorderFields"))
2189
0
        return OGRERR_FAILURE;
2190
2191
0
    if (m_poFeatureDefn->GetFieldCount() == 0)
2192
0
        return OGRERR_NONE;
2193
2194
0
    OGRErr eErr = OGRCheckPermutation(panMap, m_poFeatureDefn->GetFieldCount());
2195
0
    if (eErr != OGRERR_NONE)
2196
0
        return eErr;
2197
2198
0
    if (DBFReorderFields(m_hDBF, panMap))
2199
0
    {
2200
0
        return whileUnsealing(m_poFeatureDefn)->ReorderFieldDefns(panMap);
2201
0
    }
2202
2203
0
    return OGRERR_FAILURE;
2204
0
}
2205
2206
/************************************************************************/
2207
/*                           AlterFieldDefn()                           */
2208
/************************************************************************/
2209
2210
OGRErr OGRShapeLayer::AlterFieldDefn(int iField, OGRFieldDefn *poNewFieldDefn,
2211
                                     int nFlagsIn)
2212
0
{
2213
0
    if (!StartUpdate("AlterFieldDefn"))
2214
0
        return OGRERR_FAILURE;
2215
2216
0
    if (iField < 0 || iField >= m_poFeatureDefn->GetFieldCount())
2217
0
    {
2218
0
        CPLError(CE_Failure, CPLE_NotSupported, "Invalid field index");
2219
0
        return OGRERR_FAILURE;
2220
0
    }
2221
2222
0
    m_oSetUCFieldName.clear();
2223
2224
0
    OGRFieldDefn *poFieldDefn = m_poFeatureDefn->GetFieldDefn(iField);
2225
0
    OGRFieldType eType = poFieldDefn->GetType();
2226
2227
0
    auto oTemporaryUnsealer(poFieldDefn->GetTemporaryUnsealer());
2228
2229
    // On reading we support up to 11 characters
2230
0
    char szFieldName[XBASE_FLDNAME_LEN_READ + 1] = {};
2231
0
    int nWidth = 0;
2232
0
    int nPrecision = 0;
2233
0
    DBFGetFieldInfo(m_hDBF, iField, szFieldName, &nWidth, &nPrecision);
2234
0
    char chNativeType = DBFGetNativeFieldType(m_hDBF, iField);
2235
2236
0
    if ((nFlagsIn & ALTER_TYPE_FLAG) &&
2237
0
        poNewFieldDefn->GetType() != poFieldDefn->GetType())
2238
0
    {
2239
0
        if (poNewFieldDefn->GetType() == OFTInteger64 &&
2240
0
            poFieldDefn->GetType() == OFTInteger)
2241
0
        {
2242
0
            eType = poNewFieldDefn->GetType();
2243
0
        }
2244
0
        else if (poNewFieldDefn->GetType() != OFTString)
2245
0
        {
2246
0
            CPLError(CE_Failure, CPLE_NotSupported,
2247
0
                     "Can only convert to OFTString");
2248
0
            return OGRERR_FAILURE;
2249
0
        }
2250
0
        else
2251
0
        {
2252
0
            chNativeType = 'C';
2253
0
            eType = poNewFieldDefn->GetType();
2254
0
        }
2255
0
    }
2256
2257
0
    if (nFlagsIn & ALTER_NAME_FLAG)
2258
0
    {
2259
0
        CPLString osFieldName;
2260
0
        if (!m_osEncoding.empty())
2261
0
        {
2262
0
            CPLClearRecodeWarningFlags();
2263
0
            CPLErrorReset();
2264
0
            CPLPushErrorHandler(CPLQuietErrorHandler);
2265
0
            char *pszRecoded = CPLRecode(poNewFieldDefn->GetNameRef(),
2266
0
                                         CPL_ENC_UTF8, m_osEncoding);
2267
0
            CPLPopErrorHandler();
2268
0
            osFieldName = pszRecoded;
2269
0
            CPLFree(pszRecoded);
2270
0
            if (CPLGetLastErrorType() != 0)
2271
0
            {
2272
0
                CPLError(CE_Failure, CPLE_AppDefined,
2273
0
                         "Failed to rename field name to '%s': "
2274
0
                         "cannot convert to %s",
2275
0
                         poNewFieldDefn->GetNameRef(), m_osEncoding.c_str());
2276
0
                return OGRERR_FAILURE;
2277
0
            }
2278
0
        }
2279
0
        else
2280
0
        {
2281
0
            osFieldName = poNewFieldDefn->GetNameRef();
2282
0
        }
2283
2284
0
        strncpy(szFieldName, osFieldName, sizeof(szFieldName) - 1);
2285
0
        szFieldName[sizeof(szFieldName) - 1] = '\0';
2286
0
    }
2287
0
    if (nFlagsIn & ALTER_WIDTH_PRECISION_FLAG)
2288
0
    {
2289
0
        nWidth = poNewFieldDefn->GetWidth();
2290
0
        nPrecision = poNewFieldDefn->GetPrecision();
2291
0
    }
2292
2293
0
    if (DBFAlterFieldDefn(m_hDBF, iField, szFieldName, chNativeType, nWidth,
2294
0
                          nPrecision))
2295
0
    {
2296
0
        if (nFlagsIn & ALTER_TYPE_FLAG)
2297
0
            poFieldDefn->SetType(eType);
2298
0
        if (nFlagsIn & ALTER_NAME_FLAG)
2299
0
            poFieldDefn->SetName(poNewFieldDefn->GetNameRef());
2300
0
        if (nFlagsIn & ALTER_WIDTH_PRECISION_FLAG)
2301
0
        {
2302
0
            poFieldDefn->SetWidth(nWidth);
2303
0
            poFieldDefn->SetPrecision(nPrecision);
2304
2305
0
            TruncateDBF();
2306
0
        }
2307
0
        return OGRERR_NONE;
2308
0
    }
2309
2310
0
    return OGRERR_FAILURE;
2311
0
}
2312
2313
/************************************************************************/
2314
/*                         AlterGeomFieldDefn()                         */
2315
/************************************************************************/
2316
2317
OGRErr OGRShapeLayer::AlterGeomFieldDefn(
2318
    int iGeomField, const OGRGeomFieldDefn *poNewGeomFieldDefn, int nFlagsIn)
2319
0
{
2320
0
    if (!StartUpdate("AlterGeomFieldDefn"))
2321
0
        return OGRERR_FAILURE;
2322
2323
0
    if (iGeomField < 0 || iGeomField >= m_poFeatureDefn->GetGeomFieldCount())
2324
0
    {
2325
0
        CPLError(CE_Failure, CPLE_NotSupported, "Invalid field index");
2326
0
        return OGRERR_FAILURE;
2327
0
    }
2328
2329
0
    auto poFieldDefn = cpl::down_cast<OGRShapeGeomFieldDefn *>(
2330
0
        m_poFeatureDefn->GetGeomFieldDefn(iGeomField));
2331
0
    auto oTemporaryUnsealer(poFieldDefn->GetTemporaryUnsealer());
2332
2333
0
    if (nFlagsIn & ALTER_GEOM_FIELD_DEFN_NAME_FLAG)
2334
0
    {
2335
0
        if (strcmp(poNewGeomFieldDefn->GetNameRef(),
2336
0
                   poFieldDefn->GetNameRef()) != 0)
2337
0
        {
2338
0
            CPLError(CE_Failure, CPLE_NotSupported,
2339
0
                     "Altering the geometry field name is not supported for "
2340
0
                     "shapefiles");
2341
0
            return OGRERR_FAILURE;
2342
0
        }
2343
0
    }
2344
2345
0
    if (nFlagsIn & ALTER_GEOM_FIELD_DEFN_TYPE_FLAG)
2346
0
    {
2347
0
        if (poFieldDefn->GetType() != poNewGeomFieldDefn->GetType())
2348
0
        {
2349
0
            CPLError(CE_Failure, CPLE_NotSupported,
2350
0
                     "Altering the geometry field type is not supported for "
2351
0
                     "shapefiles");
2352
0
            return OGRERR_FAILURE;
2353
0
        }
2354
0
    }
2355
2356
0
    if (nFlagsIn & ALTER_GEOM_FIELD_DEFN_SRS_COORD_EPOCH_FLAG)
2357
0
    {
2358
0
        const auto poNewSRSRef = poNewGeomFieldDefn->GetSpatialRef();
2359
0
        if (poNewSRSRef && poNewSRSRef->GetCoordinateEpoch() > 0)
2360
0
        {
2361
0
            CPLError(CE_Failure, CPLE_NotSupported,
2362
0
                     "Setting a coordinate epoch is not supported for "
2363
0
                     "shapefiles");
2364
0
            return OGRERR_FAILURE;
2365
0
        }
2366
0
    }
2367
2368
0
    if (nFlagsIn & ALTER_GEOM_FIELD_DEFN_SRS_FLAG)
2369
0
    {
2370
0
        if (poFieldDefn->GetPrjFilename().empty())
2371
0
        {
2372
0
            poFieldDefn->SetPrjFilename(
2373
0
                CPLResetExtensionSafe(m_osFullName.c_str(), "prj").c_str());
2374
0
        }
2375
2376
0
        const auto poNewSRSRef = poNewGeomFieldDefn->GetSpatialRef();
2377
0
        if (poNewSRSRef)
2378
0
        {
2379
0
            char *pszWKT = nullptr;
2380
0
            VSILFILE *fp = nullptr;
2381
0
            const char *const apszOptions[] = {"FORMAT=WKT1_ESRI", nullptr};
2382
0
            if (poNewSRSRef->exportToWkt(&pszWKT, apszOptions) == OGRERR_NONE &&
2383
0
                (fp = VSIFOpenL(poFieldDefn->GetPrjFilename().c_str(), "wt")) !=
2384
0
                    nullptr)
2385
0
            {
2386
0
                VSIFWriteL(pszWKT, strlen(pszWKT), 1, fp);
2387
0
                VSIFCloseL(fp);
2388
0
            }
2389
0
            else
2390
0
            {
2391
0
                CPLError(CE_Failure, CPLE_FileIO, "Cannot write %s",
2392
0
                         poFieldDefn->GetPrjFilename().c_str());
2393
0
                CPLFree(pszWKT);
2394
0
                return OGRERR_FAILURE;
2395
0
            }
2396
2397
0
            CPLFree(pszWKT);
2398
2399
0
            auto poNewSRS = poNewSRSRef->Clone();
2400
0
            poFieldDefn->SetSpatialRef(poNewSRS);
2401
0
            poNewSRS->Release();
2402
0
        }
2403
0
        else
2404
0
        {
2405
0
            poFieldDefn->SetSpatialRef(nullptr);
2406
0
            VSIStatBufL sStat;
2407
0
            if (VSIStatL(poFieldDefn->GetPrjFilename().c_str(), &sStat) == 0 &&
2408
0
                VSIUnlink(poFieldDefn->GetPrjFilename().c_str()) != 0)
2409
0
            {
2410
0
                CPLError(CE_Failure, CPLE_FileIO, "Cannot delete %s",
2411
0
                         poFieldDefn->GetPrjFilename().c_str());
2412
0
                return OGRERR_FAILURE;
2413
0
            }
2414
0
        }
2415
0
        poFieldDefn->SetSRSSet();
2416
0
    }
2417
2418
0
    if (nFlagsIn & ALTER_GEOM_FIELD_DEFN_NAME_FLAG)
2419
0
        poFieldDefn->SetName(poNewGeomFieldDefn->GetNameRef());
2420
0
    if (nFlagsIn & ALTER_GEOM_FIELD_DEFN_NULLABLE_FLAG)
2421
0
        poFieldDefn->SetNullable(poNewGeomFieldDefn->IsNullable());
2422
2423
0
    return OGRERR_NONE;
2424
0
}
2425
2426
/************************************************************************/
2427
/*                           GetSpatialRef()                            */
2428
/************************************************************************/
2429
2430
const OGRSpatialReference *OGRShapeGeomFieldDefn::GetSpatialRef() const
2431
2432
0
{
2433
0
    if (m_bSRSSet)
2434
0
        return poSRS;
2435
2436
0
    m_bSRSSet = true;
2437
2438
    /* -------------------------------------------------------------------- */
2439
    /*      Is there an associated .prj file we can read?                   */
2440
    /* -------------------------------------------------------------------- */
2441
0
    std::string l_osPrjFile =
2442
0
        CPLResetExtensionSafe(m_osFullName.c_str(), "prj");
2443
2444
0
    char *apszOptions[] = {
2445
0
        const_cast<char *>("EMIT_ERROR_IF_CANNOT_OPEN_FILE=FALSE"), nullptr};
2446
0
    char **papszLines = CSLLoad2(l_osPrjFile.c_str(), -1, -1, apszOptions);
2447
0
    if (papszLines == nullptr)
2448
0
    {
2449
0
        l_osPrjFile = CPLResetExtensionSafe(m_osFullName.c_str(), "PRJ");
2450
0
        papszLines = CSLLoad2(l_osPrjFile.c_str(), -1, -1, apszOptions);
2451
0
    }
2452
2453
0
    if (papszLines != nullptr)
2454
0
    {
2455
0
        m_osPrjFile = std::move(l_osPrjFile);
2456
2457
0
        auto poSRSNonConst = new OGRSpatialReference();
2458
0
        poSRSNonConst->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
2459
        // Remove UTF-8 BOM if found
2460
        // http://lists.osgeo.org/pipermail/gdal-dev/2014-July/039527.html
2461
0
        if (static_cast<unsigned char>(papszLines[0][0]) == 0xEF &&
2462
0
            static_cast<unsigned char>(papszLines[0][1]) == 0xBB &&
2463
0
            static_cast<unsigned char>(papszLines[0][2]) == 0xBF)
2464
0
        {
2465
0
            memmove(papszLines[0], papszLines[0] + 3,
2466
0
                    strlen(papszLines[0] + 3) + 1);
2467
0
        }
2468
0
        if (STARTS_WITH_CI(papszLines[0], "GEOGCS["))
2469
0
        {
2470
            // Strip AXIS[] in GEOGCS to address use case of
2471
            // https://github.com/OSGeo/gdal/issues/8452
2472
0
            std::string osVal;
2473
0
            for (CSLConstList papszIter = papszLines; *papszIter; ++papszIter)
2474
0
                osVal += *papszIter;
2475
0
            OGR_SRSNode oSRSNode;
2476
0
            const char *pszVal = osVal.c_str();
2477
0
            if (oSRSNode.importFromWkt(&pszVal) == OGRERR_NONE)
2478
0
            {
2479
0
                oSRSNode.StripNodes("AXIS");
2480
0
                char *pszWKT = nullptr;
2481
0
                oSRSNode.exportToWkt(&pszWKT);
2482
0
                if (pszWKT)
2483
0
                {
2484
0
                    CSLDestroy(papszLines);
2485
0
                    papszLines =
2486
0
                        static_cast<char **>(CPLCalloc(2, sizeof(char *)));
2487
0
                    papszLines[0] = pszWKT;
2488
0
                }
2489
0
            }
2490
0
        }
2491
0
        if (poSRSNonConst->importFromESRI(papszLines) != OGRERR_NONE)
2492
0
        {
2493
0
            delete poSRSNonConst;
2494
0
            poSRSNonConst = nullptr;
2495
0
        }
2496
0
        CSLDestroy(papszLines);
2497
2498
0
        if (poSRSNonConst)
2499
0
        {
2500
0
            if (CPLTestBool(CPLGetConfigOption("USE_OSR_FIND_MATCHES", "YES")))
2501
0
            {
2502
0
                auto poSRSMatch = poSRSNonConst->FindBestMatch();
2503
0
                if (poSRSMatch)
2504
0
                {
2505
0
                    poSRSNonConst->Release();
2506
0
                    poSRSNonConst = poSRSMatch;
2507
0
                    poSRSNonConst->SetAxisMappingStrategy(
2508
0
                        OAMS_TRADITIONAL_GIS_ORDER);
2509
0
                }
2510
0
            }
2511
0
            else
2512
0
            {
2513
0
                poSRSNonConst->AutoIdentifyEPSG();
2514
0
            }
2515
0
            poSRS = poSRSNonConst;
2516
0
        }
2517
0
    }
2518
2519
0
    return poSRS;
2520
0
}
2521
2522
/************************************************************************/
2523
/*                           ResetGeomType()                            */
2524
/*                                                                      */
2525
/*      Modify the geometry type for this file.  Used to convert to     */
2526
/*      a different geometry type when a layer was created with a       */
2527
/*      type of unknown, and we get to the first feature to             */
2528
/*      establish the type.                                             */
2529
/************************************************************************/
2530
2531
int OGRShapeLayer::ResetGeomType(int nNewGeomType)
2532
2533
0
{
2534
0
    if (m_nTotalShapeCount > 0)
2535
0
        return FALSE;
2536
2537
0
    if (m_hSHP->fpSHX == nullptr)
2538
0
    {
2539
0
        CPLError(CE_Failure, CPLE_NotSupported,
2540
0
                 "OGRShapeLayer::ResetGeomType failed: SHX file is closed");
2541
0
        return FALSE;
2542
0
    }
2543
2544
    /* -------------------------------------------------------------------- */
2545
    /*      Update .shp header.                                             */
2546
    /* -------------------------------------------------------------------- */
2547
0
    int nStartPos = static_cast<int>(m_hSHP->sHooks.FTell(m_hSHP->fpSHP));
2548
2549
0
    char abyHeader[100] = {};
2550
0
    if (m_hSHP->sHooks.FSeek(m_hSHP->fpSHP, 0, SEEK_SET) != 0 ||
2551
0
        m_hSHP->sHooks.FRead(abyHeader, 100, 1, m_hSHP->fpSHP) != 1)
2552
0
        return FALSE;
2553
2554
0
    *(reinterpret_cast<GInt32 *>(abyHeader + 32)) = CPL_LSBWORD32(nNewGeomType);
2555
2556
0
    if (m_hSHP->sHooks.FSeek(m_hSHP->fpSHP, 0, SEEK_SET) != 0 ||
2557
0
        m_hSHP->sHooks.FWrite(abyHeader, 100, 1, m_hSHP->fpSHP) != 1)
2558
0
        return FALSE;
2559
2560
0
    if (m_hSHP->sHooks.FSeek(m_hSHP->fpSHP, nStartPos, SEEK_SET) != 0)
2561
0
        return FALSE;
2562
2563
    /* -------------------------------------------------------------------- */
2564
    /*      Update .shx header.                                             */
2565
    /* -------------------------------------------------------------------- */
2566
0
    nStartPos = static_cast<int>(m_hSHP->sHooks.FTell(m_hSHP->fpSHX));
2567
2568
0
    if (m_hSHP->sHooks.FSeek(m_hSHP->fpSHX, 0, SEEK_SET) != 0 ||
2569
0
        m_hSHP->sHooks.FRead(abyHeader, 100, 1, m_hSHP->fpSHX) != 1)
2570
0
        return FALSE;
2571
2572
0
    *(reinterpret_cast<GInt32 *>(abyHeader + 32)) = CPL_LSBWORD32(nNewGeomType);
2573
2574
0
    if (m_hSHP->sHooks.FSeek(m_hSHP->fpSHX, 0, SEEK_SET) != 0 ||
2575
0
        m_hSHP->sHooks.FWrite(abyHeader, 100, 1, m_hSHP->fpSHX) != 1)
2576
0
        return FALSE;
2577
2578
0
    if (m_hSHP->sHooks.FSeek(m_hSHP->fpSHX, nStartPos, SEEK_SET) != 0)
2579
0
        return FALSE;
2580
2581
    /* -------------------------------------------------------------------- */
2582
    /*      Update other information.                                       */
2583
    /* -------------------------------------------------------------------- */
2584
0
    m_hSHP->nShapeType = nNewGeomType;
2585
2586
0
    return TRUE;
2587
0
}
2588
2589
/************************************************************************/
2590
/*                             SyncToDisk()                             */
2591
/************************************************************************/
2592
2593
OGRErr OGRShapeLayer::SyncToDisk()
2594
2595
0
{
2596
0
    if (!TouchLayer())
2597
0
        return OGRERR_FAILURE;
2598
2599
0
    if (m_bHeaderDirty)
2600
0
    {
2601
0
        if (m_hSHP != nullptr)
2602
0
            SHPWriteHeader(m_hSHP);
2603
2604
0
        if (m_hDBF != nullptr)
2605
0
            DBFUpdateHeader(m_hDBF);
2606
2607
0
        m_bHeaderDirty = false;
2608
0
    }
2609
2610
0
    if (m_hSHP != nullptr)
2611
0
    {
2612
0
        m_hSHP->sHooks.FFlush(m_hSHP->fpSHP);
2613
0
        if (m_hSHP->fpSHX != nullptr)
2614
0
            m_hSHP->sHooks.FFlush(m_hSHP->fpSHX);
2615
0
    }
2616
2617
0
    if (m_hDBF != nullptr)
2618
0
    {
2619
0
        m_hDBF->sHooks.FFlush(m_hDBF->fp);
2620
0
    }
2621
2622
0
    if (m_eNeedRepack == YES && m_bAutoRepack)
2623
0
        Repack();
2624
2625
0
    return OGRERR_NONE;
2626
0
}
2627
2628
/************************************************************************/
2629
/*                          DropSpatialIndex()                          */
2630
/************************************************************************/
2631
2632
OGRErr OGRShapeLayer::DropSpatialIndex()
2633
2634
0
{
2635
0
    if (!StartUpdate("DropSpatialIndex"))
2636
0
        return OGRERR_FAILURE;
2637
2638
0
    if (!CheckForQIX() && !CheckForSBN())
2639
0
    {
2640
0
        CPLError(CE_Warning, CPLE_AppDefined,
2641
0
                 "Layer %s has no spatial index, DROP SPATIAL INDEX failed.",
2642
0
                 m_poFeatureDefn->GetName());
2643
0
        return OGRERR_FAILURE;
2644
0
    }
2645
2646
0
    const bool bHadQIX = m_hQIX != nullptr;
2647
2648
0
    SHPCloseDiskTree(m_hQIX);
2649
0
    m_hQIX = nullptr;
2650
0
    m_bCheckedForQIX = false;
2651
2652
0
    SBNCloseDiskTree(m_hSBN);
2653
0
    m_hSBN = nullptr;
2654
0
    m_bCheckedForSBN = false;
2655
2656
0
    if (bHadQIX)
2657
0
    {
2658
0
        const std::string osQIXFilename =
2659
0
            CPLResetExtensionSafe(m_osFullName.c_str(), "qix");
2660
0
        CPLDebug("SHAPE", "Unlinking index file %s", osQIXFilename.c_str());
2661
2662
0
        if (VSIUnlink(osQIXFilename.c_str()) != 0)
2663
0
        {
2664
0
            CPLError(CE_Failure, CPLE_AppDefined,
2665
0
                     "Failed to delete file %s.\n%s", osQIXFilename.c_str(),
2666
0
                     VSIStrerror(errno));
2667
0
            return OGRERR_FAILURE;
2668
0
        }
2669
0
    }
2670
2671
0
    if (!m_bSbnSbxDeleted)
2672
0
    {
2673
0
        const char papszExt[2][4] = {"sbn", "sbx"};
2674
0
        for (int i = 0; i < 2; i++)
2675
0
        {
2676
0
            const std::string osIndexFilename =
2677
0
                CPLResetExtensionSafe(m_osFullName.c_str(), papszExt[i]);
2678
0
            CPLDebug("SHAPE", "Trying to unlink index file %s",
2679
0
                     osIndexFilename.c_str());
2680
2681
0
            if (VSIUnlink(osIndexFilename.c_str()) != 0)
2682
0
            {
2683
0
                CPLDebug("SHAPE", "Failed to delete file %s.\n%s",
2684
0
                         osIndexFilename.c_str(), VSIStrerror(errno));
2685
0
            }
2686
0
        }
2687
0
    }
2688
0
    m_bSbnSbxDeleted = true;
2689
2690
0
    ClearSpatialFIDs();
2691
2692
0
    return OGRERR_NONE;
2693
0
}
2694
2695
/************************************************************************/
2696
/*                         CreateSpatialIndex()                         */
2697
/************************************************************************/
2698
2699
OGRErr OGRShapeLayer::CreateSpatialIndex(int nMaxDepth)
2700
2701
0
{
2702
0
    if (!StartUpdate("CreateSpatialIndex"))
2703
0
        return OGRERR_FAILURE;
2704
2705
    /* -------------------------------------------------------------------- */
2706
    /*      If we have an existing spatial index, blow it away first.       */
2707
    /* -------------------------------------------------------------------- */
2708
0
    if (CheckForQIX())
2709
0
        DropSpatialIndex();
2710
2711
0
    m_bCheckedForQIX = false;
2712
2713
    /* -------------------------------------------------------------------- */
2714
    /*      Build a quadtree structure for this file.                       */
2715
    /* -------------------------------------------------------------------- */
2716
0
    OGRShapeLayer::SyncToDisk();
2717
0
    SHPTree *psTree = SHPCreateTree(m_hSHP, 2, nMaxDepth, nullptr, nullptr);
2718
2719
0
    if (nullptr == psTree)
2720
0
    {
2721
        // TODO(mloskot): Is it better to return OGRERR_NOT_ENOUGH_MEMORY?
2722
0
        CPLDebug("SHAPE",
2723
0
                 "Index creation failure. Likely, memory allocation error.");
2724
2725
0
        return OGRERR_FAILURE;
2726
0
    }
2727
2728
    /* -------------------------------------------------------------------- */
2729
    /*      Trim unused nodes from the tree.                                */
2730
    /* -------------------------------------------------------------------- */
2731
0
    SHPTreeTrimExtraNodes(psTree);
2732
2733
    /* -------------------------------------------------------------------- */
2734
    /*      Dump tree to .qix file.                                         */
2735
    /* -------------------------------------------------------------------- */
2736
0
    char *pszQIXFilename =
2737
0
        CPLStrdup(CPLResetExtensionSafe(m_osFullName.c_str(), "qix").c_str());
2738
2739
0
    CPLDebug("SHAPE", "Creating index file %s", pszQIXFilename);
2740
2741
0
    SHPWriteTree(psTree, pszQIXFilename);
2742
0
    CPLFree(pszQIXFilename);
2743
2744
    /* -------------------------------------------------------------------- */
2745
    /*      cleanup                                                         */
2746
    /* -------------------------------------------------------------------- */
2747
0
    SHPDestroyTree(psTree);
2748
2749
0
    CPL_IGNORE_RET_VAL(CheckForQIX());
2750
2751
0
    return OGRERR_NONE;
2752
0
}
2753
2754
/************************************************************************/
2755
/*                       CheckFileDeletion()                            */
2756
/************************************************************************/
2757
2758
static void CheckFileDeletion(const CPLString &osFilename)
2759
0
{
2760
    // On Windows, sometimes the file is still triansiently reported
2761
    // as existing although being deleted, which makes QGIS things that
2762
    // an issue arose. The following helps to reduce that risk.
2763
0
    VSIStatBufL sStat;
2764
0
    if (VSIStatL(osFilename, &sStat) == 0 && VSIStatL(osFilename, &sStat) == 0)
2765
0
    {
2766
0
        CPLDebug("Shape",
2767
0
                 "File %s is still reported as existing whereas "
2768
0
                 "it should have been deleted",
2769
0
                 osFilename.c_str());
2770
0
    }
2771
0
}
2772
2773
/************************************************************************/
2774
/*                         ForceDeleteFile()                            */
2775
/************************************************************************/
2776
2777
static void ForceDeleteFile(const CPLString &osFilename)
2778
0
{
2779
0
    if (VSIUnlink(osFilename) != 0)
2780
0
    {
2781
        // In case of failure retry with a small delay (Windows specific)
2782
0
        CPLSleep(0.1);
2783
0
        if (VSIUnlink(osFilename) != 0)
2784
0
        {
2785
0
            CPLDebug("Shape", "Cannot delete %s : %s", osFilename.c_str(),
2786
0
                     VSIStrerror(errno));
2787
0
        }
2788
0
    }
2789
0
    CheckFileDeletion(osFilename);
2790
0
}
2791
2792
/************************************************************************/
2793
/*                               Repack()                               */
2794
/*                                                                      */
2795
/*      Repack the shape and dbf file, dropping deleted records.        */
2796
/*      FIDs may change.                                                */
2797
/************************************************************************/
2798
2799
OGRErr OGRShapeLayer::Repack()
2800
2801
0
{
2802
0
    if (m_eNeedRepack == NO)
2803
0
    {
2804
0
        CPLDebug("Shape", "REPACK: nothing to do. Was done previously");
2805
0
        return OGRERR_NONE;
2806
0
    }
2807
2808
0
    if (!StartUpdate("Repack"))
2809
0
        return OGRERR_FAILURE;
2810
2811
    /* -------------------------------------------------------------------- */
2812
    /*      Build a list of records to be dropped.                          */
2813
    /* -------------------------------------------------------------------- */
2814
0
    std::vector<int> anRecordsToDelete;
2815
0
    OGRErr eErr = OGRERR_NONE;
2816
2817
0
    CPLDebug("Shape", "REPACK: Checking if features have been deleted");
2818
2819
0
    if (m_hDBF != nullptr)
2820
0
    {
2821
0
        try
2822
0
        {
2823
0
            for (int iShape = 0; iShape < m_nTotalShapeCount; iShape++)
2824
0
            {
2825
0
                if (DBFIsRecordDeleted(m_hDBF, iShape))
2826
0
                {
2827
0
                    anRecordsToDelete.push_back(iShape);
2828
0
                }
2829
0
                if (VSIFEofL(VSI_SHP_GetVSIL(m_hDBF->fp)) ||
2830
0
                    VSIFErrorL(VSI_SHP_GetVSIL(m_hDBF->fp)))
2831
0
                {
2832
0
                    return OGRERR_FAILURE;  // I/O error.
2833
0
                }
2834
0
            }
2835
0
        }
2836
0
        catch (const std::bad_alloc &)
2837
0
        {
2838
0
            CPLError(CE_Failure, CPLE_OutOfMemory, "Out of memory in Repack()");
2839
0
            return OGRERR_FAILURE;
2840
0
        }
2841
0
    }
2842
2843
    /* -------------------------------------------------------------------- */
2844
    /*      If there are no records marked for deletion, we take no         */
2845
    /*      action.                                                         */
2846
    /* -------------------------------------------------------------------- */
2847
0
    if (anRecordsToDelete.empty() && !m_bSHPNeedsRepack)
2848
0
    {
2849
0
        CPLDebug("Shape", "REPACK: nothing to do");
2850
0
        return OGRERR_NONE;
2851
0
    }
2852
2853
    /* -------------------------------------------------------------------- */
2854
    /*      Find existing filenames with exact case (see #3293).            */
2855
    /* -------------------------------------------------------------------- */
2856
0
    const CPLString osDirname(CPLGetPathSafe(m_osFullName.c_str()));
2857
0
    const CPLString osBasename(CPLGetBasenameSafe(m_osFullName.c_str()));
2858
2859
0
    CPLString osDBFName;
2860
0
    CPLString osSHPName;
2861
0
    CPLString osSHXName;
2862
0
    CPLString osCPGName;
2863
0
    char **papszCandidates = VSIReadDir(osDirname);
2864
0
    int i = 0;
2865
0
    while (papszCandidates != nullptr && papszCandidates[i] != nullptr)
2866
0
    {
2867
0
        const CPLString osCandidateBasename =
2868
0
            CPLGetBasenameSafe(papszCandidates[i]);
2869
0
        const CPLString osCandidateExtension =
2870
0
            CPLGetExtensionSafe(papszCandidates[i]);
2871
#ifdef _WIN32
2872
        // On Windows, as filenames are case insensitive, a shapefile layer can
2873
        // be made of foo.shp and FOO.DBF, so use case insensitive comparison.
2874
        if (EQUAL(osCandidateBasename, osBasename))
2875
#else
2876
0
        if (osCandidateBasename.compare(osBasename) == 0)
2877
0
#endif
2878
0
        {
2879
0
            if (EQUAL(osCandidateExtension, "dbf"))
2880
0
                osDBFName =
2881
0
                    CPLFormFilenameSafe(osDirname, papszCandidates[i], nullptr);
2882
0
            else if (EQUAL(osCandidateExtension, "shp"))
2883
0
                osSHPName =
2884
0
                    CPLFormFilenameSafe(osDirname, papszCandidates[i], nullptr);
2885
0
            else if (EQUAL(osCandidateExtension, "shx"))
2886
0
                osSHXName =
2887
0
                    CPLFormFilenameSafe(osDirname, papszCandidates[i], nullptr);
2888
0
            else if (EQUAL(osCandidateExtension, "cpg"))
2889
0
                osCPGName =
2890
0
                    CPLFormFilenameSafe(osDirname, papszCandidates[i], nullptr);
2891
0
        }
2892
2893
0
        i++;
2894
0
    }
2895
0
    CSLDestroy(papszCandidates);
2896
0
    papszCandidates = nullptr;
2897
2898
0
    if (m_hDBF != nullptr && osDBFName.empty())
2899
0
    {
2900
0
        CPLError(CE_Failure, CPLE_AppDefined,
2901
0
                 "Cannot find the filename of the DBF file, but we managed to "
2902
0
                 "open it before !");
2903
        // Should not happen, really.
2904
0
        return OGRERR_FAILURE;
2905
0
    }
2906
2907
0
    if (m_hSHP != nullptr && osSHPName.empty())
2908
0
    {
2909
0
        CPLError(CE_Failure, CPLE_AppDefined,
2910
0
                 "Cannot find the filename of the SHP file, but we managed to "
2911
0
                 "open it before !");
2912
        // Should not happen, really.
2913
0
        return OGRERR_FAILURE;
2914
0
    }
2915
2916
0
    if (m_hSHP != nullptr && osSHXName.empty())
2917
0
    {
2918
0
        CPLError(CE_Failure, CPLE_AppDefined,
2919
0
                 "Cannot find the filename of the SHX file, but we managed to "
2920
0
                 "open it before !");
2921
        // Should not happen, really.
2922
0
        return OGRERR_FAILURE;
2923
0
    }
2924
2925
    /* -------------------------------------------------------------------- */
2926
    /*      Cleanup any existing spatial index.  It will become             */
2927
    /*      meaningless when the fids change.                               */
2928
    /* -------------------------------------------------------------------- */
2929
0
    if (CheckForQIX() || CheckForSBN())
2930
0
        DropSpatialIndex();
2931
2932
    /* -------------------------------------------------------------------- */
2933
    /*      Create a new dbf file, matching the old.                        */
2934
    /* -------------------------------------------------------------------- */
2935
0
    bool bMustReopenDBF = false;
2936
0
    CPLString oTempFileDBF;
2937
0
    const int nNewRecords =
2938
0
        m_nTotalShapeCount - static_cast<int>(anRecordsToDelete.size());
2939
2940
0
    if (m_hDBF != nullptr && !anRecordsToDelete.empty())
2941
0
    {
2942
0
        CPLDebug("Shape", "REPACK: repacking .dbf");
2943
0
        bMustReopenDBF = true;
2944
2945
0
        oTempFileDBF = CPLFormFilenameSafe(osDirname, osBasename, nullptr);
2946
0
        oTempFileDBF += "_packed.dbf";
2947
2948
0
        DBFHandle hNewDBF = DBFCloneEmpty(m_hDBF, oTempFileDBF);
2949
0
        if (hNewDBF == nullptr)
2950
0
        {
2951
0
            CPLError(CE_Failure, CPLE_OpenFailed,
2952
0
                     "Failed to create temp file %s.", oTempFileDBF.c_str());
2953
0
            return OGRERR_FAILURE;
2954
0
        }
2955
2956
        // Delete temporary .cpg file if existing.
2957
0
        if (!osCPGName.empty())
2958
0
        {
2959
0
            CPLString oCPGTempFile =
2960
0
                CPLFormFilenameSafe(osDirname, osBasename, nullptr);
2961
0
            oCPGTempFile += "_packed.cpg";
2962
0
            ForceDeleteFile(oCPGTempFile);
2963
0
        }
2964
2965
        /* --------------------------------------------------------------------
2966
         */
2967
        /*      Copy over all records that are not deleted. */
2968
        /* --------------------------------------------------------------------
2969
         */
2970
0
        int iDestShape = 0;
2971
0
        size_t iNextDeletedShape = 0;
2972
2973
0
        for (int iShape = 0; iShape < m_nTotalShapeCount && eErr == OGRERR_NONE;
2974
0
             iShape++)
2975
0
        {
2976
0
            if (iNextDeletedShape < anRecordsToDelete.size() &&
2977
0
                anRecordsToDelete[iNextDeletedShape] == iShape)
2978
0
            {
2979
0
                iNextDeletedShape++;
2980
0
            }
2981
0
            else
2982
0
            {
2983
0
                void *pTuple = const_cast<char *>(DBFReadTuple(m_hDBF, iShape));
2984
0
                if (pTuple == nullptr ||
2985
0
                    !DBFWriteTuple(hNewDBF, iDestShape++, pTuple))
2986
0
                {
2987
0
                    CPLError(CE_Failure, CPLE_AppDefined,
2988
0
                             "Error writing record %d in .dbf", iShape);
2989
0
                    eErr = OGRERR_FAILURE;
2990
0
                }
2991
0
            }
2992
0
        }
2993
2994
0
        DBFClose(hNewDBF);
2995
2996
0
        if (eErr != OGRERR_NONE)
2997
0
        {
2998
0
            VSIUnlink(oTempFileDBF);
2999
0
            return eErr;
3000
0
        }
3001
0
    }
3002
3003
    /* -------------------------------------------------------------------- */
3004
    /*      Now create a shapefile matching the old one.                    */
3005
    /* -------------------------------------------------------------------- */
3006
0
    bool bMustReopenSHP = m_hSHP != nullptr;
3007
0
    CPLString oTempFileSHP;
3008
0
    CPLString oTempFileSHX;
3009
3010
0
    SHPInfo sSHPInfo;
3011
0
    memset(&sSHPInfo, 0, sizeof(sSHPInfo));
3012
0
    unsigned int *panRecOffsetNew = nullptr;
3013
0
    unsigned int *panRecSizeNew = nullptr;
3014
3015
    // On Windows, use the pack-in-place approach, ie copy the content of
3016
    // the _packed files on top of the existing opened files. This avoids
3017
    // many issues with files being locked, at the expense of more I/O
3018
0
    const bool bPackInPlace =
3019
0
        CPLTestBool(CPLGetConfigOption("OGR_SHAPE_PACK_IN_PLACE",
3020
#ifdef _WIN32
3021
                                       "YES"
3022
#else
3023
0
                                       "NO"
3024
0
#endif
3025
0
                                       ));
3026
3027
0
    if (m_hSHP != nullptr)
3028
0
    {
3029
0
        CPLDebug("Shape", "REPACK: repacking .shp + .shx");
3030
3031
0
        oTempFileSHP = CPLFormFilenameSafe(osDirname, osBasename, nullptr);
3032
0
        oTempFileSHP += "_packed.shp";
3033
0
        oTempFileSHX = CPLFormFilenameSafe(osDirname, osBasename, nullptr);
3034
0
        oTempFileSHX += "_packed.shx";
3035
3036
0
        SHPHandle hNewSHP = SHPCreate(oTempFileSHP, m_hSHP->nShapeType);
3037
0
        if (hNewSHP == nullptr)
3038
0
        {
3039
0
            if (!oTempFileDBF.empty())
3040
0
                VSIUnlink(oTempFileDBF);
3041
0
            return OGRERR_FAILURE;
3042
0
        }
3043
3044
        /* --------------------------------------------------------------------
3045
         */
3046
        /*      Copy over all records that are not deleted. */
3047
        /* --------------------------------------------------------------------
3048
         */
3049
0
        size_t iNextDeletedShape = 0;
3050
3051
0
        for (int iShape = 0; iShape < m_nTotalShapeCount && eErr == OGRERR_NONE;
3052
0
             iShape++)
3053
0
        {
3054
0
            if (iNextDeletedShape < anRecordsToDelete.size() &&
3055
0
                anRecordsToDelete[iNextDeletedShape] == iShape)
3056
0
            {
3057
0
                iNextDeletedShape++;
3058
0
            }
3059
0
            else
3060
0
            {
3061
0
                SHPObject *hObject = SHPReadObject(m_hSHP, iShape);
3062
0
                if (hObject == nullptr ||
3063
0
                    SHPWriteObject(hNewSHP, -1, hObject) == -1)
3064
0
                {
3065
0
                    CPLError(CE_Failure, CPLE_AppDefined,
3066
0
                             "Error writing record %d in .shp", iShape);
3067
0
                    eErr = OGRERR_FAILURE;
3068
0
                }
3069
3070
0
                if (hObject)
3071
0
                    SHPDestroyObject(hObject);
3072
0
            }
3073
0
        }
3074
3075
0
        if (bPackInPlace)
3076
0
        {
3077
            // Backup information of the updated shape context so as to
3078
            // restore it later in the current shape context
3079
0
            memcpy(&sSHPInfo, hNewSHP, sizeof(sSHPInfo));
3080
3081
            // Use malloc like shapelib does
3082
0
            panRecOffsetNew = reinterpret_cast<unsigned int *>(
3083
0
                malloc(sizeof(unsigned int) * hNewSHP->nMaxRecords));
3084
0
            panRecSizeNew = reinterpret_cast<unsigned int *>(
3085
0
                malloc(sizeof(unsigned int) * hNewSHP->nMaxRecords));
3086
0
            if (panRecOffsetNew == nullptr || panRecSizeNew == nullptr)
3087
0
            {
3088
0
                CPLError(CE_Failure, CPLE_OutOfMemory,
3089
0
                         "Cannot allocate panRecOffsetNew/panRecSizeNew");
3090
0
                eErr = OGRERR_FAILURE;
3091
0
            }
3092
0
            else
3093
0
            {
3094
0
                memcpy(panRecOffsetNew, hNewSHP->panRecOffset,
3095
0
                       sizeof(unsigned int) * hNewSHP->nRecords);
3096
0
                memcpy(panRecSizeNew, hNewSHP->panRecSize,
3097
0
                       sizeof(unsigned int) * hNewSHP->nRecords);
3098
0
            }
3099
0
        }
3100
3101
0
        SHPClose(hNewSHP);
3102
3103
0
        if (eErr != OGRERR_NONE)
3104
0
        {
3105
0
            VSIUnlink(oTempFileSHP);
3106
0
            VSIUnlink(oTempFileSHX);
3107
0
            if (!oTempFileDBF.empty())
3108
0
                VSIUnlink(oTempFileDBF);
3109
0
            free(panRecOffsetNew);
3110
0
            free(panRecSizeNew);
3111
0
            return eErr;
3112
0
        }
3113
0
    }
3114
3115
    // We could also use pack in place for Unix but this involves extra I/O
3116
    // w.r.t to the delete and rename approach
3117
3118
0
    if (bPackInPlace)
3119
0
    {
3120
0
        if (m_hDBF != nullptr && !oTempFileDBF.empty())
3121
0
        {
3122
0
            if (!OGRShapeDataSource::CopyInPlace(VSI_SHP_GetVSIL(m_hDBF->fp),
3123
0
                                                 oTempFileDBF))
3124
0
            {
3125
0
                CPLError(
3126
0
                    CE_Failure, CPLE_FileIO,
3127
0
                    "An error occurred while copying the content of %s on top "
3128
0
                    "of %s. "
3129
0
                    "The non corrupted version is in the _packed.dbf, "
3130
0
                    "_packed.shp and _packed.shx files that you should rename "
3131
0
                    "on top of the main ones.",
3132
0
                    oTempFileDBF.c_str(), VSI_SHP_GetFilename(m_hDBF->fp));
3133
0
                free(panRecOffsetNew);
3134
0
                free(panRecSizeNew);
3135
3136
0
                DBFClose(m_hDBF);
3137
0
                m_hDBF = nullptr;
3138
0
                if (m_hSHP != nullptr)
3139
0
                {
3140
0
                    SHPClose(m_hSHP);
3141
0
                    m_hSHP = nullptr;
3142
0
                }
3143
3144
0
                return OGRERR_FAILURE;
3145
0
            }
3146
3147
            // Refresh current handle
3148
0
            m_hDBF->nRecords = nNewRecords;
3149
0
        }
3150
3151
0
        if (m_hSHP != nullptr && !oTempFileSHP.empty())
3152
0
        {
3153
0
            if (!OGRShapeDataSource::CopyInPlace(VSI_SHP_GetVSIL(m_hSHP->fpSHP),
3154
0
                                                 oTempFileSHP))
3155
0
            {
3156
0
                CPLError(
3157
0
                    CE_Failure, CPLE_FileIO,
3158
0
                    "An error occurred while copying the content of %s on top "
3159
0
                    "of %s. "
3160
0
                    "The non corrupted version is in the _packed.dbf, "
3161
0
                    "_packed.shp and _packed.shx files that you should rename "
3162
0
                    "on top of the main ones.",
3163
0
                    oTempFileSHP.c_str(), VSI_SHP_GetFilename(m_hSHP->fpSHP));
3164
0
                free(panRecOffsetNew);
3165
0
                free(panRecSizeNew);
3166
3167
0
                if (m_hDBF != nullptr)
3168
0
                {
3169
0
                    DBFClose(m_hDBF);
3170
0
                    m_hDBF = nullptr;
3171
0
                }
3172
0
                SHPClose(m_hSHP);
3173
0
                m_hSHP = nullptr;
3174
3175
0
                return OGRERR_FAILURE;
3176
0
            }
3177
0
            if (!OGRShapeDataSource::CopyInPlace(VSI_SHP_GetVSIL(m_hSHP->fpSHX),
3178
0
                                                 oTempFileSHX))
3179
0
            {
3180
0
                CPLError(
3181
0
                    CE_Failure, CPLE_FileIO,
3182
0
                    "An error occurred while copying the content of %s on top "
3183
0
                    "of %s. "
3184
0
                    "The non corrupted version is in the _packed.dbf, "
3185
0
                    "_packed.shp and _packed.shx files that you should rename "
3186
0
                    "on top of the main ones.",
3187
0
                    oTempFileSHX.c_str(), VSI_SHP_GetFilename(m_hSHP->fpSHX));
3188
0
                free(panRecOffsetNew);
3189
0
                free(panRecSizeNew);
3190
3191
0
                if (m_hDBF != nullptr)
3192
0
                {
3193
0
                    DBFClose(m_hDBF);
3194
0
                    m_hDBF = nullptr;
3195
0
                }
3196
0
                SHPClose(m_hSHP);
3197
0
                m_hSHP = nullptr;
3198
3199
0
                return OGRERR_FAILURE;
3200
0
            }
3201
3202
            // Refresh current handle
3203
0
            m_hSHP->nRecords = sSHPInfo.nRecords;
3204
0
            m_hSHP->nMaxRecords = sSHPInfo.nMaxRecords;
3205
0
            m_hSHP->nFileSize = sSHPInfo.nFileSize;
3206
0
            CPLAssert(sizeof(sSHPInfo.adBoundsMin) == 4 * sizeof(double));
3207
0
            memcpy(m_hSHP->adBoundsMin, sSHPInfo.adBoundsMin,
3208
0
                   sizeof(sSHPInfo.adBoundsMin));
3209
0
            memcpy(m_hSHP->adBoundsMax, sSHPInfo.adBoundsMax,
3210
0
                   sizeof(sSHPInfo.adBoundsMax));
3211
0
            free(m_hSHP->panRecOffset);
3212
0
            free(m_hSHP->panRecSize);
3213
0
            m_hSHP->panRecOffset = panRecOffsetNew;
3214
0
            m_hSHP->panRecSize = panRecSizeNew;
3215
0
        }
3216
0
        else
3217
0
        {
3218
            // The free() are not really necessary but CSA doesn't realize it
3219
0
            free(panRecOffsetNew);
3220
0
            free(panRecSizeNew);
3221
0
        }
3222
3223
        // Now that everything is successful, we can delete the temp files
3224
0
        if (!oTempFileDBF.empty())
3225
0
        {
3226
0
            ForceDeleteFile(oTempFileDBF);
3227
0
        }
3228
0
        if (!oTempFileSHP.empty())
3229
0
        {
3230
0
            ForceDeleteFile(oTempFileSHP);
3231
0
            ForceDeleteFile(oTempFileSHX);
3232
0
        }
3233
0
    }
3234
0
    else
3235
0
    {
3236
        /* --------------------------------------------------------------------
3237
         */
3238
        /*      Cleanup the old .dbf, .shp, .shx and rename the new ones. */
3239
        /* --------------------------------------------------------------------
3240
         */
3241
0
        if (!oTempFileDBF.empty())
3242
0
        {
3243
0
            DBFClose(m_hDBF);
3244
0
            m_hDBF = nullptr;
3245
3246
0
            if (VSIUnlink(osDBFName) != 0)
3247
0
            {
3248
0
                CPLError(CE_Failure, CPLE_FileIO,
3249
0
                         "Failed to delete old DBF file: %s",
3250
0
                         VSIStrerror(errno));
3251
3252
0
                m_hDBF =
3253
0
                    m_poDS->DS_DBFOpen(osDBFName, m_bUpdateAccess ? "r+" : "r");
3254
3255
0
                VSIUnlink(oTempFileDBF);
3256
3257
0
                return OGRERR_FAILURE;
3258
0
            }
3259
3260
0
            if (VSIRename(oTempFileDBF, osDBFName) != 0)
3261
0
            {
3262
0
                CPLError(CE_Failure, CPLE_FileIO,
3263
0
                         "Can not rename new DBF file: %s", VSIStrerror(errno));
3264
0
                return OGRERR_FAILURE;
3265
0
            }
3266
3267
0
            CheckFileDeletion(oTempFileDBF);
3268
0
        }
3269
3270
0
        if (!oTempFileSHP.empty())
3271
0
        {
3272
0
            SHPClose(m_hSHP);
3273
0
            m_hSHP = nullptr;
3274
3275
0
            if (VSIUnlink(osSHPName) != 0)
3276
0
            {
3277
0
                CPLError(CE_Failure, CPLE_FileIO,
3278
0
                         "Can not delete old SHP file: %s", VSIStrerror(errno));
3279
0
                return OGRERR_FAILURE;
3280
0
            }
3281
3282
0
            if (VSIUnlink(osSHXName) != 0)
3283
0
            {
3284
0
                CPLError(CE_Failure, CPLE_FileIO,
3285
0
                         "Can not delete old SHX file: %s", VSIStrerror(errno));
3286
0
                return OGRERR_FAILURE;
3287
0
            }
3288
3289
0
            if (VSIRename(oTempFileSHP, osSHPName) != 0)
3290
0
            {
3291
0
                CPLError(CE_Failure, CPLE_FileIO,
3292
0
                         "Can not rename new SHP file: %s", VSIStrerror(errno));
3293
0
                return OGRERR_FAILURE;
3294
0
            }
3295
3296
0
            if (VSIRename(oTempFileSHX, osSHXName) != 0)
3297
0
            {
3298
0
                CPLError(CE_Failure, CPLE_FileIO,
3299
0
                         "Can not rename new SHX file: %s", VSIStrerror(errno));
3300
0
                return OGRERR_FAILURE;
3301
0
            }
3302
3303
0
            CheckFileDeletion(oTempFileSHP);
3304
0
            CheckFileDeletion(oTempFileSHX);
3305
0
        }
3306
3307
        /* --------------------------------------------------------------------
3308
         */
3309
        /*      Reopen the shapefile */
3310
        /*                                                                      */
3311
        /* We do not need to reimplement OGRShapeDataSource::OpenFile() here */
3312
        /* with the fully featured error checking. */
3313
        /* If all operations above succeeded, then all necessary files are */
3314
        /* in the right place and accessible. */
3315
        /* --------------------------------------------------------------------
3316
         */
3317
3318
0
        const char *const pszAccess = m_bUpdateAccess ? "r+" : "r";
3319
3320
0
        if (bMustReopenSHP)
3321
0
            m_hSHP = m_poDS->DS_SHPOpen(osSHPName, pszAccess);
3322
0
        if (bMustReopenDBF)
3323
0
            m_hDBF = m_poDS->DS_DBFOpen(osDBFName, pszAccess);
3324
3325
0
        if ((bMustReopenSHP && nullptr == m_hSHP) ||
3326
0
            (bMustReopenDBF && nullptr == m_hDBF))
3327
0
            return OGRERR_FAILURE;
3328
0
    }
3329
3330
    /* -------------------------------------------------------------------- */
3331
    /*      Update total shape count.                                       */
3332
    /* -------------------------------------------------------------------- */
3333
0
    if (m_hDBF != nullptr)
3334
0
        m_nTotalShapeCount = m_hDBF->nRecords;
3335
0
    m_bSHPNeedsRepack = false;
3336
0
    m_eNeedRepack = NO;
3337
3338
0
    return OGRERR_NONE;
3339
0
}
3340
3341
/************************************************************************/
3342
/*                               ResizeDBF()                            */
3343
/*                                                                      */
3344
/*      Autoshrink columns of the DBF file to their minimum             */
3345
/*      size, according to the existing data.                           */
3346
/************************************************************************/
3347
3348
OGRErr OGRShapeLayer::ResizeDBF()
3349
3350
0
{
3351
0
    if (!StartUpdate("ResizeDBF"))
3352
0
        return OGRERR_FAILURE;
3353
3354
0
    if (m_hDBF == nullptr)
3355
0
    {
3356
0
        CPLError(
3357
0
            CE_Failure, CPLE_NotSupported,
3358
0
            "Attempt to RESIZE a shapefile with no .dbf file not supported.");
3359
0
        return OGRERR_FAILURE;
3360
0
    }
3361
3362
    /* Look which columns must be examined */
3363
0
    int *panColMap = static_cast<int *>(
3364
0
        CPLMalloc(m_poFeatureDefn->GetFieldCount() * sizeof(int)));
3365
0
    int *panBestWidth = static_cast<int *>(
3366
0
        CPLMalloc(m_poFeatureDefn->GetFieldCount() * sizeof(int)));
3367
0
    int nStringCols = 0;
3368
0
    for (int i = 0; i < m_poFeatureDefn->GetFieldCount(); i++)
3369
0
    {
3370
0
        if (m_poFeatureDefn->GetFieldDefn(i)->GetType() == OFTString ||
3371
0
            m_poFeatureDefn->GetFieldDefn(i)->GetType() == OFTInteger ||
3372
0
            m_poFeatureDefn->GetFieldDefn(i)->GetType() == OFTInteger64)
3373
0
        {
3374
0
            panColMap[nStringCols] = i;
3375
0
            panBestWidth[nStringCols] = 1;
3376
0
            nStringCols++;
3377
0
        }
3378
0
    }
3379
3380
0
    if (nStringCols == 0)
3381
0
    {
3382
        // Nothing to do.
3383
0
        CPLFree(panColMap);
3384
0
        CPLFree(panBestWidth);
3385
0
        return OGRERR_NONE;
3386
0
    }
3387
3388
0
    CPLDebug("SHAPE", "Computing optimal column size...");
3389
3390
0
    bool bAlreadyWarned = false;
3391
0
    for (int i = 0; i < m_hDBF->nRecords; i++)
3392
0
    {
3393
0
        if (!DBFIsRecordDeleted(m_hDBF, i))
3394
0
        {
3395
0
            for (int j = 0; j < nStringCols; j++)
3396
0
            {
3397
0
                if (DBFIsAttributeNULL(m_hDBF, i, panColMap[j]))
3398
0
                    continue;
3399
3400
0
                const char *pszVal =
3401
0
                    DBFReadStringAttribute(m_hDBF, i, panColMap[j]);
3402
0
                const int nLen = static_cast<int>(strlen(pszVal));
3403
0
                if (nLen > panBestWidth[j])
3404
0
                    panBestWidth[j] = nLen;
3405
0
            }
3406
0
        }
3407
0
        else if (!bAlreadyWarned)
3408
0
        {
3409
0
            bAlreadyWarned = true;
3410
0
            CPLDebug(
3411
0
                "SHAPE",
3412
0
                "DBF file would also need a REPACK due to deleted records");
3413
0
        }
3414
0
    }
3415
3416
0
    for (int j = 0; j < nStringCols; j++)
3417
0
    {
3418
0
        const int iField = panColMap[j];
3419
0
        OGRFieldDefn *const poFieldDefn = m_poFeatureDefn->GetFieldDefn(iField);
3420
3421
0
        const char chNativeType = DBFGetNativeFieldType(m_hDBF, iField);
3422
0
        char szFieldName[XBASE_FLDNAME_LEN_READ + 1] = {};
3423
0
        int nOriWidth = 0;
3424
0
        int nPrecision = 0;
3425
0
        DBFGetFieldInfo(m_hDBF, iField, szFieldName, &nOriWidth, &nPrecision);
3426
3427
0
        if (panBestWidth[j] < nOriWidth)
3428
0
        {
3429
0
            CPLDebug("SHAPE",
3430
0
                     "Shrinking field %d (%s) from %d to %d characters", iField,
3431
0
                     poFieldDefn->GetNameRef(), nOriWidth, panBestWidth[j]);
3432
3433
0
            if (!DBFAlterFieldDefn(m_hDBF, iField, szFieldName, chNativeType,
3434
0
                                   panBestWidth[j], nPrecision))
3435
0
            {
3436
0
                CPLError(
3437
0
                    CE_Failure, CPLE_AppDefined,
3438
0
                    "Shrinking field %d (%s) from %d to %d characters failed",
3439
0
                    iField, poFieldDefn->GetNameRef(), nOriWidth,
3440
0
                    panBestWidth[j]);
3441
3442
0
                CPLFree(panColMap);
3443
0
                CPLFree(panBestWidth);
3444
3445
0
                return OGRERR_FAILURE;
3446
0
            }
3447
0
            else
3448
0
            {
3449
0
                whileUnsealing(poFieldDefn)->SetWidth(panBestWidth[j]);
3450
0
            }
3451
0
        }
3452
0
    }
3453
3454
0
    TruncateDBF();
3455
3456
0
    CPLFree(panColMap);
3457
0
    CPLFree(panBestWidth);
3458
3459
0
    return OGRERR_NONE;
3460
0
}
3461
3462
/************************************************************************/
3463
/*                          TruncateDBF()                               */
3464
/************************************************************************/
3465
3466
void OGRShapeLayer::TruncateDBF()
3467
0
{
3468
0
    if (m_hDBF == nullptr)
3469
0
        return;
3470
3471
0
    m_hDBF->sHooks.FSeek(m_hDBF->fp, 0, SEEK_END);
3472
0
    vsi_l_offset nOldSize = m_hDBF->sHooks.FTell(m_hDBF->fp);
3473
0
    vsi_l_offset nNewSize =
3474
0
        m_hDBF->nRecordLength * static_cast<SAOffset>(m_hDBF->nRecords) +
3475
0
        m_hDBF->nHeaderLength;
3476
0
    if (m_hDBF->bWriteEndOfFileChar)
3477
0
        nNewSize++;
3478
0
    if (nNewSize < nOldSize)
3479
0
    {
3480
0
        CPLDebug("SHAPE",
3481
0
                 "Truncating DBF file from " CPL_FRMT_GUIB " to " CPL_FRMT_GUIB
3482
0
                 " bytes",
3483
0
                 nOldSize, nNewSize);
3484
0
        VSIFTruncateL(VSI_SHP_GetVSIL(m_hDBF->fp), nNewSize);
3485
0
    }
3486
0
    m_hDBF->sHooks.FSeek(m_hDBF->fp, 0, SEEK_SET);
3487
0
}
3488
3489
/************************************************************************/
3490
/*                        RecomputeExtent()                             */
3491
/*                                                                      */
3492
/*      Force recomputation of the extent of the .SHP file              */
3493
/************************************************************************/
3494
3495
OGRErr OGRShapeLayer::RecomputeExtent()
3496
0
{
3497
0
    if (!StartUpdate("RecomputeExtent"))
3498
0
        return OGRERR_FAILURE;
3499
3500
0
    if (m_hSHP == nullptr)
3501
0
    {
3502
0
        CPLError(CE_Failure, CPLE_AppDefined,
3503
0
                 "The RECOMPUTE EXTENT operation is not permitted on a layer "
3504
0
                 "without .SHP file.");
3505
0
        return OGRERR_FAILURE;
3506
0
    }
3507
3508
0
    double adBoundsMin[4] = {0.0, 0.0, 0.0, 0.0};
3509
0
    double adBoundsMax[4] = {0.0, 0.0, 0.0, 0.0};
3510
3511
0
    bool bHasBeenInit = false;
3512
3513
0
    for (int iShape = 0; iShape < m_nTotalShapeCount; iShape++)
3514
0
    {
3515
0
        if (m_hDBF == nullptr || !DBFIsRecordDeleted(m_hDBF, iShape))
3516
0
        {
3517
0
            SHPObject *psObject = SHPReadObject(m_hSHP, iShape);
3518
0
            if (psObject != nullptr && psObject->nSHPType != SHPT_NULL &&
3519
0
                psObject->nVertices != 0)
3520
0
            {
3521
0
                if (!bHasBeenInit)
3522
0
                {
3523
0
                    bHasBeenInit = true;
3524
0
                    adBoundsMin[0] = psObject->padfX[0];
3525
0
                    adBoundsMax[0] = psObject->padfX[0];
3526
0
                    adBoundsMin[1] = psObject->padfY[0];
3527
0
                    adBoundsMax[1] = psObject->padfY[0];
3528
0
                    if (psObject->padfZ)
3529
0
                    {
3530
0
                        adBoundsMin[2] = psObject->padfZ[0];
3531
0
                        adBoundsMax[2] = psObject->padfZ[0];
3532
0
                    }
3533
0
                    if (psObject->padfM)
3534
0
                    {
3535
0
                        adBoundsMin[3] = psObject->padfM[0];
3536
0
                        adBoundsMax[3] = psObject->padfM[0];
3537
0
                    }
3538
0
                }
3539
3540
0
                for (int i = 0; i < psObject->nVertices; i++)
3541
0
                {
3542
0
                    adBoundsMin[0] =
3543
0
                        std::min(adBoundsMin[0], psObject->padfX[i]);
3544
0
                    adBoundsMin[1] =
3545
0
                        std::min(adBoundsMin[1], psObject->padfY[i]);
3546
0
                    adBoundsMax[0] =
3547
0
                        std::max(adBoundsMax[0], psObject->padfX[i]);
3548
0
                    adBoundsMax[1] =
3549
0
                        std::max(adBoundsMax[1], psObject->padfY[i]);
3550
0
                    if (psObject->padfZ)
3551
0
                    {
3552
0
                        adBoundsMin[2] =
3553
0
                            std::min(adBoundsMin[2], psObject->padfZ[i]);
3554
0
                        adBoundsMax[2] =
3555
0
                            std::max(adBoundsMax[2], psObject->padfZ[i]);
3556
0
                    }
3557
0
                    if (psObject->padfM)
3558
0
                    {
3559
0
                        adBoundsMax[3] =
3560
0
                            std::max(adBoundsMax[3], psObject->padfM[i]);
3561
0
                        adBoundsMin[3] =
3562
0
                            std::min(adBoundsMin[3], psObject->padfM[i]);
3563
0
                    }
3564
0
                }
3565
0
            }
3566
0
            SHPDestroyObject(psObject);
3567
0
        }
3568
0
    }
3569
3570
0
    if (memcmp(m_hSHP->adBoundsMin, adBoundsMin, 4 * sizeof(double)) != 0 ||
3571
0
        memcmp(m_hSHP->adBoundsMax, adBoundsMax, 4 * sizeof(double)) != 0)
3572
0
    {
3573
0
        m_bHeaderDirty = true;
3574
0
        m_hSHP->bUpdated = TRUE;
3575
0
        memcpy(m_hSHP->adBoundsMin, adBoundsMin, 4 * sizeof(double));
3576
0
        memcpy(m_hSHP->adBoundsMax, adBoundsMax, 4 * sizeof(double));
3577
0
    }
3578
3579
0
    return OGRERR_NONE;
3580
0
}
3581
3582
/************************************************************************/
3583
/*                              TouchLayer()                            */
3584
/************************************************************************/
3585
3586
bool OGRShapeLayer::TouchLayer()
3587
0
{
3588
0
    m_poDS->SetLastUsedLayer(this);
3589
3590
0
    if (m_eFileDescriptorsState == FD_OPENED)
3591
0
        return true;
3592
0
    if (m_eFileDescriptorsState == FD_CANNOT_REOPEN)
3593
0
        return false;
3594
3595
0
    return ReopenFileDescriptors();
3596
0
}
3597
3598
/************************************************************************/
3599
/*                        ReopenFileDescriptors()                       */
3600
/************************************************************************/
3601
3602
bool OGRShapeLayer::ReopenFileDescriptors()
3603
0
{
3604
0
    CPLDebug("SHAPE", "ReopenFileDescriptors(%s)", m_osFullName.c_str());
3605
3606
0
    const bool bRealUpdateAccess =
3607
0
        m_bUpdateAccess &&
3608
0
        (!m_poDS->IsZip() || !m_poDS->GetTemporaryUnzipDir().empty());
3609
3610
0
    if (m_bHSHPWasNonNULL)
3611
0
    {
3612
0
        m_hSHP = m_poDS->DS_SHPOpen(m_osFullName.c_str(),
3613
0
                                    bRealUpdateAccess ? "r+" : "r");
3614
3615
0
        if (m_hSHP == nullptr)
3616
0
        {
3617
0
            m_eFileDescriptorsState = FD_CANNOT_REOPEN;
3618
0
            return false;
3619
0
        }
3620
0
    }
3621
3622
0
    if (m_bHDBFWasNonNULL)
3623
0
    {
3624
0
        m_hDBF = m_poDS->DS_DBFOpen(m_osFullName.c_str(),
3625
0
                                    bRealUpdateAccess ? "r+" : "r");
3626
3627
0
        if (m_hDBF == nullptr)
3628
0
        {
3629
0
            CPLError(
3630
0
                CE_Failure, CPLE_OpenFailed, "Cannot reopen %s",
3631
0
                CPLResetExtensionSafe(m_osFullName.c_str(), "dbf").c_str());
3632
0
            m_eFileDescriptorsState = FD_CANNOT_REOPEN;
3633
0
            return false;
3634
0
        }
3635
0
    }
3636
3637
0
    m_eFileDescriptorsState = FD_OPENED;
3638
3639
0
    return true;
3640
0
}
3641
3642
/************************************************************************/
3643
/*                        CloseUnderlyingLayer()                        */
3644
/************************************************************************/
3645
3646
void OGRShapeLayer::CloseUnderlyingLayer()
3647
0
{
3648
0
    CPLDebug("SHAPE", "CloseUnderlyingLayer(%s)", m_osFullName.c_str());
3649
3650
0
    if (m_hDBF != nullptr)
3651
0
        DBFClose(m_hDBF);
3652
0
    m_hDBF = nullptr;
3653
3654
0
    if (m_hSHP != nullptr)
3655
0
        SHPClose(m_hSHP);
3656
0
    m_hSHP = nullptr;
3657
3658
    // We close QIX and reset the check flag, so that CheckForQIX()
3659
    // will retry opening it if necessary when the layer is active again.
3660
0
    if (m_hQIX != nullptr)
3661
0
        SHPCloseDiskTree(m_hQIX);
3662
0
    m_hQIX = nullptr;
3663
0
    m_bCheckedForQIX = false;
3664
3665
0
    if (m_hSBN != nullptr)
3666
0
        SBNCloseDiskTree(m_hSBN);
3667
0
    m_hSBN = nullptr;
3668
0
    m_bCheckedForSBN = false;
3669
3670
0
    m_eFileDescriptorsState = FD_CLOSED;
3671
0
}
3672
3673
/************************************************************************/
3674
/*                            AddToFileList()                           */
3675
/************************************************************************/
3676
3677
void OGRShapeLayer::AddToFileList(CPLStringList &oFileList)
3678
0
{
3679
0
    if (!TouchLayer())
3680
0
        return;
3681
3682
0
    if (m_hSHP)
3683
0
    {
3684
0
        const char *pszSHPFilename = VSI_SHP_GetFilename(m_hSHP->fpSHP);
3685
0
        oFileList.AddStringDirectly(VSIGetCanonicalFilename(pszSHPFilename));
3686
0
        const std::string osSHPExt = CPLGetExtensionSafe(pszSHPFilename);
3687
0
        const std::string osSHXFilename = CPLResetExtensionSafe(
3688
0
            pszSHPFilename, (osSHPExt[0] == 's') ? "shx" : "SHX");
3689
0
        oFileList.AddStringDirectly(
3690
0
            VSIGetCanonicalFilename(osSHXFilename.c_str()));
3691
0
    }
3692
3693
0
    if (m_hDBF)
3694
0
    {
3695
0
        const char *pszDBFFilename = VSI_SHP_GetFilename(m_hDBF->fp);
3696
0
        oFileList.AddStringDirectly(VSIGetCanonicalFilename(pszDBFFilename));
3697
0
        if (m_hDBF->pszCodePage != nullptr && m_hDBF->iLanguageDriver == 0)
3698
0
        {
3699
0
            const std::string osDBFExt = CPLGetExtensionSafe(pszDBFFilename);
3700
0
            const std::string osCPGFilename = CPLResetExtensionSafe(
3701
0
                pszDBFFilename, (osDBFExt[0] == 'd') ? "cpg" : "CPG");
3702
0
            oFileList.AddStringDirectly(
3703
0
                VSIGetCanonicalFilename(osCPGFilename.c_str()));
3704
0
        }
3705
0
    }
3706
3707
0
    if (m_hSHP)
3708
0
    {
3709
0
        if (GetSpatialRef() != nullptr)
3710
0
        {
3711
0
            OGRShapeGeomFieldDefn *poGeomFieldDefn =
3712
0
                cpl::down_cast<OGRShapeGeomFieldDefn *>(
3713
0
                    GetLayerDefn()->GetGeomFieldDefn(0));
3714
0
            oFileList.AddStringDirectly(
3715
0
                VSIGetCanonicalFilename(poGeomFieldDefn->GetPrjFilename()));
3716
0
        }
3717
0
        if (CheckForQIX())
3718
0
        {
3719
0
            const std::string osQIXFilename =
3720
0
                CPLResetExtensionSafe(m_osFullName.c_str(), "qix");
3721
0
            oFileList.AddStringDirectly(
3722
0
                VSIGetCanonicalFilename(osQIXFilename.c_str()));
3723
0
        }
3724
0
        else if (CheckForSBN())
3725
0
        {
3726
0
            const std::string osSBNFilename =
3727
0
                CPLResetExtensionSafe(m_osFullName.c_str(), "sbn");
3728
0
            oFileList.AddStringDirectly(
3729
0
                VSIGetCanonicalFilename(osSBNFilename.c_str()));
3730
0
            const std::string osSBXFilename =
3731
0
                CPLResetExtensionSafe(m_osFullName.c_str(), "sbx");
3732
0
            oFileList.AddStringDirectly(
3733
0
                VSIGetCanonicalFilename(osSBXFilename.c_str()));
3734
0
        }
3735
0
    }
3736
0
}
3737
3738
/************************************************************************/
3739
/*                   UpdateFollowingDeOrRecompression()                 */
3740
/************************************************************************/
3741
3742
void OGRShapeLayer::UpdateFollowingDeOrRecompression()
3743
0
{
3744
0
    CPLAssert(m_poDS->IsZip());
3745
0
    CPLString osDSDir = m_poDS->GetTemporaryUnzipDir();
3746
0
    if (osDSDir.empty())
3747
0
        osDSDir = m_poDS->GetVSIZipPrefixeDir();
3748
3749
0
    if (GetSpatialRef() != nullptr)
3750
0
    {
3751
0
        OGRShapeGeomFieldDefn *poGeomFieldDefn =
3752
0
            cpl::down_cast<OGRShapeGeomFieldDefn *>(
3753
0
                GetLayerDefn()->GetGeomFieldDefn(0));
3754
0
        poGeomFieldDefn->SetPrjFilename(
3755
0
            CPLFormFilenameSafe(
3756
0
                osDSDir.c_str(),
3757
0
                CPLGetFilename(poGeomFieldDefn->GetPrjFilename().c_str()),
3758
0
                nullptr)
3759
0
                .c_str());
3760
0
    }
3761
3762
0
    m_osFullName = CPLFormFilenameSafe(
3763
0
        osDSDir, CPLGetFilename(m_osFullName.c_str()), nullptr);
3764
0
    CloseUnderlyingLayer();
3765
0
}
3766
3767
/************************************************************************/
3768
/*                           Rename()                                   */
3769
/************************************************************************/
3770
3771
OGRErr OGRShapeLayer::Rename(const char *pszNewName)
3772
0
{
3773
0
    if (!TestCapability(OLCRename))
3774
0
        return OGRERR_FAILURE;
3775
3776
0
    if (CPLLaunderForFilenameSafe(pszNewName, nullptr) != pszNewName)
3777
0
    {
3778
0
        CPLError(CE_Failure, CPLE_AppDefined,
3779
0
                 "Illegal characters in '%s' to form a valid filename",
3780
0
                 pszNewName);
3781
0
        return OGRERR_FAILURE;
3782
0
    }
3783
3784
0
    if (m_poDS->GetLayerByName(pszNewName) != nullptr)
3785
0
    {
3786
0
        CPLError(CE_Failure, CPLE_AppDefined, "Layer %s already exists",
3787
0
                 pszNewName);
3788
0
        return OGRERR_FAILURE;
3789
0
    }
3790
3791
0
    if (!m_poDS->UncompressIfNeeded())
3792
0
        return OGRERR_FAILURE;
3793
3794
0
    CPLStringList oFileList;
3795
0
    AddToFileList(oFileList);
3796
3797
0
    const std::string osDirname = CPLGetPathSafe(m_osFullName.c_str());
3798
0
    for (int i = 0; i < oFileList.size(); ++i)
3799
0
    {
3800
0
        const std::string osRenamedFile =
3801
0
            CPLFormFilenameSafe(osDirname.c_str(), pszNewName,
3802
0
                                CPLGetExtensionSafe(oFileList[i]).c_str());
3803
0
        VSIStatBufL sStat;
3804
0
        if (VSIStatL(osRenamedFile.c_str(), &sStat) == 0)
3805
0
        {
3806
0
            CPLError(CE_Failure, CPLE_AppDefined, "File %s already exists",
3807
0
                     osRenamedFile.c_str());
3808
0
            return OGRERR_FAILURE;
3809
0
        }
3810
0
    }
3811
3812
0
    CloseUnderlyingLayer();
3813
3814
0
    for (int i = 0; i < oFileList.size(); ++i)
3815
0
    {
3816
0
        const std::string osRenamedFile =
3817
0
            CPLFormFilenameSafe(osDirname.c_str(), pszNewName,
3818
0
                                CPLGetExtensionSafe(oFileList[i]).c_str());
3819
0
        if (VSIRename(oFileList[i], osRenamedFile.c_str()) != 0)
3820
0
        {
3821
0
            CPLError(CE_Failure, CPLE_AppDefined, "Cannot rename %s to %s",
3822
0
                     oFileList[i], osRenamedFile.c_str());
3823
0
            return OGRERR_FAILURE;
3824
0
        }
3825
0
    }
3826
3827
0
    if (GetSpatialRef() != nullptr)
3828
0
    {
3829
0
        OGRShapeGeomFieldDefn *poGeomFieldDefn =
3830
0
            cpl::down_cast<OGRShapeGeomFieldDefn *>(
3831
0
                GetLayerDefn()->GetGeomFieldDefn(0));
3832
0
        poGeomFieldDefn->SetPrjFilename(
3833
0
            CPLFormFilenameSafe(
3834
0
                osDirname.c_str(), pszNewName,
3835
0
                CPLGetExtensionSafe(poGeomFieldDefn->GetPrjFilename().c_str())
3836
0
                    .c_str())
3837
0
                .c_str());
3838
0
    }
3839
3840
0
    m_osFullName =
3841
0
        CPLFormFilenameSafe(osDirname.c_str(), pszNewName,
3842
0
                            CPLGetExtensionSafe(m_osFullName.c_str()).c_str());
3843
3844
0
    if (!ReopenFileDescriptors())
3845
0
        return OGRERR_FAILURE;
3846
3847
0
    SetDescription(pszNewName);
3848
0
    whileUnsealing(m_poFeatureDefn)->SetName(pszNewName);
3849
3850
0
    return OGRERR_NONE;
3851
0
}
3852
3853
/************************************************************************/
3854
/*                          GetDataset()                                */
3855
/************************************************************************/
3856
3857
GDALDataset *OGRShapeLayer::GetDataset()
3858
0
{
3859
0
    return m_poDS;
3860
0
}
3861
3862
/************************************************************************/
3863
/*                        GetNextArrowArray()                           */
3864
/************************************************************************/
3865
3866
// Specialized implementation restricted to situations where only retrieving
3867
// of FID values is asked (without filters)
3868
// In other cases, fall back to generic implementation.
3869
int OGRShapeLayer::GetNextArrowArray(struct ArrowArrayStream *stream,
3870
                                     struct ArrowArray *out_array)
3871
0
{
3872
0
    m_bLastGetNextArrowArrayUsedOptimizedCodePath = false;
3873
0
    if (!TouchLayer())
3874
0
    {
3875
0
        memset(out_array, 0, sizeof(*out_array));
3876
0
        return EIO;
3877
0
    }
3878
3879
0
    if (!m_hDBF || m_poAttrQuery != nullptr || m_poFilterGeom != nullptr)
3880
0
    {
3881
0
        return OGRLayer::GetNextArrowArray(stream, out_array);
3882
0
    }
3883
3884
    // If any field is not ignored, use generic implementation
3885
0
    const int nFieldCount = m_poFeatureDefn->GetFieldCount();
3886
0
    for (int i = 0; i < nFieldCount; ++i)
3887
0
    {
3888
0
        if (!m_poFeatureDefn->GetFieldDefn(i)->IsIgnored())
3889
0
            return OGRLayer::GetNextArrowArray(stream, out_array);
3890
0
    }
3891
0
    if (GetGeomType() != wkbNone &&
3892
0
        !m_poFeatureDefn->GetGeomFieldDefn(0)->IsIgnored())
3893
0
        return OGRLayer::GetNextArrowArray(stream, out_array);
3894
3895
0
    OGRArrowArrayHelper sHelper(m_poDS, m_poFeatureDefn,
3896
0
                                m_aosArrowArrayStreamOptions, out_array);
3897
0
    if (out_array->release == nullptr)
3898
0
    {
3899
0
        return ENOMEM;
3900
0
    }
3901
3902
0
    if (!sHelper.m_bIncludeFID)
3903
0
    {
3904
0
        out_array->release(out_array);
3905
0
        return OGRLayer::GetNextArrowArray(stream, out_array);
3906
0
    }
3907
3908
0
    m_bLastGetNextArrowArrayUsedOptimizedCodePath = true;
3909
0
    int nCount = 0;
3910
0
    while (m_iNextShapeId < m_nTotalShapeCount)
3911
0
    {
3912
0
        const bool bIsDeleted =
3913
0
            CPL_TO_BOOL(DBFIsRecordDeleted(m_hDBF, m_iNextShapeId));
3914
0
        if (bIsDeleted)
3915
0
        {
3916
0
            ++m_iNextShapeId;
3917
0
            continue;
3918
0
        }
3919
0
        if (VSIFEofL(VSI_SHP_GetVSIL(m_hDBF->fp)) ||
3920
0
            VSIFErrorL(VSI_SHP_GetVSIL(m_hDBF->fp)))
3921
0
        {
3922
0
            out_array->release(out_array);
3923
0
            memset(out_array, 0, sizeof(*out_array));
3924
0
            return EIO;
3925
0
        }
3926
0
        sHelper.m_panFIDValues[nCount] = m_iNextShapeId;
3927
0
        ++m_iNextShapeId;
3928
0
        ++nCount;
3929
0
        if (nCount == sHelper.m_nMaxBatchSize)
3930
0
            break;
3931
0
    }
3932
0
    sHelper.Shrink(nCount);
3933
0
    if (nCount == 0)
3934
0
    {
3935
0
        out_array->release(out_array);
3936
0
        memset(out_array, 0, sizeof(*out_array));
3937
0
    }
3938
0
    return 0;
3939
0
}
3940
3941
/************************************************************************/
3942
/*                        GetMetadataItem()                             */
3943
/************************************************************************/
3944
3945
const char *OGRShapeLayer::GetMetadataItem(const char *pszName,
3946
                                           const char *pszDomain)
3947
0
{
3948
0
    if (pszName && pszDomain && EQUAL(pszDomain, "__DEBUG__") &&
3949
0
        EQUAL(pszName, "LAST_GET_NEXT_ARROW_ARRAY_USED_OPTIMIZED_CODE_PATH"))
3950
0
    {
3951
0
        return m_bLastGetNextArrowArrayUsedOptimizedCodePath ? "YES" : "NO";
3952
0
    }
3953
0
    return OGRLayer::GetMetadataItem(pszName, pszDomain);
3954
0
}