File: mitab_mapindexblock.cpp

package info (click to toggle)
gdal 3.6.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 89,664 kB
  • sloc: cpp: 1,136,033; ansic: 197,355; python: 35,910; java: 5,511; xml: 4,011; sh: 3,950; cs: 2,443; yacc: 1,047; makefile: 288
file content (1485 lines) | stat: -rw-r--r-- 54,753 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
/**********************************************************************
 *
 * Name:     mitab_mapindexblock.cpp
 * Project:  MapInfo TAB Read/Write library
 * Language: C++
 * Purpose:  Implementation of the TABMAPIndexBlock class used to handle
 *           reading/writing of the .MAP files' index blocks
 * Author:   Daniel Morissette, dmorissette@dmsolutions.ca
 *
 **********************************************************************
 * Copyright (c) 1999, 2000, Daniel Morissette
 * Copyright (c) 2014, Even Rouault <even.rouault at spatialys.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **********************************************************************/

#include "cpl_port.h"
#include "mitab.h"

#include <cmath>
#include <cstdlib>
#include <cstring>

#include <algorithm>

#include "cpl_conv.h"
#include "cpl_error.h"
#include "cpl_vsi.h"
#include "mitab_priv.h"

/*=====================================================================
 *                      class TABMAPIndexBlock
 *====================================================================*/

/**********************************************************************
 *                   TABMAPIndexBlock::TABMAPIndexBlock()
 *
 * Constructor.
 **********************************************************************/
TABMAPIndexBlock::TABMAPIndexBlock(TABAccess eAccessMode /*= TABRead*/)
    : TABRawBinBlock(eAccessMode, TRUE), m_numEntries(0), m_nMinX(1000000000),
      m_nMinY(1000000000), m_nMaxX(-1000000000), m_nMaxY(-1000000000),
      m_poBlockManagerRef(nullptr), m_poCurChild(nullptr), m_nCurChildIndex(-1),
      m_poParentRef(nullptr)
{
    memset(m_asEntries, 0, sizeof(m_asEntries));
}

/**********************************************************************
 *                   TABMAPIndexBlock::~TABMAPIndexBlock()
 *
 * Destructor.
 **********************************************************************/
TABMAPIndexBlock::~TABMAPIndexBlock()
{
    UnsetCurChild();
}

/**********************************************************************
 *                   TABMAPIndexBlock::UnsetCurChild()
 **********************************************************************/

void TABMAPIndexBlock::UnsetCurChild()
{
    if (m_poCurChild)
    {
        if (m_eAccess == TABWrite || m_eAccess == TABReadWrite)
            m_poCurChild->CommitToFile();
        delete m_poCurChild;
        m_poCurChild = nullptr;
    }
    m_nCurChildIndex = -1;
}

/**********************************************************************
 *                   TABMAPIndexBlock::InitBlockFromData()
 *
 * Perform some initialization on the block after its binary data has
 * been set or changed (or loaded from a file).
 *
 * Returns 0 if successful or -1 if an error happened, in which case
 * CPLError() will have been called.
 **********************************************************************/
int TABMAPIndexBlock::InitBlockFromData(GByte *pabyBuf, int nBlockSize,
                                        int nSizeUsed,
                                        GBool bMakeCopy /* = TRUE */,
                                        VSILFILE *fpSrc /* = NULL */,
                                        int nOffset /* = 0 */)
{
    /*-----------------------------------------------------------------
     * First of all, we must call the base class' InitBlockFromData()
     *----------------------------------------------------------------*/
    const int nStatus = TABRawBinBlock::InitBlockFromData(
        pabyBuf, nBlockSize, nSizeUsed, bMakeCopy, fpSrc, nOffset);
    if (nStatus != 0)
        return nStatus;

    /*-----------------------------------------------------------------
     * Validate block type
     *----------------------------------------------------------------*/
    if (m_nBlockType != TABMAP_INDEX_BLOCK)
    {
        CPLError(CE_Failure, CPLE_FileIO,
                 "InitBlockFromData(): Invalid Block Type: got %d expected %d",
                 m_nBlockType, TABMAP_INDEX_BLOCK);
        CPLFree(m_pabyBuf);
        m_pabyBuf = nullptr;
        return -1;
    }

    /*-----------------------------------------------------------------
     * Init member variables
     *----------------------------------------------------------------*/
    GotoByteInBlock(0x002);
    m_numEntries = ReadInt16();

    if (m_numEntries > 0)
        ReadAllEntries();

    return 0;
}

/**********************************************************************
 *                   TABMAPIndexBlock::CommitToFile()
 *
 * Commit the current state of the binary block to the file to which
 * it has been previously attached.
 *
 * This method makes sure all values are properly set in the map object
 * block header and then calls TABRawBinBlock::CommitToFile() to do
 * the actual writing to disk.
 *
 * Returns 0 if successful or -1 if an error happened, in which case
 * CPLError() will have been called.
 **********************************************************************/
int TABMAPIndexBlock::CommitToFile()
{
    if (m_pabyBuf == nullptr)
    {
        CPLError(CE_Failure, CPLE_AssertionFailed,
                 "CommitToFile(): Block has not been initialized yet!");
        return -1;
    }

    /*-----------------------------------------------------------------
     * Commit child first
     *----------------------------------------------------------------*/
    if (m_poCurChild)
    {
        if (m_poCurChild->CommitToFile() != 0)
            return -1;
    }

    /*-----------------------------------------------------------------
     * Nothing to do here if block has not been modified
     *----------------------------------------------------------------*/
    if (!m_bModified)
        return 0;

    /*-----------------------------------------------------------------
     * Make sure 4 bytes block header is up to date.
     *----------------------------------------------------------------*/
    GotoByteInBlock(0x000);

    WriteInt16(TABMAP_INDEX_BLOCK);  // Block type code
    WriteInt16(static_cast<GInt16>(m_numEntries));

    int nStatus = CPLGetLastErrorType() == CE_Failure ? -1 : 0;

    /*-----------------------------------------------------------------
     * Loop through all entries, writing each of them, and calling
     * CommitToFile() (recursively) on any child index entries we may
     * encounter.
     *----------------------------------------------------------------*/
    for (int i = 0; nStatus == 0 && i < m_numEntries; i++)
    {
        nStatus = WriteNextEntry(&(m_asEntries[i]));
    }

    /*-----------------------------------------------------------------
     * OK, call the base class to write the block to disk.
     *----------------------------------------------------------------*/
    if (nStatus == 0)
    {
#ifdef DEBUG_VERBOSE
        CPLDebug("MITAB", "Committing INDEX block to offset %d", m_nFileOffset);
#endif
        nStatus = TABRawBinBlock::CommitToFile();
    }

    return nStatus;
}

