forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdalpamdataset.cpp
1796 lines (1509 loc) · 65.8 KB
/
gdalpamdataset.cpp
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
/******************************************************************************
*
* Project: GDAL Core
* Purpose: Implementation of GDALPamDataset, a dataset base class that
* knows how to persist auxiliary metadata into a support XML file.
* Author: Frank Warmerdam, [email protected]
*
******************************************************************************
* Copyright (c) 2005, Frank Warmerdam <[email protected]>
* Copyright (c) 2007-2013, Even Rouault <even dot 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 "gdal_pam.h"
#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <string>
#include "cpl_conv.h"
#include "cpl_error.h"
#include "cpl_minixml.h"
#include "cpl_progress.h"
#include "cpl_string.h"
#include "cpl_vsi.h"
#include "gdal.h"
#include "gdal_priv.h"
#include "ogr_core.h"
#include "ogr_spatialref.h"
CPL_CVSID("$Id$")
/************************************************************************/
/* GDALPamDataset() */
/************************************************************************/
/**
* \class GDALPamDataset "gdal_pam.h"
*
* A subclass of GDALDataset which introduces the ability to save and
* restore auxiliary information (coordinate system, gcps, metadata,
* etc) not supported by a file format via an "auxiliary metadata" file
* with the .aux.xml extension.
*
* <h3>Enabling PAM</h3>
*
* PAM support can be enabled (resp. disabled) in GDAL by setting the
* GDAL_PAM_ENABLED configuration option (via CPLSetConfigOption(), or the
* environment) to the value of YES (resp. NO). Note: The default value is
* build dependent and defaults to YES in Windows and Unix builds.
*
* <h3>PAM Proxy Files</h3>
*
* In order to be able to record auxiliary information about files on
* read-only media such as CDROMs or in directories where the user does not
* have write permissions, it is possible to enable the "PAM Proxy Database".
* When enabled the .aux.xml files are kept in a different directory, writable
* by the user. Overviews will also be stored in the PAM proxy directory.
*
* To enable this, set the GDAL_PAM_PROXY_DIR configuration option to be
* the name of the directory where the proxies should be kept. The configuration
* option must be set *before* the first access to PAM, because its value is
* cached for later access.
*
* <h3>Adding PAM to Drivers</h3>
*
* Drivers for physical file formats that wish to support persistent auxiliary
* metadata in addition to that for the format itself should derive their
* dataset class from GDALPamDataset instead of directly from GDALDataset.
* The raster band classes should also be derived from GDALPamRasterBand.
*
* They should also call something like this near the end of the Open()
* method:
*
* \code
* poDS->SetDescription( poOpenInfo->pszFilename );
* poDS->TryLoadXML();
* \endcode
*
* The SetDescription() is necessary so that the dataset will have a valid
* filename set as the description before TryLoadXML() is called. TryLoadXML()
* will look for an .aux.xml file with the same basename as the dataset and
* in the same directory. If found the contents will be loaded and kept
* track of in the GDALPamDataset and GDALPamRasterBand objects. When a
* call like GetProjectionRef() is not implemented by the format specific
* class, it will fall through to the PAM implementation which will return
* information if it was in the .aux.xml file.
*
* Drivers should also try to call the GDALPamDataset/GDALPamRasterBand
* methods as a fallback if their implementation does not find information.
* This allows using the .aux.xml for variations that can't be stored in
* the format. For instance, the GeoTIFF driver GetProjectionRef() looks
* like this:
*
* \code
* if( EQUAL(pszProjection,"") )
* return GDALPamDataset::GetProjectionRef();
* else
* return( pszProjection );
* \endcode
*
* So if the geotiff header is missing, the .aux.xml file will be
* consulted.
*
* Similarly, if SetProjection() were called with a coordinate system
* not supported by GeoTIFF, the SetProjection() method should pass it on
* to the GDALPamDataset::SetProjection() method after issuing a warning
* that the information can't be represented within the file itself.
*
* Drivers for subdataset based formats will also need to declare the
* name of the physical file they are related to, and the name of their
* subdataset before calling TryLoadXML().
*
* \code
* poDS->SetDescription( poOpenInfo->pszFilename );
* poDS->SetPhysicalFilename( poDS->pszFilename );
* poDS->SetSubdatasetName( osSubdatasetName );
*
* poDS->TryLoadXML();
* \endcode
*/
class GDALPamDataset;
GDALPamDataset::GDALPamDataset()
{
SetMOFlags( GetMOFlags() | GMO_PAM_CLASS );
}
/************************************************************************/
/* ~GDALPamDataset() */
/************************************************************************/
GDALPamDataset::~GDALPamDataset()
{
if( bSuppressOnClose )
{
if( psPam && psPam->pszPamFilename != nullptr )
VSIUnlink(psPam->pszPamFilename);
}
else if( nPamFlags & GPF_DIRTY )
{
CPLDebug( "GDALPamDataset", "In destructor with dirty metadata." );
GDALPamDataset::TrySaveXML();
}
PamClear();
}
/************************************************************************/
/* FlushCache() */
/************************************************************************/
void GDALPamDataset::FlushCache(bool bAtClosing)
{
GDALDataset::FlushCache(bAtClosing);
if( nPamFlags & GPF_DIRTY )
TrySaveXML();
}
/************************************************************************/
/* SerializeToXML() */
/************************************************************************/
//! @cond Doxygen_Suppress
CPLXMLNode *GDALPamDataset::SerializeToXML( const char *pszUnused )
{
if( psPam == nullptr )
return nullptr;
/* -------------------------------------------------------------------- */
/* Setup root node and attributes. */
/* -------------------------------------------------------------------- */
CPLXMLNode *psDSTree = CPLCreateXMLNode( nullptr, CXT_Element, "PAMDataset" );
/* -------------------------------------------------------------------- */
/* SRS */
/* -------------------------------------------------------------------- */
if( psPam->poSRS && !psPam->poSRS->IsEmpty() )
{
char* pszWKT = nullptr;
{
CPLErrorStateBackuper oErrorStateBackuper;
CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler);
if( psPam->poSRS->exportToWkt(&pszWKT) != OGRERR_NONE )
{
CPLFree(pszWKT);
pszWKT = nullptr;
const char* const apszOptions[] = { "FORMAT=WKT2", nullptr };
psPam->poSRS->exportToWkt(&pszWKT, apszOptions);
}
}
CPLXMLNode* psSRSNode = CPLCreateXMLElementAndValue( psDSTree, "SRS", pszWKT );
CPLFree(pszWKT);
const auto& mapping = psPam->poSRS->GetDataAxisToSRSAxisMapping();
CPLString osMapping;
for( size_t i = 0; i < mapping.size(); ++i )
{
if( !osMapping.empty() )
osMapping += ",";
osMapping += CPLSPrintf("%d", mapping[i]);
}
CPLAddXMLAttributeAndValue(psSRSNode, "dataAxisToSRSAxisMapping",
osMapping.c_str());
const double dfCoordinateEpoch = psPam->poSRS->GetCoordinateEpoch();
if( dfCoordinateEpoch > 0 )
{
std::string osCoordinateEpoch = CPLSPrintf("%f", dfCoordinateEpoch);
if( osCoordinateEpoch.find('.') != std::string::npos )
{
while( osCoordinateEpoch.back() == '0' )
osCoordinateEpoch.resize(osCoordinateEpoch.size()-1);
}
CPLAddXMLAttributeAndValue(psSRSNode, "coordinateEpoch",
osCoordinateEpoch.c_str());
}
}
/* -------------------------------------------------------------------- */
/* GeoTransform. */
/* -------------------------------------------------------------------- */
if( psPam->bHaveGeoTransform )
{
CPLString oFmt;
oFmt.Printf( "%24.16e,%24.16e,%24.16e,%24.16e,%24.16e,%24.16e",
psPam->adfGeoTransform[0],
psPam->adfGeoTransform[1],
psPam->adfGeoTransform[2],
psPam->adfGeoTransform[3],
psPam->adfGeoTransform[4],
psPam->adfGeoTransform[5] );
CPLSetXMLValue( psDSTree, "GeoTransform", oFmt );
}
/* -------------------------------------------------------------------- */
/* Metadata. */
/* -------------------------------------------------------------------- */
if( psPam->bHasMetadata )
{
CPLXMLNode *psMD = oMDMD.Serialize();
if( psMD != nullptr )
{
CPLAddXMLChild( psDSTree, psMD );
}
}
/* -------------------------------------------------------------------- */
/* GCPs */
/* -------------------------------------------------------------------- */
if( psPam->nGCPCount > 0 )
{
GDALSerializeGCPListToXML( psDSTree,
psPam->pasGCPList,
psPam->nGCPCount,
psPam->poGCP_SRS );
}
/* -------------------------------------------------------------------- */
/* Process bands. */
/* -------------------------------------------------------------------- */
// Find last child
CPLXMLNode* psLastChild = psDSTree->psChild;
for( ; psLastChild != nullptr && psLastChild->psNext;
psLastChild = psLastChild->psNext )
{
}
for( int iBand = 0; iBand < GetRasterCount(); iBand++ )
{
GDALRasterBand * const poBand = GetRasterBand(iBand+1);
if( poBand == nullptr || !(poBand->GetMOFlags() & GMO_PAM_CLASS) )
continue;
CPLXMLNode * const psBandTree =
cpl::down_cast<GDALPamRasterBand *>(poBand)->SerializeToXML( pszUnused );
if( psBandTree != nullptr )
{
if( psLastChild == nullptr )
{
CPLAddXMLChild( psDSTree, psBandTree );
}
else
{
psLastChild->psNext = psBandTree;
}
psLastChild = psBandTree;
}
}
/* -------------------------------------------------------------------- */
/* We don't want to return anything if we had no metadata to */
/* attach. */
/* -------------------------------------------------------------------- */
if( psDSTree->psChild == nullptr )
{
CPLDestroyXMLNode( psDSTree );
psDSTree = nullptr;
}
return psDSTree;
}
/************************************************************************/
/* PamInitialize() */
/************************************************************************/
void GDALPamDataset::PamInitialize()
{
#ifdef PAM_ENABLED
const char * const pszPamDefault = "YES";
#else
const char * const pszPamDefault = "NO";
#endif
if( psPam || (nPamFlags & GPF_DISABLED) )
return;
if( !CPLTestBool( CPLGetConfigOption( "GDAL_PAM_ENABLED",
pszPamDefault ) ) )
{
nPamFlags |= GPF_DISABLED;
return;
}
/* ERO 2011/04/13 : GPF_AUXMODE seems to be unimplemented */
if( EQUAL( CPLGetConfigOption( "GDAL_PAM_MODE", "PAM" ), "AUX") )
nPamFlags |= GPF_AUXMODE;
psPam = new GDALDatasetPamInfo;
for( int iBand = 0; iBand < GetRasterCount(); iBand++ )
{
GDALRasterBand *poBand = GetRasterBand(iBand+1);
if( poBand == nullptr || !(poBand->GetMOFlags() & GMO_PAM_CLASS) )
continue;
cpl::down_cast<GDALPamRasterBand *>(poBand)->PamInitialize();
}
}
/************************************************************************/
/* PamClear() */
/************************************************************************/
void GDALPamDataset::PamClear()
{
if( psPam )
{
CPLFree( psPam->pszPamFilename );
if( psPam->poSRS )
psPam->poSRS->Release();
if( psPam->poGCP_SRS )
psPam->poGCP_SRS->Release();
if( psPam->nGCPCount > 0 )
{
GDALDeinitGCPs( psPam->nGCPCount, psPam->pasGCPList );
CPLFree( psPam->pasGCPList );
}
delete psPam;
psPam = nullptr;
}
}
/************************************************************************/
/* XMLInit() */
/************************************************************************/
CPLErr GDALPamDataset::XMLInit( CPLXMLNode *psTree, const char *pszUnused )
{
/* -------------------------------------------------------------------- */
/* Check for an SRS node. */
/* -------------------------------------------------------------------- */
CPLXMLNode* psSRSNode = CPLGetXMLNode(psTree, "SRS");
if( psSRSNode )
{
if( psPam->poSRS )
psPam->poSRS->Release();
psPam->poSRS = new OGRSpatialReference();
psPam->poSRS->SetFromUserInput( CPLGetXMLValue(psSRSNode, nullptr, ""),
OGRSpatialReference::SET_FROM_USER_INPUT_LIMITATIONS );
const char* pszMapping =
CPLGetXMLValue(psSRSNode, "dataAxisToSRSAxisMapping", nullptr);
if( pszMapping )
{
char** papszTokens = CSLTokenizeStringComplex( pszMapping, ",", FALSE, FALSE);
std::vector<int> anMapping;
for( int i = 0; papszTokens && papszTokens[i]; i++ )
{
anMapping.push_back(atoi(papszTokens[i]));
}
CSLDestroy(papszTokens);
psPam->poSRS->SetDataAxisToSRSAxisMapping(anMapping);
}
else
{
psPam->poSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
}
const char* pszCoordinateEpoch =
CPLGetXMLValue(psSRSNode, "coordinateEpoch", nullptr);
if( pszCoordinateEpoch )
psPam->poSRS->SetCoordinateEpoch(CPLAtof(pszCoordinateEpoch));
}
/* -------------------------------------------------------------------- */
/* Check for a GeoTransform node. */
/* -------------------------------------------------------------------- */
if( strlen(CPLGetXMLValue(psTree, "GeoTransform", "")) > 0 )
{
const char *pszGT = CPLGetXMLValue(psTree, "GeoTransform", "");
char **papszTokens =
CSLTokenizeStringComplex( pszGT, ",", FALSE, FALSE );
if( CSLCount(papszTokens) != 6 )
{
CPLError( CE_Warning, CPLE_AppDefined,
"GeoTransform node does not have expected six values.");
}
else
{
for( int iTA = 0; iTA < 6; iTA++ )
psPam->adfGeoTransform[iTA] = CPLAtof(papszTokens[iTA]);
psPam->bHaveGeoTransform = TRUE;
}
CSLDestroy( papszTokens );
}
/* -------------------------------------------------------------------- */
/* Check for GCPs. */
/* -------------------------------------------------------------------- */
CPLXMLNode *psGCPList = CPLGetXMLNode( psTree, "GCPList" );
if( psGCPList != nullptr )
{
if( psPam->poGCP_SRS )
psPam->poGCP_SRS->Release();
psPam->poGCP_SRS = nullptr;
// Make sure any previous GCPs, perhaps from an .aux file, are cleared
// if we have new ones.
if( psPam->nGCPCount > 0 )
{
GDALDeinitGCPs( psPam->nGCPCount, psPam->pasGCPList );
CPLFree( psPam->pasGCPList );
psPam->nGCPCount = 0;
psPam->pasGCPList = nullptr;
}
GDALDeserializeGCPListFromXML( psGCPList,
&(psPam->pasGCPList),
&(psPam->nGCPCount),
&(psPam->poGCP_SRS) );
}
/* -------------------------------------------------------------------- */
/* Apply any dataset level metadata. */
/* -------------------------------------------------------------------- */
if( oMDMD.XMLInit( psTree, TRUE ) )
{
psPam->bHasMetadata = TRUE;
}
/* -------------------------------------------------------------------- */
/* Try loading ESRI xml encoded GeodataXform. */
/* -------------------------------------------------------------------- */
if (psPam->poSRS == nullptr)
{
// ArcGIS 9.3: GeodataXform as a root element
CPLXMLNode* psGeodataXform = CPLGetXMLNode(psTree, "=GeodataXform");
CPLXMLNode *psValueAsXML = nullptr;
if( psGeodataXform != nullptr )
{
char* apszMD[2];
apszMD[0] = CPLSerializeXMLTree(psGeodataXform);
apszMD[1] = nullptr;
oMDMD.SetMetadata( apszMD, "xml:ESRI");
CPLFree(apszMD[0]);
}
else
{
// ArcGIS 10: GeodataXform as content of xml:ESRI metadata domain.
char** papszXML = oMDMD.GetMetadata( "xml:ESRI" );
if (CSLCount(papszXML) == 1)
{
psValueAsXML = CPLParseXMLString( papszXML[0] );
if( psValueAsXML )
psGeodataXform = CPLGetXMLNode(psValueAsXML, "=GeodataXform");
}
}
if (psGeodataXform)
{
const char* pszESRI_WKT = CPLGetXMLValue(psGeodataXform,
"SpatialReference.WKT", nullptr);
if (pszESRI_WKT)
{
psPam->poSRS = new OGRSpatialReference(nullptr);
psPam->poSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
if( psPam->poSRS->importFromWkt(pszESRI_WKT) != OGRERR_NONE)
{
delete psPam->poSRS;
psPam->poSRS = nullptr;
}
}
// Parse GCPs
const CPLXMLNode* psSourceGCPS = CPLGetXMLNode(psGeodataXform, "SourceGCPs");
const CPLXMLNode* psTargetGCPs = CPLGetXMLNode(psGeodataXform, "TargetGCPs");
if( psSourceGCPS && psTargetGCPs && !psPam->bHaveGeoTransform )
{
std::vector<double> adfSource;
std::vector<double> adfTarget;
bool ySourceAllNegative = true;
for( auto psIter = psSourceGCPS->psChild; psIter; psIter = psIter->psNext )
{
if( psIter->eType == CXT_Element && strcmp(psIter->pszValue, "Double") == 0 )
{
adfSource.push_back(CPLAtof(CPLGetXMLValue(psIter, nullptr, "0")));
if( (adfSource.size() % 2) == 0 && adfSource.back() > 0 )
ySourceAllNegative = false;
}
}
for( auto psIter = psTargetGCPs->psChild; psIter; psIter = psIter->psNext )
{
if( psIter->eType == CXT_Element && strcmp(psIter->pszValue, "Double") == 0 )
{
adfTarget.push_back(CPLAtof(CPLGetXMLValue(psIter, nullptr, "0")));
}
}
if( !adfSource.empty() &&
adfSource.size() == adfTarget.size() &&
(adfSource.size() % 2) == 0 )
{
std::vector<GDAL_GCP> asGCPs;
asGCPs.resize(adfSource.size() / 2);
char szEmptyString[] = { 0 };
for( size_t i = 0; i+1 < adfSource.size(); i+= 2 )
{
asGCPs[i/2].pszId = szEmptyString;
asGCPs[i/2].pszInfo = szEmptyString;
asGCPs[i/2].dfGCPPixel = adfSource[i];
asGCPs[i/2].dfGCPLine =
ySourceAllNegative ? -adfSource[i+1] : adfSource[i+1];
asGCPs[i/2].dfGCPX = adfTarget[i];
asGCPs[i/2].dfGCPY = adfTarget[i+1];
asGCPs[i/2].dfGCPZ = 0;
}
GDALPamDataset::SetGCPs( static_cast<int>(asGCPs.size()),
&asGCPs[0],
psPam->poSRS );
delete psPam->poSRS;
psPam->poSRS = nullptr;
}
}
}
if( psValueAsXML )
CPLDestroyXMLNode(psValueAsXML);
}
/* -------------------------------------------------------------------- */
/* Process bands. */
/* -------------------------------------------------------------------- */
for( CPLXMLNode *psBandTree = psTree->psChild;
psBandTree != nullptr;
psBandTree = psBandTree->psNext )
{
if( psBandTree->eType != CXT_Element
|| !EQUAL(psBandTree->pszValue,"PAMRasterBand") )
continue;
const int nBand = atoi(CPLGetXMLValue( psBandTree, "band", "0"));
if( nBand < 1 || nBand > GetRasterCount() )
continue;
GDALRasterBand *poBand = GetRasterBand(nBand);
if( poBand == nullptr || !(poBand->GetMOFlags() & GMO_PAM_CLASS) )
continue;
GDALPamRasterBand *poPamBand = cpl::down_cast<GDALPamRasterBand *>(
GetRasterBand(nBand) );
poPamBand->XMLInit( psBandTree, pszUnused );
}
/* -------------------------------------------------------------------- */
/* Preserve Array information. */
/* -------------------------------------------------------------------- */
for( CPLXMLNode* psIter = psTree->psChild;
psIter; psIter = psIter->psNext )
{
if( psIter->eType == CXT_Element &&
strcmp(psIter->pszValue, "Array") == 0 )
{
CPLXMLNode* psNextBackup = psIter->psNext;
psIter->psNext = nullptr;
psPam->m_apoOtherNodes.emplace_back(CPLXMLTreeCloser(CPLCloneXMLTree(psIter)));
psIter->psNext = psNextBackup;
}
}
/* -------------------------------------------------------------------- */
/* Clear dirty flag. */
/* -------------------------------------------------------------------- */
nPamFlags &= ~GPF_DIRTY;
return CE_None;
}
/************************************************************************/
/* SetPhysicalFilename() */
/************************************************************************/
void GDALPamDataset::SetPhysicalFilename( const char *pszFilename )
{
PamInitialize();
if( psPam )
psPam->osPhysicalFilename = pszFilename;
}
/************************************************************************/
/* GetPhysicalFilename() */
/************************************************************************/
const char *GDALPamDataset::GetPhysicalFilename()
{
PamInitialize();
if( psPam )
return psPam->osPhysicalFilename;
return "";
}
/************************************************************************/
/* SetSubdatasetName() */
/************************************************************************/
void GDALPamDataset::SetSubdatasetName( const char *pszSubdataset )
{
PamInitialize();
if( psPam )
psPam->osSubdatasetName = pszSubdataset;
}
/************************************************************************/
/* GetSubdatasetName() */
/************************************************************************/
const char *GDALPamDataset::GetSubdatasetName()
{
PamInitialize();
if( psPam )
return psPam->osSubdatasetName;
return "";
}
/************************************************************************/
/* BuildPamFilename() */
/************************************************************************/
const char *GDALPamDataset::BuildPamFilename()
{
if( psPam == nullptr )
return nullptr;
/* -------------------------------------------------------------------- */
/* What is the name of the physical file we are referencing? */
/* We allow an override via the psPam->pszPhysicalFile item. */
/* -------------------------------------------------------------------- */
if( psPam->pszPamFilename != nullptr )
return psPam->pszPamFilename;
const char *pszPhysicalFile = psPam->osPhysicalFilename;
if( strlen(pszPhysicalFile) == 0 && GetDescription() != nullptr )
pszPhysicalFile = GetDescription();
if( strlen(pszPhysicalFile) == 0 )
return nullptr;
/* -------------------------------------------------------------------- */
/* Try a proxy lookup, otherwise just add .aux.xml. */
/* -------------------------------------------------------------------- */
const char *pszProxyPam = PamGetProxy( pszPhysicalFile );
if( pszProxyPam != nullptr )
psPam->pszPamFilename = CPLStrdup(pszProxyPam);
else
{
if( !GDALCanFileAcceptSidecarFile(pszPhysicalFile) )
return nullptr;
psPam->pszPamFilename = static_cast<char*>(CPLMalloc(strlen(pszPhysicalFile)+10));
strcpy( psPam->pszPamFilename, pszPhysicalFile );
strcat( psPam->pszPamFilename, ".aux.xml" );
}
return psPam->pszPamFilename;
}
/************************************************************************/
/* IsPamFilenameAPotentialSiblingFile() */
/************************************************************************/
int GDALPamDataset::IsPamFilenameAPotentialSiblingFile()
{
if (psPam == nullptr)
return FALSE;
/* -------------------------------------------------------------------- */
/* Determine if the PAM filename is a .aux.xml file next to the */
/* physical file, or if it comes from the ProxyDB */
/* -------------------------------------------------------------------- */
const char *pszPhysicalFile = psPam->osPhysicalFilename;
if( strlen(pszPhysicalFile) == 0 && GetDescription() != nullptr )
pszPhysicalFile = GetDescription();
size_t nLenPhysicalFile = strlen(pszPhysicalFile);
int bIsSiblingPamFile = strncmp(psPam->pszPamFilename, pszPhysicalFile,
nLenPhysicalFile) == 0 &&
strcmp(psPam->pszPamFilename + nLenPhysicalFile,
".aux.xml") == 0;
return bIsSiblingPamFile;
}
/************************************************************************/
/* TryLoadXML() */
/************************************************************************/
CPLErr GDALPamDataset::TryLoadXML(char **papszSiblingFiles)
{
PamInitialize();
/* -------------------------------------------------------------------- */
/* Clear dirty flag. Generally when we get to this point is */
/* from a call at the end of the Open() method, and some calls */
/* may have already marked the PAM info as dirty (for instance */
/* setting metadata), but really everything to this point is */
/* reproducible, and so the PAM info should not really be */
/* thought of as dirty. */
/* -------------------------------------------------------------------- */
nPamFlags &= ~GPF_DIRTY;
/* -------------------------------------------------------------------- */
/* Try reading the file. */
/* -------------------------------------------------------------------- */
if( !BuildPamFilename() )
return CE_None;
/* -------------------------------------------------------------------- */
/* In case the PAM filename is a .aux.xml file next to the */
/* physical file and we have a siblings list, then we can skip */
/* stat'ing the filesystem. */
/* -------------------------------------------------------------------- */
VSIStatBufL sStatBuf;
CPLXMLNode *psTree = nullptr;
CPLErr eLastErr = CPLGetLastErrorType();
int nLastErrNo = CPLGetLastErrorNo();
CPLString osLastErrorMsg = CPLGetLastErrorMsg();
if( papszSiblingFiles != nullptr && IsPamFilenameAPotentialSiblingFile() &&
GDALCanReliablyUseSiblingFileList(psPam->pszPamFilename) )
{
const int iSibling =
CSLFindString( papszSiblingFiles,
CPLGetFilename(psPam->pszPamFilename) );
if( iSibling >= 0 )
{
CPLErrorReset();
CPLPushErrorHandler( CPLQuietErrorHandler );
psTree = CPLParseXMLFile( psPam->pszPamFilename );
CPLPopErrorHandler();
CPLErrorReset();
}
}
else
if( VSIStatExL( psPam->pszPamFilename, &sStatBuf,
VSI_STAT_EXISTS_FLAG | VSI_STAT_NATURE_FLAG ) == 0
&& VSI_ISREG( sStatBuf.st_mode ) )
{
CPLErrorReset();
CPLPushErrorHandler( CPLQuietErrorHandler );
psTree = CPLParseXMLFile( psPam->pszPamFilename );
CPLPopErrorHandler();
CPLErrorReset();
}
if( eLastErr != CE_None )
CPLErrorSetState( eLastErr, nLastErrNo, osLastErrorMsg.c_str() );
/* -------------------------------------------------------------------- */
/* If we are looking for a subdataset, search for its subtree not. */
/* -------------------------------------------------------------------- */
if( psTree && !psPam->osSubdatasetName.empty() )
{
CPLXMLNode *psSubTree = psTree->psChild;
for( ;
psSubTree != nullptr;
psSubTree = psSubTree->psNext )
{
if( psSubTree->eType != CXT_Element
|| !EQUAL(psSubTree->pszValue,"Subdataset") )
continue;
if( !EQUAL(CPLGetXMLValue( psSubTree, "name", "" ),
psPam->osSubdatasetName) )
continue;
psSubTree = CPLGetXMLNode( psSubTree, "PAMDataset" );
break;
}
if( psSubTree != nullptr )
psSubTree = CPLCloneXMLTree( psSubTree );
CPLDestroyXMLNode( psTree );
psTree = psSubTree;
}
/* -------------------------------------------------------------------- */
/* If we fail, try .aux. */
/* -------------------------------------------------------------------- */
if( psTree == nullptr )
return TryLoadAux(papszSiblingFiles);
/* -------------------------------------------------------------------- */
/* Initialize ourselves from this XML tree. */
/* -------------------------------------------------------------------- */
CPLString osVRTPath(CPLGetPath(psPam->pszPamFilename));
const CPLErr eErr = XMLInit( psTree, osVRTPath );
CPLDestroyXMLNode( psTree );
if( eErr != CE_None )
PamClear();
return eErr;
}
/************************************************************************/
/* TrySaveXML() */
/************************************************************************/
CPLErr GDALPamDataset::TrySaveXML()
{
nPamFlags &= ~GPF_DIRTY;
if( psPam == nullptr || (nPamFlags & GPF_NOSAVE) )
return CE_None;
/* -------------------------------------------------------------------- */
/* Make sure we know the filename we want to store in. */
/* -------------------------------------------------------------------- */
if( !BuildPamFilename() )
return CE_None;
/* -------------------------------------------------------------------- */
/* Build the XML representation of the auxiliary metadata. */
/* -------------------------------------------------------------------- */
CPLXMLNode *psTree = SerializeToXML( nullptr );
if( psTree == nullptr )
{
/* If we have unset all metadata, we have to delete the PAM file */
CPLPushErrorHandler( CPLQuietErrorHandler );
VSIUnlink(psPam->pszPamFilename);
CPLPopErrorHandler();
return CE_None;
}
/* -------------------------------------------------------------------- */
/* If we are working with a subdataset, we need to integrate */
/* the subdataset tree within the whole existing pam tree, */
/* after removing any old version of the same subdataset. */
/* -------------------------------------------------------------------- */
if( !psPam->osSubdatasetName.empty() )
{
CPLXMLNode *psOldTree, *psSubTree;
CPLErrorReset();
CPLPushErrorHandler( CPLQuietErrorHandler );
psOldTree = CPLParseXMLFile( psPam->pszPamFilename );
CPLPopErrorHandler();
if( psOldTree == nullptr )
psOldTree = CPLCreateXMLNode( nullptr, CXT_Element, "PAMDataset" );
for( psSubTree = psOldTree->psChild;
psSubTree != nullptr;
psSubTree = psSubTree->psNext )
{
if( psSubTree->eType != CXT_Element
|| !EQUAL(psSubTree->pszValue,"Subdataset") )
continue;
if( !EQUAL(CPLGetXMLValue( psSubTree, "name", "" ),
psPam->osSubdatasetName) )
continue;
break;
}
if( psSubTree == nullptr )
{
psSubTree = CPLCreateXMLNode( psOldTree, CXT_Element,
"Subdataset" );
CPLCreateXMLNode(
CPLCreateXMLNode( psSubTree, CXT_Attribute, "name" ),
CXT_Text, psPam->osSubdatasetName );
}
CPLXMLNode *psOldPamDataset = CPLGetXMLNode( psSubTree, "PAMDataset");
if( psOldPamDataset != nullptr )
{
CPLRemoveXMLChild( psSubTree, psOldPamDataset );
CPLDestroyXMLNode( psOldPamDataset );
}
CPLAddXMLChild( psSubTree, psTree );
psTree = psOldTree;
}
/* -------------------------------------------------------------------- */
/* Preserve other information. */
/* -------------------------------------------------------------------- */
for( const auto& poOtherNode: psPam->m_apoOtherNodes )
{
CPLAddXMLChild(psTree, CPLCloneXMLTree(poOtherNode.get()));
}
/* -------------------------------------------------------------------- */
/* Try saving the auxiliary metadata. */
/* -------------------------------------------------------------------- */
CPLPushErrorHandler( CPLQuietErrorHandler );
const int bSaved =
CPLSerializeXMLTreeToFile( psTree, psPam->pszPamFilename );
CPLPopErrorHandler();
/* -------------------------------------------------------------------- */
/* If it fails, check if we have a proxy directory for auxiliary */
/* metadata to be stored in, and try to save there. */
/* -------------------------------------------------------------------- */
CPLErr eErr = CE_None;
if( bSaved )
eErr = CE_None;
else
{
const char *pszBasename = GetDescription();
if( psPam->osPhysicalFilename.length() > 0 )
pszBasename = psPam->osPhysicalFilename;