/
ogrspatialreference.cpp
6308 lines (5087 loc) · 219 KB
/
ogrspatialreference.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
/******************************************************************************
* $Id: ogrspatialreference.cpp 18544 2010-01-14 06:47:29Z warmerdam $
*
* Project: OpenGIS Simple Features Reference Implementation
* Purpose: The OGRSpatialReference class.
* Author: Frank Warmerdam, warmerdam@pobox.com
*
******************************************************************************
* Copyright (c) 1999, Les Technologies SoftMap Inc.
*
* 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 "ogr_spatialref.h"
#include "ogr_p.h"
#include "cpl_csv.h"
#include "cpl_http.h"
#include "cpl_atomic_ops.h"
CPL_CVSID("$Id: ogrspatialreference.cpp 18544 2010-01-14 06:47:29Z warmerdam $");
// The current opinion is that WKT longitudes like central meridian
// should be relative to greenwich, not the prime meridian in use.
// Define the following if they should be relative to the prime meridian
// of then geogcs.
#undef WKT_LONGITUDE_RELATIVE_TO_PM
/************************************************************************/
/* OGRPrintDouble() */
/************************************************************************/
static void OGRPrintDouble( char * pszStrBuf, double dfValue )
{
sprintf( pszStrBuf, "%.16g", dfValue );
int nLen = strlen(pszStrBuf);
// The following hack is intended to truncate some "precision" in cases
// that appear to be roundoff error.
if( nLen > 15
&& (strcmp(pszStrBuf+nLen-6,"999999") == 0
|| strcmp(pszStrBuf+nLen-6,"000001") == 0) )
{
sprintf( pszStrBuf, "%.15g", dfValue );
}
// force to user periods regardless of locale.
if( strchr( pszStrBuf, ',' ) != NULL )
{
char *pszDelim = strchr( pszStrBuf, ',' );
*pszDelim = '.';
}
}
/************************************************************************/
/* OGRSpatialReference() */
/************************************************************************/
/**
* \brief Constructor.
*
* This constructor takes an optional string argument which if passed
* should be a WKT representation of an SRS. Passing this is equivalent
* to not passing it, and then calling importFromWkt() with the WKT string.
*
* Note that newly created objects are given a reference count of one.
*
* The C function OSRNewSpatialReference() does the same thing as this
* constructor.
*
* @param pszWKT well known text definition to which the object should
* be initialized, or NULL (the default).
*/
OGRSpatialReference::OGRSpatialReference( const char * pszWKT )
{
bNormInfoSet = FALSE;
nRefCount = 1;
poRoot = NULL;
if( pszWKT != NULL )
importFromWkt( (char **) &pszWKT );
}
/************************************************************************/
/* OSRNewSpatialReference() */
/************************************************************************/
/**
* \brief Constructor.
*
* This function is the same as OGRSpatialReference::OGRSpatialReference()
*/
OGRSpatialReferenceH CPL_STDCALL OSRNewSpatialReference( const char *pszWKT )
{
OGRSpatialReference * poSRS;
poSRS = new OGRSpatialReference();
if( pszWKT != NULL && strlen(pszWKT) > 0 )
{
if( poSRS->importFromWkt( (char **) (&pszWKT) ) != OGRERR_NONE )
{
delete poSRS;
poSRS = NULL;
}
}
return (OGRSpatialReferenceH) poSRS;
}
/************************************************************************/
/* OGRSpatialReference() */
/* */
/* Simple copy constructor. See also Clone(). */
/************************************************************************/
OGRSpatialReference::OGRSpatialReference(const OGRSpatialReference &oOther)
{
bNormInfoSet = FALSE;
nRefCount = 1;
poRoot = NULL;
if( oOther.poRoot != NULL )
poRoot = oOther.poRoot->Clone();
}
/************************************************************************/
/* ~OGRSpatialReference() */
/************************************************************************/
/**
* \brief OGRSpatialReference destructor.
*
* The C function OSRDestroySpatialReference() does the same thing as this
* method. Preferred C++ method : OGRSpatialReference::DestroySpatialReference()
*
* @deprecated
*/
OGRSpatialReference::~OGRSpatialReference()
{
if( poRoot != NULL )
delete poRoot;
}
/************************************************************************/
/* DestroySpatialReference() */
/************************************************************************/
/**
* \brief OGRSpatialReference destructor.
*
* This static method will destroy a OGRSpatialReference. It is
* equivalent to calling delete on the object, but it ensures that the
* deallocation is properly executed within the OGR libraries heap on
* platforms where this can matter (win32).
*
* This function is the same as OSRDestroySpatialReference()
*
* @param poSRS the object to delete
*
* @since GDAL 1.7.0
*/
void OGRSpatialReference::DestroySpatialReference(OGRSpatialReference* poSRS)
{
delete poSRS;
}
/************************************************************************/
/* OSRDestroySpatialReference() */
/************************************************************************/
/**
* \brief OGRSpatialReference destructor.
*
* This function is the same as OGRSpatialReference::~OGRSpatialReference()
* and OGRSpatialReference::DestroySpatialReference()
*
* @param hSRS the object to delete
*/
void CPL_STDCALL OSRDestroySpatialReference( OGRSpatialReferenceH hSRS )
{
delete ((OGRSpatialReference *) hSRS);
}
/************************************************************************/
/* Clear() */
/************************************************************************/
/**
* \brief Wipe current definition.
*
* Returns OGRSpatialReference to a state with no definition, as it
* exists when first created. It does not affect reference counts.
*/
void OGRSpatialReference::Clear()
{
if( poRoot )
delete poRoot;
poRoot = NULL;
bNormInfoSet = FALSE;
dfFromGreenwich = 1.0;
dfToMeter = 1.0;
dfToDegrees = 1.0;
}
/************************************************************************/
/* operator=() */
/************************************************************************/
OGRSpatialReference &
OGRSpatialReference::operator=(const OGRSpatialReference &oSource)
{
Clear();
if( oSource.poRoot != NULL )
poRoot = oSource.poRoot->Clone();
return *this;
}
/************************************************************************/
/* Reference() */
/************************************************************************/
/**
* \brief Increments the reference count by one.
*
* The reference count is used keep track of the number of OGRGeometry objects
* referencing this SRS.
*
* The method does the same thing as the C function OSRReference().
*
* @return the updated reference count.
*/
int OGRSpatialReference::Reference()
{
return CPLAtomicInc(&nRefCount);
}
/************************************************************************/
/* OSRReference() */
/************************************************************************/
/**
* \brief Increments the reference count by one.
*
* This function is the same as OGRSpatialReference::Reference()
*/
int OSRReference( OGRSpatialReferenceH hSRS )
{
VALIDATE_POINTER1( hSRS, "OSRReference", 0 );
return ((OGRSpatialReference *) hSRS)->Reference();
}
/************************************************************************/
/* Dereference() */
/************************************************************************/
/**
* \brief Decrements the reference count by one.
*
* The method does the same thing as the C function OSRDereference().
*
* @return the updated reference count.
*/
int OGRSpatialReference::Dereference()
{
if( nRefCount <= 0 )
CPLDebug( "OSR",
"Dereference() called on an object with refcount %d,"
"likely already destroyed!",
nRefCount );
return CPLAtomicDec(&nRefCount);
}
/************************************************************************/
/* OSRDereference() */
/************************************************************************/
/**
* \brief Decrements the reference count by one.
*
* This function is the same as OGRSpatialReference::Dereference()
*/
int OSRDereference( OGRSpatialReferenceH hSRS )
{
VALIDATE_POINTER1( hSRS, "OSRDereference", 0 );
return ((OGRSpatialReference *) hSRS)->Dereference();
}
/************************************************************************/
/* GetReferenceCount() */
/************************************************************************/
/**
* \fn int OGRSpatialReference::GetReferenceCount() const;
*
* \brief Fetch current reference count.
*
* @return the current reference count.
*/
/************************************************************************/
/* Release() */
/************************************************************************/
/**
* \brief Decrements the reference count by one, and destroy if zero.
*
* The method does the same thing as the C function OSRRelease().
*/
void OGRSpatialReference::Release()
{
CPLAssert( NULL != this );
if( Dereference() <= 0 )
delete this;
}
/************************************************************************/
/* OSRRelease() */
/************************************************************************/
/**
* \brief Decrements the reference count by one, and destroy if zero.
*
* This function is the same as OGRSpatialReference::Release()
*/
void OSRRelease( OGRSpatialReferenceH hSRS )
{
VALIDATE_POINTER0( hSRS, "OSRRelease" );
((OGRSpatialReference *) hSRS)->Release();
}
/************************************************************************/
/* SetRoot() */
/************************************************************************/
/**
* \brief Set the root SRS node.
*
* If the object has an existing tree of OGR_SRSNodes, they are destroyed
* as part of assigning the new root. Ownership of the passed OGR_SRSNode is
* is assumed by the OGRSpatialReference.
*
* @param poNewRoot object to assign as root.
*/
void OGRSpatialReference::SetRoot( OGR_SRSNode * poNewRoot )
{
if( poRoot != NULL )
delete poRoot;
poRoot = poNewRoot;
}
/************************************************************************/
/* GetAttrNode() */
/************************************************************************/
/**
* \brief Find named node in tree.
*
* This method does a pre-order traversal of the node tree searching for
* a node with this exact value (case insensitive), and returns it. Leaf
* nodes are not considered, under the assumption that they are just
* attribute value nodes.
*
* If a node appears more than once in the tree (such as UNIT for instance),
* the first encountered will be returned. Use GetNode() on a subtree to be
* more specific.
*
* @param pszNodePath the name of the node to search for. May contain multiple
* components such as "GEOGCS|UNITS".
*
* @return a pointer to the node found, or NULL if none.
*/
OGR_SRSNode *OGRSpatialReference::GetAttrNode( const char * pszNodePath )
{
char **papszPathTokens;
OGR_SRSNode *poNode;
papszPathTokens = CSLTokenizeStringComplex(pszNodePath, "|", TRUE, FALSE);
if( CSLCount( papszPathTokens ) < 1 )
return NULL;
poNode = GetRoot();
for( int i = 0; poNode != NULL && papszPathTokens[i] != NULL; i++ )
{
poNode = poNode->GetNode( papszPathTokens[i] );
}
CSLDestroy( papszPathTokens );
return poNode;
}
const OGR_SRSNode *
OGRSpatialReference::GetAttrNode( const char * pszNodePath ) const
{
OGR_SRSNode *poNode;
poNode = ((OGRSpatialReference *) this)->GetAttrNode(pszNodePath);
return poNode;
}
/************************************************************************/
/* GetAttrValue() */
/************************************************************************/
/**
* \brief Fetch indicated attribute of named node.
*
* This method uses GetAttrNode() to find the named node, and then extracts
* the value of the indicated child. Thus a call to GetAttrValue("UNIT",1)
* would return the second child of the UNIT node, which is normally the
* length of the linear unit in meters.
*
* This method does the same thing as the C function OSRGetAttrValue().
*
* @param pszNodeName the tree node to look for (case insensitive).
* @param iAttr the child of the node to fetch (zero based).
*
* @return the requested value, or NULL if it fails for any reason.
*/
const char *OGRSpatialReference::GetAttrValue( const char * pszNodeName,
int iAttr ) const
{
const OGR_SRSNode *poNode;
poNode = GetAttrNode( pszNodeName );
if( poNode == NULL )
return NULL;
if( iAttr < 0 || iAttr >= poNode->GetChildCount() )
return NULL;
return poNode->GetChild(iAttr)->GetValue();
}
/************************************************************************/
/* OSRGetAttrValue() */
/************************************************************************/
/**
* \brief Fetch indicated attribute of named node.
*
* This function is the same as OGRSpatialReference::GetAttrValue()
*/
const char * CPL_STDCALL OSRGetAttrValue( OGRSpatialReferenceH hSRS,
const char * pszKey, int iChild )
{
VALIDATE_POINTER1( hSRS, "OSRGetAttrValue", NULL );
return ((OGRSpatialReference *) hSRS)->GetAttrValue( pszKey, iChild );
}
/************************************************************************/
/* Clone() */
/************************************************************************/
/**
* \brief Make a duplicate of this OGRSpatialReference.
*
* This method is the same as the C function OSRClone().
*
* @return a new SRS, which becomes the responsibility of the caller.
*/
OGRSpatialReference *OGRSpatialReference::Clone() const
{
OGRSpatialReference *poNewRef;
poNewRef = new OGRSpatialReference();
if( poRoot != NULL )
poNewRef->poRoot = poRoot->Clone();
return poNewRef;
}
/************************************************************************/
/* OSRClone() */
/************************************************************************/
/**
* \brief Make a duplicate of this OGRSpatialReference.
*
* This function is the same as OGRSpatialReference::Clone()
*/
OGRSpatialReferenceH CPL_STDCALL OSRClone( OGRSpatialReferenceH hSRS )
{
VALIDATE_POINTER1( hSRS, "OSRClone", NULL );
return (OGRSpatialReferenceH) ((OGRSpatialReference *) hSRS)->Clone();
}
/************************************************************************/
/* exportToPrettyWkt() */
/************************************************************************/
/**
* Convert this SRS into a a nicely formatted WKT string for display to a person.
*
* Note that the returned WKT string should be freed with OGRFree() or
* CPLFree() when no longer needed. It is the responsibility of the caller.
*
* This method is the same as the C function OSRExportToPrettyWkt().
*
* @param ppszResult the resulting string is returned in this pointer.
* @param bSimplify TRUE if the AXIS, AUTHORITY and EXTENSION nodes should be stripped off
*
* @return currently OGRERR_NONE is always returned, but the future it
* is possible error conditions will develop.
*/
OGRErr OGRSpatialReference::exportToPrettyWkt( char ** ppszResult,
int bSimplify ) const
{
if( poRoot == NULL )
{
*ppszResult = CPLStrdup("");
return OGRERR_NONE;
}
if( bSimplify )
{
OGRSpatialReference *poSimpleClone = Clone();
OGRErr eErr;
poSimpleClone->GetRoot()->StripNodes( "AXIS" );
poSimpleClone->GetRoot()->StripNodes( "AUTHORITY" );
poSimpleClone->GetRoot()->StripNodes( "EXTENSION" );
eErr = poSimpleClone->GetRoot()->exportToPrettyWkt( ppszResult, 1 );
delete poSimpleClone;
return eErr;
}
else
return poRoot->exportToPrettyWkt( ppszResult, 1 );
}
/************************************************************************/
/* OSRExportToPrettyWkt() */
/************************************************************************/
/**
* \brief Convert this SRS into a a nicely formatted WKT string for display to a person.
*
* This function is the same as OGRSpatialReference::exportToPrettyWkt().
*/
OGRErr CPL_STDCALL OSRExportToPrettyWkt( OGRSpatialReferenceH hSRS, char ** ppszReturn,
int bSimplify)
{
VALIDATE_POINTER1( hSRS, "OSRExportToPrettyWkt", CE_Failure );
*ppszReturn = NULL;
return ((OGRSpatialReference *) hSRS)->exportToPrettyWkt( ppszReturn,
bSimplify );
}
/************************************************************************/
/* exportToWkt() */
/************************************************************************/
/**
* \brief Convert this SRS into WKT format.
*
* Note that the returned WKT string should be freed with OGRFree() or
* CPLFree() when no longer needed. It is the responsibility of the caller.
*
* This method is the same as the C function OSRExportToWkt().
*
* @param ppszResult the resulting string is returned in this pointer.
*
* @return currently OGRERR_NONE is always returned, but the future it
* is possible error conditions will develop.
*/
OGRErr OGRSpatialReference::exportToWkt( char ** ppszResult ) const
{
if( poRoot == NULL )
{
*ppszResult = CPLStrdup("");
return OGRERR_NONE;
}
else
{
return poRoot->exportToWkt(ppszResult);
}
}
/************************************************************************/
/* OSRExportToWkt() */
/************************************************************************/
/**
* \brief Convert this SRS into WKT format.
*
* This function is the same as OGRSpatialReference::exportToWkt().
*/
OGRErr CPL_STDCALL OSRExportToWkt( OGRSpatialReferenceH hSRS,
char ** ppszReturn )
{
VALIDATE_POINTER1( hSRS, "OSRExportToWkt", CE_Failure );
*ppszReturn = NULL;
return ((OGRSpatialReference *) hSRS)->exportToWkt( ppszReturn );
}
/************************************************************************/
/* importFromWkt() */
/************************************************************************/
/**
* \brief Import from WKT string.
*
* This method will wipe the existing SRS definition, and
* reassign it based on the contents of the passed WKT string. Only as
* much of the input string as needed to construct this SRS is consumed from
* the input string, and the input string pointer
* is then updated to point to the remaining (unused) input.
*
* This method is the same as the C function OSRImportFromWkt().
*
* @param ppszInput Pointer to pointer to input. The pointer is updated to
* point to remaining unused input text.
*
* @return OGRERR_NONE if import succeeds, or OGRERR_CORRUPT_DATA if it
* fails for any reason.
*/
OGRErr OGRSpatialReference::importFromWkt( char ** ppszInput )
{
if ( !ppszInput || !*ppszInput )
return OGRERR_FAILURE;
Clear();
poRoot = new OGR_SRSNode();
return poRoot->importFromWkt( ppszInput );
}
/************************************************************************/
/* OSRImportFromWkt() */
/************************************************************************/
/**
* \brief Import from WKT string.
*
* This function is the same as OGRSpatialReference::importFromWkt().
*/
OGRErr OSRImportFromWkt( OGRSpatialReferenceH hSRS, char **ppszInput )
{
VALIDATE_POINTER1( hSRS, "OSRImportFromWkt", CE_Failure );
return ((OGRSpatialReference *) hSRS)->importFromWkt( ppszInput );
}
/************************************************************************/
/* SetNode() */
/************************************************************************/
/**
* \brief Set attribute value in spatial reference.
*
* Missing intermediate nodes in the path will be created if not already
* in existance. If the attribute has no children one will be created and
* assigned the value otherwise the zeroth child will be assigned the value.
*
* This method does the same as the C function OSRSetAttrValue().
*
* @param pszNodePath full path to attribute to be set. For instance
* "PROJCS|GEOGCS|UNITS".
*
* @param pszNewNodeValue value to be assigned to node, such as "meter".
* This may be NULL if you just want to force creation of the intermediate
* path.
*
* @return OGRERR_NONE on success.
*/
OGRErr OGRSpatialReference::SetNode( const char * pszNodePath,
const char * pszNewNodeValue )
{
char **papszPathTokens;
int i;
OGR_SRSNode *poNode;
papszPathTokens = CSLTokenizeStringComplex(pszNodePath, "|", TRUE, FALSE);
if( CSLCount( papszPathTokens ) < 1 )
return OGRERR_FAILURE;
if( GetRoot() == NULL || !EQUAL(papszPathTokens[0],GetRoot()->GetValue()) )
{
SetRoot( new OGR_SRSNode( papszPathTokens[0] ) );
}
poNode = GetRoot();
for( i = 1; papszPathTokens[i] != NULL; i++ )
{
int j;
for( j = 0; j < poNode->GetChildCount(); j++ )
{
if( EQUAL(poNode->GetChild( j )->GetValue(),papszPathTokens[i]) )
{
poNode = poNode->GetChild(j);
j = -1;
break;
}
}
if( j != -1 )
{
OGR_SRSNode *poNewNode = new OGR_SRSNode( papszPathTokens[i] );
poNode->AddChild( poNewNode );
poNode = poNewNode;
}
}
CSLDestroy( papszPathTokens );
if( pszNewNodeValue != NULL )
{
if( poNode->GetChildCount() > 0 )
poNode->GetChild(0)->SetValue( pszNewNodeValue );
else
poNode->AddChild( new OGR_SRSNode( pszNewNodeValue ) );
}
return OGRERR_NONE;
}
/************************************************************************/
/* OSRSetAttrValue() */
/************************************************************************/
/**
* \brief Set attribute value in spatial reference.
*
* This function is the same as OGRSpatialReference::SetNode()
*/
OGRErr CPL_STDCALL OSRSetAttrValue( OGRSpatialReferenceH hSRS,
const char * pszPath, const char * pszValue )
{
VALIDATE_POINTER1( hSRS, "OSRSetAttrValue", CE_Failure );
return ((OGRSpatialReference *) hSRS)->SetNode( pszPath, pszValue );
}
/************************************************************************/
/* SetNode() */
/************************************************************************/
OGRErr OGRSpatialReference::SetNode( const char *pszNodePath,
double dfValue )
{
char szValue[64];
if( ABS(dfValue - (int) dfValue) == 0.0 )
sprintf( szValue, "%d", (int) dfValue );
else
OGRPrintDouble( szValue, dfValue );
return SetNode( pszNodePath, szValue );
}
/************************************************************************/
/* SetAngularUnits() */
/************************************************************************/
/**
* \brief Set the angular units for the geographic coordinate system.
*
* This method creates a UNITS subnode with the specified values as a
* child of the GEOGCS node.
*
* This method does the same as the C function OSRSetAngularUnits().
*
* @param pszUnitsName the units name to be used. Some preferred units
* names can be found in ogr_srs_api.h such as SRS_UA_DEGREE.
*
* @param dfInRadians the value to multiple by an angle in the indicated
* units to transform to radians. Some standard conversion factors can
* be found in ogr_srs_api.h.
*
* @return OGRERR_NONE on success.
*/
OGRErr OGRSpatialReference::SetAngularUnits( const char * pszUnitsName,
double dfInRadians )
{
OGR_SRSNode *poCS;
OGR_SRSNode *poUnits;
char szValue[128];
bNormInfoSet = FALSE;
poCS = GetAttrNode( "GEOGCS" );
if( poCS == NULL )
return OGRERR_FAILURE;
OGRPrintDouble( szValue, dfInRadians );
if( poCS->FindChild( "UNIT" ) >= 0 )
{
poUnits = poCS->GetChild( poCS->FindChild( "UNIT" ) );
poUnits->GetChild(0)->SetValue( pszUnitsName );
poUnits->GetChild(1)->SetValue( szValue );
}
else
{
poUnits = new OGR_SRSNode( "UNIT" );
poUnits->AddChild( new OGR_SRSNode( pszUnitsName ) );
poUnits->AddChild( new OGR_SRSNode( szValue ) );
poCS->AddChild( poUnits );
}
return OGRERR_NONE;
}
/************************************************************************/
/* OSRSetAngularUnits() */
/************************************************************************/
/**
* \brief Set the angular units for the geographic coordinate system.
*
* This function is the same as OGRSpatialReference::SetAngularUnits()
*/
OGRErr OSRSetAngularUnits( OGRSpatialReferenceH hSRS,
const char * pszUnits, double dfInRadians )
{
VALIDATE_POINTER1( hSRS, "OSRSetAngularUnits", CE_Failure );
return ((OGRSpatialReference *) hSRS)->SetAngularUnits( pszUnits,
dfInRadians );
}
/************************************************************************/
/* GetAngularUnits() */
/************************************************************************/
/**
* \brief Fetch angular geographic coordinate system units.
*
* If no units are available, a value of "degree" and SRS_UA_DEGREE_CONV
* will be assumed. This method only checks directly under the GEOGCS node
* for units.
*
* This method does the same thing as the C function OSRGetAngularUnits().
*
* @param ppszName a pointer to be updated with the pointer to the
* units name. The returned value remains internal to the OGRSpatialReference
* and shouldn't be freed, or modified. It may be invalidated on the next
* OGRSpatialReference call.
*
* @return the value to multiply by angular distances to transform them to
* radians.
*/
double OGRSpatialReference::GetAngularUnits( char ** ppszName ) const
{
const OGR_SRSNode *poCS = GetAttrNode( "GEOGCS" );
if( ppszName != NULL )
*ppszName = (char* ) "degree";
if( poCS == NULL )
return CPLAtof(SRS_UA_DEGREE_CONV);
for( int iChild = 0; iChild < poCS->GetChildCount(); iChild++ )
{
const OGR_SRSNode *poChild = poCS->GetChild(iChild);
if( EQUAL(poChild->GetValue(),"UNIT")
&& poChild->GetChildCount() >= 2 )
{
if( ppszName != NULL )
*ppszName = (char *) poChild->GetChild(0)->GetValue();
return CPLAtof( poChild->GetChild(1)->GetValue() );
}
}
return 1.0;
}
/************************************************************************/
/* OSRGetAngularUnits() */
/************************************************************************/
/**
* \brief Fetch angular geographic coordinate system units.
*
* This function is the same as OGRSpatialReference::GetAngularUnits()
*/
double OSRGetAngularUnits( OGRSpatialReferenceH hSRS, char ** ppszName )
{
VALIDATE_POINTER1( hSRS, "OSRGetAngularUnits", 0 );
return ((OGRSpatialReference *) hSRS)->GetAngularUnits( ppszName );
}
/************************************************************************/
/* SetLinearUnitsAndUpdateParameters() */
/************************************************************************/
/**
* \brief Set the linear units for the projection.
*
* This method creates a UNITS subnode with the specified values as a
* child of the PROJCS or LOCAL_CS node. It works the same as the
* SetLinearUnits() method, but it also updates all existing linear
* projection parameter values from the old units to the new units.
*
* @param pszName the units name to be used. Some preferred units
* names can be found in ogr_srs_api.h such as SRS_UL_METER, SRS_UL_FOOT
* and SRS_UL_US_FOOT.
*
* @param dfInMeters the value to multiple by a length in the indicated
* units to transform to meters. Some standard conversion factors can
* be found in ogr_srs_api.h.
*