/**********************************************************************
 *                   TABMAPIndexBlock::InitNewBlock()
 *
 * Initialize a newly created block so that it knows to which file it
 * is attached, its block size, etc . and then perform any specific
 * initialization for this block type, including writing a default
 * block header, etc. and leave the block ready to receive data.
 *
 * This is an alternative to calling ReadFromFile() or InitBlockFromData()
 * that puts the block in a stable state without loading any initial
 * data in it.
 *
 * Returns 0 if successful or -1 if an error happened, in which case
 * CPLError() will have been called.
 **********************************************************************/
int TABMAPIndexBlock::InitNewBlock(VSILFILE *fpSrc, int nBlockSize,
                                   int nFileOffset /* = 0*/)
{
    /*-----------------------------------------------------------------
     * Start with the default initialization
     *----------------------------------------------------------------*/
    if (TABRawBinBlock::InitNewBlock(fpSrc, nBlockSize, nFileOffset) != 0)
        return -1;

    /*-----------------------------------------------------------------
     * And then set default values for the block header.
     *----------------------------------------------------------------*/
    m_numEntries = 0;

    m_nMinX = 1000000000;
    m_nMinY = 1000000000;
    m_nMaxX = -1000000000;
    m_nMaxY = -1000000000;

    if (m_eAccess != TABRead && nFileOffset != 0)
    {
        GotoByteInBlock(0x000);

        WriteInt16(TABMAP_INDEX_BLOCK);  // Block type code
        WriteInt16(0);                   // num. index entries
    }

    if (CPLGetLastErrorType() == CE_Failure)
        return -1;

    return 0;
}

/**********************************************************************
 *                   TABMAPIndexBlock::ReadNextEntry()
 *
 * Read the next index entry from the block and fill the sEntry
 * structure.
 *
 * Returns 0 if successful or -1 if we reached the end of the block.
 **********************************************************************/
int TABMAPIndexBlock::ReadNextEntry(TABMAPIndexEntry *psEntry)
{
    if (m_nCurPos < 4)
        GotoByteInBlock(0x004);

    if (m_nCurPos > 4 + (20 * m_numEntries))
    {
        // End of BLock
        return -1;
    }

    psEntry->XMin = ReadInt32();
    psEntry->YMin = ReadInt32();
    psEntry->XMax = ReadInt32();
    psEntry->YMax = ReadInt32();
    psEntry->nBlockPtr = ReadInt32();

    if (CPLGetLastErrorType() == CE_Failure)
        return -1;

    return 0;
}

/**********************************************************************
 *                   TABMAPIndexBlock::ReadAllEntries()
 *
 * Init the block by reading all entries from the data block.
 *
 * Returns 0 if successful or -1 on error.
 **********************************************************************/
int TABMAPIndexBlock::ReadAllEntries()
{
    CPLAssert(m_numEntries <= GetMaxEntries());
    if (m_numEntries == 0)
        return 0;

    if (GotoByteInBlock(0x004) != 0)
        return -1;

    for (int i = 0; i < m_numEntries; i++)
    {
        if (ReadNextEntry(&(m_asEntries[i])) != 0)
            return -1;
    }

    return 0;
}

/**********************************************************************
 *                   TABMAPIndexBlock::WriteNextEntry()
 *
 * Write the sEntry index entry at current position in the block.
 *
 * Returns 0 if successful or -1 if we reached the end of the block.
 **********************************************************************/
int TABMAPIndexBlock::WriteNextEntry(TABMAPIndexEntry *psEntry)
{
    if (m_nCurPos < 4)
        GotoByteInBlock(0x004);

    WriteInt32(psEntry->XMin);
    WriteInt32(psEntry->YMin);
    WriteInt32(psEntry->XMax);
    WriteInt32(psEntry->YMax);
    WriteInt32(psEntry->nBlockPtr);

    if (CPLGetLastErrorType() == CE_Failure)
        return -1;

    return 0;
}

/**********************************************************************
 *                   TABMAPIndexBlock::GetNumFreeEntries()
 *
 * Return the number of available entries in this block.
 *
 * __TODO__ This function could eventually be improved to search
 *          children leaves as well.
 **********************************************************************/
int TABMAPIndexBlock::GetNumFreeEntries()
{
    return (m_nBlockSize - 4) / 20 - m_numEntries;
}

/**********************************************************************
 *                   TABMAPIndexBlock::GetEntry()
 *
 * Fetch a reference to the requested entry.
 *
 * @param iIndex index of entry, must be from 0 to GetNumEntries()-1.
 *
 * @return a reference to the internal copy of the entry, or NULL if out
 * of range.
 **********************************************************************/
TABMAPIndexEntry *TABMAPIndexBlock::GetEntry(int iIndex)
{
    if (iIndex < 0 || iIndex >= m_numEntries)
        return nullptr;

    return m_asEntries + iIndex;
}

/**********************************************************************
 *                   TABMAPIndexBlock::GetCurMaxDepth()
 *
 * Return maximum depth in the currently loaded part of the index tree
 **********************************************************************/
int TABMAPIndexBlock::GetCurMaxDepth()
{
    if (m_poCurChild)
        return m_poCurChild->GetCurMaxDepth() + 1;

    return 1; /* No current child... this node counts for one. */
}

/**********************************************************************
 *                   TABMAPIndexBlock::GetMBR()
 *
 * Return the MBR for the current block.
 **********************************************************************/
void TABMAPIndexBlock::GetMBR(GInt32 &nXMin, GInt32 &nYMin, GInt32 &nXMax,
                              GInt32 &nYMax)
{
    nXMin = m_nMinX;
    nYMin = m_nMinY;
    nXMax = m_nMaxX;
    nYMax = m_nMaxY;
}

/**********************************************************************
 *                   TABMAPIndexBlock::SetMBR()
 *
 **********************************************************************/
void TABMAPIndexBlock::SetMBR(GInt32 nXMin, GInt32 nYMin, GInt32 nXMax,
                              GInt32 nYMax)
{
    m_nMinX = nXMin;
    m_nMinY = nYMin;
    m_nMaxX = nXMax;
    m_nMaxY = nYMax;
}

/**********************************************************************
 *                   TABMAPIndexBlock::InsertEntry()
 *
 * Add a new entry to this index block.  It is assumed that there is at
 * least one free slot available, so if the block has to be split then it
 * should have been done prior to calling this function.
 *
 * Returns 0 on success, -1 on error.
 **********************************************************************/
