-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathSpatialReference.cpp
1140 lines (983 loc) · 31.6 KB
/
SpatialReference.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
/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
* Copyright 2008-2010 Pelican Mapping
* http://osgearth.org
*
* osgEarth is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#include "SpatialReference"
#include <OpenThreads/ScopedLock>
#include <osg/Notify>
#include <ogr_api.h>
#include <ogr_spatialref.h>
#include <algorithm>
#include <sstream>
#define LC "[SpatialReference] "
//using namespace osgEarth;
#define USE_CUSTOM_MERCATOR_TRANSFORM 1
//#undef USE_CUSTOM_MERCATOR_TRANSFORM
#define OE_WARN osg::notify(osg::WARN)
#define OE_INFO osg::notify(osg::INFO)
//------------------------------------------------------------------------
#define GDAL_SCOPED_LOCK
/*namespace
{*/
std::string
getOGRAttrValue( void* _handle, const std::string& name, int child_num, bool lowercase =false)
{
GDAL_SCOPED_LOCK;
const char* val = OSRGetAttrValue( _handle, name.c_str(), child_num );
if ( val )
{
std::string t = val;
if ( lowercase )
{
std::transform( t.begin(), t.end(), t.begin(), ::tolower );
}
return t;
}
return "";
}
std::string&
replaceIn( std::string& s, const std::string& sub, const std::string& other)
{
if ( sub.empty() ) return s;
size_t b=0;
for( ; ; )
{
b = s.find( sub, b );
if ( b == s.npos ) break;
s.replace( b, sub.size(), other );
b += other.size();
}
return s;
}
//}
//------------------------------------------------------------------------
SpatialReference::SpatialReferenceCache& SpatialReference::getSpatialReferenceCache()
{
//Make sure the registry is created before the cache
//osgEarth::Registry::instance();
static SpatialReferenceCache s_cache;
return s_cache;
}
SpatialReference*
SpatialReference::createFromPROJ4( const std::string& init, const std::string& init_alias, const std::string& name )
{
SpatialReference* result = NULL;
GDAL_SCOPED_LOCK;
void* handle = OSRNewSpatialReference( NULL );
if ( OSRImportFromProj4( handle, init.c_str() ) == OGRERR_NONE )
{
result = new SpatialReference( handle, "PROJ4", init_alias, name );
}
else
{
OE_WARN << LC << "Unable to create spatial reference from PROJ4: " << init << std::endl;
OSRDestroySpatialReference( handle );
}
return result;
}
/*SpatialReference*
SpatialReference::createCube()
{
// root the cube srs with a WGS84 intermediate ellipsoid.
std::string init = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs";
SpatialReference* result = NULL;
GDAL_SCOPED_LOCK;
void* handle = OSRNewSpatialReference( NULL );
if ( OSRImportFromProj4( handle, init.c_str() ) == OGRERR_NONE )
{
result = new CubeSpatialReference( handle );
}
else
{
OE_WARN << LC << "Unable to create SRS: " << init << std::endl;
OSRDestroySpatialReference( handle );
}
return result;
}
*/
#if 0
SpatialReference*
SpatialReference::createLTP( const osg::Vec3d& refPointLLA, const SpatialReference* geoSRS )
{
GDAL_SCOPED_LOCK;
void* handle = OSRNewSpatialReference( NULL );
bool ok = false;
SpatialReference* result = 0L;
if ( geoSRS )
{
std::string init = geoSRS->getGeographicSRS()->getWKT();
char buf[4096];
char* buf_ptr = &buf[0];
strcpy( buf, init.c_str() );
ok = ( OSRImportFromWkt( handle, &buf_ptr ) == OGRERR_NONE );
}
else
{
std::string init = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs";
ok = ( OSRImportFromProj4( handle, init.c_str() ) == OGRERR_NONE );
}
if ( ok )
{
result = new LTPSpatialReference( handle, refPointLLA );
}
else
{
OE_WARN << LC << "Unable to create LTP SRS" << std::endl;
if ( handle )
OSRDestroySpatialReference( handle );
}
return result;
}
#endif
SpatialReference*
SpatialReference::createFromWKT( const std::string& init, const std::string& init_alias, const std::string& name )
{
osg::ref_ptr<SpatialReference> result;
GDAL_SCOPED_LOCK;
void* handle = OSRNewSpatialReference( NULL );
char buf[4096];
char* buf_ptr = &buf[0];
strcpy( buf, init.c_str() );
if ( OSRImportFromWkt( handle, &buf_ptr ) == OGRERR_NONE )
{
result = new SpatialReference( handle, "WKT", init_alias, name );
result = result->validate();
}
else
{
OE_WARN << LC << "Unable to create spatial reference from WKT: " << init << std::endl;
OSRDestroySpatialReference( handle );
}
return result.release();
}
SpatialReference*
SpatialReference::create( const std::string& init )
{
static OpenThreads::Mutex s_mutex;
OpenThreads::ScopedLock<OpenThreads::Mutex> exclusiveLock(s_mutex);
std::string low = init;
std::transform( low.begin(), low.end(), low.begin(), ::tolower );
SpatialReferenceCache::iterator itr = getSpatialReferenceCache().find(init);
if (itr != getSpatialReferenceCache().end())
{
//OE_NOTICE << "Returning cached SRS" << std::endl;
return itr->second.get();
}
osg::ref_ptr<SpatialReference> srs;
// shortcut for spherical-mercator:
if (low == "spherical-mercator" || low == "epsg:900913" || low == "epsg:3785" ||
low == "epsg:41001" || low == "epsg:102113" || low == "epsg:102100")
{
// note the use of nadgrids=@null (see http://proj.maptools.org/faq.html)
// adjusted +a by ONE to work around osg manipulator error until we can figure out why.. GW
srs = createFromPROJ4(
"+proj=merc +a=6378137 +b=6378137 +lon_0=0 +k=1 +x_0=0 +y_0=0 +nadgrids=@null +units=m +no_defs",
init,
"Spherical Mercator" );
}
// ellipsoidal ("world") mercator:
else if (low == "epsg:54004" || low == "epsg:9804" || low == "epsg:3832")
{
srs = createFromPROJ4(
"+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs",
init,
"World Mercator" );
}
// common WGS84:
else if (low == "epsg:4326" || low == "wgs84")
{
srs = createFromPROJ4(
"+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs",
init,
"WGS84" );
}
// custom srs for the unified cube
/* else if ( low == "unified-cube" )
{
srs = createCube();
}
*/
else if ( low.find( "+" ) == 0 )
{
srs = createFromPROJ4( low, init );
}
else if ( low.find( "epsg:" ) == 0 || low.find( "osgeo:" ) == 0 )
{
srs = createFromPROJ4( std::string("+init=") + low, init );
}
else if ( low.find( "projcs" ) == 0 || low.find( "geogcs" ) == 0 )
{
srs = createFromWKT( init, init );
}
else
{
return NULL;
}
getSpatialReferenceCache()[init] = srs;
return srs.get();
}
SpatialReference*
SpatialReference::create( osg::CoordinateSystemNode* csn )
{
SpatialReference* result = NULL;
if ( csn && !csn->getCoordinateSystem().empty() )
{
result = create( csn->getCoordinateSystem() );
}
return result;
}
SpatialReference*
SpatialReference::createFromHandle( void* ogrHandle, bool xferOwnership )
{
SpatialReference* srs = new SpatialReference( ogrHandle, xferOwnership );
return srs;
}
SpatialReference*
SpatialReference::validate()
{
std::string proj = getOGRAttrValue( _handle, "PROJECTION", 0 );
// fix invalid ESRI LCC projections:
if ( proj == "Lambert_Conformal_Conic" )
{
bool has_2_sps =
!getOGRAttrValue( _handle, "Standard_Parallel_2", 0 ).empty() ||
!getOGRAttrValue( _handle, "standard_parallel_2", 0 ).empty();
std::string new_wkt = getWKT();
if ( has_2_sps )
replaceIn( new_wkt, "Lambert_Conformal_Conic", "Lambert_Conformal_Conic_2SP" );
else
replaceIn( new_wkt, "Lambert_Conformal_Conic", "Lambert_Conformal_Conic_1SP" );
OE_INFO << LC << "Morphing Lambert_Conformal_Conic to 1SP/2SP" << std::endl;
return createFromWKT( new_wkt, _init_str, _name );
}
// fixes for ESRI Plate_Carree and Equidistant_Cylindrical projections:
else if ( proj == "Plate_Carree" )
{
std::string new_wkt = getWKT();
replaceIn( new_wkt, "Plate_Carree", "Equirectangular" );
OE_INFO << LC << "Morphing Plate_Carree to Equirectangular" << std::endl;
return createFromWKT( new_wkt, _init_str, _name ); //, input->getReferenceFrame() );
}
else if ( proj == "Equidistant_Cylindrical" )
{
std::string new_wkt = getWKT();
OE_INFO << LC << "Morphing Equidistant_Cylindrical to Equirectangular" << std::endl;
replaceIn( new_wkt, "Equidistant_Cylindrical", "Equirectangular" );
return createFromWKT( new_wkt, _init_str, _name );
}
// no changes.
return this;
}
/****************************************************************************/
SpatialReference::SpatialReference(void* handle,
const std::string& init_type,
const std::string& init_str,
const std::string& name ) :
osg::Referenced( true ),
_initialized( false ),
_handle( handle ),
_owns_handle( true ),
_name( name ),
_init_type( init_type ),
_init_str( init_str )
{
_init_str_lc = init_str;
std::transform( _init_str_lc.begin(), _init_str_lc.end(), _init_str_lc.begin(), ::tolower );
}
SpatialReference::SpatialReference(void* handle, bool ownsHandle) :
osg::Referenced( true ),
_initialized( false ),
_handle( handle ),
_owns_handle( ownsHandle )
{
//nop
}
SpatialReference::~SpatialReference()
{
if ( _handle )
{
GDAL_SCOPED_LOCK;
for (TransformHandleCache::iterator itr = _transformHandleCache.begin(); itr != _transformHandleCache.end(); ++itr)
{
OCTDestroyCoordinateTransformation(itr->second);
}
if ( _owns_handle )
{
OSRDestroySpatialReference( _handle );
}
_handle = NULL;
}
}
bool
SpatialReference::isGeographic() const
{
if ( !_initialized )
const_cast<SpatialReference*>(this)->init();
return _is_geographic;
}
bool
SpatialReference::isProjected() const
{
if ( !_initialized )
const_cast<SpatialReference*>(this)->init();
return !_is_geographic;
}
const std::string&
SpatialReference::getName() const
{
if ( !_initialized )
const_cast<SpatialReference*>(this)->init();
return _name;
}
const osg::EllipsoidModel*
SpatialReference::getEllipsoid() const
{
if ( !_initialized )
const_cast<SpatialReference*>(this)->init();
return _ellipsoid.get();
}
const std::string&
SpatialReference::getDatumName() const
{
if ( !_initialized )
const_cast<SpatialReference*>(this)->init();
return _datum;
}
const std::string&
SpatialReference::getWKT() const
{
if ( !_initialized )
const_cast<SpatialReference*>(this)->init();
return _wkt;
}
const std::string&
SpatialReference::getInitString() const
{
if ( !_initialized )
const_cast<SpatialReference*>(this)->init();
return _init_str;
}
const std::string&
SpatialReference::getInitType() const
{
if ( !_initialized )
const_cast<SpatialReference*>(this)->init();
return _init_type;
}
bool
SpatialReference::isEquivalentTo( const SpatialReference* rhs ) const
{
if ( !_initialized )
const_cast<SpatialReference*>(this)->init();
return _isEquivalentTo( rhs );
}
bool
SpatialReference::_isEquivalentTo( const SpatialReference* rhs ) const
{
if ( !rhs )
return false;
if ( this == rhs )
return true;
if (isGeographic() != rhs->isGeographic() ||
isMercator() != rhs->isMercator() ||
isNorthPolar() != rhs->isNorthPolar() ||
isSouthPolar() != rhs->isSouthPolar() ||
isContiguous() != rhs->isContiguous() ||
isUserDefined() != rhs->isUserDefined() ||
isCube() != rhs->isCube() ||
isLTP() != rhs->isLTP() )
{
return false;
}
if ( _init_str_lc == rhs->_init_str_lc )
return true;
if ( this->getWKT() == rhs->getWKT() )
return true;
if (this->isGeographic() && rhs->isGeographic() &&
this->getEllipsoid()->getRadiusEquator() == rhs->getEllipsoid()->getRadiusEquator() &&
this->getEllipsoid()->getRadiusPolar() == rhs->getEllipsoid()->getRadiusPolar())
{
return true;
}
// last resort, since it requires the lock
GDAL_SCOPED_LOCK;
return TRUE == ::OSRIsSame( _handle, rhs->_handle );
}
const SpatialReference*
SpatialReference::getGeographicSRS() const
{
if ( !_initialized )
const_cast<SpatialReference*>(this)->init();
if ( _is_geographic )
return this;
if ( !_geo_srs.valid() )
{
GDAL_SCOPED_LOCK;
if ( !_geo_srs.valid() ) // double-check pattern
{
void* new_handle = OSRNewSpatialReference( NULL );
int err = OSRCopyGeogCSFrom( new_handle, _handle );
if ( err == OGRERR_NONE )
{
const_cast<SpatialReference*>(this)->_geo_srs = new SpatialReference( new_handle );
}
else
{
OSRDestroySpatialReference( new_handle );
}
}
}
return _geo_srs.get();
}
/*
SpatialReference*
SpatialReference::createTangentPlaneSRS( const osg::Vec3d& pos ) const
{
SpatialReference* result = 0L;
osg::Vec3d lla;
if ( this->transform(pos, this->getGeographicSRS(), lla) )
{
result = new LTPSpatialReference( this->getGeographicSRS()->_handle, lla );
}
else
{
OE_WARN << LC << "Unable to create LTP SRS" << std::endl;
}
return result;
}
*/
bool
SpatialReference::isMercator() const
{
if ( !_initialized )
const_cast<SpatialReference*>(this)->init();
return _is_mercator;
}
bool
SpatialReference::isNorthPolar() const
{
if ( !_initialized )
const_cast<SpatialReference*>(this)->init();
return _is_north_polar;
}
bool
SpatialReference::isSouthPolar() const
{
if ( !_initialized )
const_cast<SpatialReference*>(this)->init();
return _is_south_polar;
}
bool
SpatialReference::isContiguous() const
{
if ( !_initialized )
const_cast<SpatialReference*>(this)->init();
return _is_contiguous;
}
bool
SpatialReference::isUserDefined() const
{
if ( !_initialized )
const_cast<SpatialReference*>(this)->init();
return _is_user_defined;
}
bool
SpatialReference::isCube() const
{
if ( !_initialized )
const_cast<SpatialReference*>(this)->init();
return _is_cube;
}
osg::CoordinateSystemNode*
SpatialReference::createCoordinateSystemNode() const
{
if ( !_initialized )
const_cast<SpatialReference*>(this)->init();
osg::CoordinateSystemNode* csn = new osg::CoordinateSystemNode();
populateCoordinateSystemNode( csn );
return csn;
}
bool
SpatialReference::populateCoordinateSystemNode( osg::CoordinateSystemNode* csn ) const
{
if ( !csn )
return false;
if ( !_initialized )
const_cast<SpatialReference*>(this)->init();
if ( !_wkt.empty() )
{
csn->setFormat( "WKT" );
csn->setCoordinateSystem( _wkt );
}
else if ( !_proj4.empty() )
{
csn->setFormat( "PROJ4" );
csn->setCoordinateSystem( _proj4 );
}
else
{
csn->setFormat( _init_type );
csn->setCoordinateSystem( _init_str );
}
csn->setEllipsoidModel( _ellipsoid.get() );
return true;
}
// Make a MatrixTransform suitable for use with a Locator object based on the given extents.
// Calling Locator::setTransformAsExtents doesn't work with OSG 2.6 due to the fact that the
// _inverse member isn't updated properly. Calling Locator::setTransform works correctly.
static osg::Matrixd
getTransformFromExtents(double minX, double minY, double maxX, double maxY)
{
osg::Matrixd transform;
transform.set(
maxX-minX, 0.0, 0.0, 0.0,
0.0, maxY-minY, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
minX, minY, 0.0, 1.0);
return transform;
}
/*GeoLocator*
SpatialReference::createLocator(double xmin, double ymin, double xmax, double ymax,
bool plate_carre ) const
{
if ( !_initialized )
const_cast<SpatialReference*>(this)->init();
GeoLocator* locator = new GeoLocator( GeoExtent(this, xmin, ymin, xmax, ymax) );
locator->setEllipsoidModel( (osg::EllipsoidModel*)getEllipsoid() );
locator->setCoordinateSystemType( isGeographic()? osgTerrain::Locator::GEOGRAPHIC : osgTerrain::Locator::PROJECTED );
// note: not setting the format/cs on purpose.
if ( isGeographic() && !plate_carre )
{
locator->setTransform( getTransformFromExtents(
osg::DegreesToRadians( xmin ),
osg::DegreesToRadians( ymin ),
osg::DegreesToRadians( xmax ),
osg::DegreesToRadians( ymax ) ) );
}
else
{
locator->setTransform( getTransformFromExtents( xmin, ymin, xmax, ymax ) );
}
return locator;
}
*/
bool
SpatialReference::transform(double x, double y, double z,
const SpatialReference* out_srs,
double& out_x, double& out_y, double& out_z,
void* context ) const
{
if ( !_initialized )
const_cast<SpatialReference*>(this)->init();
//Check for equivalence and return if the coordinate systems are the same.
if (isEquivalentTo(out_srs))
{
out_x = x;
out_y = y;
out_z = z;
return true;
}
out_x = x;
out_y = y;
bool result = transformPoints(out_srs, &out_x, &out_y, &out_z, 1, context);
return result;
}
// http://en.wikipedia.org/wiki/Mercator_projection#Mathematics_of_the_projection
static bool
mercatorToGeographic( double* x, double* y, double* z, int numPoints )
{
for( int i=0; i<numPoints; i++ )
{
double xr = -osg::PI + ((x[i]-MERC_MINX)/MERC_WIDTH)*2.0*osg::PI;
double yr = -osg::PI + ((y[i]-MERC_MINY)/MERC_HEIGHT)*2.0*osg::PI;
x[i] = osg::RadiansToDegrees( xr );
y[i] = osg::RadiansToDegrees( 2.0 * atan( exp(yr) ) - osg::PI_2 );
// z doesn't change
}
return true;
}
// http://en.wikipedia.org/wiki/Mercator_projection#Mathematics_of_the_projection
static bool
geographicToMercator( double* x, double* y, double* z, int numPoints )
{
for( int i=0; i<numPoints; i++ )
{
double xr = (osg::DegreesToRadians(x[i]) - (-osg::PI)) / (2.0*osg::PI);
double sinLat = sin(osg::DegreesToRadians(y[i]));
double oneMinusSinLat = 1-sinLat;
if ( oneMinusSinLat != 0.0 )
{
double yr = ((0.5 * log( (1+sinLat)/oneMinusSinLat )) - (-osg::PI)) / (2.0*osg::PI);
x[i] = MERC_MINX + (xr * MERC_WIDTH);
y[i] = MERC_MINY + (yr * MERC_HEIGHT);
// z doesn't change
}
}
return true;
}
bool
SpatialReference::transformPoints(const SpatialReference* out_srs,
double* x, double* y, double* z,
unsigned int numPoints,
void* context,
bool ignore_errors ) const
{
if ( !_initialized )
const_cast<SpatialReference*>(this)->init();
//Check for equivalence and return if the coordinate systems are the same.
if (isEquivalentTo(out_srs))
return true;
if ( z )
{
for (unsigned int i = 0; i < numPoints; ++i)
preTransform(x[i], y[i], z[i], context);
}
else
{
double dummyZ = 0.0;
for (unsigned int i = 0; i < numPoints; ++i)
preTransform(x[i], y[i], dummyZ, context);
}
bool success = false;
#ifdef USE_CUSTOM_MERCATOR_TRANSFORM
if ( isGeographic() && out_srs->isMercator() )
{
success = geographicToMercator( x, y, z, numPoints );
}
else if ( isMercator() && out_srs->isGeographic() )
{
success = mercatorToGeographic( x, y, z, numPoints );
}
else
#endif
{
GDAL_SCOPED_LOCK;
void* xform_handle = NULL;
TransformHandleCache::const_iterator itr = _transformHandleCache.find(out_srs->getWKT());
if (itr != _transformHandleCache.end())
{
//OE_DEBUG << "SpatialRefernece: using cached transform handle" << std::endl;
xform_handle = itr->second;
}
else
{
xform_handle = OCTNewCoordinateTransformation( _handle, out_srs->_handle);
const_cast<SpatialReference*>(this)->_transformHandleCache[out_srs->getWKT()] = xform_handle;
}
if ( !xform_handle )
{
OE_WARN << LC
<< "SRS xform not possible" << std::endl
<< " From => " << getName() << std::endl
<< " To => " << out_srs->getName() << std::endl;
return false;
}
//double* temp_z = new double[numPoints];
//success = OCTTransform( xform_handle, numPoints, x, y, temp_z ) > 0;
//delete[] temp_z;
success = OCTTransform( xform_handle, numPoints, x, y, z ) > 0;
// END GDAL_SCOPE_LOCK
}
if ( success || ignore_errors )
{
if ( z )
{
for (unsigned int i = 0; i < numPoints; ++i)
out_srs->postTransform(x[i], y[i], z[i], context);
}
else
{
double dummyZ = 0.0;
for (unsigned int i = 0; i < numPoints; ++i)
out_srs->postTransform(x[i], y[i], dummyZ, context);
}
}
else
{
OE_WARN << LC << "Failed to xform a point from "
<< getName() << " to " << out_srs->getName()
<< std::endl;
}
return success;
}
bool
SpatialReference::transformPoints(const SpatialReference* out_srs,
std::vector<osg::Vec3d>& points,
void* context,
bool ignore_errors ) const
{
if ( !_initialized )
const_cast<SpatialReference*>(this)->init();
//Check for equivalence and return if the coordinate systems are the same.
if (isEquivalentTo(out_srs))
return true;
int numPoints = points.size();
double* x = new double[numPoints];
double* y = new double[numPoints];
double* z = new double[numPoints];
for( int i=0; i<numPoints; i++ )
{
x[i] = points[i].x();
y[i] = points[i].y();
z[i] = points[i].z();
}
bool success = transformPoints( out_srs, x, y, z, numPoints, context, ignore_errors );
if ( success )
{
for( int i=0; i<numPoints; i++ )
{
points[i].x() = x[i];
points[i].y() = y[i];
points[i].z() = z[i];
}
}
delete[] x;
delete[] y;
delete[] z;
return success;
}
bool
SpatialReference::transformToECEF(const osg::Vec3d& input,
osg::Vec3d& output ) const
{
double lat = input.y(), lon = input.x(), alt = input.z();
// first convert to lat/long if necessary:
if ( !isGeographic() )
transform( input.x(), input.y(), input.z(), getGeographicSRS(), lon, lat, alt );
// then convert to ECEF.
//double z = input.z();
getGeographicSRS()->getEllipsoid()->convertLatLongHeightToXYZ(
osg::DegreesToRadians( lat ), osg::DegreesToRadians( lon ), alt,
output.x(), output.y(), output.z() );
return true;
}
bool
SpatialReference::transformToECEF(std::vector<osg::Vec3d>& points,
bool ignoreErrors ) const
{
if ( points.size() == 0 )
return false;
const SpatialReference* geoSRS = getGeographicSRS();
const osg::EllipsoidModel* ellipsoid = geoSRS->getEllipsoid();
for( unsigned i=0; i<points.size(); ++i )
{
osg::Vec3d& p = points[i];
if ( !isGeographic() )
transform( p.x(), p.y(), p.z(), geoSRS, p.x(), p.y(), p.z() );
ellipsoid->convertLatLongHeightToXYZ(
osg::DegreesToRadians( p.y() ), osg::DegreesToRadians( p.x() ), p.z(),
p.x(), p.y(), p.z() );
}
return true;
}
bool
SpatialReference::transformFromECEF(const osg::Vec3d& input,
osg::Vec3d& output ) const
{
// transform to lat/long:
osg::Vec3d geo;
getGeographicSRS()->getEllipsoid()->convertXYZToLatLongHeight(
input.x(), input.y(), input.z(),
geo.y(), geo.x(), geo.z() );
// then convert to the local SRS.
if ( isGeographic() )
{
output.set( osg::RadiansToDegrees(geo.x()), osg::RadiansToDegrees(geo.y()), geo.z() );
}
else
{
getGeographicSRS()->transform(
osg::RadiansToDegrees(geo.x()), osg::RadiansToDegrees(geo.y()), geo.z(),
this,
output.x(), output.y(), output.z() );
//output.z() = geo.z();
}
return true;
}
bool
SpatialReference::transformFromECEF(std::vector<osg::Vec3d>& points,
bool ignoreErrors ) const
{
bool ok = true;
// first convert all the points to lat/long (in place):
for( unsigned i=0; i<points.size(); ++i )
{
osg::Vec3d& p = points[i];
osg::Vec3d geo;
getGeographicSRS()->getEllipsoid()->convertXYZToLatLongHeight(
p.x(), p.y(), p.z(),
geo.y(), geo.x(), geo.z() );
geo.x() = osg::RadiansToDegrees( geo.x() );
geo.y() = osg::RadiansToDegrees( geo.y() );
p = geo;
}
// then convert them all to the local SRS if necessary.
if ( !isGeographic() )
{
ok = getGeographicSRS()->transformPoints( this, points, 0L, ignoreErrors );
}
return ok;
}
bool
SpatialReference::transformExtent(const SpatialReference* to_srs,
double& in_out_xmin,
double& in_out_ymin,
double& in_out_xmax,
double& in_out_ymax,
void* context ) const
{
if ( !_initialized )
const_cast<SpatialReference*>(this)->init();
int oks = 0;
//Transform all points and take the maximum bounding rectangle the resulting points
double llx, lly;
double ulx, uly;
double urx, ury;
double lrx, lry;
double dummyZ = 0;
//Lower Left
oks += transform( in_out_xmin, in_out_ymin, 0, to_srs, llx, lly, dummyZ, context ) == true;
//Upper Left
oks += transform( in_out_xmin, in_out_ymax, 0, to_srs, ulx, uly, dummyZ, context ) == true;
//Upper Right
oks += transform( in_out_xmax, in_out_ymax, 0, to_srs, urx, ury, dummyZ, context ) == true;
//Lower Right
oks += transform( in_out_xmax, in_out_ymin, 0, to_srs, lrx, lry, dummyZ, context ) == true;
if (oks == 4)