LCOV - code coverage report
Current view: directory - alg - gdaltransformer.cpp (source / functions) Found Hit Coverage
Test: gdal_filtered.info Lines: 866 707 81.6 %
Date: 2011-12-18 Functions: 33 32 97.0 %

       1                 : /******************************************************************************
       2                 :  * $Id: gdaltransformer.cpp 23156 2011-10-01 15:34:16Z rouault $
       3                 :  *
       4                 :  * Project:  Mapinfo Image Warper
       5                 :  * Purpose:  Implementation of one or more GDALTrasformerFunc types, including
       6                 :  *           the GenImgProj (general image reprojector) transformer.
       7                 :  * Author:   Frank Warmerdam, warmerdam@pobox.com
       8                 :  *
       9                 :  ******************************************************************************
      10                 :  * Copyright (c) 2002, i3 - information integration and imaging 
      11                 :  *                          Fort Collin, CO
      12                 :  *
      13                 :  * Permission is hereby granted, free of charge, to any person obtaining a
      14                 :  * copy of this software and associated documentation files (the "Software"),
      15                 :  * to deal in the Software without restriction, including without limitation
      16                 :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      17                 :  * and/or sell copies of the Software, and to permit persons to whom the
      18                 :  * Software is furnished to do so, subject to the following conditions:
      19                 :  *
      20                 :  * The above copyright notice and this permission notice shall be included
      21                 :  * in all copies or substantial portions of the Software.
      22                 :  *
      23                 :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
      24                 :  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      25                 :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
      26                 :  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      27                 :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
      28                 :  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
      29                 :  * DEALINGS IN THE SOFTWARE.
      30                 :  ****************************************************************************/
      31                 : 
      32                 : #include "gdal_priv.h"
      33                 : #include "gdal_alg.h"
      34                 : #include "ogr_spatialref.h"
      35                 : #include "cpl_string.h"
      36                 : #include "gdal_alg_priv.h"
      37                 : #include "cpl_list.h"
      38                 : #include "cpl_multiproc.h"
      39                 : 
      40                 : CPL_CVSID("$Id: gdaltransformer.cpp 23156 2011-10-01 15:34:16Z rouault $");
      41                 : CPL_C_START
      42                 : void *GDALDeserializeGCPTransformer( CPLXMLNode *psTree );
      43                 : void *GDALDeserializeTPSTransformer( CPLXMLNode *psTree );
      44                 : void *GDALDeserializeGeoLocTransformer( CPLXMLNode *psTree );
      45                 : void *GDALDeserializeRPCTransformer( CPLXMLNode *psTree );
      46                 : CPL_C_END
      47                 : 
      48                 : static CPLXMLNode *GDALSerializeReprojectionTransformer( void *pTransformArg );
      49                 : static void *GDALDeserializeReprojectionTransformer( CPLXMLNode *psTree );
      50                 : 
      51                 : static CPLXMLNode *GDALSerializeGenImgProjTransformer( void *pTransformArg );
      52                 : static void *GDALDeserializeGenImgProjTransformer( CPLXMLNode *psTree );
      53                 : 
      54                 : static void GDALRefreshGenImgProjTransformer(void* hTransformArg);
      55                 : 
      56                 : /************************************************************************/
      57                 : /*                          GDALTransformFunc                           */
      58                 : /*                                                                      */
      59                 : /*      Documentation for GDALTransformFunc typedef.                    */
      60                 : /************************************************************************/
      61                 : 
      62                 : /*!
      63                 : 
      64                 : \typedef int GDALTransformerFunc
      65                 : 
      66                 : Generic signature for spatial point transformers.
      67                 : 
      68                 : This function signature is used for a variety of functions that accept 
      69                 : passed in functions used to transform point locations between two coordinate
      70                 : spaces.  
      71                 : 
      72                 : The GDALCreateGenImgProjTransformer(), GDALCreateReprojectionTransformer(), 
      73                 : GDALCreateGCPTransformer() and GDALCreateApproxTransformer() functions can
      74                 : be used to prepare argument data for some built-in transformers.  As well,
      75                 : applications can implement their own transformers to the following signature.
      76                 : 
      77                 : \code
      78                 : typedef int 
      79                 : (*GDALTransformerFunc)( void *pTransformerArg, 
      80                 :                         int bDstToSrc, int nPointCount, 
      81                 :                         double *x, double *y, double *z, int *panSuccess );
      82                 : \endcode
      83                 : 
      84                 : @param pTransformerArg application supplied callback data used by the
      85                 : transformer.  
      86                 : 
      87                 : @param bDstToSrc if TRUE the transformation will be from the destination
      88                 : coordinate space to the source coordinate system, otherwise the transformation
      89                 : will be from the source coordinate system to the destination coordinate system.
      90                 : 
      91                 : @param nPointCount number of points in the x, y and z arrays.
      92                 : 
      93                 : @param x input X coordinates.  Results returned in same array.
      94                 : 
      95                 : @param y input Y coordinates.  Results returned in same array.
      96                 : 
      97                 : @param z input Z coordinates.  Results returned in same array.
      98                 : 
      99                 : @param panSuccess array of ints in which success (TRUE) or failure (FALSE)
     100                 : flags are returned for the translation of each point.
     101                 : 
     102                 : @return TRUE if the overall transformation succeeds (though some individual
     103                 : points may have failed) or FALSE if the overall transformation fails.
     104                 : 
     105                 : */
     106                 : 
     107                 : /************************************************************************/
     108                 : /*                      GDALSuggestedWarpOutput()                       */
     109                 : /************************************************************************/
     110                 : 
     111                 : /**
     112                 :  * Suggest output file size.
     113                 :  *
     114                 :  * This function is used to suggest the size, and georeferenced extents
     115                 :  * appropriate given the indicated transformation and input file.  It walks
     116                 :  * the edges of the input file (approximately 20 sample points along each
     117                 :  * edge) transforming into output coordinates in order to get an extents box.
     118                 :  *
     119                 :  * Then a resolution is computed with the intent that the length of the
     120                 :  * distance from the top left corner of the output imagery to the bottom right
     121                 :  * corner would represent the same number of pixels as in the source image. 
     122                 :  * Note that if the image is somewhat rotated the diagonal taken isnt of the
     123                 :  * whole output bounding rectangle, but instead of the locations where the
     124                 :  * top/left and bottom/right corners transform.  The output pixel size is 
     125                 :  * always square.  This is intended to approximately preserve the resolution
     126                 :  * of the input data in the output file. 
     127                 :  * 
     128                 :  * The values returned in padfGeoTransformOut, pnPixels and pnLines are
     129                 :  * the suggested number of pixels and lines for the output file, and the
     130                 :  * geotransform relating those pixels to the output georeferenced coordinates.
     131                 :  *
     132                 :  * The trickiest part of using the function is ensuring that the 
     133                 :  * transformer created is from source file pixel/line coordinates to 
     134                 :  * output file georeferenced coordinates.  This can be accomplished with 
     135                 :  * GDALCreateGenImgProjTransformer() by passing a NULL for the hDstDS.  
     136                 :  *
     137                 :  * @param hSrcDS the input image (it is assumed the whole input images is
     138                 :  * being transformed). 
     139                 :  * @param pfnTransformer the transformer function.
     140                 :  * @param pTransformArg the callback data for the transformer function.
     141                 :  * @param padfGeoTransformOut the array of six doubles in which the suggested
     142                 :  * geotransform is returned. 
     143                 :  * @param pnPixels int in which the suggest pixel width of output is returned.
     144                 :  * @param pnLines int in which the suggest pixel height of output is returned.
     145                 :  *
     146                 :  * @return CE_None if successful or CE_Failure otherwise. 
     147                 :  */
     148                 : 
     149                 : 
     150                 : CPLErr CPL_STDCALL
     151              13 : GDALSuggestedWarpOutput( GDALDatasetH hSrcDS, 
     152                 :                          GDALTransformerFunc pfnTransformer, 
     153                 :                          void *pTransformArg, 
     154                 :                          double *padfGeoTransformOut, 
     155                 :                          int *pnPixels, int *pnLines )
     156                 : 
     157                 : {
     158              13 :     VALIDATE_POINTER1( hSrcDS, "GDALSuggestedWarpOutput", CE_Failure );
     159                 : 
     160              13 :     double adfExtent[4] = { 0 };
     161                 : 
     162                 :     return GDALSuggestedWarpOutput2( hSrcDS, pfnTransformer, pTransformArg, 
     163                 :                                      padfGeoTransformOut, pnPixels, pnLines, 
     164              13 :                                      adfExtent, 0 );
     165                 : }
     166                 : 
     167                 : 
     168             316 :  static int GDALSuggestedWarpOutput2_MustAdjustForRightBorder(
     169                 :                      GDALTransformerFunc pfnTransformer, void *pTransformArg,
     170                 :                      double* padfExtent, int nPixels, int nLines,
     171                 :                      double dfPixelSizeX, double dfPixelSizeY)
     172                 :  {
     173                 :     int nSamplePoints;
     174                 :     double dfRatio;
     175                 :     int bErr;
     176                 :     int nBadCount;
     177             316 :     int    abSuccess[21] = { 0 };
     178             316 :     double adfX[21] = { 0 };
     179             316 :     double adfY[21] = { 0 };
     180             316 :     double adfZ[21] = { 0 };
     181                 :     
     182                 :     //double dfMinXOut = padfExtent[0];
     183                 :     //double dfMinYOut = padfExtent[1];
     184             316 :     double dfMaxXOut = padfExtent[2];
     185             316 :     double dfMaxYOut = padfExtent[3];
     186                 :     
     187                 :     // Take 20 steps 
     188             316 :     nSamplePoints = 0;
     189            6952 :     for( dfRatio = 0.0; dfRatio <= 1.01; dfRatio += 0.05 )
     190                 :     {
     191                 :         // Ensure we end exactly at the end.
     192            6636 :         if( dfRatio > 0.99 )
     193             316 :             dfRatio = 1.0;
     194                 : 
     195                 :         // Along right
     196            6636 :         adfX[nSamplePoints]   = dfMaxXOut;
     197            6636 :         adfY[nSamplePoints]   = dfMaxYOut - dfPixelSizeY * dfRatio * nLines;
     198            6636 :         adfZ[nSamplePoints++] = 0.0;
     199                 :     }
     200                 :     
     201             316 :     bErr = FALSE;
     202             316 :     if( !pfnTransformer( pTransformArg, TRUE, nSamplePoints, 
     203                 :                              adfX, adfY, adfZ, abSuccess ) )
     204                 :     {
     205               0 :         bErr = TRUE;
     206                 :     }
     207                 :     
     208             316 :     if( !bErr && !pfnTransformer( pTransformArg, FALSE, nSamplePoints, 
     209                 :                              adfX, adfY, adfZ, abSuccess ) )
     210                 :     {
     211               0 :         bErr = TRUE;
     212                 :     }
     213                 :     
     214             316 :     nSamplePoints = 0;
     215             316 :     nBadCount = 0;
     216            6952 :     for( dfRatio = 0.0; !bErr && dfRatio <= 1.01; dfRatio += 0.05 )
     217                 :     {
     218            6636 :         double expected_x = dfMaxXOut;
     219            6636 :         double expected_y = dfMaxYOut - dfPixelSizeY * dfRatio * nLines;
     220           12440 :         if (fabs(adfX[nSamplePoints] -  expected_x) > dfPixelSizeX ||
     221            5804 :             fabs(adfY[nSamplePoints] -  expected_y) > dfPixelSizeY)
     222             832 :             nBadCount ++;
     223            6636 :         nSamplePoints ++;
     224                 :     }
     225                 :     
     226             316 :     return (nBadCount == nSamplePoints);
     227                 : }
     228                 : 
     229                 : 
     230             310 :  static int GDALSuggestedWarpOutput2_MustAdjustForBottomBorder(
     231                 :                      GDALTransformerFunc pfnTransformer, void *pTransformArg,
     232                 :                      double* padfExtent, int nPixels, int nLines,
     233                 :                      double dfPixelSizeX, double dfPixelSizeY)
     234                 :  {
     235                 :     int nSamplePoints;
     236                 :     double dfRatio;
     237                 :     int bErr;
     238                 :     int nBadCount;
     239             310 :     int    abSuccess[21] = { 0 };
     240             310 :     double adfX[21] = { 0 };
     241             310 :     double adfY[21] = { 0 };
     242             310 :     double adfZ[21] = { 0 };
     243                 :     
     244             310 :     double dfMinXOut = padfExtent[0];
     245             310 :     double dfMinYOut = padfExtent[1];
     246                 :     //double dfMaxXOut = padfExtent[2];
     247                 :     //double dfMaxYOut = padfExtent[3];
     248                 :     
     249                 :     // Take 20 steps 
     250             310 :     nSamplePoints = 0;
     251            6820 :     for( dfRatio = 0.0; dfRatio <= 1.01; dfRatio += 0.05 )
     252                 :     {
     253                 :         // Ensure we end exactly at the end.
     254            6510 :         if( dfRatio > 0.99 )
     255             310 :             dfRatio = 1.0;
     256                 : 
     257                 :         // Along right
     258            6510 :         adfX[nSamplePoints]   = dfMinXOut + dfPixelSizeX * dfRatio * nPixels;
     259            6510 :         adfY[nSamplePoints]   = dfMinYOut;
     260            6510 :         adfZ[nSamplePoints++] = 0.0;
     261                 :     }
     262                 :     
     263             310 :     bErr = FALSE;
     264             310 :     if( !pfnTransformer( pTransformArg, TRUE, nSamplePoints, 
     265                 :                              adfX, adfY, adfZ, abSuccess ) )
     266                 :     {
     267               0 :         bErr = TRUE;
     268                 :     }
     269                 :     
     270             310 :     if( !bErr && !pfnTransformer( pTransformArg, FALSE, nSamplePoints, 
     271                 :                              adfX, adfY, adfZ, abSuccess ) )
     272                 :     {
     273               0 :         bErr = TRUE;
     274                 :     }
     275                 :     
     276             310 :     nSamplePoints = 0;
     277             310 :     nBadCount = 0;
     278            6820 :     for( dfRatio = 0.0; !bErr && dfRatio <= 1.01; dfRatio += 0.05 )
     279                 :     {
     280            6510 :         double expected_x = dfMinXOut + dfPixelSizeX * dfRatio * nPixels;
     281            6510 :         double expected_y = dfMinYOut;
     282           12338 :         if (fabs(adfX[nSamplePoints] -  expected_x) > dfPixelSizeX ||
     283            5828 :             fabs(adfY[nSamplePoints] -  expected_y) > dfPixelSizeY)
     284             682 :             nBadCount ++;
     285            6510 :         nSamplePoints ++;
     286                 :     }
     287                 :     
     288             310 :     return (nBadCount == nSamplePoints);
     289                 : }
     290                 : 
     291                 : /************************************************************************/
     292                 : /*                      GDALSuggestedWarpOutput2()                      */
     293                 : /************************************************************************/
     294                 : 
     295                 : /**
     296                 :  * Suggest output file size.
     297                 :  *
     298                 :  * This function is used to suggest the size, and georeferenced extents
     299                 :  * appropriate given the indicated transformation and input file.  It walks
     300                 :  * the edges of the input file (approximately 20 sample points along each
     301                 :  * edge) transforming into output coordinates in order to get an extents box.
     302                 :  *
     303                 :  * Then a resolution is computed with the intent that the length of the
     304                 :  * distance from the top left corner of the output imagery to the bottom right
     305                 :  * corner would represent the same number of pixels as in the source image. 
     306                 :  * Note that if the image is somewhat rotated the diagonal taken isnt of the
     307                 :  * whole output bounding rectangle, but instead of the locations where the
     308                 :  * top/left and bottom/right corners transform.  The output pixel size is 
     309                 :  * always square.  This is intended to approximately preserve the resolution
     310                 :  * of the input data in the output file. 
     311                 :  * 
     312                 :  * The values returned in padfGeoTransformOut, pnPixels and pnLines are
     313                 :  * the suggested number of pixels and lines for the output file, and the
     314                 :  * geotransform relating those pixels to the output georeferenced coordinates.
     315                 :  *
     316                 :  * The trickiest part of using the function is ensuring that the 
     317                 :  * transformer created is from source file pixel/line coordinates to 
     318                 :  * output file georeferenced coordinates.  This can be accomplished with 
     319                 :  * GDALCreateGenImgProjTransformer() by passing a NULL for the hDstDS.  
     320                 :  *
     321                 :  * @param hSrcDS the input image (it is assumed the whole input images is
     322                 :  * being transformed). 
     323                 :  * @param pfnTransformer the transformer function.
     324                 :  * @param pTransformArg the callback data for the transformer function.
     325                 :  * @param padfGeoTransformOut the array of six doubles in which the suggested
     326                 :  * geotransform is returned. 
     327                 :  * @param pnPixels int in which the suggest pixel width of output is returned.
     328                 :  * @param pnLines int in which the suggest pixel height of output is returned.
     329                 :  * @param padfExtent Four entry array to return extents as (xmin, ymin, xmax, ymax). 
     330                 :  * @param nOptions Options, currently always zero.
     331                 :  *
     332                 :  * @return CE_None if successful or CE_Failure otherwise. 
     333                 :  */
     334                 : 
     335                 : CPLErr CPL_STDCALL
     336             286 : GDALSuggestedWarpOutput2( GDALDatasetH hSrcDS, 
     337                 :                           GDALTransformerFunc pfnTransformer, 
     338                 :                           void *pTransformArg, 
     339                 :                           double *padfGeoTransformOut, 
     340                 :                           int *pnPixels, int *pnLines,
     341                 :                           double *padfExtent, int nOptions )
     342                 : 
     343                 : {
     344             286 :     VALIDATE_POINTER1( hSrcDS, "GDALSuggestedWarpOutput2", CE_Failure );
     345                 : 
     346                 : /* -------------------------------------------------------------------- */
     347                 : /*      Setup sample points all around the edge of the input raster.    */
     348                 : /* -------------------------------------------------------------------- */
     349             286 :     int    nSamplePoints = 0;
     350             286 :     int    nInXSize = GDALGetRasterXSize( hSrcDS );
     351             286 :     int    nInYSize = GDALGetRasterYSize( hSrcDS );
     352                 : 
     353             286 :     if (pfnTransformer == GDALGenImgProjTransform)
     354                 :     {
     355                 :         /* In case CHECK_WITH_INVERT_PROJ has been modified */
     356             286 :         GDALRefreshGenImgProjTransformer(pTransformArg);
     357                 :     }
     358                 : 
     359                 : #define N_PIXELSTEP 50
     360             286 :     int nSteps = (int) (double(MIN(nInYSize, nInXSize)) / N_PIXELSTEP + .5);
     361             286 :     if (nSteps < 20)
     362             280 :         nSteps = 20;
     363                 : 
     364                 : retry:
     365             286 :     int nSampleMax = (nSteps + 1)*(nSteps + 1);
     366             286 :     int *pabSuccess = NULL;
     367                 :     double *padfX, *padfY, *padfZ;
     368                 :     double *padfXRevert, *padfYRevert, *padfZRevert;
     369                 : 
     370             286 :     double dfRatio = 0.0;
     371             286 :     double dfStep = 1. / nSteps;
     372                 : 
     373             286 :     pabSuccess = (int *) VSIMalloc3(sizeof(int), nSteps + 1, nSteps + 1);
     374             286 :     padfX = (double *) VSIMalloc3(sizeof(double) * 3, nSteps + 1, nSteps + 1);
     375             286 :     padfXRevert = (double *) VSIMalloc3(sizeof(double) * 3, nSteps + 1, nSteps + 1);
     376             286 :     if (pabSuccess == NULL || padfX == NULL || padfXRevert == NULL)
     377                 :     {
     378               0 :         CPLFree( padfX );
     379               0 :         CPLFree( padfXRevert );
     380               0 :         CPLFree( pabSuccess );
     381               0 :         if (nSteps > 20)
     382                 :         {
     383               0 :             nSteps = 20;
     384               0 :             goto retry;
     385                 :         }
     386               0 :         return CE_Failure;
     387                 :     }
     388             286 :     padfY = padfX + nSampleMax;
     389             286 :     padfZ = padfX + nSampleMax * 2;
     390             286 :     padfYRevert = padfXRevert + nSampleMax;
     391             286 :     padfZRevert = padfXRevert + nSampleMax * 2;
     392                 : 
     393                 : 
     394                 :     // Take N_STEPS steps
     395                 :     int iStep;
     396            6292 :     for( iStep = 0; iStep <= nSteps; iStep ++ )
     397                 :     {
     398            6006 :         dfRatio = (iStep == nSteps) ? 1.0 : iStep * dfStep;
     399                 : 
     400                 :         // Along top 
     401            6006 :         padfX[nSamplePoints]   = dfRatio * nInXSize;
     402            6006 :         padfY[nSamplePoints]   = 0.0;
     403            6006 :         padfZ[nSamplePoints++] = 0.0;
     404                 : 
     405                 :         // Along bottom 
     406            6006 :         padfX[nSamplePoints]   = dfRatio * nInXSize;
     407            6006 :         padfY[nSamplePoints]   = nInYSize;
     408            6006 :         padfZ[nSamplePoints++] = 0.0;
     409                 : 
     410                 :         // Along left
     411            6006 :         padfX[nSamplePoints]   = 0.0;
     412            6006 :         padfY[nSamplePoints] = dfRatio * nInYSize;
     413            6006 :         padfZ[nSamplePoints++] = 0.0;
     414                 : 
     415                 :         // Along right
     416            6006 :         padfX[nSamplePoints]   = nInXSize;
     417            6006 :         padfY[nSamplePoints] = dfRatio * nInYSize;
     418            6006 :         padfZ[nSamplePoints++] = 0.0;
     419                 :     }
     420                 : 
     421             286 :     CPLAssert( nSamplePoints == 4 * (nSteps + 1) );
     422                 : 
     423             286 :     memset( pabSuccess, 1, sizeof(int) * nSampleMax );
     424                 : 
     425                 : /* -------------------------------------------------------------------- */
     426                 : /*      Transform them to the output coordinate system.                 */
     427                 : /* -------------------------------------------------------------------- */
     428             286 :     int    nFailedCount = 0, i;
     429                 : 
     430             286 :     if( !pfnTransformer( pTransformArg, FALSE, nSamplePoints, 
     431                 :                          padfX, padfY, padfZ, pabSuccess ) )
     432                 :     {
     433                 :         CPLError( CE_Failure, CPLE_AppDefined, 
     434                 :                   "GDALSuggestedWarpOutput() failed because the passed\n"
     435               0 :                   "transformer failed." );
     436               0 :         CPLFree( padfX );
     437               0 :         CPLFree( padfXRevert );
     438               0 :         CPLFree( pabSuccess );
     439               0 :         return CE_Failure;
     440                 :     }
     441                 : 
     442           24310 :     for( i = 0; i < nSamplePoints; i++ )
     443                 :     {
     444           24024 :         if( !pabSuccess[i] )
     445             299 :             nFailedCount++;
     446                 :     }
     447                 :     
     448                 : /* -------------------------------------------------------------------- */
     449                 : /*      Check if the computed target coordinates are revertable.        */
     450                 : /*      If not, try the detailed grid sampling.                         */
     451                 : /* -------------------------------------------------------------------- */
     452             286 :     if (nFailedCount == 0 )
     453                 :     {
     454             280 :         memcpy(padfXRevert, padfX, nSamplePoints * sizeof(double));
     455             280 :         memcpy(padfYRevert, padfY, nSamplePoints * sizeof(double));
     456             280 :         memcpy(padfZRevert, padfZ, nSamplePoints * sizeof(double));
     457             280 :         if( !pfnTransformer( pTransformArg, TRUE, nSamplePoints, 
     458                 :                              padfXRevert, padfYRevert, padfZRevert, pabSuccess ) )
     459                 :         {
     460               0 :             nFailedCount = 1;
     461                 :         }
     462                 :         else
     463                 :         {
     464            5863 :             for( i = 0; nFailedCount == 0 && i < nSamplePoints; i++ )
     465                 :             {
     466            5583 :                 if( !pabSuccess[i] )
     467               0 :                     nFailedCount++;
     468                 : 
     469            5583 :                 dfRatio = 0.0 + (i/4) * dfStep;
     470            5583 :                 if (dfRatio>0.99)
     471             212 :                     dfRatio = 1.0;
     472                 : 
     473                 :                 double dfExpectedX, dfExpectedY;
     474            5583 :                 if ((i % 4) == 0)
     475                 :                 {
     476            1566 :                     dfExpectedX   = dfRatio * nInXSize;
     477            1566 :                     dfExpectedY   = 0.0;
     478                 :                 }
     479            4017 :                 else if ((i % 4) == 1)
     480                 :                 {
     481            1339 :                     dfExpectedX   = dfRatio * nInXSize;
     482            1339 :                     dfExpectedY   = nInYSize;
     483                 :                 }
     484            2678 :                 else if ((i % 4) == 2)
     485                 :                 {
     486            1339 :                     dfExpectedX   = 0.0;
     487            1339 :                     dfExpectedY   = dfRatio * nInYSize;
     488                 :                 }
     489                 :                 else
     490                 :                 {
     491            1339 :                     dfExpectedX   = nInXSize;
     492            1339 :                     dfExpectedY   = dfRatio * nInYSize;
     493                 :                 }
     494                 :                 
     495           10939 :                 if (fabs(padfXRevert[i] - dfExpectedX) > nInXSize / nSteps ||
     496            5356 :                     fabs(padfYRevert[i] - dfExpectedY) > nInYSize / nSteps)
     497             227 :                     nFailedCount ++;
     498                 :             }
     499                 :         }
     500                 :     }
     501                 : 
     502                 : /* -------------------------------------------------------------------- */
     503                 : /*      If any of the edge points failed to transform, we need to       */
     504                 : /*      build a fairly detailed internal grid of points instead to      */
     505                 : /*      help identify the area that is transformable.                   */
     506                 : /* -------------------------------------------------------------------- */
     507             286 :     if( nFailedCount > 0 )
     508                 :     {
     509                 :         int iStep2;
     510                 :         double dfRatio2;
     511             233 :         nSamplePoints = 0;
     512                 : 
     513                 :         // Take N_STEPS steps 
     514            5126 :         for( iStep = 0; iStep <= nSteps; iStep ++ )
     515                 :         {
     516            4893 :             dfRatio = (iStep == nSteps) ? 1.0 : iStep * dfStep;
     517                 : 
     518          107646 :             for( iStep2 = 0; iStep2 <= nSteps; iStep2 ++ )
     519                 :             {
     520          102753 :                 dfRatio2 = (iStep2 == nSteps) ? 1.0 : iStep2 * dfStep;
     521                 : 
     522                 :                 // Along top 
     523          102753 :                 padfX[nSamplePoints]   = dfRatio2 * nInXSize;
     524          102753 :                 padfY[nSamplePoints]   = dfRatio * nInYSize;
     525          102753 :                 padfZ[nSamplePoints++] = 0.0;
     526                 :             }
     527                 :         }
     528                 : 
     529             233 :         CPLAssert( nSamplePoints == nSampleMax );
     530                 : 
     531             233 :         if( !pfnTransformer( pTransformArg, FALSE, nSamplePoints, 
     532                 :                              padfX, padfY, padfZ, pabSuccess ) )
     533                 :         {
     534                 :             CPLError( CE_Failure, CPLE_AppDefined, 
     535                 :                       "GDALSuggestedWarpOutput() failed because the passed\n"
     536               0 :                       "transformer failed." );
     537                 : 
     538               0 :             CPLFree( padfX );
     539               0 :             CPLFree( padfXRevert );
     540               0 :             CPLFree( pabSuccess );
     541                 : 
     542               0 :             return CE_Failure;
     543                 :         }
     544                 :     }
     545                 :         
     546                 : /* -------------------------------------------------------------------- */
     547                 : /*      Collect the bounds, ignoring any failed points.                 */
     548                 : /* -------------------------------------------------------------------- */
     549             286 :     double dfMinXOut=0, dfMinYOut=0, dfMaxXOut=0, dfMaxYOut=0;
     550             286 :     int    bGotInitialPoint = FALSE;
     551                 : 
     552             286 :     nFailedCount = 0;
     553          107491 :     for( i = 0; i < nSamplePoints; i++ )
     554                 :     {
     555                 :         
     556          107205 :         int x_i = i % (nSteps + 1);
     557          107205 :         int y_i = i / (nSteps + 1);
     558                 : 
     559          107205 :         if (x_i > 0 && (pabSuccess[i-1] || pabSuccess[i]))
     560                 :         {
     561          101638 :             double x_out_before = padfX[i-1];
     562          101638 :             double x_out_after = padfX[i];
     563          101638 :             int nIter = 0;
     564          101638 :             double x_in_before = (x_i - 1) * nInXSize * 1.0 / nSteps;
     565          101638 :             double x_in_after = x_i * nInXSize * 1.0 / nSteps;
     566          101638 :             int valid_before = pabSuccess[i-1];
     567          101638 :             int valid_after = pabSuccess[i];
     568                 :             
     569                 :             /* Detect discontinuity in target coordinates when the target x coordinates */
     570                 :             /* change sign. This may be a false positive when the targe tx is around 0 */
     571                 :             /* Dichotomic search to reduce the interval to near the discontinuity and */
     572                 :             /* get a better out extent */
     573          206380 :             while ( (!valid_before || !valid_after ||
     574                 :                      x_out_before * x_out_after < 0) && nIter < 16 )
     575                 :             {
     576            3104 :                 double x = (x_in_before + x_in_after) / 2;
     577            3104 :                 double y = y_i * nInYSize * 1.0 / nSteps;
     578            3104 :                 double z= 0;
     579                 :                 //fprintf(stderr, "[%d] (%f, %f) -> ", nIter, x, y);
     580            3104 :                 int bSuccess = TRUE;
     581            3104 :                 if( !pfnTransformer( pTransformArg, FALSE, 1, 
     582                 :                                      &x, &y, &z, &bSuccess ) || !bSuccess )
     583                 :                 {
     584                 :                     //fprintf(stderr, "invalid\n");
     585             243 :                     if (!valid_before)
     586                 :                     {
     587             171 :                         x_in_before = (x_in_before + x_in_after) / 2;
     588                 :                     }
     589              72 :                     else if (!valid_after)
     590                 :                     {
     591              72 :                         x_in_after = (x_in_before + x_in_after) / 2;
     592                 :                     }
     593                 :                     else
     594               0 :                         break;
     595                 :                 }
     596                 :                 else
     597                 :                 {
     598                 :                     //fprintf(stderr, "(%f, %f)\n", x, y);
     599                 :                     
     600            2861 :                     if( !bGotInitialPoint )
     601                 :                     {
     602               3 :                         bGotInitialPoint = TRUE;
     603               3 :                         dfMinXOut = dfMaxXOut = x;
     604               3 :                         dfMinYOut = dfMaxYOut = y;
     605                 :                     }
     606                 :                     else
     607                 :                     {
     608            2858 :                         dfMinXOut = MIN(dfMinXOut,x);
     609            2858 :                         dfMinYOut = MIN(dfMinYOut,y);
     610            2858 :                         dfMaxXOut = MAX(dfMaxXOut,x);
     611            2858 :                         dfMaxYOut = MAX(dfMaxYOut,y);
     612                 :                     }
     613                 :                     
     614            4205 :                     if (!valid_before || x_out_before * x < 0)
     615                 :                     {
     616            1344 :                         valid_after = TRUE;
     617            1344 :                         x_in_after = (x_in_before + x_in_after) / 2;
     618            1344 :                         x_out_after = x;
     619                 :                     }
     620                 :                     else
     621                 :                     {
     622            1517 :                         valid_before = TRUE;
     623            1517 :                         x_out_before = x;
     624            1517 :                         x_in_before = (x_in_before + x_in_after) / 2;
     625                 :                     }
     626                 :                 }
     627            3104 :                 nIter ++;
     628                 :             }
     629                 :         }
     630                 :         
     631          107205 :         if( !pabSuccess[i] )
     632                 :         {
     633             503 :             nFailedCount++;
     634             503 :             continue;
     635                 :         }
     636                 : 
     637          106702 :         if( !bGotInitialPoint )
     638                 :         {
     639             283 :             bGotInitialPoint = TRUE;
     640             283 :             dfMinXOut = dfMaxXOut = padfX[i];
     641             283 :             dfMinYOut = dfMaxYOut = padfY[i];
     642                 :         }
     643                 :         else
     644                 :         {
     645          106419 :             dfMinXOut = MIN(dfMinXOut, padfX[i]);
     646          106419 :             dfMinYOut = MIN(dfMinYOut, padfY[i]);
     647          106419 :             dfMaxXOut = MAX(dfMaxXOut, padfX[i]);
     648          106419 :             dfMaxYOut = MAX(dfMaxYOut, padfY[i]);
     649                 :         }
     650                 :     }
     651                 : 
     652             286 :     if( nFailedCount > nSamplePoints - 10 )
     653                 :     {
     654                 :         CPLError( CE_Failure, CPLE_AppDefined, 
     655                 :                   "Too many points (%d out of %d) failed to transform,\n"
     656                 :                   "unable to compute output bounds.",
     657               0 :                   nFailedCount, nSamplePoints );
     658                 : 
     659               0 :         CPLFree( padfX );
     660               0 :         CPLFree( padfXRevert );
     661               0 :         CPLFree( pabSuccess );
     662                 : 
     663               0 :         return CE_Failure;
     664                 :     }
     665                 : 
     666             286 :     if( nFailedCount > 0 )
     667                 :         CPLDebug( "GDAL", 
     668                 :                   "GDALSuggestedWarpOutput(): %d out of %d points failed to transform.", 
     669               6 :                   nFailedCount, nSamplePoints );
     670                 : 
     671                 : /* -------------------------------------------------------------------- */
     672                 : /*      Compute the distance in "georeferenced" units from the top      */
     673                 : /*      corner of the transformed input image to the bottom left        */
     674                 : /*      corner of the transformed input.  Use this distance to          */
     675                 : /*      compute an approximate pixel size in the output                 */
     676                 : /*      georeferenced coordinates.                                      */
     677                 : /* -------------------------------------------------------------------- */
     678                 :     double dfDiagonalDist, dfDeltaX, dfDeltaY;
     679                 : 
     680             566 :     if( pabSuccess[0] && pabSuccess[nSamplePoints - 1] )
     681                 :     {
     682             280 :         dfDeltaX = padfX[nSamplePoints-1] - padfX[0];
     683             280 :         dfDeltaY = padfY[nSamplePoints-1] - padfY[0];
     684                 :     }
     685                 :     else
     686                 :     {
     687               6 :         dfDeltaX = dfMaxXOut - dfMinXOut;
     688               6 :         dfDeltaY = dfMaxYOut - dfMinYOut;
     689                 :     }
     690                 : 
     691             286 :     dfDiagonalDist = sqrt( dfDeltaX * dfDeltaX + dfDeltaY * dfDeltaY );
     692                 :     
     693                 : /* -------------------------------------------------------------------- */
     694                 : /*      Compute a pixel size from this.                                 */
     695                 : /* -------------------------------------------------------------------- */
     696                 :     double dfPixelSize;
     697                 : 
     698                 :     dfPixelSize = dfDiagonalDist 
     699             286 :         / sqrt(((double)nInXSize)*nInXSize + ((double)nInYSize)*nInYSize);
     700                 : 
     701             286 :     *pnPixels = (int) ((dfMaxXOut - dfMinXOut) / dfPixelSize + 0.5);
     702             286 :     *pnLines = (int) ((dfMaxYOut - dfMinYOut) / dfPixelSize + 0.5);
     703                 :     
     704             286 :     double dfPixelSizeX = dfPixelSize;
     705             286 :     double dfPixelSizeY = dfPixelSize;
     706                 :    
     707                 :     double adfExtent[4];
     708             286 :     const double adfRatioArray[] = { 0, 0.001, 0.01, 0.1, 1 };
     709                 :     size_t nRetry;
     710                 :     
     711                 : #define N_ELEMENTS(x) (sizeof(x) / sizeof(x[0]))
     712                 : 
     713                 : /* -------------------------------------------------------------------- */
     714                 : /*      Check that the right border is not completely out of source     */
     715                 : /*      image. If so, adjust the x pixel size a bit in the hope it will */
     716                 : /*      fit.                                                            */
     717                 : /* -------------------------------------------------------------------- */
     718             318 :     for( nRetry = 0; nRetry < N_ELEMENTS(adfRatioArray); nRetry ++ )
     719                 :     {
     720                 :         double dfTryPixelSizeX =
     721             316 :             dfPixelSizeX - dfPixelSizeX * adfRatioArray[nRetry] / *pnPixels;
     722             316 :         adfExtent[0] = dfMinXOut;
     723             316 :         adfExtent[1] = dfMaxYOut - (*pnLines) * dfPixelSizeY;
     724             316 :         adfExtent[2] = dfMinXOut + (*pnPixels) * dfTryPixelSizeX;
     725             316 :         adfExtent[3] = dfMaxYOut;
     726             316 :         if (!GDALSuggestedWarpOutput2_MustAdjustForRightBorder(
     727                 :                                             pfnTransformer, pTransformArg,
     728                 :                                             adfExtent, *pnPixels,  *pnLines,
     729                 :                                             dfTryPixelSizeX, dfPixelSizeY))
     730                 :         {
     731             284 :             dfPixelSizeX = dfTryPixelSizeX;
     732             284 :             break;
     733                 :         }
     734                 :     }
     735                 :     
     736                 : /* -------------------------------------------------------------------- */
     737                 : /*      Check that the bottom border is not completely out of source    */
     738                 : /*      image. If so, adjust the y pixel size a bit in the hope it will */
     739                 : /*      fit.                                                            */
     740                 : /* -------------------------------------------------------------------- */
     741             312 :     for( nRetry = 0; nRetry < N_ELEMENTS(adfRatioArray); nRetry ++ )
     742                 :     {
     743                 :         double dfTryPixelSizeY =
     744             310 :             dfPixelSizeY - dfPixelSizeY * adfRatioArray[nRetry] / *pnLines;
     745             310 :         adfExtent[0] = dfMinXOut;
     746             310 :         adfExtent[1] = dfMaxYOut - (*pnLines) * dfTryPixelSizeY;
     747             310 :         adfExtent[2] = dfMinXOut + (*pnPixels) * dfPixelSizeX;
     748             310 :         adfExtent[3] = dfMaxYOut;
     749             310 :         if (!GDALSuggestedWarpOutput2_MustAdjustForBottomBorder(
     750                 :                                             pfnTransformer, pTransformArg,
     751                 :                                             adfExtent, *pnPixels,  *pnLines,
     752                 :                                             dfPixelSizeX, dfTryPixelSizeY))
     753                 :         {
     754             284 :             dfPixelSizeY = dfTryPixelSizeY;
     755             284 :             break;
     756                 :         }
     757                 :     }
     758                 :     
     759                 :     
     760                 : /* -------------------------------------------------------------------- */
     761                 : /*      Recompute some bounds so that all return values are consistant  */
     762                 : /* -------------------------------------------------------------------- */
     763             286 :     dfMaxXOut = dfMinXOut + (*pnPixels) * dfPixelSizeX;
     764             286 :     dfMinYOut = dfMaxYOut - (*pnLines) * dfPixelSizeY;
     765                 :     
     766                 :     /* -------------------------------------------------------------------- */
     767                 :     /*      Return raw extents.                                             */
     768                 :     /* -------------------------------------------------------------------- */
     769             286 :     padfExtent[0] = dfMinXOut;
     770             286 :     padfExtent[1] = dfMinYOut;
     771             286 :     padfExtent[2] = dfMaxXOut;
     772             286 :     padfExtent[3] = dfMaxYOut;
     773                 : 
     774                 :     /* -------------------------------------------------------------------- */
     775                 :     /*      Set the output geotransform.                                    */
     776                 :     /* -------------------------------------------------------------------- */
     777             286 :     padfGeoTransformOut[0] = dfMinXOut;
     778             286 :     padfGeoTransformOut[1] = dfPixelSizeX;
     779             286 :     padfGeoTransformOut[2] = 0.0;
     780             286 :     padfGeoTransformOut[3] = dfMaxYOut;
     781             286 :     padfGeoTransformOut[4] = 0.0;
     782             286 :     padfGeoTransformOut[5] = - dfPixelSizeY;
     783                 :     
     784             286 :     CPLFree( padfX );
     785             286 :     CPLFree( padfXRevert );
     786             286 :     CPLFree( pabSuccess );
     787                 : 
     788             286 :     return CE_None;
     789                 : }
     790                 : 
     791                 : /************************************************************************/
     792                 : /* ==================================================================== */
     793                 : /*       GDALGenImgProjTransformer                      */
     794                 : /* ==================================================================== */
     795                 : /************************************************************************/
     796                 : 
     797                 : typedef struct {
     798                 : 
     799                 :     GDALTransformerInfo sTI;
     800                 : 
     801                 :     double   adfSrcGeoTransform[6];
     802                 :     double   adfSrcInvGeoTransform[6];
     803                 : 
     804                 :     void     *pSrcGCPTransformArg;
     805                 :     void     *pSrcRPCTransformArg;
     806                 :     void     *pSrcTPSTransformArg;
     807                 :     void     *pSrcGeoLocTransformArg;
     808                 : 
     809                 :     void     *pReprojectArg;
     810                 : 
     811                 :     double   adfDstGeoTransform[6];
     812                 :     double   adfDstInvGeoTransform[6];
     813                 :     
     814                 :     void     *pDstGCPTransformArg;
     815                 : 
     816                 : } GDALGenImgProjTransformInfo;
     817                 : 
     818                 : /************************************************************************/
     819                 : /*                  GDALCreateGenImgProjTransformer()                   */
     820                 : /************************************************************************/
     821                 : 
     822                 : /**
     823                 :  * Create image to image transformer.
     824                 :  *
     825                 :  * This function creates a transformation object that maps from pixel/line
     826                 :  * coordinates on one image to pixel/line coordinates on another image.  The
     827                 :  * images may potentially be georeferenced in different coordinate systems, 
     828                 :  * and may used GCPs to map between their pixel/line coordinates and 
     829                 :  * georeferenced coordinates (as opposed to the default assumption that their
     830                 :  * geotransform should be used). 
     831                 :  *
     832                 :  * This transformer potentially performs three concatenated transformations.
     833                 :  *
     834                 :  * The first stage is from source image pixel/line coordinates to source
     835                 :  * image georeferenced coordinates, and may be done using the geotransform, 
     836                 :  * or if not defined using a polynomial model derived from GCPs.  If GCPs
     837                 :  * are used this stage is accomplished using GDALGCPTransform(). 
     838                 :  *
     839                 :  * The second stage is to change projections from the source coordinate system
     840                 :  * to the destination coordinate system, assuming they differ.  This is 
     841                 :  * accomplished internally using GDALReprojectionTransform().
     842                 :  *
     843                 :  * The third stage is converting from destination image georeferenced
     844                 :  * coordinates to destination image coordinates.  This is done using the
     845                 :  * destination image geotransform, or if not available, using a polynomial 
     846                 :  * model derived from GCPs. If GCPs are used this stage is accomplished using 
     847                 :  * GDALGCPTransform().  This stage is skipped if hDstDS is NULL when the
     848                 :  * transformation is created. 
     849                 :  * 
     850                 :  * @param hSrcDS source dataset, or NULL.
     851                 :  * @param pszSrcWKT the coordinate system for the source dataset.  If NULL, 
     852                 :  * it will be read from the dataset itself. 
     853                 :  * @param hDstDS destination dataset (or NULL). 
     854                 :  * @param pszDstWKT the coordinate system for the destination dataset.  If
     855                 :  * NULL, and hDstDS not NULL, it will be read from the destination dataset.
     856                 :  * @param bGCPUseOK TRUE if GCPs should be used if the geotransform is not
     857                 :  * available on the source dataset (not destination).
     858                 :  * @param dfGCPErrorThreshold ignored/deprecated.
     859                 :  * @param nOrder the maximum order to use for GCP derived polynomials if 
     860                 :  * possible.  Use 0 to autoselect, or -1 for thin plate splines.
     861                 :  * 
     862                 :  * @return handle suitable for use GDALGenImgProjTransform(), and to be
     863                 :  * deallocated with GDALDestroyGenImgProjTransformer().
     864                 :  */
     865                 : 
     866                 : void *
     867              47 : GDALCreateGenImgProjTransformer( GDALDatasetH hSrcDS, const char *pszSrcWKT,
     868                 :                                  GDALDatasetH hDstDS, const char *pszDstWKT,
     869                 :                                  int bGCPUseOK, double dfGCPErrorThreshold,
     870                 :                                  int nOrder )
     871                 : 
     872                 : {
     873              47 :     char **papszOptions = NULL;
     874                 :     void *pRet;
     875                 : 
     876              47 :     if( pszSrcWKT != NULL )
     877              33 :         papszOptions = CSLSetNameValue( papszOptions, "SRC_SRS", pszSrcWKT );
     878              47 :     if( pszDstWKT != NULL )
     879              31 :         papszOptions = CSLSetNameValue( papszOptions, "DST_SRS", pszDstWKT );
     880              47 :     if( !bGCPUseOK )
     881               9 :         papszOptions = CSLSetNameValue( papszOptions, "GCPS_OK", "FALSE" );
     882              47 :     if( nOrder != 0 )
     883                 :         papszOptions = CSLSetNameValue( papszOptions, "MAX_GCP_ORDER", 
     884               0 :                                         CPLString().Printf("%d",nOrder) );
     885                 : 
     886              47 :     pRet = GDALCreateGenImgProjTransformer2( hSrcDS, hDstDS, papszOptions );
     887              47 :     CSLDestroy( papszOptions );
     888                 : 
     889              47 :     return pRet;
     890                 : }
     891                 : 
     892                 : 
     893                 : 
     894                 : /************************************************************************/
     895                 : /*                          InsertCenterLong()                          */
     896                 : /*                                                                      */
     897                 : /*      Insert a CENTER_LONG Extension entry on a GEOGCS to indicate    */
     898                 : /*      the center longitude of the dataset for wrapping purposes.      */
     899                 : /************************************************************************/
     900                 : 
     901              45 : static CPLString InsertCenterLong( GDALDatasetH hDS, CPLString osWKT )
     902                 : 
     903                 : {                       
     904              45 :     if( !EQUALN(osWKT.c_str(), "GEOGCS[", 7) )
     905              21 :         return osWKT;
     906                 :     
     907              24 :     if( strstr(osWKT,"EXTENSION[\"CENTER_LONG") != NULL )
     908               0 :         return osWKT;
     909                 : 
     910                 : /* -------------------------------------------------------------------- */
     911                 : /*      For now we only do this if we have a geotransform since         */
     912                 : /*      other forms require a bunch of extra work.                      */
     913                 : /* -------------------------------------------------------------------- */
     914                 :     double   adfGeoTransform[6];
     915                 : 
     916              24 :     if( GDALGetGeoTransform( hDS, adfGeoTransform ) != CE_None )
     917               0 :         return osWKT;
     918                 : 
     919                 : /* -------------------------------------------------------------------- */
     920                 : /*      Compute min/max longitude based on testing the four corners.    */
     921                 : /* -------------------------------------------------------------------- */
     922                 :     double dfMinLong, dfMaxLong;
     923              24 :     int nXSize = GDALGetRasterXSize( hDS );
     924              24 :     int nYSize = GDALGetRasterYSize( hDS );
     925                 : 
     926                 :     dfMinLong = 
     927             504 :         MIN(MIN(adfGeoTransform[0] + 0 * adfGeoTransform[1]
     928                 :                 + 0 * adfGeoTransform[2],
     929                 :                 adfGeoTransform[0] + nXSize * adfGeoTransform[1]
     930                 :                 + 0 * adfGeoTransform[2]),
     931                 :             MIN(adfGeoTransform[0] + 0 * adfGeoTransform[1]
     932                 :                 + nYSize * adfGeoTransform[2],
     933                 :                 adfGeoTransform[0] + nXSize * adfGeoTransform[1]
     934             504 :                 + nYSize * adfGeoTransform[2]));
     935                 :     dfMaxLong = 
     936             576 :         MAX(MAX(adfGeoTransform[0] + 0 * adfGeoTransform[1]
     937                 :                 + 0 * adfGeoTransform[2],
     938                 :                 adfGeoTransform[0] + nXSize * adfGeoTransform[1]
     939                 :                 + 0 * adfGeoTransform[2]),
     940                 :             MAX(adfGeoTransform[0] + 0 * adfGeoTransform[1]
     941                 :                 + nYSize * adfGeoTransform[2],
     942                 :                 adfGeoTransform[0] + nXSize * adfGeoTransform[1]
     943             576 :                 + nYSize * adfGeoTransform[2]));
     944                 : 
     945              24 :     if( dfMaxLong - dfMinLong > 360.0 )
     946               0 :         return osWKT;
     947                 : 
     948                 : /* -------------------------------------------------------------------- */
     949                 : /*      Insert center long.                                             */
     950                 : /* -------------------------------------------------------------------- */
     951              24 :     OGRSpatialReference oSRS( osWKT );
     952              24 :     double dfCenterLong = (dfMaxLong + dfMinLong) / 2.0;
     953                 :     OGR_SRSNode *poExt;
     954                 : 
     955              24 :     poExt  = new OGR_SRSNode( "EXTENSION" );
     956              48 :     poExt->AddChild( new OGR_SRSNode( "CENTER_LONG" ) );
     957              24 :     poExt->AddChild( new OGR_SRSNode( CPLString().Printf("%g",dfCenterLong) ));
     958                 :     
     959              24 :     oSRS.GetRoot()->AddChild( poExt->Clone() );
     960              24 :     delete poExt;
     961                 : 
     962                 : /* -------------------------------------------------------------------- */
     963                 : /*      Convert back to wkt.                                            */
     964                 : /* -------------------------------------------------------------------- */
     965              24 :     char *pszWKT = NULL;
     966              24 :     oSRS.exportToWkt( &pszWKT );
     967                 :     
     968              24 :     osWKT = pszWKT;
     969              24 :     CPLFree( pszWKT );
     970                 : 
     971              24 :     return osWKT;
     972                 : }
     973                 : 
     974                 : /************************************************************************/
     975                 : /*                  GDALCreateGenImgProjTransformer2()                  */
     976                 : /************************************************************************/
     977                 : 
     978                 : /**
     979                 :  * Create image to image transformer.
     980                 :  *
     981                 :  * This function creates a transformation object that maps from pixel/line
     982                 :  * coordinates on one image to pixel/line coordinates on another image.  The
     983                 :  * images may potentially be georeferenced in different coordinate systems, 
     984                 :  * and may used GCPs to map between their pixel/line coordinates and 
     985                 :  * georeferenced coordinates (as opposed to the default assumption that their
     986                 :  * geotransform should be used). 
     987                 :  *
     988                 :  * This transformer potentially performs three concatenated transformations.
     989                 :  *
     990                 :  * The first stage is from source image pixel/line coordinates to source
     991                 :  * image georeferenced coordinates, and may be done using the geotransform, 
     992                 :  * or if not defined using a polynomial model derived from GCPs.  If GCPs
     993                 :  * are used this stage is accomplished using GDALGCPTransform(). 
     994                 :  *
     995                 :  * The second stage is to change projections from the source coordinate system
     996                 :  * to the destination coordinate system, assuming they differ.  This is 
     997                 :  * accomplished internally using GDALReprojectionTransform().
     998                 :  *
     999                 :  * The third stage is converting from destination image georeferenced
    1000                 :  * coordinates to destination image coordinates.  This is done using the
    1001                 :  * destination image geotransform, or if not available, using a polynomial 
    1002                 :  * model derived from GCPs. If GCPs are used this stage is accomplished using 
    1003                 :  * GDALGCPTransform().  This stage is skipped if hDstDS is NULL when the
    1004                 :  * transformation is created. 
    1005                 :  *
    1006                 :  * Supported Options:
    1007                 :  * <ul>
    1008                 :  * <li> SRC_SRS: WKT SRS to be used as an override for hSrcDS.
    1009                 :  * <li> DST_SRS: WKT SRS to be used as an override for hDstDS.
    1010                 :  * <li> GCPS_OK: If false, GCPs will not be used, default is TRUE. 
    1011                 :  * <li> REFINE_MINIMUM_GCPS: The minimum amount of GCPs that should be available after the refinement.
    1012                 :  * <li> REFINE_TOLERANCE: The tolernace that specifies when a GCP will be eliminated.
    1013                 :  * <li> MAX_GCP_ORDER: the maximum order to use for GCP derived polynomials if
    1014                 :  * possible.  The default is to autoselect based on the number of GCPs.  
    1015                 :  * A value of -1 triggers use of Thin Plate Spline instead of polynomials.
    1016                 :  * <li> METHOD: may have a value which is one of GEOTRANSFORM, GCP_POLYNOMIAL,
    1017                 :  * GCP_TPS, GEOLOC_ARRAY, RPC to force only one geolocation method to be
    1018                 :  * considered on the source dataset. 
    1019                 :  * <li> RPC_HEIGHT: A fixed height to be used with RPC calculations.
    1020                 :  * <li> RPC_DEM: The name of a DEM file to be used with RPC calculations.
    1021                 :  * <li> INSERT_CENTER_LONG: May be set to FALSE to disable setting up a 
    1022                 :  * CENTER_LONG value on the coordinate system to rewrap things around the
    1023                 :  * center of the image.  
    1024                 :  * </ul>
    1025                 :  * 
    1026                 :  * @param hSrcDS source dataset, or NULL.
    1027                 :  * @param hDstDS destination dataset (or NULL).
    1028                 :  * @param papszOptions NULL-terminated list of string options (or NULL).
    1029                 :  * 
    1030                 :  * @return handle suitable for use GDALGenImgProjTransform(), and to be
    1031                 :  * deallocated with GDALDestroyGenImgProjTransformer() or NULL on failure.
    1032                 :  */
    1033                 : 
    1034                 : void *
    1035             335 : GDALCreateGenImgProjTransformer2( GDALDatasetH hSrcDS, GDALDatasetH hDstDS, 
    1036                 :                                   char **papszOptions )
    1037                 : 
    1038                 : {
    1039                 :     GDALGenImgProjTransformInfo *psInfo;
    1040                 :     char **papszMD;
    1041                 :     GDALRPCInfo sRPCInfo;
    1042             335 :     const char *pszMethod = CSLFetchNameValue( papszOptions, "METHOD" );
    1043                 :     const char *pszValue;
    1044             335 :     int nOrder = 0, bGCPUseOK = TRUE, nMinimumGcps = -1, bRefine = FALSE;
    1045             335 :     double dfTolerance = 0.0;
    1046             335 :     const char *pszSrcWKT = CSLFetchNameValue( papszOptions, "SRC_SRS" );
    1047             335 :     const char *pszDstWKT = CSLFetchNameValue( papszOptions, "DST_SRS" );
    1048                 : 
    1049             335 :     pszValue = CSLFetchNameValue( papszOptions, "MAX_GCP_ORDER" );
    1050             335 :     if( pszValue )
    1051               0 :         nOrder = atoi(pszValue);
    1052                 : 
    1053             335 :     pszValue = CSLFetchNameValue( papszOptions, "GCPS_OK" );
    1054             335 :     if( pszValue )
    1055               9 :         bGCPUseOK = CSLTestBoolean(pszValue);
    1056                 : 
    1057             335 :     pszValue = CSLFetchNameValue( papszOptions, "REFINE_MINIMUM_GCPS" );
    1058             335 :     if( pszValue )
    1059                 :     {
    1060               0 :         if( atoi(pszValue) != -1)
    1061               0 :             nMinimumGcps = atoi(pszValue);
    1062                 :     }
    1063                 : 
    1064             335 :     pszValue = CSLFetchNameValue( papszOptions, "REFINE_TOLERANCE" );
    1065             335 :     if( pszValue )
    1066                 :     {
    1067               0 :         dfTolerance = atof(pszValue);
    1068               0 :         bRefine = TRUE;
    1069                 :     }
    1070                 : 
    1071                 : /* -------------------------------------------------------------------- */
    1072                 : /*      Initialize the transform info.                                  */
    1073                 : /* -------------------------------------------------------------------- */
    1074                 :     psInfo = (GDALGenImgProjTransformInfo *) 
    1075             335 :         CPLCalloc(sizeof(GDALGenImgProjTransformInfo),1);
    1076                 : 
    1077             335 :     strcpy( psInfo->sTI.szSignature, "GTI" );
    1078             335 :     psInfo->sTI.pszClassName = "GDALGenImgProjTransformer";
    1079             335 :     psInfo->sTI.pfnTransform = GDALGenImgProjTransform;
    1080             335 :     psInfo->sTI.pfnCleanup = GDALDestroyGenImgProjTransformer;
    1081             335 :     psInfo->sTI.pfnSerialize = GDALSerializeGenImgProjTransformer;
    1082                 : 
    1083                 : /* -------------------------------------------------------------------- */
    1084                 : /*      Get forward and inverse geotransform for the source image.      */
    1085                 : /* -------------------------------------------------------------------- */
    1086             335 :     if( hSrcDS == NULL )
    1087                 :     {
    1088              11 :         psInfo->adfSrcGeoTransform[0] = 0.0;
    1089              11 :         psInfo->adfSrcGeoTransform[1] = 1.0;
    1090              11 :         psInfo->adfSrcGeoTransform[2] = 0.0;
    1091              11 :         psInfo->adfSrcGeoTransform[3] = 0.0;
    1092              11 :         psInfo->adfSrcGeoTransform[4] = 0.0;
    1093              11 :         psInfo->adfSrcGeoTransform[5] = 1.0;
    1094                 :         memcpy( psInfo->adfSrcInvGeoTransform, psInfo->adfSrcGeoTransform,
    1095              11 :                 sizeof(double) * 6 );
    1096                 :     }
    1097                 : 
    1098             625 :     else if( (pszMethod == NULL || EQUAL(pszMethod,"GEOTRANSFORM"))
    1099                 :              && GDALGetGeoTransform( hSrcDS, psInfo->adfSrcGeoTransform ) 
    1100                 :              == CE_None
    1101             301 :              && (psInfo->adfSrcGeoTransform[0] != 0.0
    1102               0 :                  || psInfo->adfSrcGeoTransform[1] != 1.0
    1103               0 :                  || psInfo->adfSrcGeoTransform[2] != 0.0
    1104               0 :                  || psInfo->adfSrcGeoTransform[3] != 0.0
    1105               0 :                  || psInfo->adfSrcGeoTransform[4] != 0.0
    1106               0 :                  || ABS(psInfo->adfSrcGeoTransform[5]) != 1.0) )
    1107                 :     {
    1108                 :         GDALInvGeoTransform( psInfo->adfSrcGeoTransform, 
    1109             301 :                              psInfo->adfSrcInvGeoTransform );
    1110             301 :         if( pszSrcWKT == NULL )
    1111             271 :             pszSrcWKT = GDALGetProjectionRef( hSrcDS );
    1112                 :     }
    1113                 : 
    1114              23 :     else if( bGCPUseOK 
    1115                 :              && (pszMethod == NULL || EQUAL(pszMethod,"GCP_POLYNOMIAL") )
    1116                 :              && GDALGetGCPCount( hSrcDS ) > 0 && nOrder >= 0 )
    1117                 :     {
    1118              16 :         if(bRefine)
    1119                 :         {
    1120                 :                 psInfo->pSrcGCPTransformArg = 
    1121                 :                     GDALCreateGCPRefineTransformer( GDALGetGCPCount( hSrcDS ),
    1122                 :                                                     GDALGetGCPs( hSrcDS ), nOrder, 
    1123               0 :                                                     FALSE, dfTolerance, nMinimumGcps );
    1124                 :         }
    1125                 :         else
    1126                 :         {
    1127                 :             psInfo->pSrcGCPTransformArg = 
    1128                 :                 GDALCreateGCPTransformer( GDALGetGCPCount( hSrcDS ),
    1129                 :                                           GDALGetGCPs( hSrcDS ), nOrder, 
    1130              16 :                                           FALSE );
    1131                 :         }
    1132                 : 
    1133              16 :         if( psInfo->pSrcGCPTransformArg == NULL )
    1134                 :         {
    1135               0 :             GDALDestroyGenImgProjTransformer( psInfo );
    1136               0 :             return NULL;
    1137                 :         }
    1138                 : 
    1139              16 :         if( pszSrcWKT == NULL )
    1140              16 :             pszSrcWKT = GDALGetGCPProjection( hSrcDS );
    1141                 :     }
    1142                 : 
    1143               7 :     else if( bGCPUseOK 
    1144                 :              && GDALGetGCPCount( hSrcDS ) > 0 
    1145                 :              && nOrder <= 0
    1146                 :              && (pszMethod == NULL || EQUAL(pszMethod,"GCP_TPS")) )
    1147                 :     {
    1148                 :         psInfo->pSrcTPSTransformArg = 
    1149                 :             GDALCreateTPSTransformer( GDALGetGCPCount( hSrcDS ),
    1150               3 :                                       GDALGetGCPs( hSrcDS ), FALSE );
    1151               3 :         if( psInfo->pSrcTPSTransformArg == NULL )
    1152                 :         {
    1153               0 :             GDALDestroyGenImgProjTransformer( psInfo );
    1154               0 :             return NULL;
    1155                 :         }
    1156                 : 
    1157               3 :         if( pszSrcWKT == NULL )
    1158               3 :             pszSrcWKT = GDALGetGCPProjection( hSrcDS );
    1159                 :     }
    1160                 : 
    1161               4 :     else if( (pszMethod == NULL || EQUAL(pszMethod,"RPC"))
    1162                 :              && (papszMD = GDALGetMetadata( hSrcDS, "RPC" )) != NULL
    1163                 :              && GDALExtractRPCInfo( papszMD, &sRPCInfo ) )
    1164                 :     {
    1165                 :         psInfo->pSrcRPCTransformArg = 
    1166               3 :             GDALCreateRPCTransformer( &sRPCInfo, FALSE, 0.1, papszOptions );
    1167               3 :         if( psInfo->pSrcRPCTransformArg == NULL )
    1168                 :         {
    1169               0 :             GDALDestroyGenImgProjTransformer( psInfo );
    1170               0 :             return NULL;
    1171                 :         }
    1172               3 :         if( pszSrcWKT == NULL )
    1173               3 :             pszSrcWKT = SRS_WKT_WGS84;
    1174                 :     }
    1175                 : 
    1176               1 :     else if( (pszMethod == NULL || EQUAL(pszMethod,"GEOLOC_ARRAY"))
    1177                 :              && (papszMD = GDALGetMetadata( hSrcDS, "GEOLOCATION" )) != NULL )
    1178                 :     {
    1179                 :         psInfo->pSrcGeoLocTransformArg = 
    1180               1 :             GDALCreateGeoLocTransformer( hSrcDS, papszMD, FALSE );
    1181                 : 
    1182               1 :         if( psInfo->pSrcGeoLocTransformArg == NULL )
    1183                 :         {
    1184               0 :             GDALDestroyGenImgProjTransformer( psInfo );
    1185               0 :             return NULL;
    1186                 :         }
    1187               1 :         if( pszSrcWKT == NULL )
    1188               1 :             pszSrcWKT = CSLFetchNameValue( papszMD, "SRS" );
    1189                 :     }
    1190                 : 
    1191               0 :     else if( pszMethod != NULL )
    1192                 :     {
    1193                 :         CPLError( CE_Failure, CPLE_AppDefined, 
    1194                 :                   "Unable to compute a %s based transformation between pixel/line\n"
    1195                 :                   "and georeferenced coordinates for %s.\n", 
    1196               0 :                   pszMethod, GDALGetDescription( hSrcDS ) );
    1197                 : 
    1198               0 :         GDALDestroyGenImgProjTransformer( psInfo );
    1199               0 :         return NULL;
    1200                 :     }
    1201                 : 
    1202                 :     else
    1203                 :     {
    1204                 :         CPLError( CE_Failure, CPLE_AppDefined, 
    1205                 :                   "Unable to compute a transformation between pixel/line\n"
    1206                 :                   "and georeferenced coordinates for %s.\n"
    1207                 :                   "There is no affine transformation and no GCPs.", 
    1208               0 :                   GDALGetDescription( hSrcDS ) );
    1209                 : 
    1210               0 :         GDALDestroyGenImgProjTransformer( psInfo );
    1211               0 :         return NULL;
    1212                 :     }
    1213                 : 
    1214                 : /* -------------------------------------------------------------------- */
    1215                 : /*      Setup reprojection.                                             */
    1216                 : /* -------------------------------------------------------------------- */
    1217             335 :     if( pszDstWKT == NULL && hDstDS != NULL )
    1218              19 :         pszDstWKT = GDALGetProjectionRef( hDstDS );
    1219                 : 
    1220             335 :     if( pszSrcWKT != NULL && strlen(pszSrcWKT) > 0 
    1221                 :         && pszDstWKT != NULL && strlen(pszDstWKT) > 0 
    1222                 :         && !EQUAL(pszSrcWKT,pszDstWKT) )
    1223                 :     {
    1224              45 :         CPLString osSrcWKT = pszSrcWKT;
    1225              45 :         if (hSrcDS 
    1226                 :             && CSLFetchBoolean( papszOptions, "INSERT_CENTER_LONG", TRUE ) )
    1227              45 :             osSrcWKT = InsertCenterLong( hSrcDS, osSrcWKT );
    1228                 :         
    1229                 :         psInfo->pReprojectArg = 
    1230              45 :             GDALCreateReprojectionTransformer( osSrcWKT.c_str(), pszDstWKT );
    1231                 :     }
    1232                 :         
    1233                 : /* -------------------------------------------------------------------- */
    1234                 : /*      Get forward and inverse geotransform for destination image.     */
    1235                 : /*      If we have no destination use a unit transform.                 */
    1236                 : /* -------------------------------------------------------------------- */
    1237             335 :     if( hDstDS )
    1238                 :     {
    1239              39 :         GDALGetGeoTransform( hDstDS, psInfo->adfDstGeoTransform );
    1240                 :         GDALInvGeoTransform( psInfo->adfDstGeoTransform, 
    1241              39 :                              psInfo->adfDstInvGeoTransform );
    1242                 :     }
    1243                 :     else
    1244                 :     {
    1245             296 :         psInfo->adfDstGeoTransform[0] = 0.0;
    1246             296 :         psInfo->adfDstGeoTransform[1] = 1.0;
    1247             296 :         psInfo->adfDstGeoTransform[2] = 0.0;
    1248             296 :         psInfo->adfDstGeoTransform[3] = 0.0;
    1249             296 :         psInfo->adfDstGeoTransform[4] = 0.0;
    1250             296 :         psInfo->adfDstGeoTransform[5] = 1.0;
    1251                 :         memcpy( psInfo->adfDstInvGeoTransform, psInfo->adfDstGeoTransform,
    1252             296 :                 sizeof(double) * 6 );
    1253                 :     }
    1254                 :     
    1255             335 :     return psInfo;
    1256                 : }
    1257                 : 
    1258                 : /************************************************************************/
    1259                 : /*                  GDALRefreshGenImgProjTransformer()                  */
    1260                 : /************************************************************************/
    1261                 : 
    1262             286 : void GDALRefreshGenImgProjTransformer(void* hTransformArg)
    1263                 : {
    1264                 :     GDALGenImgProjTransformInfo *psInfo =
    1265             286 :         static_cast<GDALGenImgProjTransformInfo *>( hTransformArg );
    1266                 : 
    1267             286 :     if (psInfo->pReprojectArg)
    1268                 :     {
    1269              28 :         CPLXMLNode* psXML = GDALSerializeReprojectionTransformer(psInfo->pReprojectArg);
    1270              28 :         GDALDestroyReprojectionTransformer(psInfo->pReprojectArg);
    1271              28 :         psInfo->pReprojectArg = GDALDeserializeReprojectionTransformer(psXML);
    1272              28 :         CPLDestroyXMLNode(psXML);
    1273                 :     }
    1274             286 : }
    1275                 : 
    1276                 : /************************************************************************/
    1277                 : /*                  GDALCreateGenImgProjTransformer3()                  */
    1278                 : /************************************************************************/
    1279                 : 
    1280                 : /**
    1281                 :  * Create image to image transformer.
    1282                 :  *
    1283                 :  * This function creates a transformation object that maps from pixel/line
    1284                 :  * coordinates on one image to pixel/line coordinates on another image.  The
    1285                 :  * images may potentially be georeferenced in different coordinate systems, 
    1286                 :  * and may used GCPs to map between their pixel/line coordinates and 
    1287                 :  * georeferenced coordinates (as opposed to the default assumption that their
    1288                 :  * geotransform should be used). 
    1289                 :  *
    1290                 :  * This transformer potentially performs three concatenated transformations.
    1291                 :  *
    1292                 :  * The first stage is from source image pixel/line coordinates to source
    1293                 :  * image georeferenced coordinates, and may be done using the geotransform, 
    1294                 :  * or if not defined using a polynomial model derived from GCPs.  If GCPs
    1295                 :  * are used this stage is accomplished using GDALGCPTransform(). 
    1296                 :  *
    1297                 :  * The second stage is to change projections from the source coordinate system
    1298                 :  * to the destination coordinate system, assuming they differ.  This is 
    1299                 :  * accomplished internally using GDALReprojectionTransform().
    1300                 :  *
    1301                 :  * The third stage is converting from destination image georeferenced
    1302                 :  * coordinates to destination image coordinates.  This is done using the
    1303                 :  * destination image geotransform, or if not available, using a polynomial 
    1304                 :  * model derived from GCPs. If GCPs are used this stage is accomplished using 
    1305                 :  * GDALGCPTransform().  This stage is skipped if hDstDS is NULL when the
    1306                 :  * transformation is created. 
    1307                 :  *
    1308                 :  * @param pszSrcWKT source WKT (or NULL).
    1309                 :  * @param padfSrcGeoTransform source geotransform (or NULL).
    1310                 :  * @param pszDstWKT destination WKT (or NULL).
    1311                 :  * @param padfDstGeoTransform destination geotransform (or NULL).
    1312                 :  * 
    1313                 :  * @return handle suitable for use GDALGenImgProjTransform(), and to be
    1314                 :  * deallocated with GDALDestroyGenImgProjTransformer() or NULL on failure.
    1315                 :  */
    1316                 : 
    1317                 : void *
    1318               0 : GDALCreateGenImgProjTransformer3( const char *pszSrcWKT,
    1319                 :                                   const double *padfSrcGeoTransform,
    1320                 :                                   const char *pszDstWKT,
    1321                 :                                   const double *padfDstGeoTransform )
    1322                 : 
    1323                 : {
    1324                 :     GDALGenImgProjTransformInfo *psInfo;
    1325                 : 
    1326                 : /* -------------------------------------------------------------------- */
    1327                 : /*      Initialize the transform info.                                  */
    1328                 : /* -------------------------------------------------------------------- */
    1329                 :     psInfo = (GDALGenImgProjTransformInfo *) 
    1330               0 :         CPLCalloc(sizeof(GDALGenImgProjTransformInfo),1);
    1331                 : 
    1332               0 :     strcpy( psInfo->sTI.szSignature, "GTI" );
    1333               0 :     psInfo->sTI.pszClassName = "GDALGenImgProjTransformer";
    1334               0 :     psInfo->sTI.pfnTransform = GDALGenImgProjTransform;
    1335               0 :     psInfo->sTI.pfnCleanup = GDALDestroyGenImgProjTransformer;
    1336               0 :     psInfo->sTI.pfnSerialize = GDALSerializeGenImgProjTransformer;
    1337                 : 
    1338                 : /* -------------------------------------------------------------------- */
    1339                 : /*      Get forward and inverse geotransform for the source image.      */
    1340                 : /* -------------------------------------------------------------------- */
    1341               0 :     if( padfSrcGeoTransform )
    1342                 :     {
    1343                 :         memcpy( psInfo->adfSrcGeoTransform, padfSrcGeoTransform,
    1344               0 :                 sizeof(psInfo->adfSrcGeoTransform) );
    1345                 :         GDALInvGeoTransform( psInfo->adfSrcGeoTransform, 
    1346               0 :                              psInfo->adfSrcInvGeoTransform );
    1347                 :     }
    1348                 :     else
    1349                 :     {
    1350               0 :         psInfo->adfSrcGeoTransform[0] = 0.0;
    1351               0 :         psInfo->adfSrcGeoTransform[1] = 1.0;
    1352               0 :         psInfo->adfSrcGeoTransform[2] = 0.0;
    1353               0 :         psInfo->adfSrcGeoTransform[3] = 0.0;
    1354               0 :         psInfo->adfSrcGeoTransform[4] = 0.0;
    1355               0 :         psInfo->adfSrcGeoTransform[5] = 1.0;
    1356                 :         memcpy( psInfo->adfSrcInvGeoTransform, psInfo->adfSrcGeoTransform,
    1357               0 :                 sizeof(double) * 6 );
    1358                 :     }
    1359                 : 
    1360                 : /* -------------------------------------------------------------------- */
    1361                 : /*      Setup reprojection.                                             */
    1362                 : /* -------------------------------------------------------------------- */
    1363               0 :     if( pszSrcWKT != NULL && strlen(pszSrcWKT) > 0 
    1364                 :         && pszDstWKT != NULL && strlen(pszDstWKT) > 0 
    1365                 :         && !EQUAL(pszSrcWKT, pszDstWKT) )
    1366                 :     {
    1367                 :         psInfo->pReprojectArg = 
    1368               0 :             GDALCreateReprojectionTransformer( pszSrcWKT, pszDstWKT );
    1369                 :     }
    1370                 :         
    1371                 : /* -------------------------------------------------------------------- */
    1372                 : /*      Get forward and inverse geotransform for destination image.     */
    1373                 : /*      If we have no destination matrix use a unit transform.          */
    1374                 : /* -------------------------------------------------------------------- */
    1375               0 :     if( padfDstGeoTransform )
    1376                 :     {
    1377                 :         memcpy( psInfo->adfDstGeoTransform, padfDstGeoTransform,
    1378               0 :                 sizeof(psInfo->adfDstGeoTransform) );
    1379                 :         GDALInvGeoTransform( psInfo->adfDstGeoTransform, 
    1380               0 :                              psInfo->adfDstInvGeoTransform );
    1381                 :     }
    1382                 :     else
    1383                 :     {
    1384               0 :         psInfo->adfDstGeoTransform[0] = 0.0;
    1385               0 :         psInfo->adfDstGeoTransform[1] = 1.0;
    1386               0 :         psInfo->adfDstGeoTransform[2] = 0.0;
    1387               0 :         psInfo->adfDstGeoTransform[3] = 0.0;
    1388               0 :         psInfo->adfDstGeoTransform[4] = 0.0;
    1389               0 :         psInfo->adfDstGeoTransform[5] = 1.0;
    1390                 :         memcpy( psInfo->adfDstInvGeoTransform, psInfo->adfDstGeoTransform,
    1391               0 :                 sizeof(double) * 6 );
    1392                 :     }
    1393                 :     
    1394               0 :     return psInfo;
    1395                 : }
    1396                 : 
    1397                 : /************************************************************************/
    1398                 : /*            GDALSetGenImgProjTransformerDstGeoTransform()             */
    1399                 : /************************************************************************/
    1400                 : 
    1401                 : /**
    1402                 :  * Set GenImgProj output geotransform.
    1403                 :  *
    1404                 :  * Normally the "destination geotransform", or transformation between 
    1405                 :  * georeferenced output coordinates and pixel/line coordinates on the
    1406                 :  * destination file is extracted from the destination file by 
    1407                 :  * GDALCreateGenImgProjTransformer() and stored in the GenImgProj private
    1408                 :  * info.  However, sometimes it is inconvenient to have an output file
    1409                 :  * handle with appropriate geotransform information when creating the
    1410                 :  * transformation.  For these cases, this function can be used to apply
    1411                 :  * the destination geotransform. 
    1412                 :  *
    1413                 :  * @param hTransformArg the handle to update.
    1414                 :  * @param padfGeoTransform the destination geotransform to apply (six doubles).
    1415                 :  */
    1416                 : 
    1417             279 : void GDALSetGenImgProjTransformerDstGeoTransform( 
    1418                 :     void *hTransformArg, const double *padfGeoTransform )
    1419                 : 
    1420                 : {
    1421             279 :     VALIDATE_POINTER0( hTransformArg, "GDALSetGenImgProjTransformerDstGeoTransform" );
    1422                 : 
    1423                 :     GDALGenImgProjTransformInfo *psInfo = 
    1424             279 :         static_cast<GDALGenImgProjTransformInfo *>( hTransformArg );
    1425                 : 
    1426             279 :     memcpy( psInfo->adfDstGeoTransform, padfGeoTransform, sizeof(double) * 6 );
    1427                 :     GDALInvGeoTransform( psInfo->adfDstGeoTransform, 
    1428             279 :                          psInfo->adfDstInvGeoTransform );
    1429                 : }
    1430                 : 
    1431                 : /************************************************************************/
    1432                 : /*                  GDALDestroyGenImgProjTransformer()                  */
    1433                 : /************************************************************************/
    1434                 : 
    1435                 : /**
    1436                 :  * GenImgProjTransformer deallocator.
    1437                 :  *
    1438                 :  * This function is used to deallocate the handle created with
    1439                 :  * GDALCreateGenImgProjTransformer().
    1440                 :  *
    1441                 :  * @param hTransformArg the handle to deallocate. 
    1442                 :  */
    1443                 : 
    1444             375 : void GDALDestroyGenImgProjTransformer( void *hTransformArg )
    1445                 : 
    1446                 : {
    1447             375 :     VALIDATE_POINTER0( hTransformArg, "GDALDestroyGenImgProjTransformer" );
    1448                 : 
    1449                 :     GDALGenImgProjTransformInfo *psInfo = 
    1450             375 :         (GDALGenImgProjTransformInfo *) hTransformArg;
    1451                 : 
    1452             375 :     if( psInfo->pSrcGCPTransformArg != NULL )
    1453              21 :         GDALDestroyGCPTransformer( psInfo->pSrcGCPTransformArg );
    1454                 : 
    1455             375 :     if( psInfo->pSrcTPSTransformArg != NULL )
    1456               4 :         GDALDestroyTPSTransformer( psInfo->pSrcTPSTransformArg );
    1457                 : 
    1458             375 :     if( psInfo->pSrcRPCTransformArg != NULL )
    1459               3 :         GDALDestroyRPCTransformer( psInfo->pSrcRPCTransformArg );
    1460                 : 
    1461             375 :     if( psInfo->pSrcGeoLocTransformArg != NULL )
    1462               2 :         GDALDestroyGeoLocTransformer( psInfo->pSrcGeoLocTransformArg );
    1463                 : 
    1464             375 :     if( psInfo->pDstGCPTransformArg != NULL )
    1465               0 :         GDALDestroyGCPTransformer( psInfo->pDstGCPTransformArg );
    1466                 : 
    1467             375 :     if( psInfo->pReprojectArg != NULL )
    1468              53 :         GDALDestroyReprojectionTransformer( psInfo->pReprojectArg );
    1469                 : 
    1470             375 :     CPLFree( psInfo );
    1471                 : }
    1472                 : 
    1473                 : /************************************************************************/
    1474                 : /*                      GDALGenImgProjTransform()                       */
    1475                 : /************************************************************************/
    1476                 : 
    1477                 : /**
    1478                 :  * Perform general image reprojection transformation.
    1479                 :  *
    1480                 :  * Actually performs the transformation setup in 
    1481                 :  * GDALCreateGenImgProjTransformer().  This function matches the signature
    1482                 :  * required by the GDALTransformerFunc(), and more details on the arguments
    1483                 :  * can be found in that topic. 
    1484                 :  */
    1485                 : 
    1486          298117 : int GDALGenImgProjTransform( void *pTransformArg, int bDstToSrc, 
    1487                 :                              int nPointCount, 
    1488                 :                              double *padfX, double *padfY, double *padfZ,
    1489                 :                              int *panSuccess )
    1490                 : {
    1491                 :     GDALGenImgProjTransformInfo *psInfo = 
    1492          298117 :         (GDALGenImgProjTransformInfo *) pTransformArg;
    1493                 :     int   i;
    1494                 :     double *padfGeoTransform;
    1495                 :     void *pGCPTransformArg;
    1496                 :     void *pRPCTransformArg;
    1497                 :     void *pTPSTransformArg;
    1498                 :     void *pGeoLocTransformArg;
    1499                 : 
    1500                 : /* -------------------------------------------------------------------- */
    1501                 : /*      Convert from src (dst) pixel/line to src (dst)                  */
    1502                 : /*      georeferenced coordinates.                                      */
    1503                 : /* -------------------------------------------------------------------- */
    1504          298117 :     if( bDstToSrc )
    1505                 :     {
    1506          176605 :         padfGeoTransform = psInfo->adfDstGeoTransform;
    1507          176605 :         pGCPTransformArg = psInfo->pDstGCPTransformArg;
    1508          176605 :         pRPCTransformArg = NULL;
    1509          176605 :         pTPSTransformArg = NULL;
    1510          176605 :         pGeoLocTransformArg = NULL;
    1511                 :     }
    1512                 :     else
    1513                 :     {
    1514          121512 :         padfGeoTransform = psInfo->adfSrcGeoTransform;
    1515          121512 :         pGCPTransformArg = psInfo->pSrcGCPTransformArg;
    1516          121512 :         pRPCTransformArg = psInfo->pSrcRPCTransformArg;
    1517          121512 :         pTPSTransformArg = psInfo->pSrcTPSTransformArg;
    1518          121512 :         pGeoLocTransformArg = psInfo->pSrcGeoLocTransformArg;
    1519                 :     }
    1520                 : 
    1521          298117 :     if( pGCPTransformArg != NULL )
    1522                 :     {
    1523            5801 :         if( !GDALGCPTransform( pGCPTransformArg, FALSE, 
    1524                 :                                nPointCount, padfX, padfY, padfZ,
    1525                 :                                panSuccess ) )
    1526               0 :             return FALSE;
    1527                 :     }
    1528          292316 :     else if( pTPSTransformArg != NULL )
    1529                 :     {
    1530             889 :         if( !GDALTPSTransform( pTPSTransformArg, FALSE, 
    1531                 :                                nPointCount, padfX, padfY, padfZ,
    1532                 :                                panSuccess ) )
    1533               0 :             return FALSE;
    1534                 :     }
    1535          291427 :     else if( pRPCTransformArg != NULL )
    1536                 :     {
    1537               4 :         if( !GDALRPCTransform( pRPCTransformArg, FALSE, 
    1538                 :                                nPointCount, padfX, padfY, padfZ,
    1539                 :                                panSuccess ) )
    1540               0 :             return FALSE;
    1541                 :     }
    1542          291423 :     else if( pGeoLocTransformArg != NULL )
    1543                 :     {
    1544               1 :         if( !GDALGeoLocTransform( pGeoLocTransformArg, FALSE, 
    1545                 :                                   nPointCount, padfX, padfY, padfZ,
    1546                 :                                   panSuccess ) )
    1547               0 :             return FALSE;
    1548                 :     }
    1549                 :     else 
    1550                 :     {
    1551         7925547 :         for( i = 0; i < nPointCount; i++ )
    1552                 :         {
    1553                 :             double dfNewX, dfNewY;
    1554                 :             
    1555         7634125 :             if( padfX[i] == HUGE_VAL || padfY[i] == HUGE_VAL )
    1556                 :             {
    1557             657 :                 panSuccess[i] = FALSE;
    1558             657 :                 continue;
    1559                 :             }
    1560                 : 
    1561         7633468 :             dfNewX = padfGeoTransform[0]
    1562         7633468 :                 + padfX[i] * padfGeoTransform[1]
    1563         7633468 :                 + padfY[i] * padfGeoTransform[2];
    1564         7633468 :             dfNewY = padfGeoTransform[3]
    1565         7633468 :                 + padfX[i] * padfGeoTransform[4]
    1566         7633468 :                 + padfY[i] * padfGeoTransform[5];
    1567                 :             
    1568         7633468 :             padfX[i] = dfNewX;
    1569         7633468 :             padfY[i] = dfNewY;
    1570                 :         }
    1571                 :     }
    1572                 : 
    1573                 : /* -------------------------------------------------------------------- */
    1574                 : /*      Reproject if needed.                                            */
    1575                 : /* -------------------------------------------------------------------- */
    1576          298117 :     if( psInfo->pReprojectArg )
    1577                 :     {
    1578           34968 :         if( !GDALReprojectionTransform( psInfo->pReprojectArg, bDstToSrc, 
    1579                 :                                         nPointCount, padfX, padfY, padfZ,
    1580                 :                                         panSuccess ) )
    1581               0 :             return FALSE;
    1582                 :     }
    1583                 :     else
    1584                 :     {
    1585         3991153 :         for( i = 0; i < nPointCount; i++ )
    1586         3728004 :             panSuccess[i] = 1;
    1587                 :     }
    1588                 : 
    1589                 : /* -------------------------------------------------------------------- */
    1590                 : /*      Convert dst (src) georef coordinates back to pixel/line.        */
    1591                 : /* -------------------------------------------------------------------- */
    1592          298117 :     if( bDstToSrc )
    1593                 :     {
    1594          176605 :         padfGeoTransform = psInfo->adfSrcInvGeoTransform;
    1595          176605 :         pGCPTransformArg = psInfo->pSrcGCPTransformArg;
    1596          176605 :         pRPCTransformArg = psInfo->pSrcRPCTransformArg;
    1597          176605 :         pTPSTransformArg = psInfo->pSrcTPSTransformArg;
    1598          176605 :         pGeoLocTransformArg = psInfo->pSrcGeoLocTransformArg;
    1599                 :     }
    1600                 :     else
    1601                 :     {
    1602          121512 :         padfGeoTransform = psInfo->adfDstInvGeoTransform;
    1603          121512 :         pGCPTransformArg = psInfo->pDstGCPTransformArg;
    1604          121512 :         pRPCTransformArg = NULL;
    1605          121512 :         pTPSTransformArg = NULL;
    1606          121512 :         pGeoLocTransformArg = NULL;
    1607                 :     }
    1608                 :         
    1609          298117 :     if( pGCPTransformArg != NULL )
    1610                 :     {
    1611            6406 :         if( !GDALGCPTransform( pGCPTransformArg, TRUE,
    1612                 :                                nPointCount, padfX, padfY, padfZ,
    1613                 :                                panSuccess ) )
    1614               0 :             return FALSE;
    1615                 :     }
    1616          291711 :     else if( pTPSTransformArg != NULL )
    1617                 :     {
    1618             931 :         if( !GDALTPSTransform( pTPSTransformArg, TRUE,
    1619                 :                                nPointCount, padfX, padfY, padfZ,
    1620                 :                                panSuccess ) )
    1621               0 :             return FALSE;
    1622                 :     }
    1623          290780 :     else if( pRPCTransformArg != NULL )
    1624                 :     {
    1625               4 :         if( !GDALRPCTransform( pRPCTransformArg, TRUE,
    1626                 :                                nPointCount, padfX, padfY, padfZ,
    1627                 :                                panSuccess ) )
    1628               0 :             return FALSE;
    1629                 :     }
    1630          290776 :     else if( pGeoLocTransformArg != NULL )
    1631                 :     {
    1632             259 :         if( !GDALGeoLocTransform( pGeoLocTransformArg, TRUE,
    1633                 :                                   nPointCount, padfX, padfY, padfZ,
    1634                 :                                   panSuccess ) )
    1635               0 :             return FALSE;
    1636                 :     }
    1637                 :     else
    1638                 :     {
    1639         7784980 :         for( i = 0; i < nPointCount; i++ )
    1640                 :         {
    1641                 :             double dfNewX, dfNewY;
    1642                 : 
    1643         7494463 :             if( !panSuccess[i] )
    1644          159983 :                 continue;
    1645                 :             
    1646         7334480 :             dfNewX = padfGeoTransform[0]
    1647         7334480 :                 + padfX[i] * padfGeoTransform[1]
    1648         7334480 :                 + padfY[i] * padfGeoTransform[2];
    1649         7334480 :             dfNewY = padfGeoTransform[3]
    1650         7334480 :                 + padfX[i] * padfGeoTransform[4]
    1651         7334480 :                 + padfY[i] * padfGeoTransform[5];
    1652                 :             
    1653         7334480 :             padfX[i] = dfNewX;
    1654         7334480 :             padfY[i] = dfNewY;
    1655                 :         }
    1656                 :     }
    1657                 :         
    1658          298117 :     return TRUE;
    1659                 : }
    1660                 : 
    1661                 : /************************************************************************/
    1662                 : /*                 GDALSerializeGenImgProjTransformer()                 */
    1663                 : /************************************************************************/
    1664                 : 
    1665                 : static CPLXMLNode *
    1666               5 : GDALSerializeGenImgProjTransformer( void *pTransformArg )
    1667                 : 
    1668                 : {
    1669                 :     char szWork[200];
    1670                 :     CPLXMLNode *psTree;
    1671                 :     GDALGenImgProjTransformInfo *psInfo = 
    1672               5 :         (GDALGenImgProjTransformInfo *) pTransformArg;
    1673                 : 
    1674               5 :     psTree = CPLCreateXMLNode( NULL, CXT_Element, "GenImgProjTransformer" );
    1675                 : 
    1676                 : /* -------------------------------------------------------------------- */
    1677                 : /*      Handle GCP transformation.                                      */
    1678                 : /* -------------------------------------------------------------------- */
    1679               5 :     if( psInfo->pSrcGCPTransformArg != NULL )
    1680                 :     {
    1681                 :         CPLXMLNode *psTransformerContainer;
    1682                 :         CPLXMLNode *psTransformer;
    1683                 : 
    1684                 :         psTransformerContainer = 
    1685               3 :             CPLCreateXMLNode( psTree, CXT_Element, "SrcGCPTransformer" );
    1686                 : 
    1687                 :         psTransformer = GDALSerializeTransformer( GDALGCPTransform,
    1688               3 :                                                   psInfo->pSrcGCPTransformArg);
    1689               3 :         if( psTransformer != NULL )
    1690               3 :             CPLAddXMLChild( psTransformerContainer, psTransformer );
    1691                 :     }
    1692                 : 
    1693                 : /* -------------------------------------------------------------------- */
    1694                 : /*      Handle TPS transformation.                                      */
    1695                 : /* -------------------------------------------------------------------- */
    1696               2 :     else if( psInfo->pSrcTPSTransformArg != NULL )
    1697                 :     {
    1698                 :         CPLXMLNode *psTransformerContainer;
    1699                 :         CPLXMLNode *psTransformer;
    1700                 : 
    1701                 :         psTransformerContainer = 
    1702               1 :             CPLCreateXMLNode( psTree, CXT_Element, "SrcTPSTransformer" );
    1703                 : 
    1704                 :         psTransformer = 
    1705               1 :             GDALSerializeTransformer( NULL, psInfo->pSrcTPSTransformArg);
    1706               1 :         if( psTransformer != NULL )
    1707               1 :             CPLAddXMLChild( psTransformerContainer, psTransformer );
    1708                 :     }
    1709                 : 
    1710                 : /* -------------------------------------------------------------------- */
    1711                 : /*      Handle GeoLoc transformation.                                   */
    1712                 : /* -------------------------------------------------------------------- */
    1713               1 :     else if( psInfo->pSrcGeoLocTransformArg != NULL )
    1714                 :     {
    1715                 :         CPLXMLNode *psTransformerContainer;
    1716                 :         CPLXMLNode *psTransformer;
    1717                 : 
    1718                 :         psTransformerContainer = 
    1719               0 :             CPLCreateXMLNode( psTree, CXT_Element, "SrcGeoLocTransformer" );
    1720                 : 
    1721                 :         psTransformer = 
    1722               0 :             GDALSerializeTransformer( NULL, psInfo->pSrcGeoLocTransformArg);
    1723               0 :         if( psTransformer != NULL )
    1724               0 :             CPLAddXMLChild( psTransformerContainer, psTransformer );
    1725                 :     }
    1726                 : 
    1727                 : /* -------------------------------------------------------------------- */
    1728                 : /*      Handle RPC transformation.                                      */
    1729                 : /* -------------------------------------------------------------------- */
    1730               1 :     else if( psInfo->pSrcRPCTransformArg != NULL )
    1731                 :     {
    1732                 :         CPLXMLNode *psTransformerContainer;
    1733                 :         CPLXMLNode *psTransformer;
    1734                 : 
    1735                 :         psTransformerContainer = 
    1736               0 :             CPLCreateXMLNode( psTree, CXT_Element, "SrcRPCTransformer" );
    1737                 : 
    1738                 :         psTransformer = 
    1739               0 :             GDALSerializeTransformer( NULL, psInfo->pSrcRPCTransformArg);
    1740               0 :         if( psTransformer != NULL )
    1741               0 :             CPLAddXMLChild( psTransformerContainer, psTransformer );
    1742                 :     }
    1743                 : 
    1744                 : /* -------------------------------------------------------------------- */
    1745                 : /*      Handle source geotransforms.                                    */
    1746                 : /* -------------------------------------------------------------------- */
    1747                 :     else
    1748                 :     {
    1749                 :         sprintf( szWork, "%.16g,%.16g,%.16g,%.16g,%.16g,%.16g", 
    1750                 :                  psInfo->adfSrcGeoTransform[0],
    1751                 :                  psInfo->adfSrcGeoTransform[1],
    1752                 :                  psInfo->adfSrcGeoTransform[2],
    1753                 :                  psInfo->adfSrcGeoTransform[3],
    1754                 :                  psInfo->adfSrcGeoTransform[4],
    1755               1 :                  psInfo->adfSrcGeoTransform[5] );
    1756               1 :         CPLCreateXMLElementAndValue( psTree, "SrcGeoTransform", szWork );
    1757                 :         
    1758                 :         sprintf( szWork, "%.16g,%.16g,%.16g,%.16g,%.16g,%.16g", 
    1759                 :                  psInfo->adfSrcInvGeoTransform[0],
    1760                 :                  psInfo->adfSrcInvGeoTransform[1],
    1761                 :                  psInfo->adfSrcInvGeoTransform[2],
    1762                 :                  psInfo->adfSrcInvGeoTransform[3],
    1763                 :                  psInfo->adfSrcInvGeoTransform[4],
    1764               1 :                  psInfo->adfSrcInvGeoTransform[5] );
    1765               1 :         CPLCreateXMLElementAndValue( psTree, "SrcInvGeoTransform", szWork );
    1766                 :     }
    1767                 :     
    1768                 : /* -------------------------------------------------------------------- */
    1769                 : /*      Handle destination geotransforms.                               */
    1770                 : /* -------------------------------------------------------------------- */
    1771                 :     sprintf( szWork, "%.16g,%.16g,%.16g,%.16g,%.16g,%.16g", 
    1772                 :              psInfo->adfDstGeoTransform[0],
    1773                 :              psInfo->adfDstGeoTransform[1],
    1774                 :              psInfo->adfDstGeoTransform[2],
    1775                 :              psInfo->adfDstGeoTransform[3],
    1776                 :              psInfo->adfDstGeoTransform[4],
    1777               5 :              psInfo->adfDstGeoTransform[5] );
    1778               5 :     CPLCreateXMLElementAndValue( psTree, "DstGeoTransform", szWork );
    1779                 :     
    1780                 :     sprintf( szWork, "%.16g,%.16g,%.16g,%.16g,%.16g,%.16g", 
    1781                 :              psInfo->adfDstInvGeoTransform[0],
    1782                 :              psInfo->adfDstInvGeoTransform[1],
    1783                 :              psInfo->adfDstInvGeoTransform[2],
    1784                 :              psInfo->adfDstInvGeoTransform[3],
    1785                 :              psInfo->adfDstInvGeoTransform[4],
    1786               5 :              psInfo->adfDstInvGeoTransform[5] );
    1787               5 :     CPLCreateXMLElementAndValue( psTree, "DstInvGeoTransform", szWork );
    1788                 : 
    1789                 : /* -------------------------------------------------------------------- */
    1790                 : /*      Do we have a reprojection transformer?                          */
    1791                 : /* -------------------------------------------------------------------- */
    1792               5 :     if( psInfo->pReprojectArg != NULL )
    1793                 :     {
    1794                 :         CPLXMLNode *psTransformerContainer;
    1795                 :         CPLXMLNode *psTransformer;
    1796                 : 
    1797                 :         psTransformerContainer = 
    1798               0 :             CPLCreateXMLNode( psTree, CXT_Element, "ReprojectTransformer" );
    1799                 : 
    1800                 :         psTransformer = GDALSerializeTransformer( GDALReprojectionTransform,
    1801               0 :                                                   psInfo->pReprojectArg );
    1802               0 :         if( psTransformer != NULL )
    1803               0 :             CPLAddXMLChild( psTransformerContainer, psTransformer );
    1804                 :     }
    1805                 :     
    1806               5 :     return psTree;
    1807                 : }
    1808                 : 
    1809                 : /************************************************************************/
    1810                 : /*                GDALDeserializeGenImgProjTransformer()                */
    1811                 : /************************************************************************/
    1812                 : 
    1813              40 : void *GDALDeserializeGenImgProjTransformer( CPLXMLNode *psTree )
    1814                 : 
    1815                 : {
    1816                 :     GDALGenImgProjTransformInfo *psInfo;
    1817                 :     CPLXMLNode *psSubtree;
    1818                 : 
    1819                 : /* -------------------------------------------------------------------- */
    1820                 : /*      Initialize the transform info.                                  */
    1821                 : /* -------------------------------------------------------------------- */
    1822                 :     psInfo = (GDALGenImgProjTransformInfo *) 
    1823              40 :         CPLCalloc(sizeof(GDALGenImgProjTransformInfo),1);
    1824                 : 
    1825              40 :     strcpy( psInfo->sTI.szSignature, "GTI" );
    1826              40 :     psInfo->sTI.pszClassName = "GDALGenImgProjTransformer";
    1827              40 :     psInfo->sTI.pfnTransform = GDALGenImgProjTransform;
    1828              40 :     psInfo->sTI.pfnCleanup = GDALDestroyGenImgProjTransformer;
    1829              40 :     psInfo->sTI.pfnSerialize = GDALSerializeGenImgProjTransformer;
    1830                 : 
    1831                 : /* -------------------------------------------------------------------- */
    1832                 : /*      SrcGeotransform                                                 */
    1833                 : /* -------------------------------------------------------------------- */
    1834              40 :     if( CPLGetXMLNode( psTree, "SrcGeoTransform" ) != NULL )
    1835                 :     {
    1836                 :         sscanf( CPLGetXMLValue( psTree, "SrcGeoTransform", "" ), 
    1837                 :                 "%lg,%lg,%lg,%lg,%lg,%lg", 
    1838                 :                 psInfo->adfSrcGeoTransform + 0,
    1839                 :                 psInfo->adfSrcGeoTransform + 1,
    1840                 :                 psInfo->adfSrcGeoTransform + 2,
    1841                 :                 psInfo->adfSrcGeoTransform + 3,
    1842                 :                 psInfo->adfSrcGeoTransform + 4,
    1843              33 :                 psInfo->adfSrcGeoTransform + 5 );
    1844                 : 
    1845              33 :         if( CPLGetXMLNode( psTree, "SrcInvGeoTransform" ) != NULL )
    1846                 :         {
    1847                 :             sscanf( CPLGetXMLValue( psTree, "SrcInvGeoTransform", "" ), 
    1848                 :                     "%lg,%lg,%lg,%lg,%lg,%lg", 
    1849                 :                     psInfo->adfSrcInvGeoTransform + 0,
    1850                 :                     psInfo->adfSrcInvGeoTransform + 1,
    1851                 :                     psInfo->adfSrcInvGeoTransform + 2,
    1852                 :                     psInfo->adfSrcInvGeoTransform + 3,
    1853                 :                     psInfo->adfSrcInvGeoTransform + 4,
    1854              33 :                     psInfo->adfSrcInvGeoTransform + 5 );
    1855                 :             
    1856                 :         }
    1857                 :         else
    1858                 :             GDALInvGeoTransform( psInfo->adfSrcGeoTransform,
    1859               0 :                                  psInfo->adfSrcInvGeoTransform );
    1860                 :     }
    1861                 : 
    1862                 : /* -------------------------------------------------------------------- */
    1863                 : /*      Src GCP Transform                                               */
    1864                 : /* -------------------------------------------------------------------- */
    1865              40 :     psSubtree = CPLGetXMLNode( psTree, "SrcGCPTransformer" );
    1866              40 :     if( psSubtree != NULL && psSubtree->psChild != NULL )
    1867                 :     {
    1868                 :         psInfo->pSrcGCPTransformArg = 
    1869               5 :             GDALDeserializeGCPTransformer( psSubtree->psChild );
    1870                 :     }
    1871                 : 
    1872                 : /* -------------------------------------------------------------------- */
    1873                 : /*      Src TPS Transform                                               */
    1874                 : /* -------------------------------------------------------------------- */
    1875              40 :     psSubtree = CPLGetXMLNode( psTree, "SrcTPSTransformer" );
    1876              40 :     if( psSubtree != NULL && psSubtree->psChild != NULL )
    1877                 :     {
    1878                 :         psInfo->pSrcTPSTransformArg = 
    1879               1 :             GDALDeserializeTPSTransformer( psSubtree->psChild );
    1880                 :     }
    1881                 : 
    1882                 : /* -------------------------------------------------------------------- */
    1883                 : /*      Src GeoLoc Transform                                            */
    1884                 : /* -------------------------------------------------------------------- */
    1885              40 :     psSubtree = CPLGetXMLNode( psTree, "SrcGeoLocTransformer" );
    1886              40 :     if( psSubtree != NULL && psSubtree->psChild != NULL )
    1887                 :     {
    1888                 :         psInfo->pSrcGeoLocTransformArg = 
    1889               1 :             GDALDeserializeGeoLocTransformer( psSubtree->psChild );
    1890                 :     }
    1891                 : 
    1892                 : /* -------------------------------------------------------------------- */
    1893                 : /*      Src RPC Transform                                               */
    1894                 : /* -------------------------------------------------------------------- */
    1895              40 :     psSubtree = CPLGetXMLNode( psTree, "SrcRPCTransformer" );
    1896              40 :     if( psSubtree != NULL && psSubtree->psChild != NULL )
    1897                 :     {
    1898                 :         psInfo->pSrcRPCTransformArg = 
    1899               0 :             GDALDeserializeRPCTransformer( psSubtree->psChild );
    1900                 :     }
    1901                 : 
    1902                 : /* -------------------------------------------------------------------- */
    1903                 : /*      DstGeotransform                                                 */
    1904                 : /* -------------------------------------------------------------------- */
    1905              40 :     if( CPLGetXMLNode( psTree, "DstGeoTransform" ) != NULL )
    1906                 :     {
    1907                 :         sscanf( CPLGetXMLValue( psTree, "DstGeoTransform", "" ), 
    1908                 :                 "%lg,%lg,%lg,%lg,%lg,%lg", 
    1909                 :                 psInfo->adfDstGeoTransform + 0,
    1910                 :                 psInfo->adfDstGeoTransform + 1,
    1911                 :                 psInfo->adfDstGeoTransform + 2,
    1912                 :                 psInfo->adfDstGeoTransform + 3,
    1913                 :                 psInfo->adfDstGeoTransform + 4,
    1914              40 :                 psInfo->adfDstGeoTransform + 5 );
    1915                 : 
    1916              40 :         if( CPLGetXMLNode( psTree, "DstInvGeoTransform" ) != NULL )
    1917                 :         {
    1918                 :             sscanf( CPLGetXMLValue( psTree, "DstInvGeoTransform", "" ), 
    1919                 :                     "%lg,%lg,%lg,%lg,%lg,%lg", 
    1920                 :                     psInfo->adfDstInvGeoTransform + 0,
    1921                 :                     psInfo->adfDstInvGeoTransform + 1,
    1922                 :                     psInfo->adfDstInvGeoTransform + 2,
    1923                 :                     psInfo->adfDstInvGeoTransform + 3,
    1924                 :                     psInfo->adfDstInvGeoTransform + 4,
    1925              24 :                     psInfo->adfDstInvGeoTransform + 5 );
    1926                 :             
    1927                 :         }
    1928                 :         else
    1929                 :             GDALInvGeoTransform( psInfo->adfDstGeoTransform,
    1930              16 :                                  psInfo->adfDstInvGeoTransform );
    1931                 :     }
    1932                 :     
    1933                 : /* -------------------------------------------------------------------- */
    1934                 : /*      Reproject transformer                                           */
    1935                 : /* -------------------------------------------------------------------- */
    1936              40 :     psSubtree = CPLGetXMLNode( psTree, "ReprojectTransformer" );
    1937              40 :     if( psSubtree != NULL && psSubtree->psChild != NULL )
    1938                 :     {
    1939                 :         psInfo->pReprojectArg = 
    1940               8 :             GDALDeserializeReprojectionTransformer( psSubtree->psChild );
    1941                 :     }
    1942                 : 
    1943              40 :     return psInfo;
    1944                 : }
    1945                 : 
    1946                 : /************************************************************************/
    1947                 : /* ==================================================================== */
    1948                 : /*       GDALReprojectionTransformer                    */
    1949                 : /* ==================================================================== */
    1950                 : /************************************************************************/
    1951                 : 
    1952                 : typedef struct {
    1953                 :     GDALTransformerInfo sTI;
    1954                 : 
    1955                 :     OGRCoordinateTransformation *poForwardTransform;
    1956                 :     OGRCoordinateTransformation *poReverseTransform;
    1957                 : } GDALReprojectionTransformInfo;
    1958                 : 
    1959                 : /************************************************************************/
    1960                 : /*                 GDALCreateReprojectionTransformer()                  */
    1961                 : /************************************************************************/
    1962                 : 
    1963                 : /**
    1964                 :  * Create reprojection transformer.
    1965                 :  *
    1966                 :  * Creates a callback data structure suitable for use with 
    1967                 :  * GDALReprojectionTransformation() to represent a transformation from
    1968                 :  * one geographic or projected coordinate system to another.  On input
    1969                 :  * the coordinate systems are described in OpenGIS WKT format. 
    1970                 :  *
    1971                 :  * Internally the OGRCoordinateTransformation object is used to implement
    1972                 :  * the reprojection.
    1973                 :  *
    1974                 :  * @param pszSrcWKT the coordinate system for the source coordinate system.
    1975                 :  * @param pszDstWKT the coordinate system for the destination coordinate 
    1976                 :  * system.
    1977                 :  *
    1978                 :  * @return Handle for use with GDALReprojectionTransform(), or NULL if the 
    1979                 :  * system fails to initialize the reprojection. 
    1980                 :  **/
    1981                 : 
    1982              81 : void *GDALCreateReprojectionTransformer( const char *pszSrcWKT, 
    1983                 :                                          const char *pszDstWKT )
    1984                 : 
    1985                 : {
    1986              81 :     OGRSpatialReference oSrcSRS, oDstSRS;
    1987                 :     OGRCoordinateTransformation *poForwardTransform;
    1988                 : 
    1989                 : /* -------------------------------------------------------------------- */
    1990                 : /*      Ingest the SRS definitions.                                     */
    1991                 : /* -------------------------------------------------------------------- */
    1992              81 :     if( oSrcSRS.importFromWkt( (char **) &pszSrcWKT ) != OGRERR_NONE )
    1993                 :     {
    1994                 :         CPLError( CE_Failure, CPLE_AppDefined, 
    1995                 :                   "Failed to import coordinate system `%s'.", 
    1996               0 :                   pszSrcWKT );
    1997               0 :         return NULL;
    1998                 :     }
    1999              81 :     if( oDstSRS.importFromWkt( (char **) &pszDstWKT ) != OGRERR_NONE )
    2000                 :     {
    2001                 :         CPLError( CE_Failure, CPLE_AppDefined, 
    2002                 :                   "Failed to import coordinate system `%s'.", 
    2003               0 :                   pszSrcWKT );
    2004               0 :         return NULL;
    2005                 :     }
    2006                 : 
    2007                 : /* -------------------------------------------------------------------- */
    2008                 : /*      Build the forward coordinate transformation.                    */
    2009                 : /* -------------------------------------------------------------------- */
    2010              81 :     poForwardTransform = OGRCreateCoordinateTransformation(&oSrcSRS,&oDstSRS);
    2011                 : 
    2012              81 :     if( poForwardTransform == NULL )
    2013                 :         // OGRCreateCoordinateTransformation() will report errors on its own.
    2014               0 :         return NULL;
    2015                 : 
    2016                 : /* -------------------------------------------------------------------- */
    2017                 : /*      Create a structure to hold the transform info, and also         */
    2018                 : /*      build reverse transform.  We assume that if the forward         */
    2019                 : /*      transform can be created, then so can the reverse one.          */
    2020                 : /* -------------------------------------------------------------------- */
    2021                 :     GDALReprojectionTransformInfo *psInfo;
    2022                 : 
    2023                 :     psInfo = (GDALReprojectionTransformInfo *) 
    2024              81 :         CPLCalloc(sizeof(GDALReprojectionTransformInfo),1);
    2025                 : 
    2026              81 :     psInfo->poForwardTransform = poForwardTransform;
    2027                 :     psInfo->poReverseTransform = 
    2028              81 :         OGRCreateCoordinateTransformation(&oDstSRS,&oSrcSRS);
    2029                 : 
    2030              81 :     strcpy( psInfo->sTI.szSignature, "GTI" );
    2031              81 :     psInfo->sTI.pszClassName = "GDALReprojectionTransformer";
    2032              81 :     psInfo->sTI.pfnTransform = GDALReprojectionTransform;
    2033              81 :     psInfo->sTI.pfnCleanup = GDALDestroyReprojectionTransformer;
    2034              81 :     psInfo->sTI.pfnSerialize = GDALSerializeReprojectionTransformer;
    2035                 : 
    2036              81 :     return psInfo;
    2037                 : }
    2038                 : 
    2039                 : /************************************************************************/
    2040                 : /*                 GDALDestroyReprojectionTransformer()                 */
    2041                 : /************************************************************************/
    2042                 : 
    2043                 : /**
    2044                 :  * Destroy reprojection transformation.
    2045                 :  *
    2046                 :  * @param pTransformArg the transformation handle returned by
    2047                 :  * GDALCreateReprojectionTransformer().
    2048                 :  */
    2049                 : 
    2050              81 : void GDALDestroyReprojectionTransformer( void *pTransformArg )
    2051                 : 
    2052                 : {
    2053              81 :     VALIDATE_POINTER0( pTransformArg, "GDALDestroyReprojectionTransformer" );
    2054                 : 
    2055                 :     GDALReprojectionTransformInfo *psInfo = 
    2056              81 :         (GDALReprojectionTransformInfo *) pTransformArg;    
    2057                 : 
    2058              81 :     if( psInfo->poForwardTransform )
    2059              81 :         delete psInfo->poForwardTransform;
    2060                 : 
    2061              81 :     if( psInfo->poReverseTransform )
    2062              81 :         delete psInfo->poReverseTransform;
    2063                 : 
    2064              81 :     CPLFree( psInfo );
    2065                 : }
    2066                 : 
    2067                 : /************************************************************************/
    2068                 : /*                     GDALReprojectionTransform()                      */
    2069                 : /************************************************************************/
    2070                 : 
    2071                 : /**
    2072                 :  * Perform reprojection transformation.
    2073                 :  *
    2074                 :  * Actually performs the reprojection transformation described in 
    2075                 :  * GDALCreateReprojectionTransformer().  This function matches the 
    2076                 :  * GDALTransformerFunc() signature.  Details of the arguments are described
    2077                 :  * there. 
    2078                 :  */
    2079                 : 
    2080           34968 : int GDALReprojectionTransform( void *pTransformArg, int bDstToSrc, 
    2081                 :                                 int nPointCount, 
    2082                 :                                 double *padfX, double *padfY, double *padfZ,
    2083                 :                                 int *panSuccess )
    2084                 : 
    2085                 : {
    2086                 :     GDALReprojectionTransformInfo *psInfo = 
    2087           34968 :         (GDALReprojectionTransformInfo *) pTransformArg;    
    2088                 :     int bSuccess;
    2089                 : 
    2090           34968 :     if( bDstToSrc )
    2091                 :         bSuccess = psInfo->poReverseTransform->TransformEx( 
    2092           28840 :             nPointCount, padfX, padfY, padfZ, panSuccess );
    2093                 :     else
    2094                 :         bSuccess = psInfo->poForwardTransform->TransformEx( 
    2095            6128 :             nPointCount, padfX, padfY, padfZ, panSuccess );
    2096                 : 
    2097           34968 :     return bSuccess;
    2098                 : }
    2099                 : 
    2100                 : /************************************************************************/
    2101                 : /*                GDALSerializeReprojectionTransformer()                */
    2102                 : /************************************************************************/
    2103                 : 
    2104                 : static CPLXMLNode *
    2105              28 : GDALSerializeReprojectionTransformer( void *pTransformArg )
    2106                 : 
    2107                 : {
    2108                 :     CPLXMLNode *psTree;
    2109                 :     GDALReprojectionTransformInfo *psInfo = 
    2110              28 :         (GDALReprojectionTransformInfo *) pTransformArg;
    2111                 : 
    2112              28 :     psTree = CPLCreateXMLNode( NULL, CXT_Element, "ReprojectionTransformer" );
    2113                 : 
    2114                 : /* -------------------------------------------------------------------- */
    2115                 : /*      Handle SourceCS.                                                */
    2116                 : /* -------------------------------------------------------------------- */
    2117                 :     OGRSpatialReference *poSRS;
    2118              28 :     char *pszWKT = NULL;
    2119                 : 
    2120              28 :     poSRS = psInfo->poForwardTransform->GetSourceCS();
    2121              28 :     poSRS->exportToWkt( &pszWKT );
    2122              28 :     CPLCreateXMLElementAndValue( psTree, "SourceSRS", pszWKT );
    2123              28 :     CPLFree( pszWKT );
    2124                 : 
    2125                 : /* -------------------------------------------------------------------- */
    2126                 : /*      Handle DestinationCS.                                           */
    2127                 : /* -------------------------------------------------------------------- */
    2128              28 :     poSRS = psInfo->poForwardTransform->GetTargetCS();
    2129              28 :     poSRS->exportToWkt( &pszWKT );
    2130              28 :     CPLCreateXMLElementAndValue( psTree, "TargetSRS", pszWKT );
    2131              28 :     CPLFree( pszWKT );
    2132                 : 
    2133              28 :     return psTree;
    2134                 : }
    2135                 : 
    2136                 : /************************************************************************/
    2137                 : /*               GDALDeserializeReprojectionTransformer()               */
    2138                 : /************************************************************************/
    2139                 : 
    2140                 : static void *
    2141              36 : GDALDeserializeReprojectionTransformer( CPLXMLNode *psTree )
    2142                 : 
    2143                 : {
    2144              36 :     const char *pszSourceSRS = CPLGetXMLValue( psTree, "SourceSRS", NULL );
    2145              36 :     const char *pszTargetSRS= CPLGetXMLValue( psTree, "TargetSRS", NULL );
    2146              36 :     char *pszSourceWKT = NULL, *pszTargetWKT = NULL;
    2147              36 :     void *pResult = NULL;
    2148                 : 
    2149              36 :     if( pszSourceSRS != NULL )
    2150                 :     {
    2151              36 :         OGRSpatialReference oSRS;
    2152                 : 
    2153              36 :         if( oSRS.SetFromUserInput( pszSourceSRS ) == OGRERR_NONE )
    2154              36 :             oSRS.exportToWkt( &pszSourceWKT );
    2155                 :     }
    2156                 : 
    2157              36 :     if( pszTargetSRS != NULL )
    2158                 :     {
    2159              36 :         OGRSpatialReference oSRS;
    2160                 : 
    2161              36 :         if( oSRS.SetFromUserInput( pszTargetSRS ) == OGRERR_NONE )
    2162              36 :             oSRS.exportToWkt( &pszTargetWKT );
    2163                 :     }
    2164                 : 
    2165              72 :     if( pszSourceWKT != NULL && pszTargetWKT != NULL )
    2166                 :     {
    2167                 :         pResult = GDALCreateReprojectionTransformer( pszSourceWKT,
    2168              36 :                                                      pszTargetWKT );
    2169                 :     }
    2170                 :     else
    2171                 :     {
    2172                 :         CPLError( CE_Failure, CPLE_AppDefined, 
    2173                 :                   "ReprojectionTransformer definition missing either\n"
    2174               0 :                   "SourceSRS or TargetSRS definition." );
    2175                 :     }
    2176                 : 
    2177              36 :     CPLFree( pszSourceWKT );
    2178              36 :     CPLFree( pszTargetWKT );
    2179                 : 
    2180              36 :     return pResult;
    2181                 : }
    2182                 : 
    2183                 : /************************************************************************/
    2184                 : /* ==================================================================== */
    2185                 : /*      Approximate transformer.                                        */
    2186                 : /* ==================================================================== */
    2187                 : /************************************************************************/
    2188                 : 
    2189                 : typedef struct 
    2190                 : {
    2191                 :     GDALTransformerInfo sTI;
    2192                 : 
    2193                 :     GDALTransformerFunc pfnBaseTransformer;
    2194                 :     void             *pBaseCBData;
    2195                 :     double        dfMaxError;
    2196                 : 
    2197                 :     int               bOwnSubtransformer;
    2198                 : } ApproxTransformInfo;
    2199                 : 
    2200                 : /************************************************************************/
    2201                 : /*                   GDALSerializeApproxTransformer()                   */
    2202                 : /************************************************************************/
    2203                 : 
    2204                 : static CPLXMLNode *
    2205               2 : GDALSerializeApproxTransformer( void *pTransformArg )
    2206                 : 
    2207                 : {
    2208                 :     CPLXMLNode *psTree;
    2209               2 :     ApproxTransformInfo *psInfo = (ApproxTransformInfo *) pTransformArg;
    2210                 : 
    2211               2 :     psTree = CPLCreateXMLNode( NULL, CXT_Element, "ApproxTransformer" );
    2212                 : 
    2213                 : /* -------------------------------------------------------------------- */
    2214                 : /*      Attach max error.                                               */
    2215                 : /* -------------------------------------------------------------------- */
    2216                 :     CPLCreateXMLElementAndValue( psTree, "MaxError", 
    2217               2 :                                  CPLString().Printf("%g",psInfo->dfMaxError) );
    2218                 : 
    2219                 : /* -------------------------------------------------------------------- */
    2220                 : /*      Capture underlying transformer.                                 */
    2221                 : /* -------------------------------------------------------------------- */
    2222                 :     CPLXMLNode *psTransformerContainer;
    2223                 :     CPLXMLNode *psTransformer;
    2224                 : 
    2225                 :     psTransformerContainer = 
    2226               2 :         CPLCreateXMLNode( psTree, CXT_Element, "BaseTransformer" );
    2227                 :     
    2228                 :     psTransformer = GDALSerializeTransformer( psInfo->pfnBaseTransformer,
    2229               2 :                                               psInfo->pBaseCBData );
    2230               2 :     if( psTransformer != NULL )
    2231               2 :         CPLAddXMLChild( psTransformerContainer, psTransformer );
    2232                 : 
    2233               2 :     return psTree;
    2234                 : }
    2235                 : 
    2236                 : /************************************************************************/
    2237                 : /*                    GDALCreateApproxTransformer()                     */
    2238                 : /************************************************************************/
    2239                 : 
    2240                 : /**
    2241                 :  * Create an approximating transformer.
    2242                 :  *
    2243                 :  * This function creates a context for an approximated transformer.  Basically
    2244                 :  * a high precision transformer is supplied as input and internally linear
    2245                 :  * approximations are computed to generate results to within a defined
    2246                 :  * precision. 
    2247                 :  *
    2248                 :  * The approximation is actually done at the point where GDALApproxTransform()
    2249                 :  * calls are made, and depend on the assumption that the roughly linear.  The
    2250                 :  * first and last point passed in must be the extreme values and the 
    2251                 :  * intermediate values should describe a curve between the end points.  The
    2252                 :  * approximator transforms and center using the approximate transformer, and
    2253                 :  * then compares the true middle transformed value to a linear approximation
    2254                 :  * based on the end points.  If the error is within the supplied threshold
    2255                 :  * then the end points are used to linearly approximate all the values 
    2256                 :  * otherwise the inputs points are split into two smaller sets, and the
    2257                 :  * function recursively called till a sufficiently small set of points if found
    2258                 :  * that the linear approximation is OK, or that all the points are exactly
    2259                 :  * computed. 
    2260                 :  *
    2261                 :  * This function is very suitable for approximating transformation results
    2262                 :  * from output pixel/line space to input coordinates for warpers that operate
    2263                 :  * on one input scanline at a time.  Care should be taken using it in other
    2264                 :  * circumstances as little internal validation is done, in order to keep things
    2265                 :  * fast. 
    2266                 :  *
    2267                 :  * @param pfnBaseTransformer the high precision transformer which should be
    2268                 :  * approximated. 
    2269                 :  * @param pBaseTransformArg the callback argument for the high precision 
    2270                 :  * transformer. 
    2271                 :  * @param dfMaxError the maximum cartesian error in the "output" space that
    2272                 :  * is to be accepted in the linear approximation.
    2273                 :  * 
    2274                 :  * @return callback pointer suitable for use with GDALApproxTransform().  It
    2275                 :  * should be deallocated with GDALDestroyApproxTransformer().
    2276                 :  */
    2277                 : 
    2278             289 : void *GDALCreateApproxTransformer( GDALTransformerFunc pfnBaseTransformer,
    2279                 :                                    void *pBaseTransformArg, double dfMaxError)
    2280                 : 
    2281                 : {
    2282                 :     ApproxTransformInfo *psATInfo;
    2283                 : 
    2284             289 :     psATInfo = (ApproxTransformInfo*) CPLMalloc(sizeof(ApproxTransformInfo));
    2285             289 :     psATInfo->pfnBaseTransformer = pfnBaseTransformer;
    2286             289 :     psATInfo->pBaseCBData = pBaseTransformArg;
    2287             289 :     psATInfo->dfMaxError = dfMaxError;
    2288             289 :     psATInfo->bOwnSubtransformer = FALSE;
    2289                 : 
    2290             289 :     strcpy( psATInfo->sTI.szSignature, "GTI" );
    2291             289 :     psATInfo->sTI.pszClassName = "GDALApproxTransformer";
    2292             289 :     psATInfo->sTI.pfnTransform = GDALApproxTransform;
    2293             289 :     psATInfo->sTI.pfnCleanup = GDALDestroyApproxTransformer;
    2294             289 :     psATInfo->sTI.pfnSerialize = GDALSerializeApproxTransformer;
    2295                 : 
    2296             289 :     return psATInfo;
    2297                 : }
    2298                 : 
    2299                 : /************************************************************************/
    2300                 : /*              GDALApproxTransformerOwnsSubtransformer()               */
    2301                 : /************************************************************************/
    2302                 : 
    2303              21 : void GDALApproxTransformerOwnsSubtransformer( void *pCBData, int bOwnFlag )
    2304                 : 
    2305                 : {
    2306              21 :     ApproxTransformInfo *psATInfo = (ApproxTransformInfo *) pCBData;
    2307                 : 
    2308              21 :     psATInfo->bOwnSubtransformer = bOwnFlag;
    2309              21 : }
    2310                 : 
    2311                 : /************************************************************************/
    2312                 : /*                    GDALDestroyApproxTransformer()                    */
    2313                 : /************************************************************************/
    2314                 : 
    2315                 : /**
    2316                 :  * Cleanup approximate transformer.
    2317                 :  *
    2318                 :  * Deallocates the resources allocated by GDALCreateApproxTransformer().
    2319                 :  * 
    2320                 :  * @param pCBData callback data originally returned by 
    2321                 :  * GDALCreateApproxTransformer().
    2322                 :  */
    2323                 : 
    2324             289 : void GDALDestroyApproxTransformer( void * pCBData )
    2325                 : 
    2326                 : {
    2327             289 :     VALIDATE_POINTER0( pCBData, "GDALDestroyApproxTransformer" );
    2328                 : 
    2329             289 :     ApproxTransformInfo *psATInfo = (ApproxTransformInfo *) pCBData;
    2330                 : 
    2331             289 :     if( psATInfo->bOwnSubtransformer ) 
    2332              21 :         GDALDestroyTransformer( psATInfo->pBaseCBData );
    2333                 : 
    2334             289 :     CPLFree( pCBData );
    2335                 : }
    2336                 : 
    2337                 : /************************************************************************/
    2338                 : /*                        GDALApproxTransform()                         */
    2339                 : /************************************************************************/
    2340                 : 
    2341                 : /**
    2342                 :  * Perform approximate transformation.
    2343                 :  *
    2344                 :  * Actually performs the approximate transformation described in 
    2345                 :  * GDALCreateApproxTransformer().  This function matches the 
    2346                 :  * GDALTransformerFunc() signature.  Details of the arguments are described
    2347                 :  * there. 
    2348                 :  */
    2349                 :  
    2350           47511 : int GDALApproxTransform( void *pCBData, int bDstToSrc, int nPoints, 
    2351                 :                          double *x, double *y, double *z, int *panSuccess )
    2352                 : 
    2353                 : {
    2354           47511 :     ApproxTransformInfo *psATInfo = (ApproxTransformInfo *) pCBData;
    2355                 :     double x2[3], y2[3], z2[3], dfDeltaX, dfDeltaY, dfError, dfDist, dfDeltaZ;
    2356                 :     int nMiddle, anSuccess2[3], i, bSuccess;
    2357                 : 
    2358           47511 :     nMiddle = (nPoints-1)/2;
    2359                 : 
    2360                 : /* -------------------------------------------------------------------- */
    2361                 : /*      Bail if our preconditions are not met, or if error is not       */
    2362                 : /*      acceptable.                                                     */
    2363                 : /* -------------------------------------------------------------------- */
    2364          141126 :     if( y[0] != y[nPoints-1] || y[0] != y[nMiddle]
    2365           93615 :         || x[0] == x[nPoints-1] || x[0] == x[nMiddle]
    2366                 :         || psATInfo->dfMaxError == 0.0 || nPoints <= 5 )
    2367                 :     {
    2368                 :         return psATInfo->pfnBaseTransformer( psATInfo->pBaseCBData, bDstToSrc,
    2369            1006 :                                              nPoints, x, y, z, panSuccess );
    2370                 :     }
    2371                 : 
    2372                 : /* -------------------------------------------------------------------- */
    2373                 : /*      Transform first, last and middle point.                         */
    2374                 : /* -------------------------------------------------------------------- */
    2375           46505 :     x2[0] = x[0];
    2376           46505 :     y2[0] = y[0];
    2377           46505 :     z2[0] = z[0];
    2378           46505 :     x2[1] = x[nMiddle];
    2379           46505 :     y2[1] = y[nMiddle];
    2380           46505 :     z2[1] = z[nMiddle];
    2381           46505 :     x2[2] = x[nPoints-1];
    2382           46505 :     y2[2] = y[nPoints-1];
    2383           46505 :     z2[2] = z[nPoints-1];
    2384                 : 
    2385                 :     bSuccess = 
    2386                 :         psATInfo->pfnBaseTransformer( psATInfo->pBaseCBData, bDstToSrc, 3, 
    2387           46505 :                                       x2, y2, z2, anSuccess2 );
    2388           46505 :     if( !bSuccess || !anSuccess2[0] || !anSuccess2[1] || !anSuccess2[2] )
    2389                 :         return psATInfo->pfnBaseTransformer( psATInfo->pBaseCBData, bDstToSrc,
    2390            1446 :                                              nPoints, x, y, z, panSuccess );
    2391                 :     
    2392                 : /* -------------------------------------------------------------------- */
    2393                 : /*      Is the error at the middle acceptable relative to an            */
    2394                 : /*      interpolation of the middle position?                           */
    2395                 : /* -------------------------------------------------------------------- */
    2396           45059 :     dfDeltaX = (x2[2] - x2[0]) / (x[nPoints-1] - x[0]);
    2397           45059 :     dfDeltaY = (y2[2] - y2[0]) / (x[nPoints-1] - x[0]);
    2398           45059 :     dfDeltaZ = (z2[2] - z2[0]) / (x[nPoints-1] - x[0]);
    2399                 : 
    2400           90118 :     dfError = fabs((x2[0] + dfDeltaX * (x[nMiddle] - x[0])) - x2[1])
    2401           90118 :         + fabs((y2[0] + dfDeltaY * (x[nMiddle] - x[0])) - y2[1]);
    2402                 : 
    2403           45059 :     if( dfError > psATInfo->dfMaxError )
    2404                 :     {
    2405                 : #ifdef notdef
    2406                 :         CPLDebug( "GDAL", "ApproxTransformer - "
    2407                 :                   "error %g over threshold %g, subdivide %d points.",
    2408                 :                   dfError, psATInfo->dfMaxError, nPoints );
    2409                 : #endif
    2410                 : 
    2411                 :         bSuccess = 
    2412                 :             GDALApproxTransform( psATInfo, bDstToSrc, nMiddle, 
    2413            7295 :                                  x, y, z, panSuccess );
    2414                 :             
    2415            7295 :         if( !bSuccess )
    2416               0 :             return FALSE;
    2417                 : 
    2418                 :         bSuccess = 
    2419                 :             GDALApproxTransform( psATInfo, bDstToSrc, nPoints - nMiddle,
    2420                 :                                  x+nMiddle, y+nMiddle, z+nMiddle,
    2421            7295 :                                  panSuccess+nMiddle );
    2422                 : 
    2423            7295 :         if( !bSuccess )
    2424               0 :             return FALSE;
    2425                 : 
    2426            7295 :         return TRUE;
    2427                 :     }
    2428                 : 
    2429                 : /* -------------------------------------------------------------------- */
    2430                 : /*      Error is OK since this is just used to compute output bounds    */
    2431                 : /*      of newly created file for gdalwarper.  So just use affine       */
    2432                 : /*      approximation of the reverse transform.  Eventually we          */
    2433                 : /*      should implement iterative searching to find a result within    */
    2434                 : /*      our error threshold.                                            */
    2435                 : /* -------------------------------------------------------------------- */
    2436         8559810 :     for( i = nPoints-1; i >= 0; i-- )
    2437                 :     {
    2438         8522046 :         dfDist = (x[i] - x[0]);
    2439         8522046 :         y[i] = y2[0] + dfDeltaY * dfDist;
    2440         8522046 :         x[i] = x2[0] + dfDeltaX * dfDist;
    2441         8522046 :         z[i] = z2[0] + dfDeltaZ * dfDist;
    2442         8522046 :         panSuccess[i] = TRUE;
    2443                 :     }
    2444                 :     
    2445           37764 :     return TRUE;
    2446                 : }
    2447                 : 
    2448                 : /************************************************************************/
    2449                 : /*                  GDALDeserializeApproxTransformer()                  */
    2450                 : /************************************************************************/
    2451                 : 
    2452                 : static void *
    2453              20 : GDALDeserializeApproxTransformer( CPLXMLNode *psTree )
    2454                 : 
    2455                 : {
    2456              20 :     double dfMaxError = atof(CPLGetXMLValue( psTree, "MaxError",  "0.25" ));
    2457                 :     CPLXMLNode *psContainer;
    2458              20 :     GDALTransformerFunc pfnBaseTransform = NULL;
    2459              20 :     void *pBaseCBData = NULL;
    2460                 : 
    2461              20 :     psContainer = CPLGetXMLNode( psTree, "BaseTransformer" );
    2462                 : 
    2463              20 :     if( psContainer != NULL && psContainer->psChild != NULL )
    2464                 :     {
    2465                 :         GDALDeserializeTransformer( psContainer->psChild, 
    2466                 :                                     &pfnBaseTransform, 
    2467              20 :                                     &pBaseCBData );
    2468                 :         
    2469                 :     }
    2470                 :     
    2471              20 :     if( pfnBaseTransform == NULL )
    2472                 :     {
    2473                 :         CPLError( CE_Failure, CPLE_AppDefined,
    2474               0 :                   "Cannot get base transform for approx transformer." );
    2475               0 :         return NULL;
    2476                 :     }
    2477                 :     else
    2478                 :     {
    2479                 :         void *pApproxCBData = GDALCreateApproxTransformer( pfnBaseTransform,
    2480                 :                                                            pBaseCBData, 
    2481              20 :                                                            dfMaxError );
    2482              20 :         GDALApproxTransformerOwnsSubtransformer( pApproxCBData, TRUE );
    2483                 : 
    2484              20 :         return pApproxCBData;
    2485                 :     }
    2486                 : }
    2487                 : 
    2488                 : /************************************************************************/
    2489                 : /*                       GDALApplyGeoTransform()                        */
    2490                 : /************************************************************************/
    2491                 : 
    2492                 : /**
    2493                 :  * Apply GeoTransform to x/y coordinate.
    2494                 :  *
    2495                 :  * Applies the following computation, converting a (pixel,line) coordinate
    2496                 :  * into a georeferenced (geo_x,geo_y) location. 
    2497                 :  *
    2498                 :  *  *pdfGeoX = padfGeoTransform[0] + dfPixel * padfGeoTransform[1]
    2499                 :  *                                 + dfLine  * padfGeoTransform[2];
    2500                 :  *  *pdfGeoY = padfGeoTransform[3] + dfPixel * padfGeoTransform[4]
    2501                 :  *                                 + dfLine  * padfGeoTransform[5];
    2502                 :  *
    2503                 :  * @param padfGeoTransform Six coefficient GeoTransform to apply.
    2504                 :  * @param dfPixel Input pixel position.
    2505                 :  * @param dfLine Input line position. 
    2506                 :  * @param *pdfGeoX output location where geo_x (easting/longitude) location is placed.
    2507                 :  * @param *pdfGeoY output location where geo_y (northing/latitude) location is placed.
    2508                 :  */
    2509                 : 
    2510               3 : void CPL_STDCALL GDALApplyGeoTransform( double *padfGeoTransform, 
    2511                 :                             double dfPixel, double dfLine, 
    2512                 :                             double *pdfGeoX, double *pdfGeoY )
    2513                 : {
    2514               3 :     *pdfGeoX = padfGeoTransform[0] + dfPixel * padfGeoTransform[1]
    2515               3 :                                    + dfLine  * padfGeoTransform[2];
    2516               3 :     *pdfGeoY = padfGeoTransform[3] + dfPixel * padfGeoTransform[4]
    2517               3 :                                    + dfLine  * padfGeoTransform[5];
    2518               3 : }
    2519                 : 
    2520                 : /************************************************************************/
    2521                 : /*                        GDALInvGeoTransform()                         */
    2522                 : /************************************************************************/
    2523                 : 
    2524                 : /**
    2525                 :  * Invert Geotransform.
    2526                 :  *
    2527                 :  * This function will invert a standard 3x2 set of GeoTransform coefficients.
    2528                 :  * This converts the equation from being pixel to geo to being geo to pixel. 
    2529                 :  *
    2530                 :  * @param gt_in Input geotransform (six doubles - unaltered).
    2531                 :  * @param gt_out Output geotransform (six doubles - updated). 
    2532                 :  *
    2533                 :  * @return TRUE on success or FALSE if the equation is uninvertable. 
    2534                 :  */
    2535                 : 
    2536             641 : int CPL_STDCALL GDALInvGeoTransform( double *gt_in, double *gt_out )
    2537                 : 
    2538                 : {
    2539                 :     double  det, inv_det;
    2540                 : 
    2541                 :     /* we assume a 3rd row that is [1 0 0] */
    2542                 : 
    2543                 :     /* Compute determinate */
    2544                 : 
    2545             641 :     det = gt_in[1] * gt_in[5] - gt_in[2] * gt_in[4];
    2546                 : 
    2547             641 :     if( fabs(det) < 0.000000000000001 )
    2548               0 :         return 0;
    2549                 : 
    2550             641 :     inv_det = 1.0 / det;
    2551                 : 
    2552                 :     /* compute adjoint, and devide by determinate */
    2553                 : 
    2554             641 :     gt_out[1] =  gt_in[5] * inv_det;
    2555             641 :     gt_out[4] = -gt_in[4] * inv_det;
    2556                 : 
    2557             641 :     gt_out[2] = -gt_in[2] * inv_det;
    2558             641 :     gt_out[5] =  gt_in[1] * inv_det;
    2559                 : 
    2560             641 :     gt_out[0] = ( gt_in[2] * gt_in[3] - gt_in[0] * gt_in[5]) * inv_det;
    2561             641 :     gt_out[3] = (-gt_in[1] * gt_in[3] + gt_in[0] * gt_in[4]) * inv_det;
    2562                 : 
    2563             641 :     return 1;
    2564                 : }
    2565                 : 
    2566                 : /************************************************************************/
    2567                 : /*                      GDALSerializeTransformer()                      */
    2568                 : /************************************************************************/
    2569                 : 
    2570              11 : CPLXMLNode *GDALSerializeTransformer( GDALTransformerFunc pfnFunc,
    2571                 :                                       void *pTransformArg )
    2572                 : 
    2573                 : {
    2574              11 :     VALIDATE_POINTER1( pTransformArg, "GDALSerializeTransformer", NULL );
    2575                 : 
    2576              11 :     GDALTransformerInfo *psInfo = static_cast<GDALTransformerInfo *>(pTransformArg);
    2577                 : 
    2578              11 :     if( psInfo == NULL || !EQUAL(psInfo->szSignature,"GTI") )
    2579                 :     {
    2580                 :         CPLError( CE_Failure, CPLE_AppDefined,
    2581               0 :                   "Attempt to serialize non-GTI transformer." );
    2582               0 :         return NULL;
    2583                 :     }
    2584              11 :     else if ( psInfo->pfnSerialize == NULL )
    2585                 :     {
    2586                 :         CPLError( CE_Failure, CPLE_AppDefined,
    2587               0 :                   "No serialization function available for this transformer." );
    2588               0 :         return NULL;
    2589                 :     }
    2590                 :     else
    2591                 :     {
    2592              11 :         return psInfo->pfnSerialize( pTransformArg );
    2593                 :     }
    2594                 : }
    2595                 : 
    2596                 : /************************************************************************/
    2597                 : /*                  GDALRegisterTransformDeserializer()                 */
    2598                 : /************************************************************************/
    2599                 : 
    2600                 : static CPLList* psListDeserializer = NULL;
    2601                 : static void* hDeserializerMutex = NULL;
    2602                 : 
    2603                 : typedef struct
    2604                 : {
    2605                 :     char* pszTransformName;
    2606                 :     GDALTransformerFunc pfnTransformerFunc;
    2607                 :     GDALTransformDeserializeFunc pfnDeserializeFunc;
    2608                 : } TransformDeserializerInfo;
    2609                 : 
    2610             537 : void* GDALRegisterTransformDeserializer(const char* pszTransformName,
    2611                 :                                        GDALTransformerFunc pfnTransformerFunc,
    2612                 :                                        GDALTransformDeserializeFunc pfnDeserializeFunc)
    2613                 : {
    2614                 :     TransformDeserializerInfo* psInfo =
    2615             537 :         (TransformDeserializerInfo*)CPLMalloc(sizeof(TransformDeserializerInfo));
    2616             537 :     psInfo->pszTransformName = CPLStrdup(pszTransformName);
    2617             537 :     psInfo->pfnTransformerFunc = pfnTransformerFunc;
    2618             537 :     psInfo->pfnDeserializeFunc = pfnDeserializeFunc;
    2619                 : 
    2620             537 :     CPLMutexHolderD(&hDeserializerMutex);
    2621             537 :     psListDeserializer = CPLListInsert(psListDeserializer, psInfo, 0);
    2622                 : 
    2623             537 :     return psInfo;
    2624                 : }
    2625                 : 
    2626                 : /************************************************************************/
    2627                 : /*                GDALUnregisterTransformDeserializer()                 */
    2628                 : /************************************************************************/
    2629                 : 
    2630             522 : void GDALUnregisterTransformDeserializer(void* pData)
    2631                 : {
    2632             522 :     CPLMutexHolderD(&hDeserializerMutex);
    2633             522 :     CPLList* psList = psListDeserializer;
    2634             522 :     CPLList* psLast = NULL;
    2635            1044 :     while(psList)
    2636                 :     {
    2637             522 :         if (psList->pData == pData)
    2638                 :         {
    2639                 :             TransformDeserializerInfo* psInfo =
    2640             522 :                     (TransformDeserializerInfo*)pData;
    2641             522 :             CPLFree(psInfo->pszTransformName);
    2642             522 :             CPLFree(pData);
    2643             522 :             if (psLast)
    2644               0 :                 psLast->psNext = psList->psNext;
    2645                 :             else
    2646             522 :                 psListDeserializer = NULL;
    2647             522 :             CPLFree(psList);
    2648             522 :             break;
    2649                 :         }
    2650               0 :         psLast = psList;
    2651               0 :         psList = psList->psNext;
    2652             522 :     }
    2653             522 : }
    2654                 : 
    2655                 : 
    2656                 : /************************************************************************/
    2657                 : /*                     GDALDeserializeTransformer()                     */
    2658                 : /************************************************************************/
    2659                 : 
    2660              60 : CPLErr GDALDeserializeTransformer( CPLXMLNode *psTree, 
    2661                 :                                    GDALTransformerFunc *ppfnFunc,
    2662                 :                                    void **ppTransformArg )
    2663                 : 
    2664                 : {
    2665              60 :     *ppfnFunc = NULL;
    2666              60 :     *ppTransformArg = NULL;
    2667                 : 
    2668              60 :     CPLErrorReset();
    2669                 : 
    2670              60 :     if( psTree == NULL || psTree->eType != CXT_Element )
    2671                 :         CPLError( CE_Failure, CPLE_AppDefined, 
    2672               0 :                   "Malformed element in GDALDeserializeTransformer" );
    2673              60 :     else if( EQUAL(psTree->pszValue,"GenImgProjTransformer") )
    2674                 :     {
    2675              40 :         *ppfnFunc = GDALGenImgProjTransform;
    2676              40 :         *ppTransformArg = GDALDeserializeGenImgProjTransformer( psTree );
    2677                 :     }
    2678              20 :     else if( EQUAL(psTree->pszValue,"ReprojectionTransformer") )
    2679                 :     {
    2680               0 :         *ppfnFunc = GDALReprojectionTransform;
    2681               0 :         *ppTransformArg = GDALDeserializeReprojectionTransformer( psTree );
    2682                 :     }
    2683              20 :     else if( EQUAL(psTree->pszValue,"GCPTransformer") )
    2684                 :     {
    2685               0 :         *ppfnFunc = GDALGCPTransform;
    2686               0 :         *ppTransformArg = GDALDeserializeGCPTransformer( psTree );
    2687                 :     }
    2688              20 :     else if( EQUAL(psTree->pszValue,"TPSTransformer") )
    2689                 :     {
    2690               0 :         *ppfnFunc = GDALTPSTransform;
    2691               0 :         *ppTransformArg = GDALDeserializeTPSTransformer( psTree );
    2692                 :     }
    2693              20 :     else if( EQUAL(psTree->pszValue,"GeoLocTransformer") )
    2694                 :     {
    2695               0 :         *ppfnFunc = GDALGeoLocTransform;
    2696               0 :         *ppTransformArg = GDALDeserializeGeoLocTransformer( psTree );
    2697                 :     }
    2698              20 :     else if( EQUAL(psTree->pszValue,"RPCTransformer") )
    2699                 :     {
    2700               0 :         *ppfnFunc = GDALRPCTransform;
    2701               0 :         *ppTransformArg = GDALDeserializeRPCTransformer( psTree );
    2702                 :     }
    2703              20 :     else if( EQUAL(psTree->pszValue,"ApproxTransformer") )
    2704                 :     {
    2705              20 :         *ppfnFunc = GDALApproxTransform;
    2706              20 :         *ppTransformArg = GDALDeserializeApproxTransformer( psTree );
    2707                 :     }
    2708                 :     else
    2709                 :     {
    2710               0 :         GDALTransformDeserializeFunc pfnDeserializeFunc = NULL;
    2711                 :         {
    2712               0 :             CPLMutexHolderD(&hDeserializerMutex);
    2713               0 :             CPLList* psList = psListDeserializer;
    2714               0 :             while(psList)
    2715                 :             {
    2716                 :                 TransformDeserializerInfo* psInfo =
    2717               0 :                             (TransformDeserializerInfo*)psList->pData;
    2718               0 :                 if (strcmp(psInfo->pszTransformName, psTree->pszValue) == 0)
    2719                 :                 {
    2720               0 :                     *ppfnFunc = psInfo->pfnTransformerFunc;
    2721               0 :                     pfnDeserializeFunc = psInfo->pfnDeserializeFunc;
    2722               0 :                     break;
    2723                 :                 }
    2724               0 :                 psList = psList->psNext;
    2725               0 :             }
    2726                 :         }
    2727                 : 
    2728               0 :         if (pfnDeserializeFunc != NULL)
    2729                 :         {
    2730               0 :             *ppTransformArg = pfnDeserializeFunc( psTree );
    2731                 :         }
    2732                 :         else
    2733                 :         {
    2734                 :             CPLError( CE_Failure, CPLE_AppDefined,
    2735                 :                     "Unrecognised element '%s' GDALDeserializeTransformer",
    2736               0 :                     psTree->pszValue );
    2737                 :         }
    2738                 :     }
    2739                 : 
    2740              60 :     return CPLGetLastErrorType();
    2741                 : }
    2742                 : 
    2743                 : /************************************************************************/
    2744                 : /*                       GDALDestroyTransformer()                       */
    2745                 : /************************************************************************/
    2746                 : 
    2747              96 : void GDALDestroyTransformer( void *pTransformArg )
    2748                 : 
    2749                 : {
    2750              96 :     GDALTransformerInfo *psInfo = (GDALTransformerInfo *) pTransformArg;
    2751                 : 
    2752              96 :     if( psInfo == NULL || !EQUAL(psInfo->szSignature,"GTI") )
    2753                 :     {
    2754                 :         CPLError( CE_Failure, CPLE_AppDefined,
    2755               0 :                   "Attempt to destroy non-GTI transformer." );
    2756                 :     }
    2757                 :     else
    2758              96 :         psInfo->pfnCleanup( pTransformArg );
    2759              96 : }
    2760                 : 
    2761                 : /************************************************************************/
    2762                 : /*                         GDALUseTransformer()                         */
    2763                 : /************************************************************************/
    2764                 : 
    2765              17 : int GDALUseTransformer( void *pTransformArg,
    2766                 :                         int bDstToSrc, int nPointCount, 
    2767                 :                         double *x, double *y, double *z, 
    2768                 :                         int *panSuccess )
    2769                 : {
    2770              17 :     GDALTransformerInfo *psInfo = (GDALTransformerInfo *) pTransformArg;
    2771                 : 
    2772              17 :     if( psInfo == NULL || !EQUAL(psInfo->szSignature,"GTI") )
    2773                 :     {
    2774                 :         CPLError( CE_Failure, CPLE_AppDefined,
    2775               0 :                   "Attempt to use non-GTI transformer." );
    2776               0 :         return FALSE;
    2777                 :     }
    2778                 :     else
    2779                 :         return psInfo->pfnTransform( pTransformArg, bDstToSrc, nPointCount, 
    2780              17 :                                      x, y, z, panSuccess );
    2781                 : }
    2782                 : 

Generated by: LCOV version 1.7