int TABMAPIndexBlock::InsertEntry(GInt32 nXMin, GInt32 nYMin, GInt32 nXMax,
                                  GInt32 nYMax, GInt32 nBlockPtr)
{
    if (m_eAccess != TABWrite && m_eAccess != TABReadWrite)
    {
        CPLError(
            CE_Failure, CPLE_AssertionFailed,
            "Failed adding index entry: File not opened for write access.");
        return -1;
    }

    if (GetNumFreeEntries() < 1)
    {
        CPLError(CE_Failure, CPLE_AssertionFailed,
                 "Current Block Index is full, cannot add new entry.");
        return -1;
    }

    /*-----------------------------------------------------------------
     * Update count of entries and store new entry.
     *----------------------------------------------------------------*/
    m_numEntries++;
    CPLAssert(m_numEntries <= GetMaxEntries());

    m_asEntries[m_numEntries - 1].XMin = nXMin;
    m_asEntries[m_numEntries - 1].YMin = nYMin;
    m_asEntries[m_numEntries - 1].XMax = nXMax;
    m_asEntries[m_numEntries - 1].YMax = nYMax;
    m_asEntries[m_numEntries - 1].nBlockPtr = nBlockPtr;

    m_bModified = TRUE;

    return 0;
}

/**********************************************************************
 *                   TABMAPIndexBlock::ChooseSubEntryForInsert()
 *
 * Select the entry in this index block in which the new entry should
 * be inserted. The criteria used is to select the node whose MBR needs
 * the least enlargement to include the new entry. We resolve ties by
 * choosing the entry with the rectangle of smallest area.
 * (This is the ChooseSubtree part of Guttman's "ChooseLeaf" algorithm.)
 *
 * Returns the index of the best candidate or -1 of node is empty.
 **********************************************************************/
int TABMAPIndexBlock::ChooseSubEntryForInsert(GInt32 nXMin, GInt32 nYMin,
                                              GInt32 nXMax, GInt32 nYMax)
{
    GInt32 nBestCandidate = -1;

    double dOptimalAreaDiff = 0.0;

    const double dNewEntryArea = MITAB_AREA(nXMin, nYMin, nXMax, nYMax);

    for (GInt32 i = 0; i < m_numEntries; i++)
    {
        double dAreaDiff = 0.0;
        const double dAreaBefore =
            MITAB_AREA(m_asEntries[i].XMin, m_asEntries[i].YMin,
                       m_asEntries[i].XMax, m_asEntries[i].YMax);

        /* Does this entry fully contain the new entry's MBR ?
         */
        const GBool bIsContained =
            nXMin >= m_asEntries[i].XMin && nYMin >= m_asEntries[i].YMin &&
            nXMax <= m_asEntries[i].XMax && nYMax <= m_asEntries[i].YMax;

        if (bIsContained)
        {
            /* If new entry is fully contained in this entry then
             * the area difference will be the difference between the area
             * of the entry to insert and the area of m_asEntries[i]
             *
             * The diff value is negative in this case.
             */
            dAreaDiff = dNewEntryArea - dAreaBefore;
        }
        else
        {
            /* Need to calculate the expanded MBR to calculate the area
             * difference.
             */
            GInt32 nXMin2 = std::min(m_asEntries[i].XMin, nXMin);
            GInt32 nYMin2 = std::min(m_asEntries[i].YMin, nYMin);
            GInt32 nXMax2 = std::max(m_asEntries[i].XMax, nXMax);
            GInt32 nYMax2 = std::max(m_asEntries[i].YMax, nYMax);

            dAreaDiff =
                MITAB_AREA(nXMin2, nYMin2, nXMax2, nYMax2) - dAreaBefore;
        }

        /* Is this a better candidate?
         * Note, possible Optimization: In case of tie, we could to pick the
         * candidate with the smallest area
         */

        if (/* No best candidate yet */
            (nBestCandidate == -1)
            /* or current candidate is contained and best candidate is not
               contained */
            || (dAreaDiff < 0 && dOptimalAreaDiff >= 0)
            /* or if both are either contained or not contained then use the one
             * with the smallest area diff, which means maximum coverage in the
             * case of contained rects, or minimum area increase when not
             * contained
             */
            || (((dOptimalAreaDiff < 0 && dAreaDiff < 0) ||
                 (dOptimalAreaDiff > 0 && dAreaDiff > 0)) &&
                std::abs(dAreaDiff) < std::abs(dOptimalAreaDiff)))
        {
            nBestCandidate = i;
            dOptimalAreaDiff = dAreaDiff;
        }
    }

    return nBestCandidate;
}

/**********************************************************************
 *                   TABMAPIndexBlock::ChooseLeafForInsert()
 *
 * Recursively search the tree until we find the best leaf to
 * contain the specified object MBR.
 *
 * Returns the nBlockPtr of the selected leaf node entry (should be a
 * ref to a TABMAPObjectBlock) or -1 on error.
 *
 * After this call, m_poCurChild will be pointing at the selected child
 * node, for use by later calls to UpdateLeafEntry()
 **********************************************************************/
GInt32 TABMAPIndexBlock::ChooseLeafForInsert(GInt32 nXMin, GInt32 nYMin,
                                             GInt32 nXMax, GInt32 nYMax)
{
    GBool bFound = FALSE;

    if (m_numEntries < 0)
        return -1;

    /*-----------------------------------------------------------------
     * Look for the best candidate to contain the new entry
     *----------------------------------------------------------------*/

    // Make sure blocks currently in memory are written to disk.
    // TODO: Could we avoid deleting m_poCurChild if it is already
    //       the best candidate for insert?
    if (m_poCurChild)
    {
        m_poCurChild->CommitToFile();
        delete m_poCurChild;
        m_poCurChild = nullptr;
        m_nCurChildIndex = -1;
    }

    int nBestCandidate = ChooseSubEntryForInsert(nXMin, nYMin, nXMax, nYMax);

    CPLAssert(nBestCandidate != -1);
    if (nBestCandidate == -1)
        return -1; /* This should never happen! */

    // Try to load corresponding child... if it fails then we are
    // likely in a leaf node, so we'll add the new entry in the current
    // node.

    // Prevent error message if referred block not committed yet.
    CPLPushErrorHandler(CPLQuietErrorHandler);

    TABRawBinBlock *poBlock =
        TABCreateMAPBlockFromFile(m_fp, m_asEntries[nBestCandidate].nBlockPtr,
                                  m_nBlockSize, TRUE, TABReadWrite);
    if (poBlock != nullptr && poBlock->GetBlockClass() == TABMAP_INDEX_BLOCK)
    {
        m_poCurChild = cpl::down_cast<TABMAPIndexBlock *>(poBlock);
        poBlock = nullptr;
        m_nCurChildIndex = nBestCandidate;
        m_poCurChild->SetParentRef(this);
        m_poCurChild->SetMAPBlockManagerRef(m_poBlockManagerRef);
        bFound = TRUE;
    }

    if (poBlock)
        delete poBlock;

    CPLPopErrorHandler();
    CPLErrorReset();

    if (bFound)
    {
        /*-------------------------------------------------------------
         * Found a child leaf... pass the call to it.
         *------------------------------------------------------------*/
        return m_poCurChild->ChooseLeafForInsert(nXMin, nYMin, nXMax, nYMax);
    }

    /*-------------------------------------------------------------
     * Found no child index node... we must be at the leaf level
     * (leaf points at map object data blocks) so we return a ref
     * to the TABMAPObjBlock for insertion
     *------------------------------------------------------------*/
    return m_asEntries[nBestCandidate].nBlockPtr;
}

