Coverage Report

Created: 2025-11-16 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/ogr/ogrsf_frmts/shape/ogrshapelayer.cpp
Line
Count
Source
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) const
1781
1782
0
{
1783
0
    if (!const_cast<OGRShapeLayer *>(this)->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 ||
1795
0
              const_cast<OGRShapeLayer *>(this)->CheckForQIX() ||
1796
0
              const_cast<OGRShapeLayer *>(this)->CheckForSBN()))
1797
0
            return FALSE;
1798
1799
0
        if (m_poAttrQuery != nullptr)
1800
0
        {
1801
0
            const_cast<OGRShapeLayer *>(this)->InitializeIndexSupport(
1802
0
                m_osFullName.c_str());
1803
0
            return m_poAttrQuery->CanUseIndex(
1804
0
                const_cast<OGRShapeLayer *>(this));
1805
0
        }
1806
0
        return TRUE;
1807
0
    }
1808
1809
0
    if (EQUAL(pszCap, OLCDeleteFeature))
1810
0
        return m_bUpdateAccess;
1811
1812
0
    if (EQUAL(pszCap, OLCFastSpatialFilter))
1813
0
        return const_cast<OGRShapeLayer *>(this)->CheckForQIX() ||
1814
0
               const_cast<OGRShapeLayer *>(this)->CheckForSBN();
1815
1816
0
    if (EQUAL(pszCap, OLCFastGetExtent))
1817
0
        return TRUE;
1818
1819
0
    if (EQUAL(pszCap, OLCFastGetExtent3D))
1820
0
        return m_poFilterGeom == nullptr && m_poAttrQuery == nullptr;
1821
1822
0
    if (EQUAL(pszCap, OLCFastSetNextByIndex))
1823
0
        return m_poFilterGeom == nullptr && m_poAttrQuery == nullptr;
1824
1825
0
    if (EQUAL(pszCap, OLCCreateField))
1826
0
        return m_bUpdateAccess;
1827
1828
0
    if (EQUAL(pszCap, OLCDeleteField))
1829
0
        return m_bUpdateAccess;
1830
1831
0
    if (EQUAL(pszCap, OLCReorderFields))
1832
0
        return m_bUpdateAccess;
1833
1834
0
    if (EQUAL(pszCap, OLCAlterFieldDefn) ||
1835
0
        EQUAL(pszCap, OLCAlterGeomFieldDefn))
1836
0
        return m_bUpdateAccess;
1837
1838
0
    if (EQUAL(pszCap, OLCRename))
1839
0
        return m_bUpdateAccess;
1840
1841
0
    if (EQUAL(pszCap, OLCIgnoreFields))
1842
0
        return TRUE;
1843
1844
0
    if (EQUAL(pszCap, OLCStringsAsUTF8))
1845
0
    {
1846
        // No encoding defined: we don't know.
1847
0
        if (m_osEncoding.empty())
1848
0
            return FALSE;
1849
1850
0
        if (m_hDBF == nullptr || DBFGetFieldCount(m_hDBF) == 0)
1851
0
            return TRUE;
1852
1853
        // Otherwise test that we can re-encode field names to UTF-8.
1854
0
        const int nFieldCount = DBFGetFieldCount(m_hDBF);
1855
0
        for (int i = 0; i < nFieldCount; i++)
1856
0
        {
1857
0
            char szFieldName[XBASE_FLDNAME_LEN_READ + 1] = {};
1858
0
            int nWidth = 0;
1859
0
            int nPrecision = 0;
1860
1861
0
            DBFGetFieldInfo(m_hDBF, i, szFieldName, &nWidth, &nPrecision);
1862
1863
0
            if (!CPLCanRecode(szFieldName, m_osEncoding, CPL_ENC_UTF8))
1864
0
            {
1865
0
                return FALSE;
1866
0
            }
1867
0
        }
1868
1869
0
        return TRUE;
1870
0
    }
1871
1872
0
    if (EQUAL(pszCap, OLCMeasuredGeometries))
1873
0
        return TRUE;
1874
1875
0
    if (EQUAL(pszCap, OLCZGeometries))
1876
0
        return TRUE;
1877
1878
0
    return FALSE;
1879
0
}
1880
1881
/************************************************************************/
1882
/*                            CreateField()                             */
1883
/************************************************************************/
1884
1885
OGRErr OGRShapeLayer::CreateField(const OGRFieldDefn *poFieldDefn,
1886
                                  int bApproxOK)
1887
1888
0
{
1889
0
    if (!StartUpdate("CreateField"))
1890
0
        return OGRERR_FAILURE;
1891
1892
0
    CPLAssert(nullptr != poFieldDefn);
1893
1894
0
    bool bDBFJustCreated = false;
1895
0
    if (m_hDBF == nullptr)
1896
0
    {
1897
0
        const CPLString osFilename =
1898
0
            CPLResetExtensionSafe(m_osFullName.c_str(), "dbf");
1899
0
        m_hDBF = DBFCreate(osFilename);
1900
1901
0
        if (m_hDBF == nullptr)
1902
0
        {
1903
0
            CPLError(CE_Failure, CPLE_OpenFailed,
1904
0
                     "Failed to create DBF file `%s'.", osFilename.c_str());
1905
0
            return OGRERR_FAILURE;
1906
0
        }
1907
1908
0
        bDBFJustCreated = true;
1909
0
    }
1910
1911
0
    if (m_hDBF->nHeaderLength + XBASE_FLDHDR_SZ > 65535)
1912
0
    {
1913
0
        CPLError(CE_Failure, CPLE_NotSupported,
1914
0
                 "Cannot add field %s. Header length limit reached "
1915
0
                 "(max 65535 bytes, 2046 fields).",
1916
0
                 poFieldDefn->GetNameRef());
1917
0
        return OGRERR_FAILURE;
1918
0
    }
1919
1920
0
    CPLErrorReset();
1921
1922
0
    if (m_poFeatureDefn->GetFieldCount() == 255)
1923
0
    {
1924
0
        CPLError(CE_Warning, CPLE_AppDefined,
1925
0
                 "Creating a 256th field, "
1926
0
                 "but some DBF readers might only support 255 fields");
1927
0
    }
1928
1929
    /* -------------------------------------------------------------------- */
1930
    /*      Normalize field name                                            */
1931
    /* -------------------------------------------------------------------- */
1932
0
    CPLString osFieldName;
1933
0
    if (!m_osEncoding.empty())
1934
0
    {
1935
0
        CPLClearRecodeWarningFlags();
1936
0
        CPLPushErrorHandler(CPLQuietErrorHandler);
1937
0
        CPLErr eLastErr = CPLGetLastErrorType();
1938
0
        char *const pszRecoded =
1939
0
            CPLRecode(poFieldDefn->GetNameRef(), CPL_ENC_UTF8, m_osEncoding);
1940
0
        CPLPopErrorHandler();
1941
0
        osFieldName = pszRecoded;
1942
0
        CPLFree(pszRecoded);
1943
0
        if (CPLGetLastErrorType() != eLastErr)
1944
0
        {
1945
0
            CPLError(CE_Failure, CPLE_AppDefined,
1946
0
                     "Failed to create field name '%s': cannot convert to %s",
1947
0
                     poFieldDefn->GetNameRef(), m_osEncoding.c_str());
1948
0
            return OGRERR_FAILURE;
1949
0
        }
1950
0
    }
1951
0
    else
1952
0
    {
1953
0
        osFieldName = poFieldDefn->GetNameRef();
1954
0
    }
1955
1956
0
    const int nNameSize = static_cast<int>(osFieldName.size());
1957
0
    char szNewFieldName[XBASE_FLDNAME_LEN_WRITE + 1];
1958
0
    CPLString osRadixFieldName;
1959
0
    CPLString osRadixFieldNameUC;
1960
0
    {
1961
0
        char *pszTmp = CPLScanString(
1962
0
            osFieldName, std::min(nNameSize, XBASE_FLDNAME_LEN_WRITE), TRUE,
1963
0
            TRUE);
1964
0
        strncpy(szNewFieldName, pszTmp, sizeof(szNewFieldName) - 1);
1965
0
        szNewFieldName[sizeof(szNewFieldName) - 1] = '\0';
1966
0
        osRadixFieldName = pszTmp;
1967
0
        osRadixFieldNameUC = CPLString(osRadixFieldName).toupper();
1968
0
        CPLFree(pszTmp);
1969
0
    }
1970
1971
0
    CPLString osNewFieldNameUC(szNewFieldName);
1972
0
    osNewFieldNameUC.toupper();
1973
1974
0
    if (m_oSetUCFieldName.empty())
1975
0
    {
1976
0
        for (int i = 0; i < m_poFeatureDefn->GetFieldCount(); i++)
1977
0
        {
1978
0
            m_oSetUCFieldName.insert(
1979
0
                CPLString(m_poFeatureDefn->GetFieldDefn(i)->GetNameRef())
1980
0
                    .toupper());
1981
0
        }
1982
0
    }
1983
1984
0
    bool bFoundFieldName =
1985
0
        m_oSetUCFieldName.find(osNewFieldNameUC) != m_oSetUCFieldName.end();
1986
1987
0
    if (!bApproxOK && (bFoundFieldName || !EQUAL(osFieldName, szNewFieldName)))
1988
0
    {
1989
0
        CPLError(CE_Failure, CPLE_NotSupported,
1990
0
                 "Failed to add field named '%s'", poFieldDefn->GetNameRef());
1991
1992
0
        return OGRERR_FAILURE;
1993
0
    }
1994
1995
0
    if (bFoundFieldName)
1996
0
    {
1997
0
        int nRenameNum = 1;
1998
0
        while (bFoundFieldName && nRenameNum < 10)
1999
0
        {
2000
0
            CPLsnprintf(szNewFieldName, sizeof(szNewFieldName), "%.8s_%.1d",
2001
0
                        osRadixFieldName.c_str(), nRenameNum);
2002
0
            osNewFieldNameUC.Printf("%.8s_%.1d", osRadixFieldNameUC.c_str(),
2003
0
                                    nRenameNum);
2004
0
            bFoundFieldName = m_oSetUCFieldName.find(osNewFieldNameUC) !=
2005
0
                              m_oSetUCFieldName.end();
2006
0
            nRenameNum++;
2007
0
        }
2008
2009
0
        while (bFoundFieldName && nRenameNum < 100)
2010
0
        {
2011
0
            CPLsnprintf(szNewFieldName, sizeof(szNewFieldName), "%.8s%.2d",
2012
0
                        osRadixFieldName.c_str(), nRenameNum);
2013
0
            osNewFieldNameUC.Printf("%.8s%.2d", osRadixFieldNameUC.c_str(),
2014
0
                                    nRenameNum);
2015
0
            bFoundFieldName = m_oSetUCFieldName.find(osNewFieldNameUC) !=
2016
0
                              m_oSetUCFieldName.end();
2017
0
            nRenameNum++;
2018
0
        }
2019
2020
0
        if (bFoundFieldName)
2021
0
        {
2022
            // One hundred similar field names!!?
2023
0
            CPLError(
2024
0
                CE_Failure, CPLE_NotSupported,
2025
0
                "Too many field names like '%s' when truncated to %d letters "
2026
0
                "for Shapefile format.",
2027
0
                poFieldDefn->GetNameRef(), XBASE_FLDNAME_LEN_WRITE);
2028
0
            return OGRERR_FAILURE;
2029
0
        }
2030
0
    }
2031
2032
0
    OGRFieldDefn oModFieldDefn(poFieldDefn);
2033
2034
0
    if (!EQUAL(osFieldName, szNewFieldName))
2035
0
    {
2036
0
        CPLError(CE_Warning, CPLE_NotSupported,
2037
0
                 "Normalized/laundered field name: '%s' to '%s'",
2038
0
                 poFieldDefn->GetNameRef(), szNewFieldName);
2039
2040
        // Set field name with normalized value.
2041
0
        oModFieldDefn.SetName(szNewFieldName);
2042
0
    }
2043
2044
    /* -------------------------------------------------------------------- */
2045
    /*      Add field to layer                                              */
2046
    /* -------------------------------------------------------------------- */
2047
0
    char chType = 'C';
2048
0
    int nWidth = 0;
2049
0
    int nDecimals = 0;
2050
2051
0
    switch (oModFieldDefn.GetType())
2052
0
    {
2053
0
        case OFTInteger:
2054
0
            if (oModFieldDefn.GetSubType() == OFSTBoolean)
2055
0
            {
2056
0
                chType = 'L';
2057
0
                nWidth = 1;
2058
0
            }
2059
0
            else
2060
0
            {
2061
0
                chType = 'N';
2062
0
                nWidth = oModFieldDefn.GetWidth();
2063
0
                if (nWidth == 0)
2064
0
                    nWidth = 9;
2065
0
            }
2066
0
            break;
2067
2068
0
        case OFTInteger64:
2069
0
            chType = 'N';
2070
0
            nWidth = oModFieldDefn.GetWidth();
2071
0
            if (nWidth == 0)
2072
0
                nWidth = 18;
2073
0
            break;
2074
2075
0
        case OFTReal:
2076
0
            chType = 'N';
2077
0
            nWidth = oModFieldDefn.GetWidth();
2078
0
            nDecimals = oModFieldDefn.GetPrecision();
2079
0
            if (nWidth == 0)
2080
0
            {
2081
0
                nWidth = 24;
2082
0
                nDecimals = 15;
2083
0
            }
2084
0
            break;
2085
2086
0
        case OFTString:
2087
0
            chType = 'C';
2088
0
            nWidth = oModFieldDefn.GetWidth();
2089
0
            if (nWidth == 0)
2090
0
                nWidth = 80;
2091
0
            else if (nWidth > OGR_DBF_MAX_FIELD_WIDTH)
2092
0
            {
2093
0
                CPLError(CE_Warning, CPLE_AppDefined,
2094
0
                         "Field %s of width %d truncated to %d.",
2095
0
                         szNewFieldName, nWidth, OGR_DBF_MAX_FIELD_WIDTH);
2096
0
                nWidth = OGR_DBF_MAX_FIELD_WIDTH;
2097
0
            }
2098
0
            break;
2099
2100
0
        case OFTDate:
2101
0
            chType = 'D';
2102
0
            nWidth = 8;
2103
0
            break;
2104
2105
0
        case OFTDateTime:
2106
0
            CPLError(
2107
0
                CE_Warning, CPLE_NotSupported,
2108
0
                "Field %s created as String field, though DateTime requested.",
2109
0
                szNewFieldName);
2110
0
            chType = 'C';
2111
0
            nWidth = static_cast<int>(strlen("YYYY-MM-DDTHH:MM:SS.sss+HH:MM"));
2112
0
            oModFieldDefn.SetType(OFTString);
2113
0
            break;
2114
2115
0
        default:
2116
0
            CPLError(CE_Failure, CPLE_NotSupported,
2117
0
                     "Can't create fields of type %s on shapefile layers.",
2118
0
                     OGRFieldDefn::GetFieldTypeName(oModFieldDefn.GetType()));
2119
2120
0
            return OGRERR_FAILURE;
2121
0
    }
2122
2123
0
    oModFieldDefn.SetWidth(nWidth);
2124
0
    oModFieldDefn.SetPrecision(nDecimals);
2125
2126
    // Suppress the dummy FID field if we have created it just before.
2127
0
    if (DBFGetFieldCount(m_hDBF) == 1 && m_poFeatureDefn->GetFieldCount() == 0)
2128
0
    {
2129
0
        DBFDeleteField(m_hDBF, 0);
2130
0
    }
2131
2132
0
    const int iNewField = DBFAddNativeFieldType(m_hDBF, szNewFieldName, chType,
2133
0
                                                nWidth, nDecimals);
2134
2135
0
    if (iNewField != -1)
2136
0
    {
2137
0
        m_oSetUCFieldName.insert(std::move(osNewFieldNameUC));
2138
2139
0
        whileUnsealing(m_poFeatureDefn)->AddFieldDefn(&oModFieldDefn);
2140
2141
0
        if (bDBFJustCreated)
2142
0
        {
2143
0
            for (int i = 0; i < m_nTotalShapeCount; i++)
2144
0
            {
2145
0
                DBFWriteNULLAttribute(m_hDBF, i, 0);
2146
0
            }
2147
0
        }
2148
2149
0
        return OGRERR_NONE;
2150
0
    }
2151
2152
0
    CPLError(CE_Failure, CPLE_AppDefined,
2153
0
             "Can't create field %s in Shape DBF file, reason unknown.",
2154
0
             szNewFieldName);
2155
2156
0
    return OGRERR_FAILURE;
2157
0
}
2158
2159
/************************************************************************/
2160
/*                            DeleteField()                             */
2161
/************************************************************************/
2162
2163
OGRErr OGRShapeLayer::DeleteField(int iField)
2164
0
{
2165
0
    if (!StartUpdate("DeleteField"))
2166
0
        return OGRERR_FAILURE;
2167
2168
0
    if (iField < 0 || iField >= m_poFeatureDefn->GetFieldCount())
2169
0
    {
2170
0
        CPLError(CE_Failure, CPLE_NotSupported, "Invalid field index");
2171
0
        return OGRERR_FAILURE;
2172
0
    }
2173
2174
0
    m_oSetUCFieldName.clear();
2175
2176
0
    if (DBFDeleteField(m_hDBF, iField))
2177
0
    {
2178
0
        TruncateDBF();
2179
2180
0
        return whileUnsealing(m_poFeatureDefn)->DeleteFieldDefn(iField);
2181
0
    }
2182
2183
0
    return OGRERR_FAILURE;
2184
0
}
2185
2186
/************************************************************************/
2187
/*                           ReorderFields()                            */
2188
/************************************************************************/
2189
2190
OGRErr OGRShapeLayer::ReorderFields(int *panMap)
2191
0
{
2192
0
    if (!StartUpdate("ReorderFields"))
2193
0
        return OGRERR_FAILURE;
2194
2195
0
    if (m_poFeatureDefn->GetFieldCount() == 0)
2196
0
        return OGRERR_NONE;
2197
2198
0
    OGRErr eErr = OGRCheckPermutation(panMap, m_poFeatureDefn->GetFieldCount());
2199
0
    if (eErr != OGRERR_NONE)
2200
0
        return eErr;
2201
2202
0
    if (DBFReorderFields(m_hDBF, panMap))
2203
0
    {
2204
0
        return whileUnsealing(m_poFeatureDefn)->ReorderFieldDefns(panMap);
2205
0
    }
2206
2207
0
    return OGRERR_FAILURE;
2208
0
}
2209
2210
/************************************************************************/
2211
/*                           AlterFieldDefn()                           */
2212
/************************************************************************/
2213
2214
OGRErr OGRShapeLayer::AlterFieldDefn(int iField, OGRFieldDefn *poNewFieldDefn,
2215
                                     int nFlagsIn)
