1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
|
/******************************************************************************
* $Id: ogrct.cpp 25627 2013-02-10 10:17:19Z rouault $
*
* Project: OpenGIS Simple Features Reference Implementation
* Purpose: The OGRSCoordinateTransformation class.
* Author: Frank Warmerdam, warmerdam@pobox.com
*
******************************************************************************
* Copyright (c) 2000, Frank Warmerdam
*
* 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 "cpl_port.h"
#include "cpl_error.h"
#include "cpl_conv.h"
#include "cpl_string.h"
#include "cpl_multiproc.h"
#ifdef PROJ_STATIC
#include "proj_api.h"
#endif
CPL_CVSID("$Id: ogrct.cpp 25627 2013-02-10 10:17:19Z rouault $");
/* ==================================================================== */
/* PROJ.4 interface stuff. */
/* ==================================================================== */
#ifndef PROJ_STATIC
typedef struct { double u, v; } projUV;
#define projPJ void *
#define projCtx void *
#define RAD_TO_DEG 57.29577951308232
#define DEG_TO_RAD .0174532925199432958
#else
#if PJ_VERSION < 480
#define projCtx void *
#endif
#endif
static void *hPROJMutex = NULL;
static projPJ (*pfn_pj_init_plus)(const char *) = NULL;
static projPJ (*pfn_pj_init)(int, char**) = NULL;
static void (*pfn_pj_free)(projPJ) = NULL;
static int (*pfn_pj_transform)(projPJ, projPJ, long, int,
double *, double *, double * ) = NULL;
static int *(*pfn_pj_get_errno_ref)(void) = NULL;
static char *(*pfn_pj_strerrno)(int) = NULL;
static char *(*pfn_pj_get_def)(projPJ,int) = NULL;
static void (*pfn_pj_dalloc)(void *) = NULL;
static projPJ (*pfn_pj_init_plus_ctx)( projCtx, const char * ) = NULL;
static int (*pfn_pj_ctx_get_errno)( projCtx ) = NULL;
static projCtx (*pfn_pj_ctx_alloc)(void) = NULL;
static void (*pfn_pj_ctx_free)( projCtx ) = NULL;
#if (defined(WIN32) || defined(WIN32CE)) && !defined(__MINGW32__)
# define LIBNAME "proj.dll"
#elif defined(__MINGW32__)
// XXX: If PROJ.4 library was properly built using libtool in Cygwin or MinGW
// environments it has the interface version number embedded in the file name
// (it is CURRENT-AGE number). If DLL came somewhere else (e.g. from MSVC
// build) it can be named either way, so use PROJSO environment variable to
// specify the right library name. By default assume that in Cygwin/MinGW all
// components were buit in the same way.
# define LIBNAME "libproj-0.dll"
#elif defined(__CYGWIN__)
# define LIBNAME "cygproj-0.dll"
#elif defined(__APPLE__)
# define LIBNAME "libproj.dylib"
#else
# define LIBNAME "libproj.so"
#endif
/************************************************************************/
/* OCTCleanupProjMutex() */
/************************************************************************/
void OCTCleanupProjMutex()
{
if( hPROJMutex != NULL )
{
CPLDestroyMutex(hPROJMutex);
hPROJMutex = NULL;
}
}
/************************************************************************/
/* OGRProj4CT */
/************************************************************************/
class OGRProj4CT : public OGRCoordinateTransformation
{
OGRSpatialReference *poSRSSource;
void *psPJSource;
int bSourceLatLong;
double dfSourceToRadians;
double dfSourceFromRadians;
int bSourceWrap;
double dfSourceWrapLong;
OGRSpatialReference *poSRSTarget;
void *psPJTarget;
int bTargetLatLong;
double dfTargetToRadians;
double dfTargetFromRadians;
int bTargetWrap;
double dfTargetWrapLong;
int nErrorCount;
int bCheckWithInvertProj;
double dfThreshold;
projCtx pjctx;
int InitializeNoLock( OGRSpatialReference *poSource,
OGRSpatialReference *poTarget );
int nMaxCount;
double *padfOriX;
double *padfOriY;
double *padfOriZ;
double *padfTargetX;
double *padfTargetY;
double *padfTargetZ;
public:
OGRProj4CT();
virtual ~OGRProj4CT();
int Initialize( OGRSpatialReference *poSource,
OGRSpatialReference *poTarget );
virtual OGRSpatialReference *GetSourceCS();
virtual OGRSpatialReference *GetTargetCS();
virtual int Transform( int nCount,
double *x, double *y, double *z = NULL );
virtual int TransformEx( int nCount,
double *x, double *y, double *z = NULL,
int *panSuccess = NULL );
};
/************************************************************************/
/* GetProjLibraryName() */
/************************************************************************/
static const char* GetProjLibraryName()
{
const char *pszLibName = LIBNAME;
#if !defined(WIN32CE)
if( CPLGetConfigOption("PROJSO",NULL) != NULL )
pszLibName = CPLGetConfigOption("PROJSO",NULL);
#endif
return pszLibName;
}
/************************************************************************/
/* LoadProjLibrary() */
/************************************************************************/
static int LoadProjLibrary()
{
CPLMutexHolderD( &hPROJMutex );
static int bTriedToLoad = FALSE;
const char *pszLibName;
if( bTriedToLoad )
return( pfn_pj_transform != NULL );
bTriedToLoad = TRUE;
pszLibName = GetProjLibraryName();
#ifdef PROJ_STATIC
pfn_pj_init = pj_init;
pfn_pj_init_plus = pj_init_plus;
pfn_pj_free = pj_free;
pfn_pj_transform = pj_transform;
pfn_pj_get_errno_ref = (int *(*)(void)) pj_get_errno_ref;
pfn_pj_strerrno = pj_strerrno;
pfn_pj_dalloc = pj_dalloc;
#if PJ_VERSION >= 446
pfn_pj_get_def = pj_get_def;
#endif
#if PJ_VERSION >= 480
pfn_pj_ctx_alloc = pj_ctx_alloc;
pfn_pj_ctx_free = pj_ctx_free;
pfn_pj_init_plus_ctx = pj_init_plus_ctx;
pfn_pj_ctx_get_errno = pj_ctx_get_errno;
#endif
#else
CPLPushErrorHandler( CPLQuietErrorHandler );
pfn_pj_init = (projPJ (*)(int, char**)) CPLGetSymbol( pszLibName,
"pj_init" );
CPLPopErrorHandler();
if( pfn_pj_init == NULL )
return( FALSE );
pfn_pj_init_plus = (projPJ (*)(const char *))
CPLGetSymbol( pszLibName, "pj_init_plus" );
pfn_pj_free = (void (*)(projPJ))
CPLGetSymbol( pszLibName, "pj_free" );
pfn_pj_transform = (int (*)(projPJ,projPJ,long,int,double*,
double*,double*))
CPLGetSymbol( pszLibName, "pj_transform" );
pfn_pj_get_errno_ref = (int *(*)(void))
CPLGetSymbol( pszLibName, "pj_get_errno_ref" );
pfn_pj_strerrno = (char *(*)(int))
CPLGetSymbol( pszLibName, "pj_strerrno" );
CPLPushErrorHandler( CPLQuietErrorHandler );
pfn_pj_get_def = (char *(*)(projPJ,int))
CPLGetSymbol( pszLibName, "pj_get_def" );
pfn_pj_dalloc = (void (*)(void*))
CPLGetSymbol( pszLibName, "pj_dalloc" );
/* PROJ 4.8.0 symbols */
pfn_pj_ctx_alloc = (projCtx (*)( void ))
CPLGetSymbol( pszLibName, "pj_ctx_alloc" );
pfn_pj_ctx_free = (void (*)( projCtx ))
CPLGetSymbol( pszLibName, "pj_ctx_free" );
pfn_pj_init_plus_ctx = (projPJ (*)( projCtx, const char * ))
CPLGetSymbol( pszLibName, "pj_init_plus_ctx" );
pfn_pj_ctx_get_errno = (int (*)( projCtx ))
CPLGetSymbol( pszLibName, "pj_ctx_get_errno" );
CPLPopErrorHandler();
CPLErrorReset();
#endif
if (pfn_pj_ctx_alloc != NULL &&
pfn_pj_ctx_free != NULL &&
pfn_pj_init_plus_ctx != NULL &&
pfn_pj_ctx_get_errno != NULL &&
CSLTestBoolean(CPLGetConfigOption("USE_PROJ_480_FEATURES", "YES")))
{
CPLDebug("OGRCT", "PROJ >= 4.8.0 features enabled");
}
else
{
pfn_pj_ctx_alloc = NULL;
pfn_pj_ctx_free = NULL;
pfn_pj_init_plus_ctx = NULL;
pfn_pj_ctx_get_errno = NULL;
}
if( pfn_pj_transform == NULL )
{
CPLError( CE_Failure, CPLE_AppDefined,
"Attempt to load %s, but couldn't find pj_transform.\n"
"Please upgrade to PROJ 4.1.2 or later.",
pszLibName );
return FALSE;
}
return( TRUE );
}
/************************************************************************/
/* OCTProj4Normalize() */
/* */
/* This function is really just here since we already have all */
/* the code to load libproj.so. It is intended to "normalize" */
/* a proj.4 definition, expanding +init= definitions and so */
/* forth as possible. */
/************************************************************************/
char *OCTProj4Normalize( const char *pszProj4Src )
{
char *pszNewProj4Def, *pszCopy;
projPJ psPJSource = NULL;
CPLMutexHolderD( &hPROJMutex );
if( !LoadProjLibrary() || pfn_pj_dalloc == NULL || pfn_pj_get_def == NULL )
return CPLStrdup( pszProj4Src );
psPJSource = pfn_pj_init_plus( pszProj4Src );
if( psPJSource == NULL )
return CPLStrdup( pszProj4Src );
pszNewProj4Def = pfn_pj_get_def( psPJSource, 0 );
pfn_pj_free( psPJSource );
if( pszNewProj4Def == NULL )
return CPLStrdup( pszProj4Src );
pszCopy = CPLStrdup( pszNewProj4Def );
pfn_pj_dalloc( pszNewProj4Def );
return pszCopy;
}
/************************************************************************/
/* OCTDestroyCoordinateTransformation() */
/************************************************************************/
/**
* \brief OGRCoordinateTransformation destructor.
*
* This function is the same as OGRCoordinateTransformation::DestroyCT()
*
* @param hCT the object to delete
*/
void CPL_STDCALL
OCTDestroyCoordinateTransformation( OGRCoordinateTransformationH hCT )
{
delete (OGRCoordinateTransformation *) hCT;
}
/************************************************************************/
/* DestroyCT() */
/************************************************************************/
/**
* \brief OGRCoordinateTransformation destructor.
*
* This function is the same as OGRCoordinateTransformation::~OGRCoordinateTransformation()
* and OCTDestroyCoordinateTransformation()
*
* This static method will destroy a OGRCoordinateTransformation. 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).
*
* @param poCT the object to delete
*
* @since GDAL 1.7.0
*/
void OGRCoordinateTransformation::DestroyCT(OGRCoordinateTransformation* poCT)
{
delete poCT;
}
/************************************************************************/
/* OGRCreateCoordinateTransformation() */
/************************************************************************/
/**
* Create transformation object.
*
* This is the same as the C function OCTNewCoordinateTransformation().
*
* Input spatial reference system objects are assigned
* by copy (calling clone() method) and no ownership transfer occurs.
*
* The delete operator, or OCTDestroyCoordinateTransformation() should
* be used to destroy transformation objects.
*
* The PROJ.4 library must be available at run-time.
*
* @param poSource source spatial reference system.
* @param poTarget target spatial reference system.
* @return NULL on failure or a ready to use transformation object.
*/
OGRCoordinateTransformation*
OGRCreateCoordinateTransformation( OGRSpatialReference *poSource,
OGRSpatialReference *poTarget )
{
OGRProj4CT *poCT;
if( pfn_pj_init == NULL && !LoadProjLibrary() )
{
CPLError( CE_Failure, CPLE_NotSupported,
"Unable to load PROJ.4 library (%s), creation of\n"
"OGRCoordinateTransformation failed.",
GetProjLibraryName() );
return NULL;
}
poCT = new OGRProj4CT();
if( !poCT->Initialize( poSource, poTarget ) )
{
delete poCT;
return NULL;
}
else
{
return poCT;
}
}
/************************************************************************/
/* OCTNewCoordinateTransformation() */
/************************************************************************/
/**
* Create transformation object.
*
* This is the same as the C++ function OGRCreateCoordinateTransformation().
*
* Input spatial reference system objects are assigned
* by copy (calling clone() method) and no ownership transfer occurs.
*
* OCTDestroyCoordinateTransformation() should
* be used to destroy transformation objects.
*
* The PROJ.4 library must be available at run-time.
*
* @param hSourceSRS source spatial reference system.
* @param hTargetSRS target spatial reference system.
* @return NULL on failure or a ready to use transformation object.
*/
OGRCoordinateTransformationH CPL_STDCALL
OCTNewCoordinateTransformation(
OGRSpatialReferenceH hSourceSRS, OGRSpatialReferenceH hTargetSRS )
{
return (OGRCoordinateTransformationH)
OGRCreateCoordinateTransformation(
(OGRSpatialReference *) hSourceSRS,
(OGRSpatialReference *) hTargetSRS );
}
/************************************************************************/
/* OGRProj4CT() */
/************************************************************************/
OGRProj4CT::OGRProj4CT()
{
poSRSSource = NULL;
poSRSTarget = NULL;
psPJSource = NULL;
psPJTarget = NULL;
nErrorCount = 0;
bCheckWithInvertProj = FALSE;
dfThreshold = 0;
nMaxCount = 0;
padfOriX = NULL;
padfOriY = NULL;
padfOriZ = NULL;
padfTargetX = NULL;
padfTargetY = NULL;
padfTargetZ = NULL;
if (pfn_pj_ctx_alloc != NULL)
pjctx = pfn_pj_ctx_alloc();
else
pjctx = NULL;
}
/************************************************************************/
/* ~OGRProj4CT() */
/************************************************************************/
OGRProj4CT::~OGRProj4CT()
{
if( poSRSSource != NULL )
{
if( poSRSSource->Dereference() <= 0 )
delete poSRSSource;
}
if( poSRSTarget != NULL )
{
if( poSRSTarget->Dereference() <= 0 )
delete poSRSTarget;
}
if (pjctx != NULL)
{
pfn_pj_ctx_free(pjctx);
if( psPJSource != NULL )
pfn_pj_free( psPJSource );
if( psPJTarget != NULL )
pfn_pj_free( psPJTarget );
}
else
{
CPLMutexHolderD( &hPROJMutex );
if( psPJSource != NULL )
pfn_pj_free( psPJSource );
if( psPJTarget != NULL )
pfn_pj_free( psPJTarget );
}
CPLFree(padfOriX);
CPLFree(padfOriY);
CPLFree(padfOriZ);
CPLFree(padfTargetX);
CPLFree(padfTargetY);
CPLFree(padfTargetZ);
}
/************************************************************************/
/* Initialize() */
/************************************************************************/
int OGRProj4CT::Initialize( OGRSpatialReference * poSourceIn,
OGRSpatialReference * poTargetIn )
{
if (pjctx != NULL)
{
return InitializeNoLock(poSourceIn, poTargetIn);
}
CPLMutexHolderD( &hPROJMutex );
return InitializeNoLock(poSourceIn, poTargetIn);
}
/************************************************************************/
/* InitializeNoLock() */
/************************************************************************/
int OGRProj4CT::InitializeNoLock( OGRSpatialReference * poSourceIn,
OGRSpatialReference * poTargetIn )
{
if( poSourceIn == NULL || poTargetIn == NULL )
return FALSE;
poSRSSource = poSourceIn->Clone();
poSRSTarget = poTargetIn->Clone();
bSourceLatLong = poSRSSource->IsGeographic();
bTargetLatLong = poSRSTarget->IsGeographic();
/* -------------------------------------------------------------------- */
/* Setup source and target translations to radians for lat/long */
/* systems. */
/* -------------------------------------------------------------------- */
dfSourceToRadians = DEG_TO_RAD;
dfSourceFromRadians = RAD_TO_DEG;
bSourceWrap = FALSE;
dfSourceWrapLong = 0.0;
if( bSourceLatLong )
{
OGR_SRSNode *poUNITS = poSRSSource->GetAttrNode( "GEOGCS|UNIT" );
if( poUNITS && poUNITS->GetChildCount() >= 2 )
{
dfSourceToRadians = atof(poUNITS->GetChild(1)->GetValue());
if( dfSourceToRadians == 0.0 )
dfSourceToRadians = DEG_TO_RAD;
else
dfSourceFromRadians = 1 / dfSourceToRadians;
}
}
dfTargetToRadians = DEG_TO_RAD;
dfTargetFromRadians = RAD_TO_DEG;
bTargetWrap = FALSE;
dfTargetWrapLong = 0.0;
if( bTargetLatLong )
{
OGR_SRSNode *poUNITS = poSRSTarget->GetAttrNode( "GEOGCS|UNIT" );
if( poUNITS && poUNITS->GetChildCount() >= 2 )
{
dfTargetToRadians = atof(poUNITS->GetChild(1)->GetValue());
if( dfTargetToRadians == 0.0 )
dfTargetToRadians = DEG_TO_RAD;
else
dfTargetFromRadians = 1 / dfTargetToRadians;
}
}
/* -------------------------------------------------------------------- */
/* Preliminary logic to setup wrapping. */
/* -------------------------------------------------------------------- */
const char *pszCENTER_LONG;
if( CPLGetConfigOption( "CENTER_LONG", NULL ) != NULL )
{
bSourceWrap = bTargetWrap = TRUE;
dfSourceWrapLong = dfTargetWrapLong =
atof(CPLGetConfigOption( "CENTER_LONG", "" ));
CPLDebug( "OGRCT", "Wrap at %g.", dfSourceWrapLong );
}
pszCENTER_LONG = poSRSSource->GetExtension( "GEOGCS", "CENTER_LONG" );
if( pszCENTER_LONG != NULL )
{
dfSourceWrapLong = atof(pszCENTER_LONG);
bSourceWrap = TRUE;
CPLDebug( "OGRCT", "Wrap source at %g.", dfSourceWrapLong );
}
pszCENTER_LONG = poSRSTarget->GetExtension( "GEOGCS", "CENTER_LONG" );
if( pszCENTER_LONG != NULL )
{
dfTargetWrapLong = atof(pszCENTER_LONG);
bTargetWrap = TRUE;
CPLDebug( "OGRCT", "Wrap target at %g.", dfTargetWrapLong );
}
bCheckWithInvertProj = CSLTestBoolean(CPLGetConfigOption( "CHECK_WITH_INVERT_PROJ", "NO" ));
/* The threshold is rather experimental... Works well with the cases of ticket #2305 */
if (bSourceLatLong)
dfThreshold = atof(CPLGetConfigOption( "THRESHOLD", ".1" ));
else
/* 1 works well for most projections, except for +proj=aeqd that requires */
/* a tolerance of 10000 */
dfThreshold = atof(CPLGetConfigOption( "THRESHOLD", "10000" ));
/* -------------------------------------------------------------------- */
/* Establish PROJ.4 handle for source if projection. */
/* -------------------------------------------------------------------- */
// OGRThreadSafety: The following variable is not a thread safety issue
// since the only issue is incrementing while accessing which at worse
// means debug output could be one "increment" late.
static int nDebugReportCount = 0;
char *pszProj4Defn = NULL;
if( poSRSSource->exportToProj4( &pszProj4Defn ) != OGRERR_NONE )
{
CPLFree( pszProj4Defn );
return FALSE;
}
if( strlen(pszProj4Defn) == 0 )
{
CPLFree( pszProj4Defn );
CPLError( CE_Failure, CPLE_AppDefined,
"No PROJ.4 translation for source SRS, coordinate\n"
"transformation initialization has failed." );
return FALSE;
}
if (pjctx)
psPJSource = pfn_pj_init_plus_ctx( pjctx, pszProj4Defn );
else
psPJSource = pfn_pj_init_plus( pszProj4Defn );
if( psPJSource == NULL )
{
if( pjctx != NULL)
{
int pj_errno = pfn_pj_ctx_get_errno(pjctx);
/* pfn_pj_strerrno not yet thread-safe in PROJ 4.8.0 */
CPLMutexHolderD(&hPROJMutex);
CPLError( CE_Failure, CPLE_NotSupported,
"Failed to initialize PROJ.4 with `%s'.\n%s",
pszProj4Defn, pfn_pj_strerrno(pj_errno) );
}
else if( pfn_pj_get_errno_ref != NULL
&& pfn_pj_strerrno != NULL )
{
int *p_pj_errno = pfn_pj_get_errno_ref();
CPLError( CE_Failure, CPLE_NotSupported,
"Failed to initialize PROJ.4 with `%s'.\n%s",
pszProj4Defn, pfn_pj_strerrno(*p_pj_errno) );
}
else
{
CPLError( CE_Failure, CPLE_NotSupported,
"Failed to initialize PROJ.4 with `%s'.\n",
pszProj4Defn );
}
}
if( nDebugReportCount < 10 )
CPLDebug( "OGRCT", "Source: %s", pszProj4Defn );
CPLFree( pszProj4Defn );
if( psPJSource == NULL )
return FALSE;
/* -------------------------------------------------------------------- */
/* Establish PROJ.4 handle for target if projection. */
/* -------------------------------------------------------------------- */
pszProj4Defn = NULL;
if( poSRSTarget->exportToProj4( &pszProj4Defn ) != OGRERR_NONE )
{
CPLFree( pszProj4Defn );
return FALSE;
}
if( strlen(pszProj4Defn) == 0 )
{
CPLFree( pszProj4Defn );
CPLError( CE_Failure, CPLE_AppDefined,
"No PROJ.4 translation for destination SRS, coordinate\n"
"transformation initialization has failed." );
return FALSE;
}
if (pjctx)
psPJTarget = pfn_pj_init_plus_ctx( pjctx, pszProj4Defn );
else
psPJTarget = pfn_pj_init_plus( pszProj4Defn );
if( psPJTarget == NULL )
CPLError( CE_Failure, CPLE_NotSupported,
"Failed to initialize PROJ.4 with `%s'.",
pszProj4Defn );
if( nDebugReportCount < 10 )
{
CPLDebug( "OGRCT", "Target: %s", pszProj4Defn );
nDebugReportCount++;
}
CPLFree( pszProj4Defn );
if( psPJTarget == NULL )
return FALSE;
return TRUE;
}
/************************************************************************/
/* GetSourceCS() */
/************************************************************************/
OGRSpatialReference *OGRProj4CT::GetSourceCS()
{
return poSRSSource;
}
/************************************************************************/
/* GetTargetCS() */
/************************************************************************/
OGRSpatialReference *OGRProj4CT::GetTargetCS()
{
return poSRSTarget;
}
/************************************************************************/
/* Transform() */
/* */
/* This is a small wrapper for the extended transform version. */
/************************************************************************/
int OGRProj4CT::Transform( int nCount, double *x, double *y, double *z )
{
int *pabSuccess = (int *) CPLMalloc(sizeof(int) * nCount );
int bOverallSuccess, i;
bOverallSuccess = TransformEx( nCount, x, y, z, pabSuccess );
for( i = 0; i < nCount; i++ )
{
if( !pabSuccess[i] )
{
bOverallSuccess = FALSE;
break;
}
}
CPLFree( pabSuccess );
return bOverallSuccess;
}
/************************************************************************/
/* OCTTransform() */
/************************************************************************/
int CPL_STDCALL OCTTransform( OGRCoordinateTransformationH hTransform,
int nCount, double *x, double *y, double *z )
{
VALIDATE_POINTER1( hTransform, "OCTTransform", FALSE );
return ((OGRCoordinateTransformation*) hTransform)->
Transform( nCount, x, y,z );
}
/************************************************************************/
/* TransformEx() */
/************************************************************************/
int OGRProj4CT::TransformEx( int nCount, double *x, double *y, double *z,
int *pabSuccess )
{
int err, i;
/* -------------------------------------------------------------------- */
/* Potentially transform to radians. */
/* -------------------------------------------------------------------- */
if( bSourceLatLong )
{
if( bSourceWrap )
{
for( i = 0; i < nCount; i++ )
{
if( x[i] != HUGE_VAL && y[i] != HUGE_VAL )
{
if( x[i] < dfSourceWrapLong - 180.0 )
x[i] += 360.0;
else if( x[i] > dfSourceWrapLong + 180 )
x[i] -= 360.0;
}
}
}
for( i = 0; i < nCount; i++ )
{
if( x[i] != HUGE_VAL )
{
x[i] *= dfSourceToRadians;
y[i] *= dfSourceToRadians;
}
}
}
/* -------------------------------------------------------------------- */
/* Do the transformation using PROJ.4. */
/* -------------------------------------------------------------------- */
if (pjctx == NULL)
{
/* The mutex has already been created */
CPLAssert(hPROJMutex != NULL);
CPLAcquireMutex(hPROJMutex, 1000.0);
}
if (bCheckWithInvertProj)
{
/* For some projections, we cannot detect if we are trying to reproject */
/* coordinates outside the validity area of the projection. So let's do */
/* the reverse reprojection and compare with the source coordinates */
if (nCount > nMaxCount)
{
nMaxCount = nCount;
padfOriX = (double*) CPLRealloc(padfOriX, sizeof(double)*nCount);
padfOriY = (double*) CPLRealloc(padfOriY, sizeof(double)*nCount);
padfOriZ = (double*) CPLRealloc(padfOriZ, sizeof(double)*nCount);
padfTargetX = (double*) CPLRealloc(padfTargetX, sizeof(double)*nCount);
padfTargetY = (double*) CPLRealloc(padfTargetY, sizeof(double)*nCount);
padfTargetZ = (double*) CPLRealloc(padfTargetZ, sizeof(double)*nCount);
}
memcpy(padfOriX, x, sizeof(double)*nCount);
memcpy(padfOriY, y, sizeof(double)*nCount);
if (z)
{
memcpy(padfOriZ, z, sizeof(double)*nCount);
}
err = pfn_pj_transform( psPJSource, psPJTarget, nCount, 1, x, y, z );
if (err == 0)
{
memcpy(padfTargetX, x, sizeof(double)*nCount);
memcpy(padfTargetY, y, sizeof(double)*nCount);
if (z)
{
memcpy(padfTargetZ, z, sizeof(double)*nCount);
}
err = pfn_pj_transform( psPJTarget, psPJSource , nCount, 1,
padfTargetX, padfTargetY, (z) ? padfTargetZ : NULL);
if (err == 0)
{
for( i = 0; i < nCount; i++ )
{
if ( x[i] != HUGE_VAL && y[i] != HUGE_VAL &&
(fabs(padfTargetX[i] - padfOriX[i]) > dfThreshold ||
fabs(padfTargetY[i] - padfOriY[i]) > dfThreshold) )
{
x[i] = HUGE_VAL;
y[i] = HUGE_VAL;
}
}
}
}
}
else
{
err = pfn_pj_transform( psPJSource, psPJTarget, nCount, 1, x, y, z );
}
/* -------------------------------------------------------------------- */
/* Try to report an error through CPL. Get proj.4 error string */
/* if possible. Try to avoid reporting thousands of error */
/* ... supress further error reporting on this OGRProj4CT if we */
/* have already reported 20 errors. */
/* -------------------------------------------------------------------- */
if( err != 0 )
{
if( pabSuccess )
memset( pabSuccess, 0, sizeof(int) * nCount );
if( ++nErrorCount < 20 )
{
if (pjctx != NULL)
/* pfn_pj_strerrno not yet thread-safe in PROJ 4.8.0 */
CPLAcquireMutex(hPROJMutex, 1000.0);
const char *pszError = NULL;
if( pfn_pj_strerrno != NULL )
pszError = pfn_pj_strerrno( err );
if( pszError == NULL )
CPLError( CE_Failure, CPLE_AppDefined,
"Reprojection failed, err = %d",
err );
else
CPLError( CE_Failure, CPLE_AppDefined, "%s", pszError );
if (pjctx != NULL)
/* pfn_pj_strerrno not yet thread-safe in PROJ 4.8.0 */
CPLReleaseMutex(hPROJMutex);
}
else if( nErrorCount == 20 )
{
CPLError( CE_Failure, CPLE_AppDefined,
"Reprojection failed, err = %d, further errors will be supressed on the transform object.",
err );
}
if (pjctx == NULL)
CPLReleaseMutex(hPROJMutex);
return FALSE;
}
if (pjctx == NULL)
CPLReleaseMutex(hPROJMutex);
/* -------------------------------------------------------------------- */
/* Potentially transform back to degrees. */
/* -------------------------------------------------------------------- */
if( bTargetLatLong )
{
for( i = 0; i < nCount; i++ )
{
if( x[i] != HUGE_VAL && y[i] != HUGE_VAL )
{
x[i] *= dfTargetFromRadians;
y[i] *= dfTargetFromRadians;
}
}
if( bTargetWrap )
{
for( i = 0; i < nCount; i++ )
{
if( x[i] != HUGE_VAL && y[i] != HUGE_VAL )
{
if( x[i] < dfTargetWrapLong - 180.0 )
x[i] += 360.0;
else if( x[i] > dfTargetWrapLong + 180 )
x[i] -= 360.0;
}
}
}
}
/* -------------------------------------------------------------------- */
/* Establish error information if pabSuccess provided. */
/* -------------------------------------------------------------------- */
if( pabSuccess )
{
for( i = 0; i < nCount; i++ )
{
if( x[i] == HUGE_VAL || y[i] == HUGE_VAL )
pabSuccess[i] = FALSE;
else
pabSuccess[i] = TRUE;
}
}
return TRUE;
}
/************************************************************************/
/* OCTTransformEx() */
/************************************************************************/
int CPL_STDCALL OCTTransformEx( OGRCoordinateTransformationH hTransform,
int nCount, double *x, double *y, double *z,
int *pabSuccess )
{
VALIDATE_POINTER1( hTransform, "OCTTransformEx", FALSE );
return ((OGRCoordinateTransformation*) hTransform)->
TransformEx( nCount, x, y, z, pabSuccess );
}
|