/**********************************************************************
 *                   TABMAPIndexBlock::GetCurLeafEntryMBR()
 *
 * Get the MBR for specified nBlockPtr in the leaf at the end of the
 * chain of m_poCurChild refs.
 *
 * This method requires that the chain of m_poCurChild refs already point
 * to a leaf that contains the specified nBlockPtr, it is usually called
 * right after ChooseLeafForInsert().
 *
 * Returns 0 on success, -1 on error.
 **********************************************************************/
int TABMAPIndexBlock::GetCurLeafEntryMBR(GInt32 nBlockPtr, GInt32 &nXMin,
                                         GInt32 &nYMin, GInt32 &nXMax,
                                         GInt32 &nYMax)
{
    if (m_poCurChild)
    {
        /* Pass the call down to current child */
        return m_poCurChild->GetCurLeafEntryMBR(nBlockPtr, nXMin, nYMin, nXMax,
                                                nYMax);
    }

    /* We're at the leaf level, look for the entry */
    for (int i = 0; i < m_numEntries; i++)
    {
        if (m_asEntries[i].nBlockPtr == nBlockPtr)
        {
            /* Found it. Return its MBR */
            nXMin = m_asEntries[i].XMin;
            nYMin = m_asEntries[i].YMin;
            nXMax = m_asEntries[i].XMax;
            nYMax = m_asEntries[i].YMax;

            return 0;
        }
    }

    /* Not found! This should not happen if method is used properly. */
    CPLError(CE_Failure, CPLE_AssertionFailed,
             "Entry to update not found in GetCurLeafEntryMBR()!");
    return -1;
}

/**********************************************************************
 *                   TABMAPIndexBlock::UpdateLeafEntry()
 *
 * Update the MBR for specified nBlockPtr in the leaf at the end of the
 * chain of m_poCurChild refs and update MBR of parents if required.
 *
 * This method requires that the chain of m_poCurChild refs already point
 * to a leaf that contains the specified nBlockPtr, it is usually called
 * right after ChooseLeafForInsert().
 *
 * Returns 0 on success, -1 on error.
 **********************************************************************/
int TABMAPIndexBlock::UpdateLeafEntry(GInt32 nBlockPtr, GInt32 nXMin,
                                      GInt32 nYMin, GInt32 nXMax, GInt32 nYMax)
{
    if (m_poCurChild)
    {
        /* Pass the call down to current child */
        return m_poCurChild->UpdateLeafEntry(nBlockPtr, nXMin, nYMin, nXMax,
                                             nYMax);
    }

    /* We're at the leaf level, look for the entry to update */
    for (int i = 0; i < m_numEntries; i++)
    {
        if (m_asEntries[i].nBlockPtr == nBlockPtr)
        {
            /* Found it. */
            TABMAPIndexEntry *psEntry = &m_asEntries[i];

            if (psEntry->XMin != nXMin || psEntry->YMin != nYMin ||
                psEntry->XMax != nXMax || psEntry->YMax != nYMax)
            {
                /* MBR changed. Update MBR of entry */
                psEntry->XMin = nXMin;
                psEntry->YMin = nYMin;
                psEntry->XMax = nXMax;
                psEntry->YMax = nYMax;

                m_bModified = TRUE;

                /* Update MBR of this node and all parents */
                RecomputeMBR();
            }

            return 0;
        }
    }

    /* Not found! This should not happen if method is used properly. */
    CPLError(CE_Failure, CPLE_AssertionFailed,
             "Entry to update not found in UpdateLeafEntry()!");
    return -1;
}

/**********************************************************************
 *                   TABMAPIndexBlock::AddEntry()
 *
 * Recursively search the tree until we encounter the best leaf to
 * contain the specified object MBR and add the new entry to it.
 *
 * In the even that the selected leaf node would be full, then it will be
 * split and this split can propagate up to its parent, etc.
 *
 * If bAddInThisNodeOnly=TRUE, then the entry is added only locally and
 * we do not try to update the child node.  This is used when the parent
 * of a node that is being split has to be updated.
 *
 * Returns 0 on success, -1 on error.
 **********************************************************************/
int TABMAPIndexBlock::AddEntry(GInt32 nXMin, GInt32 nYMin, GInt32 nXMax,
                               GInt32 nYMax, GInt32 nBlockPtr,
                               GBool bAddInThisNodeOnly /*=FALSE*/)
{
    GBool bFound = FALSE;

    if (m_eAccess != TABWrite && m_eAccess != TABReadWrite)
    {
        CPLError(
            CE_Failure, CPLE_AssertionFailed,
            "Failed adding index entry: File not opened for write access.");
        return -1;
    }

    /*-----------------------------------------------------------------
     * Look for the best candidate to contain the new entry
     *----------------------------------------------------------------*/

    /*-----------------------------------------------------------------
     * If bAddInThisNodeOnly=TRUE then we add the entry only locally
     * and do not need to look for the proper leaf to insert it.
     *----------------------------------------------------------------*/
    if (bAddInThisNodeOnly)
        bFound = TRUE;

    if (!bFound && m_numEntries > 0)
    {
        // Make sure blocks currently in memory are written to disk.
        if (m_poCurChild)
        {
            m_poCurChild->CommitToFile();
            delete m_poCurChild;
            m_poCurChild = nullptr;
            m_nCurChildIndex = -1;
        }

        int nBestCandidate =
            ChooseSubEntryForInsert(nXMin, nYMin, nXMax, nYMax);

        CPLAssert(nBestCandidate != -1);

        if (nBestCandidate != -1)
        {
            // Try to load corresponding child... if it fails then we are
            // likely in a leaf node, so we'll add the new entry in the current
            // node.

            // Prevent error message if referred block not committed yet.
            CPLPushErrorHandler(CPLQuietErrorHandler);

            TABRawBinBlock *poBlock = TABCreateMAPBlockFromFile(
                m_fp, m_asEntries[nBestCandidate].nBlockPtr, m_nBlockSize, TRUE,
                TABReadWrite);
            if (poBlock != nullptr &&
                poBlock->GetBlockClass() == TABMAP_INDEX_BLOCK)
            {
                m_poCurChild = cpl::down_cast<TABMAPIndexBlock *>(poBlock);
                poBlock = nullptr;
                m_nCurChildIndex = nBestCandidate;
                m_poCurChild->SetParentRef(this);
                m_poCurChild->SetMAPBlockManagerRef(m_poBlockManagerRef);
                bFound = TRUE;
            }

            if (poBlock)
                delete poBlock;

            CPLPopErrorHandler();
            CPLErrorReset();
        }
    }

    if (bFound && !bAddInThisNodeOnly)
    {
        /*-------------------------------------------------------------
         * Found a child leaf... pass the call to it.
         *------------------------------------------------------------*/
        if (m_poCurChild->AddEntry(nXMin, nYMin, nXMax, nYMax, nBlockPtr) != 0)
            return -1;
    }
    else
    {
        /*-------------------------------------------------------------
         * Found no child to store new object... we're likely at the leaf
         * level so we'll store new object in current node
         *------------------------------------------------------------*/

        /*-------------------------------------------------------------
         * First thing to do is make sure that there is room for a new
         * entry in this node, and to split it if necessary.
         *------------------------------------------------------------*/
        if (GetNumFreeEntries() < 1)
        {
            if (m_poParentRef == nullptr)
            {
                /*-----------------------------------------------------
                 * Splitting the root node adds one level to the tree, so
                 * after splitting we just redirect the call to the new
                 * child that's just been created.
                 *----------------------------------------------------*/
                if (SplitRootNode(nXMin, nYMin, nXMax, nYMax) != 0)
                    return -1;  // Error happened and has already been reported

                CPLAssert(m_poCurChild);
                return m_poCurChild->AddEntry(nXMin, nYMin, nXMax, nYMax,
                                              nBlockPtr, TRUE);
            }
            else
            {
                /*-----------------------------------------------------
                 * Splitting a regular node
                 *----------------------------------------------------*/
                if (SplitNode(nXMin, nYMin, nXMax, nYMax) != 0)
                    return -1;
            }
        }

        if (InsertEntry(nXMin, nYMin, nXMax, nYMax, nBlockPtr) != 0)
            return -1;
    }

    /*-----------------------------------------------------------------
     * Update current node MBR and the reference to it in our parent.
     *----------------------------------------------------------------*/
    RecomputeMBR();

    return 0;
}