2216
0
{
2217
0
    if (!StartUpdate("AlterFieldDefn"))
2218
0
        return OGRERR_FAILURE;
2219
2220
0
    if (iField < 0 || iField >= m_poFeatureDefn->GetFieldCount())
2221
0
    {
2222
0
        CPLError(CE_Failure, CPLE_NotSupported, "Invalid field index");
2223
0
        return OGRERR_FAILURE;
2224
0
    }
2225
2226
0
    m_oSetUCFieldName.clear();
2227
2228
0
    OGRFieldDefn *poFieldDefn = m_poFeatureDefn->GetFieldDefn(iField);
2229
0
    OGRFieldType eType = poFieldDefn->GetType();
2230
2231
0
    auto oTemporaryUnsealer(poFieldDefn->GetTemporaryUnsealer());
2232
2233
    // On reading we support up to 11 characters
2234
0
    char szFieldName[XBASE_FLDNAME_LEN_READ + 1] = {};
2235
0
    int nWidth = 0;
2236
0
    int nPrecision = 0;
2237
0
    DBFGetFieldInfo(m_hDBF, iField, szFieldName, &nWidth, &nPrecision);
2238
0
    char chNativeType = DBFGetNativeFieldType(m_hDBF, iField);
2239
2240
0
    if ((nFlagsIn & ALTER_TYPE_FLAG) &&
2241
0
        poNewFieldDefn->GetType() != poFieldDefn->GetType())
2242
0
    {
2243
0
        if (poNewFieldDefn->GetType() == OFTInteger64 &&
2244
0
            poFieldDefn->GetType() == OFTInteger)
2245
0
        {
2246
0
            eType = poNewFieldDefn->GetType();
2247
0
        }
2248
0
        else if (poNewFieldDefn->GetType() != OFTString)
2249
0
        {
2250
0
            CPLError(CE_Failure, CPLE_NotSupported,
2251
0
                     "Can only convert to OFTString");
2252
0
            return OGRERR_FAILURE;
2253
0
        }
2254
0
        else
2255
0
        {
2256
0
            chNativeType = 'C';
2257
0
            eType = poNewFieldDefn->GetType();
2258
0
        }
2259
0
    }
2260
2261
0
    if (nFlagsIn & ALTER_NAME_FLAG)
2262
0
    {
2263
0
        CPLString osFieldName;
2264
0
        if (!m_osEncoding.empty())
2265
0
        {
2266
0
            CPLClearRecodeWarningFlags();
2267
0
            CPLErrorReset();
2268
0
            CPLPushErrorHandler(CPLQuietErrorHandler);
2269
0
            char *pszRecoded = CPLRecode(poNewFieldDefn->GetNameRef(),
2270
0
                                         CPL_ENC_UTF8, m_osEncoding);
2271
0
            CPLPopErrorHandler();
2272
0
            osFieldName = pszRecoded;
2273
0
            CPLFree(pszRecoded);
2274
0
            if (CPLGetLastErrorType() != 0)
2275
0
            {
2276
0
                CPLError(CE_Failure, CPLE_AppDefined,
2277
0
                         "Failed to rename field name to '%s': "
2278
0
                         "cannot convert to %s",
2279
0
                         poNewFieldDefn->GetNameRef(), m_osEncoding.c_str());
2280
0
                return OGRERR_FAILURE;
2281
0
            }
2282
0
        }
2283
0
        else
2284
0
        {
2285
0
            osFieldName = poNewFieldDefn->GetNameRef();
2286
0
        }
2287
2288
0
        strncpy(szFieldName, osFieldName, sizeof(szFieldName) - 1);
2289
0
        szFieldName[sizeof(szFieldName) - 1] = '\0';
2290
0
    }
2291
0
    if (nFlagsIn & ALTER_WIDTH_PRECISION_FLAG)
2292
0
    {
2293
0
        nWidth = poNewFieldDefn->GetWidth();
2294
0
        nPrecision = poNewFieldDefn->GetPrecision();
2295
0
    }
2296
2297
0
    if (DBFAlterFieldDefn(m_hDBF, iField, szFieldName, chNativeType, nWidth,
2298
0
                          nPrecision))
2299
0
    {
2300
0
        if (nFlagsIn & ALTER_TYPE_FLAG)
2301
0
            poFieldDefn->SetType(eType);
2302
0
        if (nFlagsIn & ALTER_NAME_FLAG)
2303
0
            poFieldDefn->SetName(poNewFieldDefn->GetNameRef());
2304
0
        if (nFlagsIn & ALTER_WIDTH_PRECISION_FLAG)
2305
0
        {
2306
0
            poFieldDefn->SetWidth(nWidth);
2307
0
            poFieldDefn->SetPrecision(nPrecision);
2308
2309
0
            TruncateDBF();
2310
0
        }
2311
0
        return OGRERR_NONE;
2312
0
    }
2313
2314
0
    return OGRERR_FAILURE;
2315
0
}
2316
2317
/************************************************************************/
2318
/*                         AlterGeomFieldDefn()                         */
2319
/************************************************************************/
2320
2321
OGRErr OGRShapeLayer::AlterGeomFieldDefn(
2322
    int iGeomField, const OGRGeomFieldDefn *poNewGeomFieldDefn, int nFlagsIn)