/**********************************************************************
 *                   TABMAPIndexBlock::ComputeAreaDiff()
 *
 * (static method, also used by the TABMAPObjBlock class)
 *
 * Compute the area difference between two MBRs. Used in the SplitNode
 * algorithm to decide to which of the two nodes an entry should be added.
 *
 * The returned AreaDiff value is positive if NodeMBR has to be enlarged
 * and negative if new Entry is fully contained in the NodeMBR.
 **********************************************************************/
double TABMAPIndexBlock::ComputeAreaDiff(GInt32 nNodeXMin, GInt32 nNodeYMin,
                                         GInt32 nNodeXMax, GInt32 nNodeYMax,
                                         GInt32 nEntryXMin, GInt32 nEntryYMin,
                                         GInt32 nEntryXMax, GInt32 nEntryYMax)
{
    double dAreaDiff = 0.0;

    const double dNodeAreaBefore =
        MITAB_AREA(nNodeXMin, nNodeYMin, nNodeXMax, nNodeYMax);

    // Does the node fully contain the new entry's MBR?
    const GBool bIsContained =
        nEntryXMin >= nNodeXMin && nEntryYMin >= nNodeYMin &&
        nEntryXMax <= nNodeXMax && nEntryYMax <= nNodeYMax;

    if (bIsContained)
    {
        /* If new entry is fully contained in this entry then
         * the area difference will be the difference between the area
         * of the entry to insert and the area of the node
         */
        dAreaDiff = MITAB_AREA(nEntryXMin, nEntryYMin, nEntryXMax, nEntryYMax) -
                    dNodeAreaBefore;
    }
    else
    {
        /* Need to calculate the expanded MBR to calculate the area
         * difference.
         */
        nNodeXMin = std::min(nNodeXMin, nEntryXMin);
        nNodeYMin = std::min(nNodeYMin, nEntryYMin);
        nNodeXMax = std::max(nNodeXMax, nEntryXMax);
        nNodeYMax = std::max(nNodeYMax, nEntryYMax);

        dAreaDiff = MITAB_AREA(nNodeXMin, nNodeYMin, nNodeXMax, nNodeYMax) -
                    dNodeAreaBefore;
    }

    return dAreaDiff;
}

/**********************************************************************
 *                   TABMAPIndexBlock::PickSeedsForSplit()
 *
 * (static method, also used by the TABMAPObjBlock class)
 *
 * Pick two seeds to use to start splitting this node.
 *
 * Guttman's LinearPickSeed:
 * - Along each dimension find the entry whose rectangle has the
 *   highest low side, and the one with the lowest high side
 * - Calculate the separation for each pair
 * - Normalize the separation by dividing by the extents of the
 *   corresponding dimension
 * - Choose the pair with the greatest normalized separation along
 *   any dimension
 **********************************************************************/
int TABMAPIndexBlock::PickSeedsForSplit(
    TABMAPIndexEntry *pasEntries, int numEntries, int nSrcCurChildIndex,
    GInt32 nNewEntryXMin, GInt32 nNewEntryYMin, GInt32 nNewEntryXMax,
    GInt32 nNewEntryYMax, int &nSeed1, int &nSeed2)
{
    GInt32 nSrcMinX = 0;
    GInt32 nSrcMinY = 0;
    GInt32 nSrcMaxX = 0;
    GInt32 nSrcMaxY = 0;
    int nLowestMaxX = -1;
    int nHighestMinX = -1;
    int nLowestMaxY = -1;
    int nHighestMinY = -1;
    GInt32 nLowestMaxXId = -1;
    GInt32 nHighestMinXId = -1;
    GInt32 nLowestMaxYId = -1;
    GInt32 nHighestMinYId = -1;

    nSeed1 = -1;
    nSeed2 = -1;

    // Along each dimension find the entry whose rectangle has the
    // highest low side, and the one with the lowest high side
    for (int iEntry = 0; iEntry < numEntries; iEntry++)
    {
        if (nLowestMaxXId == -1 || pasEntries[iEntry].XMax < nLowestMaxX)
        {
            nLowestMaxX = pasEntries[iEntry].XMax;
            nLowestMaxXId = iEntry;
        }

        if (nHighestMinXId == -1 || pasEntries[iEntry].XMin > nHighestMinX)
        {
            nHighestMinX = pasEntries[iEntry].XMin;
            nHighestMinXId = iEntry;
        }

        if (nLowestMaxYId == -1 || pasEntries[iEntry].YMax < nLowestMaxY)
        {
            nLowestMaxY = pasEntries[iEntry].YMax;
            nLowestMaxYId = iEntry;
        }

        if (nHighestMinYId == -1 || pasEntries[iEntry].YMin > nHighestMinY)
        {
            nHighestMinY = pasEntries[iEntry].YMin;
            nHighestMinYId = iEntry;
        }

        // Also keep track of MBR of all entries
        if (iEntry == 0)
        {
            nSrcMinX = pasEntries[iEntry].XMin;
            nSrcMinY = pasEntries[iEntry].YMin;
            nSrcMaxX = pasEntries[iEntry].XMax;
            nSrcMaxY = pasEntries[iEntry].YMax;
        }
        else
        {
            nSrcMinX = std::min(nSrcMinX, pasEntries[iEntry].XMin);
            nSrcMinY = std::min(nSrcMinY, pasEntries[iEntry].YMin);
            nSrcMaxX = std::max(nSrcMaxX, pasEntries[iEntry].XMax);
            nSrcMaxY = std::max(nSrcMaxY, pasEntries[iEntry].YMax);
        }
    }

    const double dfSrcWidth =
        std::abs(static_cast<double>(nSrcMaxX) - nSrcMinX);
    const double dfSrcHeight =
        std::abs(static_cast<double>(nSrcMaxY) - nSrcMinY);

    // Calculate the separation for each pair (note that it may be negative
    // in case of overlap)
    // Normalize the separation by dividing by the extents of the
    // corresponding dimension
    const double dX =
        dfSrcWidth == 0.0
            ? 0.0
            : (static_cast<double>(nHighestMinX) - nLowestMaxX) / dfSrcWidth;
    const double dY =
        dfSrcHeight == 0.0
            ? 0.0
            : (static_cast<double>(nHighestMinY) - nLowestMaxY) / dfSrcHeight;

    // Choose the pair with the greatest normalized separation along
    // any dimension
    if (dX > dY)
    {
        nSeed1 = nHighestMinXId;
        nSeed2 = nLowestMaxXId;
    }
    else
    {
        nSeed1 = nHighestMinYId;
        nSeed2 = nLowestMaxYId;
    }

    // If nSeed1==nSeed2 then just pick any two (giving pref to current child)
    if (nSeed1 == nSeed2)
    {
        if (nSeed1 != nSrcCurChildIndex && nSrcCurChildIndex != -1)
            nSeed1 = nSrcCurChildIndex;
        else if (nSeed1 != 0)
            nSeed1 = 0;
        else
            nSeed1 = 1;
    }

    // Decide which of the two seeds best matches the new entry. That seed and
    // the new entry will stay in current node (new entry will be added by the
    // caller later). The other seed will go in the 2nd node
    const double dAreaDiff1 = ComputeAreaDiff(
        pasEntries[nSeed1].XMin, pasEntries[nSeed1].YMin,
        pasEntries[nSeed1].XMax, pasEntries[nSeed1].YMax, nNewEntryXMin,
        nNewEntryYMin, nNewEntryXMax, nNewEntryYMax);

    const double dAreaDiff2 = ComputeAreaDiff(
        pasEntries[nSeed2].XMin, pasEntries[nSeed2].YMin,
        pasEntries[nSeed2].XMax, pasEntries[nSeed2].YMax, nNewEntryXMin,
        nNewEntryYMin, nNewEntryXMax, nNewEntryYMax);

    /* Note that we want to keep this node's current child in here.
     * Since splitting happens only during an addentry() operation and
     * then both the current child and the New Entry should fit in the same
     * area.
     */
    if (nSeed1 != nSrcCurChildIndex &&
        (dAreaDiff1 > dAreaDiff2 || nSeed2 == nSrcCurChildIndex))
    {
        // Seed2 stays in this node, Seed1 moves to new node
        // ... swap Seed1 and Seed2 indices
        int nTmp = nSeed1;
        nSeed1 = nSeed2;
        nSeed2 = nTmp;
    }

    return 0;
}

/**********************************************************************
 *                   TABMAPIndexBlock::SplitNode()
 *
 * Split current Node, update the references in the parent node, etc.
 * Note that Root Nodes cannot be split using this method... SplitRootNode()
 * should be used instead.
 *
 * nNewEntry* are the coord. of the new entry that
 * will be added after the split.  The split is done so that the current
 * node will be the one in which the new object should be stored.
 *
 * Returns 0 on success, -1 on error.
 **********************************************************************/