2323
0
{
2324
0
    if (!StartUpdate("AlterGeomFieldDefn"))
2325
0
        return OGRERR_FAILURE;
2326
2327
0
    if (iGeomField < 0 || iGeomField >= m_poFeatureDefn->GetGeomFieldCount())
2328
0
    {
2329
0
        CPLError(CE_Failure, CPLE_NotSupported, "Invalid field index");
2330
0
        return OGRERR_FAILURE;
2331
0
    }
2332
2333
0
    auto poFieldDefn = cpl::down_cast<OGRShapeGeomFieldDefn *>(
2334
0
        m_poFeatureDefn->GetGeomFieldDefn(iGeomField));
2335
0
    auto oTemporaryUnsealer(poFieldDefn->GetTemporaryUnsealer());
2336
2337
0
    if (nFlagsIn & ALTER_GEOM_FIELD_DEFN_NAME_FLAG)
2338
0
    {
2339
0
        if (strcmp(poNewGeomFieldDefn->GetNameRef(),
2340
0
                   poFieldDefn->GetNameRef()) != 0)
2341
0
        {
2342
0
            CPLError(CE_Failure, CPLE_NotSupported,
2343
0
                     "Altering the geometry field name is not supported for "
2344
0
                     "shapefiles");
2345
0
            return OGRERR_FAILURE;
2346
0
        }
2347
0
    }
2348
2349
0
    if (nFlagsIn & ALTER_GEOM_FIELD_DEFN_TYPE_FLAG)
2350
0
    {
2351
0
        if (poFieldDefn->GetType() != poNewGeomFieldDefn->GetType())
2352
0
        {
2353
0
            CPLError(CE_Failure, CPLE_NotSupported,
2354
0
                     "Altering the geometry field type is not supported for "
2355
0
                     "shapefiles");
2356
0
            return OGRERR_FAILURE;
2357
0
        }
2358
0
    }
2359
2360
0
    if (nFlagsIn & ALTER_GEOM_FIELD_DEFN_SRS_COORD_EPOCH_FLAG)
2361
0
    {
2362
0
        const auto poNewSRSRef = poNewGeomFieldDefn->GetSpatialRef();
2363
0
        if (poNewSRSRef && poNewSRSRef->GetCoordinateEpoch() > 0)
2364
0
        {
2365
0
            CPLError(CE_Failure, CPLE_NotSupported,
2366
0
                     "Setting a coordinate epoch is not supported for "
2367
0
                     "shapefiles");
2368
0
            return OGRERR_FAILURE;
2369
0
        }
2370
0
    }
2371
2372
0
    if (nFlagsIn & ALTER_GEOM_FIELD_DEFN_SRS_FLAG)
2373
0
    {
2374
0
        if (poFieldDefn->GetPrjFilename().empty())
2375
0
        {
2376
0
            poFieldDefn->SetPrjFilename(
2377
0
                CPLResetExtensionSafe(m_osFullName.c_str(), "prj").c_str());
2378
0
        }
2379
2380
0
        const auto poNewSRSRef = poNewGeomFieldDefn->GetSpatialRef();
2381
0
        if (poNewSRSRef)
2382
0
        {
2383
0
            char *pszWKT = nullptr;
2384
0
            VSILFILE *fp = nullptr;
2385
0
            const char *const apszOptions[] = {"FORMAT=WKT1_ESRI", nullptr};
2386
0
            if (poNewSRSRef->exportToWkt(&pszWKT, apszOptions) == OGRERR_NONE &&
2387
0
                (fp = VSIFOpenL(poFieldDefn->GetPrjFilename().c_str(), "wt")) !=
2388
0
                    nullptr)
2389
0
            {
2390
0
                VSIFWriteL(pszWKT, strlen(pszWKT), 1, fp);
2391
0
                VSIFCloseL(fp);
2392
0
            }
2393
0
            else
2394
0
            {
2395
0
                CPLError(CE_Failure, CPLE_FileIO, "Cannot write %s",
2396
0
                         poFieldDefn->GetPrjFilename().c_str());
2397
0
                CPLFree(pszWKT);
2398
0
                return OGRERR_FAILURE;
2399
0
            }
2400
2401
0
            CPLFree(pszWKT);
2402
2403
0
            auto poNewSRS = poNewSRSRef->Clone();
2404
0
            poFieldDefn->SetSpatialRef(poNewSRS);
2405
0
            poNewSRS->Release();
2406
0
        }
2407
0
        else
2408
0
        {
2409
0
            poFieldDefn->SetSpatialRef(nullptr);
2410
0
            VSIStatBufL sStat;
2411
0
            if (VSIStatL(poFieldDefn->GetPrjFilename().c_str(), &sStat) == 0 &&
2412
0
                VSIUnlink(poFieldDefn->GetPrjFilename().c_str()) != 0)
2413
0
            {
2414
0
                CPLError(CE_Failure, CPLE_FileIO, "Cannot delete %s",
2415
0
                         poFieldDefn->GetPrjFilename().c_str());
2416
0
                return OGRERR_FAILURE;
2417
0
            }
2418
0
        }
2419
0
        poFieldDefn->SetSRSSet();
2420
0
    }
2421
2422
0
    if (nFlagsIn & ALTER_GEOM_FIELD_DEFN_NAME_FLAG)
2423
0
        poFieldDefn->SetName(poNewGeomFieldDefn->GetNameRef());
2424
0
    if (nFlagsIn & ALTER_GEOM_FIELD_DEFN_NULLABLE_FLAG)
2425
0
        poFieldDefn->SetNullable(poNewGeomFieldDefn->IsNullable());
2426
2427
0
    return OGRERR_NONE;
2428
0
}
2429
2430
/************************************************************************/
2431
/*                           GetSpatialRef()                            */
2432
/************************************************************************/
2433
2434
const OGRSpatialReference *OGRShapeGeomFieldDefn::GetSpatialRef() const
2435
2436
0
{
2437
0
    if (m_bSRSSet)
2438
0
        return poSRS;
2439
2440
0
    m_bSRSSet = true;
2441
2442
    /* -------------------------------------------------------------------- */
2443
    /*      Is there an associated .prj file we can read?                   */
2444
    /* -------------------------------------------------------------------- */
2445
0
    std::string l_osPrjFile =
2446
0
        CPLResetExtensionSafe(m_osFullName.c_str(), "prj");
2447
2448
0
    char *apszOptions[] = {
2449
0
        const_cast<char *>("EMIT_ERROR_IF_CANNOT_OPEN_FILE=FALSE"), nullptr};
2450
0
    char **papszLines = CSLLoad2(l_osPrjFile.c_str(), -1, -1, apszOptions);
2451
0
    if (papszLines == nullptr)
2452
0
    {
2453
0
        l_osPrjFile = CPLResetExtensionSafe(m_osFullName.c_str(), "PRJ");
2454
0
        papszLines = CSLLoad2(l_osPrjFile.c_str(), -1, -1, apszOptions);
2455
0
    }
2456
2457
0
    if (papszLines != nullptr)
2458
0
    {
2459
0
        m_osPrjFile = std::move(l_osPrjFile);
2460
2461
0
        auto poSRSNonConst = new OGRSpatialReference();
2462
0
        poSRSNonConst->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
2463
        // Remove UTF-8 BOM if found
2464
        // http://lists.osgeo.org/pipermail/gdal-dev/2014-July/039527.html
2465
0
        if (static_cast<unsigned char>(papszLines[0][0]) == 0xEF &&
2466
0
            static_cast<unsigned char>(papszLines[0][1]) == 0xBB &&
2467
0
            static_cast<unsigned char>(papszLines[0][2]) == 0xBF)
2468
0
        {
2469
0
            memmove(papszLines[0], papszLines[0] + 3,
2470
0
                    strlen(papszLines[0] + 3) + 1);
2471
0
        }
2472
0
        if (STARTS_WITH_CI(papszLines[0], "GEOGCS["))
2473
0
        {
2474
            // Strip AXIS[] in GEOGCS to address use case of
2475
            // https://github.com/OSGeo/gdal/issues/8452
2476
0
            std::string osVal;
2477
0
            for (CSLConstList papszIter = papszLines; *papszIter; ++papszIter)
2478
0
                osVal += *papszIter;
2479
0
            OGR_SRSNode oSRSNode;
2480
0
            const char *pszVal = osVal.c_str();
2481
0
            if (oSRSNode.importFromWkt(&pszVal) == OGRERR_NONE)
2482
0
            {
2483
0
                oSRSNode.StripNodes("AXIS");
2484
0
                char *pszWKT = nullptr;
2485
0
                oSRSNode.exportToWkt(&pszWKT);
2486
0
                if (pszWKT)
2487
0
                {
2488
0
                    CSLDestroy(papszLines);
2489
0
                    papszLines =
2490
0
                        static_cast<char **>(CPLCalloc(2, sizeof(char *)));
2491
0
                    papszLines[0] = pszWKT;
2492
0
                }
2493
0
            }
2494
0
        }
2495
0
        if (poSRSNonConst->importFromESRI(papszLines) != OGRERR_NONE)
2496
0
        {
2497
0
            delete poSRSNonConst;
2498
0
            poSRSNonConst = nullptr;
2499
0
        }
2500
0
        CSLDestroy(papszLines);
2501
2502
0
        if (poSRSNonConst)
2503
0
        {
2504
0
            double adfTOWGS84[7];
2505
0
            const char *pszSRSName = poSRSNonConst->GetName();
2506
0
            if (CPLTestBool(
2507
0
                    CPLGetConfigOption("USE_OSR_FIND_MATCHES", "YES")) &&
2508
                // Below works around bug fixed in PROJ per
2509
                // https://github.com/OSGeo/PROJ/pull/4599
2510
0
                !(pszSRSName && strstr(pszSRSName, "NTF (Paris)") != nullptr &&
2511
0
                  poSRSNonConst->GetTOWGS84(adfTOWGS84) == OGRERR_NONE))
2512
0
            {
2513
0
                auto poSRSMatch = poSRSNonConst->FindBestMatch();
2514
0
                if (poSRSMatch)
2515
0
                {
2516
0
                    poSRSNonConst->Release();
2517
0
                    poSRSNonConst = poSRSMatch;
2518
0
                    poSRSNonConst->SetAxisMappingStrategy(
2519
0
                        OAMS_TRADITIONAL_GIS_ORDER);
2520
0
                }
2521
0
            }
2522
0
            else
2523
0
            {
2524
0
                poSRSNonConst->AutoIdentifyEPSG();
2525
0
            }
2526
0
            poSRS = poSRSNonConst;
2527
0
        }
2528
0
    }
2529
2530
0
    return poSRS;
2531
0
}
2532
2533
/************************************************************************/
2534
/*                           ResetGeomType()                            */
2535
/*                                                                      */
2536
/*      Modify the geometry type for this file.  Used to convert to     */
2537
/*      a different geometry type when a layer was created with a       */
2538
/*      type of unknown, and we get to the first feature to             */
2539
/*      establish the type.                                             */
2540
/************************************************************************/
2541
2542
int OGRShapeLayer::ResetGeomType(int nNewGeomType)
2543
2544
0
{
2545
0
    if (m_nTotalShapeCount > 0)
2546
0
        return FALSE;
2547
2548
0
    if (m_hSHP->fpSHX == nullptr)
2549
0
    {
2550
0
        CPLError(CE_Failure, CPLE_NotSupported,
2551
0
                 "OGRShapeLayer::ResetGeomType failed: SHX file is closed");
2552
0
        return FALSE;
2553
0
    }
2554
2555
    /* -------------------------------------------------------------------- */
2556
    /*      Update .shp header.                                             */
2557
    /* -------------------------------------------------------------------- */
2558
0
    int nStartPos = static_cast<int>(m_hSHP->sHooks.FTell(m_hSHP->fpSHP));
2559
2560
0
    char abyHeader[100] = {};
2561
0
    if (m_hSHP->sHooks.FSeek(m_hSHP->fpSHP, 0, SEEK_SET) != 0 ||
2562
0
        m_hSHP->sHooks.FRead(abyHeader, 100, 1, m_hSHP->fpSHP) != 1)
2563
0
        return FALSE;
2564
2565
0
    *(reinterpret_cast<GInt32 *>(abyHeader + 32)) = CPL_LSBWORD32(nNewGeomType);
2566
2567
0
    if (m_hSHP->sHooks.FSeek(m_hSHP->fpSHP, 0, SEEK_SET) != 0 ||
2568
0
        m_hSHP->sHooks.FWrite(abyHeader, 100, 1, m_hSHP->fpSHP) != 1)
2569
0
        return FALSE;
2570
2571
0
    if (m_hSHP->sHooks.FSeek(m_hSHP->fpSHP, nStartPos, SEEK_SET) != 0)
2572
0
        return FALSE;
2573
2574
    /* -------------------------------------------------------------------- */
2575
    /*      Update .shx header.                                             */
2576
    /* -------------------------------------------------------------------- */
2577
0
    nStartPos = static_cast<int>(m_hSHP->sHooks.FTell(m_hSHP->fpSHX));
2578
2579
0
    if (m_hSHP->sHooks.FSeek(m_hSHP->fpSHX, 0, SEEK_SET) != 0 ||
2580
0
        m_hSHP->sHooks.FRead(abyHeader, 100, 1, m_hSHP->fpSHX) != 1)
2581
0
        return FALSE;
2582
2583
0
    *(reinterpret_cast<GInt32 *>(abyHeader + 32)) = CPL_LSBWORD32(nNewGeomType);
2584
2585
0
    if (m_hSHP->sHooks.FSeek(m_hSHP->fpSHX, 0, SEEK_SET) != 0 ||
2586
0
        m_hSHP->sHooks.FWrite(abyHeader, 100, 1, m_hSHP->fpSHX) != 1)
2587
0
        return FALSE;
2588
2589
0
    if (m_hSHP->sHooks.FSeek(m_hSHP->fpSHX, nStartPos, SEEK_SET) != 0)
2590
0
        return FALSE;
2591
2592
    /* -------------------------------------------------------------------- */
2593
    /*      Update other information.                                       */
2594
    /* -------------------------------------------------------------------- */
2595
0
    m_hSHP->nShapeType = nNewGeomType;
2596
2597
0
    return TRUE;
2598
0
}
2599
2600
/************************************************************************/
2601
/*                             SyncToDisk()                             */
2602
/************************************************************************/
2603
2604
OGRErr OGRShapeLayer::SyncToDisk()
2605
2606
0
{
2607
0
    if (!TouchLayer())
2608
0
        return OGRERR_FAILURE;
2609
2610
0
    if (m_bHeaderDirty)
2611
0
    {
2612
0
        if (m_hSHP != nullptr)
2613
0
            SHPWriteHeader(m_hSHP);
2614
2615
0
        if (m_hDBF != nullptr)
2616
0
            DBFUpdateHeader(m_hDBF);
2617
2618
0
        m_bHeaderDirty = false;
2619
0
    }
2620
2621
0
    if (m_hSHP != nullptr)
2622
0
    {
2623
0
        m_hSHP->sHooks.FFlush(m_hSHP->fpSHP);
2624
0
        if (m_hSHP->fpSHX != nullptr)
2625
0
            m_hSHP->sHooks.FFlush(m_hSHP->fpSHX);
2626
0
    }
2627
2628
0
    if (m_hDBF != nullptr)
2629
0
    {
2630
0
        m_hDBF->sHooks.FFlush(m_hDBF->fp);
2631
0
    }
2632
2633
0
    if (m_eNeedRepack == YES && m_bAutoRepack)
2634
0
        Repack();
2635
2636
0
    return OGRERR_NONE;
2637
0
}
2638
2639
/************************************************************************/
2640
/*                          DropSpatialIndex()                          */
2641
/************************************************************************/
2642
2643
OGRErr OGRShapeLayer::DropSpatialIndex()
2644
2645
0
{
2646
0
    if (!StartUpdate("DropSpatialIndex"))
2647
0
        return OGRERR_FAILURE;
2648
2649
0
    if (!CheckForQIX() && !CheckForSBN())
2650
0
    {
2651
0
        CPLError(CE_Warning, CPLE_AppDefined,
2652
0
                 "Layer %s has no spatial index, DROP SPATIAL INDEX failed.",
2653
0
                 m_poFeatureDefn->GetName());
2654
0
        return OGRERR_FAILURE;
2655
0
    }
2656
2657
0
    const bool bHadQIX = m_hQIX != nullptr;
2658
2659
0
    SHPCloseDiskTree(m_hQIX);
2660
0
    m_hQIX = nullptr;
2661
0
    m_bCheckedForQIX = false;
2662
2663
0
    SBNCloseDiskTree(m_hSBN);
2664
0
    m_hSBN = nullptr;
2665
0
    m_bCheckedForSBN = false;
2666
2667
0
    if (bHadQIX)
2668
0
    {
2669
0
        const std::string osQIXFilename =
2670
0
            CPLResetExtensionSafe(m_osFullName.c_str(), "qix");
2671
0
        CPLDebug("SHAPE", "Unlinking index file %s", osQIXFilename.c_str());
2672
2673
0
        if (VSIUnlink(osQIXFilename.c_str()) != 0)
2674
0
        {
2675
0
            CPLError(CE_Failure, CPLE_AppDefined,
2676
0
                     "Failed to delete file %s.\n%s", osQIXFilename.c_str(),
2677
0
                     VSIStrerror(errno));
2678
0
            return OGRERR_FAILURE;
2679
0
        }
2680
0
    }
2681
2682
0
    if (!m_bSbnSbxDeleted)
2683
0
    {
2684
0
        const char papszExt[2][4] = {"sbn", "sbx"};
2685
0
        for (int i = 0; i < 2; i++)
2686
0
        {
2687
0
            const std::string osIndexFilename =
2688
0
                CPLResetExtensionSafe(m_osFullName.c_str(), papszExt[i]);
2689
0
            CPLDebug("SHAPE", "Trying to unlink index file %s",
2690
0
                     osIndexFilename.c_str());
2691
2692
0
            if (VSIUnlink(osIndexFilename.c_str()) != 0)
2693
0
            {
2694
0
                CPLDebug("SHAPE", "Failed to delete file %s.\n%s",
2695
0
                         osIndexFilename.c_str(), VSIStrerror(errno));
2696
0
            }
2697
0
        }
2698
0
    }
2699
0
    m_bSbnSbxDeleted = true;
2700
2701
0
    ClearSpatialFIDs();
2702
2703
0
    return OGRERR_NONE;
2704
0
}
2705
2706
/************************************************************************/
2707
/*                         CreateSpatialIndex()                         */
2708
/************************************************************************/
2709
2710
OGRErr OGRShapeLayer::CreateSpatialIndex(int nMaxDepth)
2711
2712
0
{
2713
0
    if (!StartUpdate("CreateSpatialIndex"))
2714
0
        return OGRERR_FAILURE;
2715
2716
    /* -------------------------------------------------------------------- */
2717
    /*      If we have an existing spatial index, blow it away first.       */
2718
    /* -------------------------------------------------------------------- */
2719
0
    if (CheckForQIX())
2720
0
        DropSpatialIndex();
2721
2722
0
    m_bCheckedForQIX = false;
2723
2724
    /* -------------------------------------------------------------------- */
2725
    /*      Build a quadtree structure for this file.                       */
2726
    /* -------------------------------------------------------------------- */
2727
0
    OGRShapeLayer::SyncToDisk();
2728
0
    SHPTree *psTree = SHPCreateTree(m_hSHP, 2, nMaxDepth, nullptr, nullptr);
2729
2730
0
    if (nullptr == psTree)
2731
0
    {
2732
        // TODO(mloskot): Is it better to return OGRERR_NOT_ENOUGH_MEMORY?
2733
0
        CPLDebug("SHAPE",
2734
0
                 "Index creation failure. Likely, memory allocation error.");
2735
2736
0
        return OGRERR_FAILURE;
2737
0
    }
2738
2739
    /* -------------------------------------------------------------------- */
2740
    /*      Trim unused nodes from the tree.                                */
2741
    /* -------------------------------------------------------------------- */
2742
0
    SHPTreeTrimExtraNodes(psTree);
2743
2744
    /* -------------------------------------------------------------------- */
2745
    /*      Dump tree to .qix file.                                         */
2746
    /* -------------------------------------------------------------------- */
2747
0
    char *pszQIXFilename =
2748
0
        CPLStrdup(CPLResetExtensionSafe(m_osFullName.c_str(), "qix").c_str());
2749
2750
0
    CPLDebug("SHAPE", "Creating index file %s", pszQIXFilename);
2751
2752
0
    SHPWriteTree(psTree, pszQIXFilename);
2753
0
    CPLFree(pszQIXFilename);
2754
2755
    /* -------------------------------------------------------------------- */
2756
    /*      cleanup                                                         */
2757
    /* -------------------------------------------------------------------- */
2758
0
    SHPDestroyTree(psTree);
2759
2760
0
    CPL_IGNORE_RET_VAL(CheckForQIX());
2761
2762
0
    return OGRERR_NONE;
2763
0
}
2764
2765
/************************************************************************/
2766
/*                       CheckFileDeletion()                            */
2767
/************************************************************************/
2768
2769
static void CheckFileDeletion(const CPLString &osFilename)
2770
0
{
2771
    // On Windows, sometimes the file is still triansiently reported
2772
    // as existing although being deleted, which makes QGIS things that
2773
    // an issue arose. The following helps to reduce that risk.
2774
0
    VSIStatBufL sStat;
2775
0
    if (VSIStatL(osFilename, &sStat) == 0 && VSIStatL(osFilename, &sStat) == 0)
2776
0
    {
2777
0
        CPLDebug("Shape",
2778
0
                 "File %s is still reported as existing whereas "
2779
0
                 "it should have been deleted",
2780
0
                 osFilename.c_str());
2781
0
    }
2782
0
}
2783
2784
/************************************************************************/
2785
/*                         ForceDeleteFile()                            */
2786
/************************************************************************/
2787
2788
static void ForceDeleteFile(const CPLString &osFilename)
2789
0
{
2790
0
    if (VSIUnlink(osFilename) != 0)
2791
0
    {
2792
        // In case of failure retry with a small delay (Windows specific)
2793
0
        CPLSleep(0.1);
2794
0
        if (VSIUnlink(osFilename) != 0)
2795
0
        {
2796
0
            CPLDebug("Shape", "Cannot delete %s : %s", osFilename.c_str(),
2797
0
                     VSIStrerror(errno));
2798
0
        }
2799
0
    }
2800
0
    CheckFileDeletion(osFilename);
2801
0
}
2802
2803
/************************************************************************/
2804
/*                               Repack()                               */
2805
/*                                                                      */
2806
/*      Repack the shape and dbf file, dropping deleted records.        */
2807
/*      FIDs may change.                                                */
2808
/************************************************************************/
2809
2810
OGRErr OGRShapeLayer::Repack()
2811
2812
0
{
2813
0
    if (m_eNeedRepack == NO)
2814
0
    {
2815
0
        CPLDebug("Shape", "REPACK: nothing to do. Was done previously");
2816
0
        return OGRERR_NONE;
2817
0
    }
2818
2819
0
    if (!StartUpdate("Repack"))
2820
0
        return OGRERR_FAILURE;
2821
2822
    /* -------------------------------------------------------------------- */
2823
    /*      Build a list of records to be dropped.                          */
2824
    /* -------------------------------------------------------------------- */
2825
0
    std::vector<int> anRecordsToDelete;
2826
0
    OGRErr eErr = OGRERR_NONE;
2827
2828
0
    CPLDebug("Shape", "REPACK: Checking if features have been deleted");
2829
2830
0
    if (m_hDBF != nullptr)
2831
0
    {
2832
0
        try
2833
0
        {
2834
0
            for (int iShape = 0; iShape < m_nTotalShapeCount; iShape++)
2835
0
            {
2836
0
                if (DBFIsRecordDeleted(m_hDBF, iShape))
2837
0
                {
2838
0
                    anRecordsToDelete.push_back(iShape);
2839
0
                }
2840
0
                if (VSIFEofL(VSI_SHP_GetVSIL(m_hDBF->fp)) ||
2841
0
                    VSIFErrorL(VSI_SHP_GetVSIL(m_hDBF->fp)))
2842
0
                {
2843
0
                    return OGRERR_FAILURE;  // I/O error.
2844
0
                }
2845
0
            }
2846
0
        }
2847
0
        catch (const std::bad_alloc &)
2848
0
        {
2849
0
            CPLError(CE_Failure, CPLE_OutOfMemory, "Out of memory in Repack()");
2850
0
            return OGRERR_FAILURE;
2851
0
        }
2852
0
    }
2853
2854
    /* -------------------------------------------------------------------- */
2855
    /*      If there are no records marked for deletion, we take no         */
2856
    /*      action.                                                         */
2857
    /* -------------------------------------------------------------------- */
2858
0
    if (anRecordsToDelete.empty() && !m_bSHPNeedsRepack)
2859
0
    {
2860
0
        CPLDebug("Shape", "REPACK: nothing to do");
2861
0
        return OGRERR_NONE;
2862
0
    }
2863
2864
    /* -------------------------------------------------------------------- */
2865
    /*      Find existing filenames with exact case (see #3293).            */
2866
    /* -------------------------------------------------------------------- */
2867
0
    const CPLString osDirname(CPLGetPathSafe(m_osFullName.c_str()));
2868
0
    const CPLString osBasename(CPLGetBasenameSafe(m_osFullName.c_str()));
2869
2870
0
    CPLString osDBFName;
2871
0
    CPLString osSHPName;
2872
0
    CPLString osSHXName;
2873
0
    CPLString osCPGName;
2874
0
    char **papszCandidates = VSIReadDir(osDirname);
2875
0
    int i = 0;
2876
0
    while (papszCandidates != nullptr && papszCandidates[i] != nullptr)
2877
0
    {
2878
0
        const CPLString osCandidateBasename =
2879
0
            CPLGetBasenameSafe(papszCandidates[i]);
2880
0
        const CPLString osCandidateExtension =
2881
0
            CPLGetExtensionSafe(papszCandidates[i]);
2882
#ifdef _WIN32
2883
        // On Windows, as filenames are case insensitive, a shapefile layer can
2884
        // be made of foo.shp and FOO.DBF, so use case insensitive comparison.
2885
        if (EQUAL(osCandidateBasename, osBasename))
2886
#else
2887
0
        if (osCandidateBasename.compare(osBasename) == 0)
2888
0
#endif
2889
0
        {
2890
0
            if (EQUAL(osCandidateExtension, "dbf"))
2891
0
                osDBFName =
2892
0
                    CPLFormFilenameSafe(osDirname, papszCandidates[i], nullptr);
2893
0
            else if (EQUAL(osCandidateExtension, "shp"))
2894
0
                osSHPName =
2895
0
                    CPLFormFilenameSafe(osDirname, papszCandidates[i], nullptr);
2896
0
            else if (EQUAL(osCandidateExtension, "shx"))
2897
0
                osSHXName =
2898
0
                    CPLFormFilenameSafe(osDirname, papszCandidates[i], nullptr);
2899
0
            else if (EQUAL(osCandidateExtension, "cpg"))
2900
0
                osCPGName =
2901
0
                    CPLFormFilenameSafe(osDirname, papszCandidates[i], nullptr);
2902
0
        }
2903
2904
0
        i++;
2905
0
    }
2906
0
    CSLDestroy(papszCandidates);
2907
0
    papszCandidates = nullptr;
2908
2909
0
    if (m_hDBF != nullptr && osDBFName.empty())
2910
0
    {
2911
0
        CPLError(CE_Failure, CPLE_AppDefined,
2912
0
                 "Cannot find the filename of the DBF file, but we managed to "
2913
0
                 "open it before !");
2914
        // Should not happen, really.
2915
0
        return OGRERR_FAILURE;
2916
0
    }
2917
2918
0
    if (m_hSHP != nullptr && osSHPName.empty())
2919
0
    {
2920
0
        CPLError(CE_Failure, CPLE_AppDefined,
2921
0
                 "Cannot find the filename of the SHP file, but we managed to "
2922
0
                 "open it before !");
2923
        // Should not happen, really.
2924
0
        return OGRERR_FAILURE;
2925
0
    }
2926
2927
0
    if (m_hSHP != nullptr && osSHXName.empty())
2928
0
    {
2929
0
        CPLError(CE_Failure, CPLE_AppDefined,
2930
0
                 "Cannot find the filename of the SHX file, but we managed to "
2931
0
                 "open it before !");
2932
        // Should not happen, really.
2933
0
        return OGRERR_FAILURE;
2934
0
    }
2935
2936
    /* -------------------------------------------------------------------- */
2937
    /*      Cleanup any existing spatial index.  It will become             */
2938
    /*      meaningless when the fids change.                               */
2939
    /* -------------------------------------------------------------------- */
2940
0
    if (CheckForQIX() || CheckForSBN())
2941
0
        DropSpatialIndex();
2942
2943
    /* -------------------------------------------------------------------- */
2944
    /*      Create a new dbf file, matching the old.                        */
2945
    /* -------------------------------------------------------------------- */
2946
0
    bool bMustReopenDBF = false;
2947
0
    CPLString oTempFileDBF;
2948
0
    const int nNewRecords =
2949
0
        m_nTotalShapeCount - static_cast<int>(anRecordsToDelete.size());
2950
2951
0
    if (m_hDBF != nullptr && !anRecordsToDelete.empty())
2952
0
    {
2953
0
        CPLDebug("Shape", "REPACK: repacking .dbf");
2954
0
        bMustReopenDBF = true;
2955
2956
0
        oTempFileDBF = CPLFormFilenameSafe(osDirname, osBasename, nullptr);
2957
0
        oTempFileDBF += "_packed.dbf";
2958
2959
0
        DBFHandle hNewDBF = DBFCloneEmpty(m_hDBF, oTempFileDBF);
2960
0
        if (hNewDBF == nullptr)
2961
0
        {
2962
0
            CPLError(CE_Failure, CPLE_OpenFailed,
2963
0
                     "Failed to create temp file %s.", oTempFileDBF.c_str());
2964
0
            return OGRERR_FAILURE;
2965
0
        }
2966
2967
        // Delete temporary .cpg file if existing.
2968
0
        if (!osCPGName.empty())
2969
0
        {
2970
0
            CPLString oCPGTempFile =
2971
0
                CPLFormFilenameSafe(osDirname, osBasename, nullptr);
2972
0
            oCPGTempFile += "_packed.cpg";
2973
0
            ForceDeleteFile(oCPGTempFile);
2974
0
        }
2975
2976
        /* --------------------------------------------------------------------
2977
         */
2978
        /*      Copy over all records that are not deleted. */
2979
        /* --------------------------------------------------------------------
2980
         */
2981
0
        int iDestShape = 0;
2982
0
        size_t iNextDeletedShape = 0;
2983
2984
0
        for (int iShape = 0; iShape < m_nTotalShapeCount && eErr == OGRERR_NONE;
2985
0
             iShape++)
2986
0
        {
2987
0
            if (iNextDeletedShape < anRecordsToDelete.size() &&
2988
0
                anRecordsToDelete[iNextDeletedShape] == iShape)
2989
0
            {
2990
0
                iNextDeletedShape++;
2991
0
            }
2992
0
            else
2993
0
            {
2994
0
                void *pTuple = const_cast<char *>(DBFReadTuple(m_hDBF, iShape));
2995
0
                if (pTuple == nullptr ||
2996
0
                    !DBFWriteTuple(hNewDBF, iDestShape++, pTuple))
2997
0
                {
2998
0
                    CPLError(CE_Failure, CPLE_AppDefined,
2999
0
                             "Error writing record %d in .dbf", iShape);
3000
0
                    eErr = OGRERR_FAILURE;
3001
0
                }
3002
0
            }
3003
0
        }
3004
3005
0
        DBFClose(hNewDBF);
3006
3007
0
        if (eErr != OGRERR_NONE)
3008
0
        {
3009
0
            VSIUnlink(oTempFileDBF);
3010
0
            return eErr;
3011
0
        }
3012
0
    }
3013
3014
    /* -------------------------------------------------------------------- */
3015
    /*      Now create a shapefile matching the old one.                    */
3016
    /* -------------------------------------------------------------------- */
3017
0
    bool bMustReopenSHP = m_hSHP != nullptr;
3018
0
    CPLString oTempFileSHP;
3019
0
    CPLString oTempFileSHX;
3020
3021
0
    SHPInfo sSHPInfo;
3022
0
    memset(&sSHPInfo, 0, sizeof(sSHPInfo));
3023
0
    unsigned int *panRecOffsetNew = nullptr;
3024
0
    unsigned int *panRecSizeNew = nullptr;
3025
3026
    // On Windows, use the pack-in-place approach, ie copy the content of
3027
    // the _packed files on top of the existing opened files. This avoids
3028
    // many issues with files being locked, at the expense of more I/O
3029
0
    const bool bPackInPlace =
3030
0
        CPLTestBool(CPLGetConfigOption("OGR_SHAPE_PACK_IN_PLACE",
3031
#ifdef _WIN32
3032
                                       "YES"
3033
#else
3034
0
                                       "NO"
3035
0
#endif
3036
0
                                       ));
3037
3038
0
    if (m_hSHP != nullptr)
3039
0
    {
3040
0
        CPLDebug("Shape", "REPACK: repacking .shp + .shx");
3041
3042
0
        oTempFileSHP = CPLFormFilenameSafe(osDirname, osBasename, nullptr);
3043
0
        oTempFileSHP += "_packed.shp";
3044
0
        oTempFileSHX = CPLFormFilenameSafe(osDirname, osBasename, nullptr);
3045
0
        oTempFileSHX += "_packed.shx";
3046
3047
0
        SHPHandle hNewSHP = SHPCreate(oTempFileSHP, m_hSHP->nShapeType);
3048
0
        if (hNewSHP == nullptr)
3049
0
        {
3050
0
            if (!oTempFileDBF.empty())
3051
0
                VSIUnlink(oTempFileDBF);
3052
0
            return OGRERR_FAILURE;
3053
0
        }
3054
3055
        /* --------------------------------------------------------------------
3056
         */
3057
        /*      Copy over all records that are not deleted. */
3058
        /* --------------------------------------------------------------------
3059
         */
3060
0
        size_t iNextDeletedShape = 0;
3061
3062
0
        for (int iShape = 0; iShape < m_nTotalShapeCount && eErr == OGRERR_NONE;
3063
0
             iShape++)
3064
0
        {
3065
0
            if (iNextDeletedShape < anRecordsToDelete.size() &&
3066
0
                anRecordsToDelete[iNextDeletedShape] == iShape)
3067
0
            {
3068
0
                iNextDeletedShape++;
3069
0
            }
3070
0
            else
3071
0
            {
3072
0
                SHPObject *hObject = SHPReadObject(m_hSHP, iShape);
3073
0
                if (hObject == nullptr ||
3074
0
                    SHPWriteObject(hNewSHP, -1, hObject) == -1)
3075
0
                {
3076
0
                    CPLError(CE_Failure, CPLE_AppDefined,
3077
0
                             "Error writing record %d in .shp", iShape);
3078
0
                    eErr = OGRERR_FAILURE;
3079
0
                }
3080
3081
0
                if (hObject)
3082
0
                    SHPDestroyObject(hObject);
3083
0
            }
3084
0
        }
3085
3086
0
        if (bPackInPlace)
3087
0
        {
3088
            // Backup information of the updated shape context so as to
3089
            // restore it later in the current shape context
3090
0
            memcpy(&sSHPInfo, hNewSHP, sizeof(sSHPInfo));
3091
3092
            // Use malloc like shapelib does
3093
0
            panRecOffsetNew = reinterpret_cast<unsigned int *>(
3094
0
                malloc(sizeof(unsigned int) * hNewSHP->nMaxRecords));
3095
0
            panRecSizeNew = reinterpret_cast<unsigned int *>(
3096
0
                malloc(sizeof(unsigned int) * hNewSHP->nMaxRecords));
3097
0
            if (panRecOffsetNew == nullptr || panRecSizeNew == nullptr)
3098
0
            {
3099
0
                CPLError(CE_Failure, CPLE_OutOfMemory,
3100
0
                         "Cannot allocate panRecOffsetNew/panRecSizeNew");
3101
0
                eErr = OGRERR_FAILURE;
3102
0
            }
3103
0
            else
3104
0
            {
3105
0
                memcpy(panRecOffsetNew, hNewSHP->panRecOffset,
3106
0
                       sizeof(unsigned int) * hNewSHP->nRecords);
3107
0
                memcpy(panRecSizeNew, hNewSHP->panRecSize,
3108
0
                       sizeof(unsigned int) * hNewSHP->nRecords);
3109
0
            }
3110
0
        }
3111
3112
0
        SHPClose(hNewSHP);
3113
3114
0
        if (eErr != OGRERR_NONE)
3115
0
        {
3116
0
            VSIUnlink(oTempFileSHP);
3117
0
            VSIUnlink(oTempFileSHX);
3118
0
            if (!oTempFileDBF.empty())
3119
0
                VSIUnlink(oTempFileDBF);
3120
0
            free(panRecOffsetNew);
3121
0
            free(panRecSizeNew);
3122
0
            return eErr;
3123
0
        }
3124
0
    }
3125
3126
    // We could also use pack in place for Unix but this involves extra I/O
3127
    // w.r.t to the delete and rename approach
3128
3129
0
    if (bPackInPlace)
3130
0
    {
3131
0
        if (m_hDBF != nullptr && !oTempFileDBF.empty())
3132
0
        {
3133
0
            if (!OGRShapeDataSource::CopyInPlace(VSI_SHP_GetVSIL(m_hDBF->fp),
3134
0
                                                 oTempFileDBF))
3135
0
            {
3136
0
                CPLError(
3137
0
                    CE_Failure, CPLE_FileIO,
3138
0
                    "An error occurred while copying the content of %s on top "
3139
0
                    "of %s. "
3140
0
                    "The non corrupted version is in the _packed.dbf, "
3141
0
                    "_packed.shp and _packed.shx files that you should rename "
3142
0
                    "on top of the main ones.",
3143
0
                    oTempFileDBF.c_str(), VSI_SHP_GetFilename(m_hDBF->fp));
3144
0
                free(panRecOffsetNew);
3145
0
                free(panRecSizeNew);
3146
3147
0
                DBFClose(m_hDBF);
3148
0
                m_hDBF = nullptr;
3149
0
                if (m_hSHP != nullptr)
3150
0
                {
3151
0
                    SHPClose(m_hSHP);
3152
0
                    m_hSHP = nullptr;
3153
0
                }
3154
3155
0
                return OGRERR_FAILURE;
3156
0
            }
3157
3158
            // Refresh current handle
3159
0
            m_hDBF->nRecords = nNewRecords;
3160
0
        }
3161
3162
0
        if (m_hSHP != nullptr && !oTempFileSHP.empty())
3163
0
        {
3164
0
            if (!OGRShapeDataSource::CopyInPlace(VSI_SHP_GetVSIL(m_hSHP->fpSHP),
3165
0
                                                 oTempFileSHP))
3166
0
            {
3167
0
                CPLError(
3168
0
                    CE_Failure, CPLE_FileIO,
3169
0
                    "An error occurred while copying the content of %s on top "
3170
0
                    "of %s. "
3171
0
                    "The non corrupted version is in the _packed.dbf, "
3172
0
                    "_packed.shp and _packed.shx files that you should rename "
3173
0
                    "on top of the main ones.",
3174
0
                    oTempFileSHP.c_str(), VSI_SHP_GetFilename(m_hSHP->fpSHP));
3175
0
                free(panRecOffsetNew);
3176
0
                free(panRecSizeNew);
3177
3178
0
                if (m_hDBF != nullptr)
3179
0
                {
3180
0
                    DBFClose(m_hDBF);
3181
0
                    m_hDBF = nullptr;
3182
0
                }
3183
0
                SHPClose(m_hSHP);
3184
0
                m_hSHP = nullptr;
3185
3186
0
                return OGRERR_FAILURE;
3187
0
            }
3188
0
            if (!OGRShapeDataSource::CopyInPlace(VSI_SHP_GetVSIL(m_hSHP->fpSHX),
3189
0
                                                 oTempFileSHX))
3190
0
            {
3191
0
                CPLError(
3192
0
                    CE_Failure, CPLE_FileIO,
3193
0
                    "An error occurred while copying the content of %s on top "
3194
0
                    "of %s. "
3195
0
                    "The non corrupted version is in the _packed.dbf, "
3196
0
                    "_packed.shp and _packed.shx files that you should rename "
3197
0
                    "on top of the main ones.",
3198
0
                    oTempFileSHX.c_str(), VSI_SHP_GetFilename(m_hSHP->fpSHX));
3199
0
                free(panRecOffsetNew);
3200
0
                free(panRecSizeNew);
3201
3202
0
                if (m_hDBF != nullptr)
3203
0
                {
3204
0
                    DBFClose(m_hDBF);
3205
0
                    m_hDBF = nullptr;
3206
0
                }
3207
0
                SHPClose(m_hSHP);
3208
0
                m_hSHP = nullptr;
3209
3210
0
                return OGRERR_FAILURE;
3211
0
            }
3212
3213
            // Refresh current handle
3214
0
            m_hSHP->nRecords = sSHPInfo.nRecords;
3215
0
            m_hSHP->nMaxRecords = sSHPInfo.nMaxRecords;
3216
0
            m_hSHP->nFileSize = sSHPInfo.nFileSize;
3217
0
            CPLAssert(sizeof(sSHPInfo.adBoundsMin) == 4 * sizeof(double));
3218
0
            memcpy(m_hSHP->adBoundsMin, sSHPInfo.adBoundsMin,
3219
0
                   sizeof(sSHPInfo.adBoundsMin));
3220
0
            memcpy(m_hSHP->adBoundsMax, sSHPInfo.adBoundsMax,
3221
0
                   sizeof(sSHPInfo.adBoundsMax));
3222
0
            free(m_hSHP->panRecOffset);
3223
0
            free(m_hSHP->panRecSize);
3224
0
            m_hSHP->panRecOffset = panRecOffsetNew;
3225
0
            m_hSHP->panRecSize = panRecSizeNew;
3226
0
        }
3227
0
        else
3228
0
        {
3229
            // The free() are not really necessary but CSA doesn't realize it
3230
0
            free(panRecOffsetNew);
3231
0
            free(panRecSizeNew);
3232
0
        }
3233
3234
        // Now that everything is successful, we can delete the temp files
3235
0
        if (!oTempFileDBF.empty())
3236
0
        {
3237
0
            ForceDeleteFile(oTempFileDBF);
3238
0
        }
3239
0
        if (!oTempFileSHP.empty())
3240
0
        {
3241
0
            ForceDeleteFile(oTempFileSHP);
3242
0
            ForceDeleteFile(oTempFileSHX);
3243
0
        }
3244
0
    }
3245
0
    else
3246
0
    {
3247
        /* --------------------------------------------------------------------
3248
         */
3249
        /*      Cleanup the old .dbf, .shp, .shx and rename the new ones. */
3250
        /* --------------------------------------------------------------------
3251
         */
3252
0
        if (!oTempFileDBF.empty())
3253
0
        {
3254
0
            DBFClose(m_hDBF);
3255
0
            m_hDBF = nullptr;
3256
3257
0
            if (VSIUnlink(osDBFName) != 0)
3258
0
            {
3259
0
                CPLError(CE_Failure, CPLE_FileIO,
3260
0
                         "Failed to delete old DBF file: %s",
3261
0
                         VSIStrerror(errno));
3262
3263
0
                m_hDBF =
3264
0
                    m_poDS->DS_DBFOpen(osDBFName, m_bUpdateAccess ? "r+" : "r");
3265
3266
0
                VSIUnlink(oTempFileDBF);
3267
3268
0
                return OGRERR_FAILURE;
3269
0
            }
3270
3271
0
            if (VSIRename(oTempFileDBF, osDBFName) != 0)
3272
0
            {
3273
0
                CPLError(CE_Failure, CPLE_FileIO,
3274
0
                         "Can not rename new DBF file: %s", VSIStrerror(errno));
3275
0
                return OGRERR_FAILURE;
3276
0
            }
3277
3278
0
            CheckFileDeletion(oTempFileDBF);
3279
0
        }
3280
3281
0
        if (!oTempFileSHP.empty())
3282
0
        {
3283
0
            SHPClose(m_hSHP);
3284
0
            m_hSHP = nullptr;
3285
3286
0
            if (VSIUnlink(osSHPName) != 0)
3287
0
            {
3288
0
                CPLError(CE_Failure, CPLE_FileIO,
3289
0
                         "Can not delete old SHP file: %s", VSIStrerror(errno));
3290
0
                return OGRERR_FAILURE;
3291
0
            }
3292
3293
0
            if (VSIUnlink(osSHXName) != 0)
3294
0
            {
3295
0
                CPLError(CE_Failure, CPLE_FileIO,
3296
0
                         "Can not delete old SHX file: %s", VSIStrerror(errno));
3297
0
                return OGRERR_FAILURE;
3298
0
            }
3299
3300
0
            if (VSIRename(oTempFileSHP, osSHPName) != 0)
3301
0
            {
3302
0
                CPLError(CE_Failure, CPLE_FileIO,
3303
0
                         "Can not rename new SHP file: %s", VSIStrerror(errno));
3304
0
                return OGRERR_FAILURE;
3305
0
            }
3306
3307
0
            if (VSIRename(oTempFileSHX, osSHXName) != 0)
3308
0
            {
3309
0
                CPLError(CE_Failure, CPLE_FileIO,
3310
0
                         "Can not rename new SHX file: %s", VSIStrerror(errno));
3311
0
                return OGRERR_FAILURE;
3312
0
            }
3313
3314
0
            CheckFileDeletion(oTempFileSHP);
3315
0
            CheckFileDeletion(oTempFileSHX);
3316
0
        }
3317
3318
        /* --------------------------------------------------------------------
3319
         */
3320
        /*      Reopen the shapefile */
3321
        /*                                                                      */
3322
        /* We do not need to reimplement OGRShapeDataSource::OpenFile() here */
3323
        /* with the fully featured error checking. */
3324
        /* If all operations above succeeded, then all necessary files are */
3325
        /* in the right place and accessible. */
3326
        /* --------------------------------------------------------------------
3327
         */
3328
3329
0
        const char *const pszAccess = m_bUpdateAccess ? "r+" : "r";
3330
3331
0
        if (bMustReopenSHP)
3332
0
            m_hSHP = m_poDS->DS_SHPOpen(osSHPName, pszAccess);
3333
0
        if (bMustReopenDBF)
3334
0
            m_hDBF = m_poDS->DS_DBFOpen(osDBFName, pszAccess);
3335
3336
0
        if ((bMustReopenSHP && nullptr == m_hSHP) ||
3337
0
            (bMustReopenDBF && nullptr == m_hDBF))
3338
0
            return OGRERR_FAILURE;
3339
0
    }
3340
3341
    /* -------------------------------------------------------------------- */
3342
    /*      Update total shape count.                                       */
3343
    /* -------------------------------------------------------------------- */
3344
0
    if (m_hDBF != nullptr)
3345
0
        m_nTotalShapeCount = m_hDBF->nRecords;
3346
0
    m_bSHPNeedsRepack = false;
3347
0
    m_eNeedRepack = NO;
3348
3349
0
    return OGRERR_NONE;
3350
0
}
3351
3352
/************************************************************************/
3353
/*                               ResizeDBF()                            */
3354
/*                                                                      */
3355
/*      Autoshrink columns of the DBF file to their minimum             */
3356
/*      size, according to the existing data.                           */
3357
/************************************************************************/
3358
3359
OGRErr OGRShapeLayer::ResizeDBF()
3360
3361
0
{
3362
0
    if (!StartUpdate("ResizeDBF"))
3363
0
        return OGRERR_FAILURE;
3364
3365
0
    if (m_hDBF == nullptr)
3366
0
    {
3367
0
        CPLError(
3368
0
            CE_Failure, CPLE_NotSupported,
3369
0
            "Attempt to RESIZE a shapefile with no .dbf file not supported.");
3370
0
        return OGRERR_FAILURE;
3371
0
    }
3372
3373
    /* Look which columns must be examined */
3374
0
    int *panColMap = static_cast<int *>(
3375
0
        CPLMalloc(m_poFeatureDefn->GetFieldCount() * sizeof(int)));
3376
0
    int *panBestWidth = static_cast<int *>(
3377
0
        CPLMalloc(m_poFeatureDefn->GetFieldCount() * sizeof(int)));
3378
0
    int nStringCols = 0;
3379
0
    for (int i = 0; i < m_poFeatureDefn->GetFieldCount(); i++)
3380
0
    {
3381
0
        if (m_poFeatureDefn->GetFieldDefn(i)->GetType() == OFTString ||
3382
0
            m_poFeatureDefn->GetFieldDefn(i)->GetType() == OFTInteger ||
3383
0
            m_poFeatureDefn->GetFieldDefn(i)->GetType() == OFTInteger64)
3384
0
        {
3385
0
            panColMap[nStringCols] = i;
3386
0
            panBestWidth[nStringCols] = 1;
3387
0
            nStringCols++;
3388
0
        }
3389
0
    }
3390
3391
0
    if (nStringCols == 0)
3392
0
    {
3393
        // Nothing to do.
3394
0
        CPLFree(panColMap);
3395
0
        CPLFree(panBestWidth);
3396
0
        return OGRERR_NONE;
3397
0
    }
3398
3399
0
    CPLDebug("SHAPE", "Computing optimal column size...");
3400
3401
0
    bool bAlreadyWarned = false;
3402
0
    for (int i = 0; i < m_hDBF->nRecords; i++)
3403
0
    {
3404
0
        if (!DBFIsRecordDeleted(m_hDBF, i))
3405
0
        {
3406
0
            for (int j = 0; j < nStringCols; j++)
3407
0
            {
3408
0
                if (DBFIsAttributeNULL(m_hDBF, i, panColMap[j]))
3409
0
                    continue;
3410
3411
0
                const char *pszVal =
3412
0
                    DBFReadStringAttribute(m_hDBF, i, panColMap[j]);
3413
0
                const int nLen = static_cast<int>(strlen(pszVal));
3414
0
                if (nLen > panBestWidth[j])
3415
0
                    panBestWidth[j] = nLen;
3416
0
            }
3417
0
        }
3418
0
        else if (!bAlreadyWarned)
3419
0
        {
3420
0
            bAlreadyWarned = true;
3421
0
            CPLDebug(
3422
0
                "SHAPE",
3423
0
                "DBF file would also need a REPACK due to deleted records");
3424
0
        }
3425
0
    }
3426
3427
0
    for (int j = 0; j < nStringCols; j++)
3428
0
    {
3429
0
        const int iField = panColMap[j];
3430
0
        OGRFieldDefn *const poFieldDefn = m_poFeatureDefn->GetFieldDefn(iField);
3431
3432
0
        const char chNativeType = DBFGetNativeFieldType(m_hDBF, iField);
3433
0
        char szFieldName[XBASE_FLDNAME_LEN_READ + 1] = {};
3434
0
        int nOriWidth = 0;
3435
0
        int nPrecision = 0;
3436
0
        DBFGetFieldInfo(m_hDBF, iField, szFieldName, &nOriWidth, &nPrecision);
3437
3438
0
        if (panBestWidth[j] < nOriWidth)
3439
0
        {
3440
0
            CPLDebug("SHAPE",
3441
0
                     "Shrinking field %d (%s) from %d to %d characters", iField,
3442
0
                     poFieldDefn->GetNameRef(), nOriWidth, panBestWidth[j]);
3443
3444
0
            if (!DBFAlterFieldDefn(m_hDBF, iField, szFieldName, chNativeType,
3445
0
                                   panBestWidth[j], nPrecision))
3446
0
            {
3447
0
                CPLError(
3448
0
                    CE_Failure, CPLE_AppDefined,
3449
0
                    "Shrinking field %d (%s) from %d to %d characters failed",
3450
0
                    iField, poFieldDefn->GetNameRef(), nOriWidth,
3451
0
                    panBestWidth[j]);
3452
3453
0
                CPLFree(panColMap);
3454
0
                CPLFree(panBestWidth);
3455
3456
0
                return OGRERR_FAILURE;
3457
0
            }
3458
0
            else
3459
0
            {
3460
0
                whileUnsealing(poFieldDefn)->SetWidth(panBestWidth[j]);
3461
0
            }
3462
0
        }
3463
0
    }
3464
3465
0
    TruncateDBF();
3466
3467
0
    CPLFree(panColMap);
3468
0
    CPLFree(panBestWidth);
3469
3470
0
    return OGRERR_NONE;
3471
0
}
3472
3473
/************************************************************************/
3474
/*                          TruncateDBF()                               */
3475
/************************************************************************/
3476
3477
void OGRShapeLayer::TruncateDBF()
3478
0
{
3479
0
    if (m_hDBF == nullptr)
3480
0
        return;
3481
3482
0
    m_hDBF->sHooks.FSeek(m_hDBF->fp, 0, SEEK_END);
3483
0
    vsi_l_offset nOldSize = m_hDBF->sHooks.FTell(m_hDBF->fp);
3484
0
    vsi_l_offset nNewSize =
3485
0
        m_hDBF->nRecordLength * static_cast<SAOffset>(m_hDBF->nRecords) +
3486
0
        m_hDBF->nHeaderLength;
3487
0
    if (m_hDBF->bWriteEndOfFileChar)
3488
0
        nNewSize++;
3489
0
    if (nNewSize < nOldSize)
3490
0
    {
3491
0
        CPLDebug("SHAPE",
3492
0
                 "Truncating DBF file from " CPL_FRMT_GUIB " to " CPL_FRMT_GUIB
3493
0
                 " bytes",
3494
0
                 nOldSize, nNewSize);
3495
0
        VSIFTruncateL(VSI_SHP_GetVSIL(m_hDBF->fp), nNewSize);
3496
0
    }
3497
0
    m_hDBF->sHooks.FSeek(m_hDBF->fp, 0, SEEK_SET);
3498
0
}
3499
3500
/************************************************************************/
3501
/*                        RecomputeExtent()                             */
3502
/*                                                                      */
3503
/*      Force recomputation of the extent of the .SHP file              */
3504
/************************************************************************/
3505
3506
OGRErr OGRShapeLayer::RecomputeExtent()
3507
0
{
3508
0
    if (!StartUpdate("RecomputeExtent"))
3509
0
        return OGRERR_FAILURE;
3510
3511
0
    if (m_hSHP == nullptr)
3512
0
    {
3513
0
        CPLError(CE_Failure, CPLE_AppDefined,
3514
0
                 "The RECOMPUTE EXTENT operation is not permitted on a layer "
3515
0
                 "without .SHP file.");
3516
0
        return OGRERR_FAILURE;
3517
0
    }
3518
3519
0
    double adBoundsMin[4] = {0.0, 0.0, 0.0, 0.0};
3520
0
    double adBoundsMax[4] = {0.0, 0.0, 0.0, 0.0};
3521
3522
0
    bool bHasBeenInit = false;
3523
3524
0
    for (int iShape = 0; iShape < m_nTotalShapeCount; iShape++)
3525
0
    {
3526
0
        if (m_hDBF == nullptr || !DBFIsRecordDeleted(m_hDBF, iShape))
3527
0
        {
3528
0
            SHPObject *psObject = SHPReadObject(m_hSHP, iShape);
3529
0
            if (psObject != nullptr && psObject->nSHPType != SHPT_NULL &&
3530
0
                psObject->nVertices != 0)
3531
0
            {
3532
0
                if (!bHasBeenInit)
3533
0
                {
3534
0
                    bHasBeenInit = true;
3535
0
                    adBoundsMin[0] = psObject->padfX[0];
3536
0
                    adBoundsMax[0] = psObject->padfX[0];
3537
0
                    adBoundsMin[1] = psObject->padfY[0];
3538
0
                    adBoundsMax[1] = psObject->padfY[0];
3539
0
                    if (psObject->padfZ)
3540
0
                    {
3541
0
                        adBoundsMin[2] = psObject->padfZ[0];
3542
0
                        adBoundsMax[2] = psObject->padfZ[0];
3543
0
                    }
3544
0
                    if (psObject->padfM)
3545
0
                    {
3546
0
                        adBoundsMin[3] = psObject->padfM[0];
3547
0
                        adBoundsMax[3] = psObject->padfM[0];
3548
0
                    }
3549
0
                }
3550
3551
0
                for (int i = 0; i < psObject->nVertices; i++)
3552
0
                {
3553
0
                    adBoundsMin[0] =
3554
0
                        std::min(adBoundsMin[0], psObject->padfX[i]);
3555
0
                    adBoundsMin[1] =
3556
0
                        std::min(adBoundsMin[1], psObject->padfY[i]);
3557
0
                    adBoundsMax[0] =
3558
0
                        std::max(adBoundsMax[0], psObject->padfX[i]);
3559
0
                    adBoundsMax[1] =
3560
0
                        std::max(adBoundsMax[1], psObject->padfY[i]);
3561
0
                    if (psObject->padfZ)
3562
0
                    {
3563
0
                        adBoundsMin[2] =
3564
0
                            std::min(adBoundsMin[2], psObject->padfZ[i]);
3565
0
                        adBoundsMax[2] =
3566
0
                            std::max(adBoundsMax[2], psObject->padfZ[i]);
3567
0
                    }
3568
0
                    if (psObject->padfM)
3569
0
                    {
3570
0
                        adBoundsMax[3] =
3571
0
                            std::max(adBoundsMax[3], psObject->padfM[i]);
3572
0
                        adBoundsMin[3] =
3573
0
                            std::min(adBoundsMin[3], psObject->padfM[i]);
3574
0
                    }
3575
0
                }
3576
0
            }
3577
0
            SHPDestroyObject(psObject);
3578
0
        }
3579
0
    }
3580
3581
0
    if (memcmp(m_hSHP->adBoundsMin, adBoundsMin, 4 * sizeof(double)) != 0 ||
3582
0
        memcmp(m_hSHP->adBoundsMax, adBoundsMax, 4 * sizeof(double)) != 0)
3583
0
    {
3584
0
        m_bHeaderDirty = true;
3585
0
        m_hSHP->bUpdated = TRUE;
3586
0
        memcpy(m_hSHP->adBoundsMin, adBoundsMin, 4 * sizeof(double));
3587
0
        memcpy(m_hSHP->adBoundsMax, adBoundsMax, 4 * sizeof(double));
3588
0
    }
3589
3590
0
    return OGRERR_NONE;
3591
0
}
3592
3593
/************************************************************************/
3594
/*                              TouchLayer()                            */
3595
/************************************************************************/
3596
3597
bool OGRShapeLayer::TouchLayer()
3598
0
{
3599
0
    m_poDS->SetLastUsedLayer(this);
3600
3601
0
    if (m_eFileDescriptorsState == FD_OPENED)
3602
0
        return true;
3603
0
    if (m_eFileDescriptorsState == FD_CANNOT_REOPEN)
3604
0
        return false;
3605
3606
0
    return ReopenFileDescriptors();
3607
0
}
3608
3609
/************************************************************************/
3610
/*                        ReopenFileDescriptors()                       */
3611
/************************************************************************/
3612
3613
bool OGRShapeLayer::ReopenFileDescriptors()
3614
0
{
3615
0
    CPLDebug("SHAPE", "ReopenFileDescriptors(%s)", m_osFullName.c_str());
3616
3617
0
    const bool bRealUpdateAccess =
3618
0
        m_bUpdateAccess &&
3619
0
        (!m_poDS->IsZip() || !m_poDS->GetTemporaryUnzipDir().empty());
3620
3621
0
    if (m_bHSHPWasNonNULL)
3622
0
    {
3623
0
        m_hSHP = m_poDS->DS_SHPOpen(m_osFullName.c_str(),
3624
0
                                    bRealUpdateAccess ? "r+" : "r");
3625
3626
0
        if (m_hSHP == nullptr)
3627
0
        {
3628
0
            m_eFileDescriptorsState = FD_CANNOT_REOPEN;
3629
0
            return false;
3630
0
        }
3631
0
    }
3632
3633
0
    if (m_bHDBFWasNonNULL)
3634
0
    {
3635
0
        m_hDBF = m_poDS->DS_DBFOpen(m_osFullName.c_str(),
3636
0
                                    bRealUpdateAccess ? "r+" : "r");
3637
3638
0
        if (m_hDBF == nullptr)
3639
0
        {
3640
0
            CPLError(
3641
0
                CE_Failure, CPLE_OpenFailed, "Cannot reopen %s",
3642
0
                CPLResetExtensionSafe(m_osFullName.c_str(), "dbf").c_str());
3643
0
            m_eFileDescriptorsState = FD_CANNOT_REOPEN;
3644
0
            return false;
3645
0
        }
3646
0
    }
3647
3648
0
    m_eFileDescriptorsState = FD_OPENED;
3649
3650
0
    return true;
3651
0
}
3652
3653
/************************************************************************/
3654
/*                        CloseUnderlyingLayer()                        */
3655
/************************************************************************/
3656
3657
void OGRShapeLayer::CloseUnderlyingLayer()
3658
0
{
3659
0
    CPLDebug("SHAPE", "CloseUnderlyingLayer(%s)", m_osFullName.c_str());
3660
3661
0
    if (m_hDBF != nullptr)
3662
0
        DBFClose(m_hDBF);
3663
0
    m_hDBF = nullptr;
3664
3665
0
    if (m_hSHP != nullptr)
3666
0
        SHPClose(m_hSHP);
3667
0
    m_hSHP = nullptr;
3668
3669
    // We close QIX and reset the check flag, so that CheckForQIX()
3670
    // will retry opening it if necessary when the layer is active again.
3671
0
    if (m_hQIX != nullptr)
3672
0
        SHPCloseDiskTree(m_hQIX);
3673
0
    m_hQIX = nullptr;
3674
0
    m_bCheckedForQIX = false;
3675
3676
0
    if (m_hSBN != nullptr)
3677
0
        SBNCloseDiskTree(m_hSBN);
3678
0
    m_hSBN = nullptr;
3679
0
    m_bCheckedForSBN = false;
3680
3681
0
    m_eFileDescriptorsState = FD_CLOSED;
3682
0
}
3683
3684
/************************************************************************/
3685
/*                            AddToFileList()                           */
3686
/************************************************************************/
3687
3688
void OGRShapeLayer::AddToFileList(CPLStringList &oFileList)
3689
0
{
3690
0
    if (!TouchLayer())
3691
0
        return;
3692
3693
0
    if (m_hSHP)
3694
0
    {
3695
0
        const char *pszSHPFilename = VSI_SHP_GetFilename(m_hSHP->fpSHP);
3696
0
        oFileList.AddStringDirectly(VSIGetCanonicalFilename(pszSHPFilename));
3697
0
        const std::string osSHPExt = CPLGetExtensionSafe(pszSHPFilename);
3698
0
        const std::string osSHXFilename = CPLResetExtensionSafe(
3699
0
            pszSHPFilename, (osSHPExt[0] == 's') ? "shx" : "SHX");
3700
0
        oFileList.AddStringDirectly(
3701
0
            VSIGetCanonicalFilename(osSHXFilename.c_str()));
3702
0
    }
3703
3704
0
    if (m_hDBF)
3705
0
    {
3706
0
        const char *pszDBFFilename = VSI_SHP_GetFilename(m_hDBF->fp);
3707
0
        oFileList.AddStringDirectly(VSIGetCanonicalFilename(pszDBFFilename));
3708
0
        if (m_hDBF->pszCodePage != nullptr && m_hDBF->iLanguageDriver == 0)
3709
0
        {
3710
0
            const std::string osDBFExt = CPLGetExtensionSafe(pszDBFFilename);
3711
0
            const std::string osCPGFilename = CPLResetExtensionSafe(
3712
0
                pszDBFFilename, (osDBFExt[0] == 'd') ? "cpg" : "CPG");
3713
0
            oFileList.AddStringDirectly(
3714
0
                VSIGetCanonicalFilename(osCPGFilename.c_str()));
3715
0
        }
3716
0
    }
3717
3718
0
    if (m_hSHP)
3719
0
    {
3720
0
        if (GetSpatialRef() != nullptr)
3721
0
        {
3722
0
            const OGRShapeGeomFieldDefn *poGeomFieldDefn =
3723
0
                cpl::down_cast<const OGRShapeGeomFieldDefn *>(
3724
0
                    GetLayerDefn()->GetGeomFieldDefn(0));
3725
0
            oFileList.AddStringDirectly(
3726
0
                VSIGetCanonicalFilename(poGeomFieldDefn->GetPrjFilename()));
3727
0
        }
3728
0
        if (CheckForQIX())
3729
0
        {
3730
0
            const std::string osQIXFilename =
3731
0
                CPLResetExtensionSafe(m_osFullName.c_str(), "qix");
3732
0
            oFileList.AddStringDirectly(
3733
0
                VSIGetCanonicalFilename(osQIXFilename.c_str()));
3734
0
        }
3735
0
        else if (CheckForSBN())
3736
0
        {
3737
0
            const std::string osSBNFilename =
3738
0
                CPLResetExtensionSafe(m_osFullName.c_str(), "sbn");
3739
0
            oFileList.AddStringDirectly(
3740
0
                VSIGetCanonicalFilename(osSBNFilename.c_str()));
3741
0
            const std::string osSBXFilename =
3742
0
                CPLResetExtensionSafe(m_osFullName.c_str(), "sbx");
3743
0
            oFileList.AddStringDirectly(
3744
0
                VSIGetCanonicalFilename(osSBXFilename.c_str()));
3745
0
        }
3746
0
    }
3747
0
}
3748
3749
/************************************************************************/
3750
/*                   UpdateFollowingDeOrRecompression()                 */
3751
/************************************************************************/
3752
3753
void OGRShapeLayer::UpdateFollowingDeOrRecompression()
3754
0
{
3755
0
    CPLAssert(m_poDS->IsZip());
3756
0
    CPLString osDSDir = m_poDS->GetTemporaryUnzipDir();
3757
0
    if (osDSDir.empty())
3758
0
        osDSDir = m_poDS->GetVSIZipPrefixeDir();
3759
3760
0
    if (GetSpatialRef() != nullptr)
3761
0
    {
3762
0
        OGRShapeGeomFieldDefn *poGeomFieldDefn =
3763
0
            cpl::down_cast<OGRShapeGeomFieldDefn *>(
3764
0
                GetLayerDefn()->GetGeomFieldDefn(0));
3765
0
        poGeomFieldDefn->SetPrjFilename(
3766
0
            CPLFormFilenameSafe(
3767
0
                osDSDir.c_str(),
3768
0
                CPLGetFilename(poGeomFieldDefn->GetPrjFilename().c_str()),
3769
0
                nullptr)
3770
0
                .c_str());
3771
0
    }
3772
3773
0
    m_osFullName = CPLFormFilenameSafe(
3774
0
        osDSDir, CPLGetFilename(m_osFullName.c_str()), nullptr);
3775
0
    CloseUnderlyingLayer();
3776
0
}
3777
3778
/************************************************************************/
3779
/*                           Rename()                                   */
3780
/************************************************************************/
3781
3782
OGRErr OGRShapeLayer::Rename(const char *pszNewName)
3783
0
{
3784
0
    if (!TestCapability(OLCRename))
3785
0
        return OGRERR_FAILURE;
3786
3787
0
    if (CPLLaunderForFilenameSafe(pszNewName, nullptr) != pszNewName)
3788
0
    {
3789
0
        CPLError(CE_Failure, CPLE_AppDefined,
3790
0
                 "Illegal characters in '%s' to form a valid filename",
3791
0
                 pszNewName);
3792
0
        return OGRERR_FAILURE;
3793
0
    }
3794
3795
0
    if (m_poDS->GetLayerByName(pszNewName) != nullptr)
3796
0
    {
3797
0
        CPLError(CE_Failure, CPLE_AppDefined, "Layer %s already exists",
3798
0
                 pszNewName);
3799
0
        return OGRERR_FAILURE;
3800
0
    }
3801
3802
0
    if (!m_poDS->UncompressIfNeeded())
3803
0
        return OGRERR_FAILURE;
3804
3805
0
    CPLStringList oFileList;
3806
0
    AddToFileList(oFileList);
3807
3808
0
    const std::string osDirname = CPLGetPathSafe(m_osFullName.c_str());
3809
0
    for (int i = 0; i < oFileList.size(); ++i)
3810
0
    {
3811
0
        const std::string osRenamedFile =
3812
0
            CPLFormFilenameSafe(osDirname.c_str(), pszNewName,
3813
0
                                CPLGetExtensionSafe(oFileList[i]).c_str());
3814
0
        VSIStatBufL sStat;
3815
0
        if (VSIStatL(osRenamedFile.c_str(), &sStat) == 0)
3816
0
        {
3817
0
            CPLError(CE_Failure, CPLE_AppDefined, "File %s already exists",
3818
0
                     osRenamedFile.c_str());
3819
0
            return OGRERR_FAILURE;
3820
0
        }
3821
0
    }
3822
3823
0
    CloseUnderlyingLayer();
3824
3825
0
    for (int i = 0; i < oFileList.size(); ++i)
3826
0
    {
3827
0
        const std::string osRenamedFile =
3828
0
            CPLFormFilenameSafe(osDirname.c_str(), pszNewName,
3829
0
                                CPLGetExtensionSafe(oFileList[i]).c_str());
3830
0
        if (VSIRename(oFileList[i], osRenamedFile.c_str()) != 0)
3831
0
        {
3832
0
            CPLError(CE_Failure, CPLE_AppDefined, "Cannot rename %s to %s",
3833
0
                     oFileList[i], osRenamedFile.c_str());
3834
0
            return OGRERR_FAILURE;
3835
0
        }
3836
0
    }
3837
3838
0
    if (GetSpatialRef() != nullptr)
3839
0
    {
3840
0
        OGRShapeGeomFieldDefn *poGeomFieldDefn =
3841
0
            cpl::down_cast<OGRShapeGeomFieldDefn *>(
3842
0
                GetLayerDefn()->GetGeomFieldDefn(0));
3843
0
        poGeomFieldDefn->SetPrjFilename(
3844
0
            CPLFormFilenameSafe(
3845
0
                osDirname.c_str(), pszNewName,
3846
0
                CPLGetExtensionSafe(poGeomFieldDefn->GetPrjFilename().c_str())
3847
0
                    .c_str())
3848
0
                .c_str());
3849
0
    }
3850
3851
0
    m_osFullName =
3852
0
        CPLFormFilenameSafe(osDirname.c_str(), pszNewName,
3853
0
                            CPLGetExtensionSafe(m_osFullName.c_str()).c_str());
3854
3855
0
    if (!ReopenFileDescriptors())
3856
0
        return OGRERR_FAILURE;
3857
3858
0
    SetDescription(pszNewName);
3859
0
    whileUnsealing(m_poFeatureDefn)->SetName(pszNewName);
3860
3861
0
    return OGRERR_NONE;
3862
0
}
3863
3864
/************************************************************************/
3865
/*                          GetDataset()                                */
3866
/************************************************************************/
3867
3868
GDALDataset *OGRShapeLayer::GetDataset()
3869
0
{
3870
0
    return m_poDS;
3871
0
}
3872
3873
/************************************************************************/
3874
/*                        GetNextArrowArray()                           */
3875
/************************************************************************/
3876
3877
// Specialized implementation restricted to situations where only retrieving
3878
// of FID values is asked (without filters)
3879
// In other cases, fall back to generic implementation.
3880
int OGRShapeLayer::GetNextArrowArray(struct ArrowArrayStream *stream,
3881
                                     struct ArrowArray *out_array)