int TABMAPIndexBlock::SplitNode(GInt32 nNewEntryXMin, GInt32 nNewEntryYMin,
                                GInt32 nNewEntryXMax, GInt32 nNewEntryYMax)
{
    CPLAssert(m_poBlockManagerRef);

    /*-----------------------------------------------------------------
     * Create a 2nd node
     *----------------------------------------------------------------*/
    TABMAPIndexBlock *poNewNode = new TABMAPIndexBlock(m_eAccess);
    if (poNewNode->InitNewBlock(m_fp, m_nBlockSize,
                                m_poBlockManagerRef->AllocNewBlock("INDEX")) !=
        0)
    {
        return -1;
    }
    poNewNode->SetMAPBlockManagerRef(m_poBlockManagerRef);

    /*-----------------------------------------------------------------
     * Make a temporary copy of the entries in current node
     *----------------------------------------------------------------*/
    int nSrcEntries = m_numEntries;
    TABMAPIndexEntry *pasSrcEntries = static_cast<TABMAPIndexEntry *>(
        CPLMalloc(m_numEntries * sizeof(TABMAPIndexEntry)));
    memcpy(pasSrcEntries, &m_asEntries,
           m_numEntries * sizeof(TABMAPIndexEntry));

    int nSrcCurChildIndex = m_nCurChildIndex;

    /*-----------------------------------------------------------------
     * Pick Seeds for each node
     *----------------------------------------------------------------*/
    int nSeed1, nSeed2;
    PickSeedsForSplit(pasSrcEntries, nSrcEntries, nSrcCurChildIndex,
                      nNewEntryXMin, nNewEntryYMin, nNewEntryXMax,
                      nNewEntryYMax, nSeed1, nSeed2);

    /*-----------------------------------------------------------------
     * Reset number of entries in this node and start moving new entries
     *----------------------------------------------------------------*/
    m_numEntries = 0;

    // Insert nSeed1 in this node
    InsertEntry(pasSrcEntries[nSeed1].XMin, pasSrcEntries[nSeed1].YMin,
                pasSrcEntries[nSeed1].XMax, pasSrcEntries[nSeed1].YMax,
                pasSrcEntries[nSeed1].nBlockPtr);

    // Move nSeed2 to 2nd node
    poNewNode->InsertEntry(
        pasSrcEntries[nSeed2].XMin, pasSrcEntries[nSeed2].YMin,
        pasSrcEntries[nSeed2].XMax, pasSrcEntries[nSeed2].YMax,
        pasSrcEntries[nSeed2].nBlockPtr);

    // Update cur child index if necessary
    if (nSeed1 == nSrcCurChildIndex)
        m_nCurChildIndex = m_numEntries - 1;

    /*-----------------------------------------------------------------
     * Go through the rest of the entries and assign them to one
     * of the 2 nodes.
     *
     * Criteria is minimal area difference.
     * Resolve ties by adding the entry to the node with smaller total
     * area, then to the one with fewer entries, then to either.
     *----------------------------------------------------------------*/
    for (int iEntry = 0; iEntry < nSrcEntries; iEntry++)
    {
        if (iEntry == nSeed1 || iEntry == nSeed2)
            continue;

        // If one of the two nodes is almost full then all remaining
        // entries should go to the other node
        // The entry corresponding to the current child also automatically
        // stays in this node.
        if (iEntry == nSrcCurChildIndex)
        {
            InsertEntry(pasSrcEntries[iEntry].XMin, pasSrcEntries[iEntry].YMin,
                        pasSrcEntries[iEntry].XMax, pasSrcEntries[iEntry].YMax,
                        pasSrcEntries[iEntry].nBlockPtr);

            // Update current child index
            m_nCurChildIndex = m_numEntries - 1;

            continue;
        }
        else if (m_numEntries >= GetMaxEntries() - 1)
        {
            poNewNode->InsertEntry(
                pasSrcEntries[iEntry].XMin, pasSrcEntries[iEntry].YMin,
                pasSrcEntries[iEntry].XMax, pasSrcEntries[iEntry].YMax,
                pasSrcEntries[iEntry].nBlockPtr);
            continue;
        }
        else if (poNewNode->GetNumEntries() >= GetMaxEntries() - 1)
        {
            InsertEntry(pasSrcEntries[iEntry].XMin, pasSrcEntries[iEntry].YMin,
                        pasSrcEntries[iEntry].XMax, pasSrcEntries[iEntry].YMax,
                        pasSrcEntries[iEntry].nBlockPtr);
            continue;
        }

        // Decide which of the two nodes to put this entry in
        RecomputeMBR();
        const double dAreaDiff1 = ComputeAreaDiff(
            m_nMinX, m_nMinY, m_nMaxX, m_nMaxY, pasSrcEntries[iEntry].XMin,
            pasSrcEntries[iEntry].YMin, pasSrcEntries[iEntry].XMax,
            pasSrcEntries[iEntry].YMax);

        GInt32 nXMin2 = 0;
        GInt32 nYMin2 = 0;
        GInt32 nXMax2 = 0;
        GInt32 nYMax2 = 0;
        poNewNode->RecomputeMBR();
        poNewNode->GetMBR(nXMin2, nYMin2, nXMax2, nYMax2);
        const double dAreaDiff2 = ComputeAreaDiff(
            nXMin2, nYMin2, nXMax2, nYMax2, pasSrcEntries[iEntry].XMin,
            pasSrcEntries[iEntry].YMin, pasSrcEntries[iEntry].XMax,
            pasSrcEntries[iEntry].YMax);
        if (dAreaDiff1 < dAreaDiff2)
        {
            // This entry stays in this node.
            InsertEntry(pasSrcEntries[iEntry].XMin, pasSrcEntries[iEntry].YMin,
                        pasSrcEntries[iEntry].XMax, pasSrcEntries[iEntry].YMax,
                        pasSrcEntries[iEntry].nBlockPtr);
        }
        else
        {
            // This entry goes to new node
            poNewNode->InsertEntry(
                pasSrcEntries[iEntry].XMin, pasSrcEntries[iEntry].YMin,
                pasSrcEntries[iEntry].XMax, pasSrcEntries[iEntry].YMax,
                pasSrcEntries[iEntry].nBlockPtr);
        }
    }

    /*-----------------------------------------------------------------
     * Recompute MBR and update current node info in parent
     *----------------------------------------------------------------*/
    RecomputeMBR();
    poNewNode->RecomputeMBR();

    /*-----------------------------------------------------------------
     * Add second node info to parent and then flush it to disk.
     * This may trigger splitting of parent
     *----------------------------------------------------------------*/
    CPLAssert(m_poParentRef);
    int nMinX, nMinY, nMaxX, nMaxY;
    poNewNode->GetMBR(nMinX, nMinY, nMaxX, nMaxY);
    m_poParentRef->AddEntry(nMinX, nMinY, nMaxX, nMaxY,
                            poNewNode->GetNodeBlockPtr(), TRUE);
    poNewNode->CommitToFile();
    delete poNewNode;

    CPLFree(pasSrcEntries);

    return 0;
}

/**********************************************************************
 *                   TABMAPIndexBlock::SplitRootNode()
 *
 * (private method)
 *
 * Split a Root Node.
 * First, a level of nodes must be added to the tree, then the contents
 * of what used to be the root node is moved 1 level down and then that
 * node is split like a regular node.
 *
 * Returns 0 on success, -1 on error
 **********************************************************************/
int TABMAPIndexBlock::SplitRootNode(GInt32 nNewEntryXMin, GInt32 nNewEntryYMin,
                                    GInt32 nNewEntryXMax, GInt32 nNewEntryYMax)
{
    CPLAssert(m_poBlockManagerRef);
    CPLAssert(m_poParentRef == nullptr);

    /*-----------------------------------------------------------------
     * Since a root note cannot be split, we add a level of nodes
     * under it and we'll do the split at that level.
     *----------------------------------------------------------------*/
    TABMAPIndexBlock *poNewNode = new TABMAPIndexBlock(m_eAccess);

    if (poNewNode->InitNewBlock(m_fp, m_nBlockSize,
                                m_poBlockManagerRef->AllocNewBlock("INDEX")) !=
        0)
    {
        return -1;
    }
    poNewNode->SetMAPBlockManagerRef(m_poBlockManagerRef);

    // Move all entries to the new child
    int nSrcEntries = m_numEntries;
    m_numEntries = 0;
    for (int iEntry = 0; iEntry < nSrcEntries; iEntry++)
    {
        poNewNode->InsertEntry(
            m_asEntries[iEntry].XMin, m_asEntries[iEntry].YMin,
            m_asEntries[iEntry].XMax, m_asEntries[iEntry].YMax,
            m_asEntries[iEntry].nBlockPtr);
    }

    /*-----------------------------------------------------------------
     * Transfer current child object to new node.
     *----------------------------------------------------------------*/
    if (m_poCurChild)
    {
        poNewNode->SetCurChildRef(m_poCurChild, m_nCurChildIndex);
        m_poCurChild->SetParentRef(poNewNode);
        m_poCurChild = nullptr;
        m_nCurChildIndex = -1;
    }

    /*-----------------------------------------------------------------
     * Place info about new child in current node.
     *----------------------------------------------------------------*/
    poNewNode->RecomputeMBR();
    int nMinX, nMinY, nMaxX, nMaxY;
    poNewNode->GetMBR(nMinX, nMinY, nMaxX, nMaxY);
    InsertEntry(nMinX, nMinY, nMaxX, nMaxY, poNewNode->GetNodeBlockPtr());

    /*-----------------------------------------------------------------
     * Keep a reference to the new child
     *----------------------------------------------------------------*/
    poNewNode->SetParentRef(this);
    m_poCurChild = poNewNode;
    m_nCurChildIndex = m_numEntries - 1;

    /*-----------------------------------------------------------------
     * And finally force the child to split itself
     *----------------------------------------------------------------*/
    return m_poCurChild->SplitNode(nNewEntryXMin, nNewEntryYMin, nNewEntryXMax,
                                   nNewEntryYMax);
}

/**********************************************************************
 *                   TABMAPIndexBlock::RecomputeMBR()
 *
 * Recompute current block MBR, and update info in parent.
 **********************************************************************/
void TABMAPIndexBlock::RecomputeMBR()
{
    GInt32 nMinX, nMinY, nMaxX, nMaxY;

    nMinX = 1000000000;
    nMinY = 1000000000;
    nMaxX = -1000000000;
    nMaxY = -1000000000;

    for (int i = 0; i < m_numEntries; i++)
    {
        if (m_asEntries[i].XMin < nMinX)
            nMinX = m_asEntries[i].XMin;
        if (m_asEntries[i].XMax > nMaxX)
            nMaxX = m_asEntries[i].XMax;

        if (m_asEntries[i].YMin < nMinY)
            nMinY = m_asEntries[i].YMin;
        if (m_asEntries[i].YMax > nMaxY)
            nMaxY = m_asEntries[i].YMax;
    }

    if (m_nMinX != nMinX || m_nMinY != nMinY || m_nMaxX != nMaxX ||
        m_nMaxY != nMaxY)
    {
        m_nMinX = nMinX;
        m_nMinY = nMinY;
        m_nMaxX = nMaxX;
        m_nMaxY = nMaxY;

        m_bModified = TRUE;

        if (m_poParentRef)
            m_poParentRef->UpdateCurChildMBR(m_nMinX, m_nMinY, m_nMaxX, m_nMaxY,
                                             GetNodeBlockPtr());
    }
}

/**********************************************************************
 *                   TABMAPIndexBlock::UpdateCurChildMBR()
 *
 * Update current child MBR info, and propagate info in parent.
 *
 * nBlockPtr is passed only to validate the consistency of the tree.
 **********************************************************************/
void TABMAPIndexBlock::UpdateCurChildMBR(GInt32 nXMin, GInt32 nYMin,
                                         GInt32 nXMax, GInt32 nYMax,
                                         CPL_UNUSED GInt32 nBlockPtr)
{
    CPLAssert(m_poCurChild);
    CPLAssert(m_asEntries[m_nCurChildIndex].nBlockPtr == nBlockPtr);

    if (m_asEntries[m_nCurChildIndex].XMin == nXMin &&
        m_asEntries[m_nCurChildIndex].YMin == nYMin &&
        m_asEntries[m_nCurChildIndex].XMax == nXMax &&
        m_asEntries[m_nCurChildIndex].YMax == nYMax)
    {
        return; /* Nothing changed... nothing to do */
    }

    m_bModified = TRUE;

    m_asEntries[m_nCurChildIndex].XMin = nXMin;
    m_asEntries[m_nCurChildIndex].YMin = nYMin;
    m_asEntries[m_nCurChildIndex].XMax = nXMax;
    m_asEntries[m_nCurChildIndex].YMax = nYMax;

    m_nMinX = 1000000000;
    m_nMinY = 1000000000;
    m_nMaxX = -1000000000;
    m_nMaxY = -1000000000;

    for (int i = 0; i < m_numEntries; i++)
    {
        if (m_asEntries[i].XMin < m_nMinX)
            m_nMinX = m_asEntries[i].XMin;
        if (m_asEntries[i].XMax > m_nMaxX)
            m_nMaxX = m_asEntries[i].XMax;

        if (m_asEntries[i].YMin < m_nMinY)
            m_nMinY = m_asEntries[i].YMin;
        if (m_asEntries[i].YMax > m_nMaxY)
            m_nMaxY = m_asEntries[i].YMax;
    }

    if (m_poParentRef)
        m_poParentRef->UpdateCurChildMBR(m_nMinX, m_nMinY, m_nMaxX, m_nMaxY,
                                         GetNodeBlockPtr());
}

/**********************************************************************
 *                   TABMAPIndexBlock::SetMAPBlockManagerRef()
 *
 * Pass a reference to the block manager object for the file this
 * block belongs to.  The block manager will be used by this object
 * when it needs to automatically allocate a new block.
 **********************************************************************/
void TABMAPIndexBlock::SetMAPBlockManagerRef(TABBinBlockManager *poBlockMgr)
{
    m_poBlockManagerRef = poBlockMgr;
}

/**********************************************************************
 *                   TABMAPIndexBlock::SetParentRef()
 *
 * Used to pass a reference to this node's parent.
 **********************************************************************/
void TABMAPIndexBlock::SetParentRef(TABMAPIndexBlock *poParent)
{
    m_poParentRef = poParent;
}

/**********************************************************************
 *                   TABMAPIndexBlock::SetCurChildRef()
 *
 * Used to transfer a child object from one node to another
 **********************************************************************/
void TABMAPIndexBlock::SetCurChildRef(TABMAPIndexBlock *poChild,
                                      int nChildIndex)
{
    m_poCurChild = poChild;
    m_nCurChildIndex = nChildIndex;
}

/**********************************************************************
 *                   TABMAPIndexBlock::Dump()
 *
 * Dump block contents... available only in DEBUG mode.
 **********************************************************************/
#ifdef DEBUG

void TABMAPIndexBlock::Dump(FILE *fpOut /*=NULL*/)
{
    if (fpOut == nullptr)
        fpOut = stdout;

    fprintf(fpOut, "----- TABMAPIndexBlock::Dump() -----\n");
    if (m_pabyBuf == nullptr)
    {
        fprintf(fpOut, "Block has not been initialized yet.");
    }
    else
    {
        fprintf(fpOut, "Index Block (type %d) at offset %d.\n", m_nBlockType,
                m_nFileOffset);
        fprintf(fpOut, "  m_numEntries          = %d\n", m_numEntries);

        /*-------------------------------------------------------------
         * Loop through all entries, dumping each of them
         *------------------------------------------------------------*/
        if (m_numEntries > 0)
            ReadAllEntries();

        for (int i = 0; i < m_numEntries; i++)
        {
            fprintf(fpOut, "    %6d -> (%d, %d) - (%d, %d)\n",
                    m_asEntries[i].nBlockPtr, m_asEntries[i].XMin,
                    m_asEntries[i].YMin, m_asEntries[i].XMax,
                    m_asEntries[i].YMax);
        }
    }

    fflush(fpOut);
}
#endif  // DEBUG