3882
0
{
3883
0
    m_bLastGetNextArrowArrayUsedOptimizedCodePath = false;
3884
0
    if (!TouchLayer())
3885
0
    {
3886
0
        memset(out_array, 0, sizeof(*out_array));
3887
0
        return EIO;
3888
0
    }
3889
3890
0
    if (!m_hDBF || m_poAttrQuery != nullptr || m_poFilterGeom != nullptr)
3891
0
    {
3892
0
        return OGRLayer::GetNextArrowArray(stream, out_array);
3893
0
    }
3894
3895
    // If any field is not ignored, use generic implementation
3896
0
    const int nFieldCount = m_poFeatureDefn->GetFieldCount();
3897
0
    for (int i = 0; i < nFieldCount; ++i)
3898
0
    {
3899
0
        if (!m_poFeatureDefn->GetFieldDefn(i)->IsIgnored())
3900
0
            return OGRLayer::GetNextArrowArray(stream, out_array);
3901
0
    }
3902
0
    if (GetGeomType() != wkbNone &&
3903
0
        !m_poFeatureDefn->GetGeomFieldDefn(0)->IsIgnored())
3904
0
        return OGRLayer::GetNextArrowArray(stream, out_array);
3905
3906
0
    OGRArrowArrayHelper sHelper(m_poDS, m_poFeatureDefn,
3907
0
                                m_aosArrowArrayStreamOptions, out_array);
3908
0
    if (out_array->release == nullptr)
3909
0
    {
3910
0
        return ENOMEM;
3911
0
    }
3912
3913
0
    if (!sHelper.m_bIncludeFID)
3914
0
    {
3915
0
        out_array->release(out_array);
3916
0
        return OGRLayer::GetNextArrowArray(stream, out_array);
3917
0
    }
3918
3919
0
    m_bLastGetNextArrowArrayUsedOptimizedCodePath = true;
3920
0
    int nCount = 0;
3921
0
    while (m_iNextShapeId < m_nTotalShapeCount)
3922
0
    {
3923
0
        const bool bIsDeleted =
3924
0
            CPL_TO_BOOL(DBFIsRecordDeleted(m_hDBF, m_iNextShapeId));
3925
0
        if (bIsDeleted)
3926
0
        {
3927
0
            ++m_iNextShapeId;
3928
0
            continue;
3929
0
        }
3930
0
        if (VSIFEofL(VSI_SHP_GetVSIL(m_hDBF->fp)) ||
3931
0
            VSIFErrorL(VSI_SHP_GetVSIL(m_hDBF->fp)))
3932
0
        {
3933
0
            out_array->release(out_array);
3934
0
            memset(out_array, 0, sizeof(*out_array));
3935
0
            return EIO;
3936
0
        }
3937
0
        sHelper.m_panFIDValues[nCount] = m_iNextShapeId;
3938
0
        ++m_iNextShapeId;
3939
0
        ++nCount;
3940
0
        if (nCount == sHelper.m_nMaxBatchSize)
3941
0
            break;
3942
0
    }
3943
0
    sHelper.Shrink(nCount);
3944
0
    if (nCount == 0)
3945
0
    {
3946
0
        out_array->release(out_array);
3947
0
        memset(out_array, 0, sizeof(*out_array));
3948
0
    }
3949
0
    return 0;
3950
0
}
3951
3952
/************************************************************************/
3953
/*                        GetMetadataItem()                             */
3954
/************************************************************************/
3955
3956
const char *OGRShapeLayer::GetMetadataItem(const char *pszName,
3957
                                           const char *pszDomain)
3958
0
{
3959
0
    if (pszName && pszDomain && EQUAL(pszDomain, "__DEBUG__") &&
3960
0
        EQUAL(pszName, "LAST_GET_NEXT_ARROW_ARRAY_USED_OPTIMIZED_CODE_PATH"))
3961
0
    {
3962
0
        return m_bLastGetNextArrowArrayUsedOptimizedCodePath ? "YES" : "NO";
3963
0
    }
3964
0
    return OGRLayer::GetMetadataItem(pszName, pszDomain);
3965
0
}