Changeset 19537


Ignore:
Timestamp:
Apr 26, 2010 6:31:57 PM (6 years ago)
Author:
warmerdam
Message:

Added NITFPossibleIGEOLOReorientation() in an attempt to deal with
files written with the IGEOLO corners out of order.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gdal/frmts/nitf/nitfimage.c

    r19319 r19537  
    4949static int NITFReadGEOLOB( NITFImage *psImage );
    5050static void NITFLoadAttributeSection( NITFImage *psImage );
     51static void NITFPossibleIGEOLOReorientation( NITFImage *psImage );
    5152
    5253void NITFGetGCP ( const char* pachCoord, double *pdfXYs, int iCoord );
     
    334335        nOffset += 60;
    335336    }
     337
     338/* -------------------------------------------------------------------- */
     339/*      Should we reorient the IGEOLO points in an attempt to handle    */
     340/*      files where they were written in the wrong order?               */
     341/* -------------------------------------------------------------------- */
     342    if( psImage->bHaveIGEOLO )
     343        NITFPossibleIGEOLOReorientation( psImage );
    336344
    337345/* -------------------------------------------------------------------- */
     
    35463554    return 0;
    35473555}
     3556
     3557/************************************************************************/
     3558/*                        NITFDoLinesIntersect()                        */
     3559/************************************************************************/
     3560
     3561static int NITFDoLinesIntersect( double dfL1X1, double dfL1Y1,
     3562                                 double dfL1X2, double dfL1Y2,
     3563                                 double dfL2X1, double dfL2Y1,
     3564                                 double dfL2X2, double dfL2Y2 )
     3565
     3566{
     3567    double dfL1M, dfL1B, dfL2M, dfL2B;
     3568   
     3569    if( dfL1X1 == dfL1X2 )
     3570    {
     3571        dfL1M = 1e10;
     3572        dfL1B = 0.0;
     3573    }
     3574    else
     3575    {
     3576        dfL1M = (dfL1Y2 - dfL1Y1 ) / (dfL1X2 - dfL1X1);
     3577        dfL1B = dfL1Y2 - dfL1M * dfL1X2;
     3578    }
     3579
     3580    if( dfL2X1 == dfL2X2 )
     3581    {
     3582        dfL2M = 1e10;
     3583        dfL2B = 0.0;
     3584    }
     3585    else
     3586    {
     3587        dfL2M = (dfL2Y2 - dfL2Y1 ) / (dfL2X2 - dfL2X1);
     3588        dfL2B = dfL2Y2 - dfL2M * dfL2X2;
     3589    }
     3590   
     3591    if( dfL2M == dfL1M )
     3592    {
     3593        // parallel .. no meaningful intersection.
     3594        return FALSE;
     3595    }
     3596    else
     3597    {
     3598        double dfX, dfY;
     3599       
     3600        dfX = (dfL2B - dfL1B) / (dfL1M-dfL2M);
     3601        dfY = dfL2M * dfX + dfL2B;
     3602
     3603        /*
     3604        ** Is this intersection on the line between
     3605        ** our corner points or "out somewhere" else?
     3606        */
     3607        return ((dfX >= dfL1X1 && dfX <= dfL1X2)
     3608                || (dfX >= dfL1X2 && dfX <= dfL1X1))
     3609                && ((dfX >= dfL2X1 && dfX <= dfL2X2)
     3610                    || (dfX >= dfL2X2 && dfX <= dfL2X1));
     3611    }
     3612}
     3613
     3614/************************************************************************/
     3615/*                  NITFPossibleIGEOLOReorientation()                   */
     3616/************************************************************************/
     3617
     3618static void NITFPossibleIGEOLOReorientation( NITFImage *psImage )
     3619
     3620{
     3621/* -------------------------------------------------------------------- */
     3622/*      Check whether the vector from top left to bottom left           */
     3623/*      intersects the line from top right to bottom right.  If this    */
     3624/*      is true, then we believe the corner coordinate order was        */
     3625/*      written improperly.                                             */
     3626/* -------------------------------------------------------------------- */
     3627#if 1
     3628    if( !NITFDoLinesIntersect( psImage->dfULX, psImage->dfULY,
     3629                               psImage->dfLLX, psImage->dfLLY,
     3630                               psImage->dfURX, psImage->dfURY,
     3631                               psImage->dfLRX, psImage->dfLRY ) )
     3632        return;
     3633    else
     3634        CPLDebug( "NITF", "It appears the IGEOLO corner coordinates were written improperly!" );
     3635#endif
     3636   
     3637/* -------------------------------------------------------------------- */
     3638/*      Divide the lat/long extents of this image into four             */
     3639/*      quadrants and assign the corners based on which point falls     */
     3640/*      into which quadrant.  This is intended to correct images        */
     3641/*      with the corner points written improperly.  Unfortunately it    */
     3642/*      also breaks images which are mirrored, or rotated more than     */
     3643/*      90 degrees from simple north up.                                */
     3644/* -------------------------------------------------------------------- */
     3645    {
     3646       
     3647        double dfXMax = MAX(MAX(psImage->dfULX,psImage->dfURX),
     3648                            MAX(psImage->dfLRX,psImage->dfLLX));
     3649        double dfXMin = MIN(MIN(psImage->dfULX,psImage->dfURX),
     3650                            MIN(psImage->dfLRX,psImage->dfLLX));
     3651        double dfYMax = MAX(MAX(psImage->dfULY,psImage->dfURY),
     3652                            MAX(psImage->dfLRY,psImage->dfLLY));
     3653        double dfYMin = MIN(MIN(psImage->dfULY,psImage->dfURY),
     3654                            MIN(psImage->dfLRY,psImage->dfLLY));
     3655        double dfXPivot = (dfXMax + dfXMin) * 0.5;
     3656        double dfYPivot = (dfYMax + dfYMin) * 0.5;
     3657
     3658        double dfNewULX, dfNewULY, dfNewURX, dfNewURY,
     3659            dfNewLLX, dfNewLLY, dfNewLRX, dfNewLRY;
     3660        int bGotUL = FALSE, bGotUR = FALSE,
     3661            bGotLL = FALSE, bGotLR = FALSE;
     3662        int iCoord, bChange = FALSE;
     3663   
     3664        for( iCoord = 0; iCoord < 4; iCoord++ )
     3665        {
     3666            double *pdfXY = &(psImage->dfULX) + iCoord*2;
     3667
     3668            if( pdfXY[0] < dfXPivot && pdfXY[1] < dfYPivot )
     3669            {
     3670                bGotLL = TRUE;
     3671                dfNewLLX = pdfXY[0];
     3672                dfNewLLY = pdfXY[1];
     3673                bChange |= iCoord != 3;
     3674            }
     3675            else if( pdfXY[0] > dfXPivot && pdfXY[1] < dfYPivot )
     3676            {
     3677                bGotLR = TRUE;
     3678                dfNewLRX = pdfXY[0];
     3679                dfNewLRY = pdfXY[1];
     3680                bChange |= iCoord != 2;
     3681            }
     3682            else if( pdfXY[0] > dfXPivot && pdfXY[1] > dfYPivot )
     3683            {
     3684                bGotUR = TRUE;
     3685                dfNewURX = pdfXY[0];
     3686                dfNewURY = pdfXY[1];
     3687                bChange |= iCoord != 1;
     3688            }
     3689            else
     3690            {
     3691                bGotUL = TRUE;
     3692                dfNewULX = pdfXY[0];
     3693                dfNewULY = pdfXY[1];
     3694                bChange |= iCoord != 0;
     3695            }
     3696        }
     3697
     3698        if( !bGotUL || !bGotUR || !bGotLL || !bGotLR )
     3699        {
     3700            CPLDebug( "NITF",
     3701                      "Unable to reorient corner points sensibly in NITFPossibleIGEOLOReorganization(), discarding IGEOLO locations." );
     3702            psImage->bHaveIGEOLO = FALSE;
     3703            return;
     3704        }
     3705
     3706        if( !bChange )
     3707            return;
     3708
     3709        psImage->dfULX = dfNewULX;
     3710        psImage->dfULY = dfNewULY;
     3711        psImage->dfURX = dfNewURX;
     3712        psImage->dfURY = dfNewURY;
     3713        psImage->dfLRX = dfNewLRX;
     3714        psImage->dfLRY = dfNewLRY;
     3715        psImage->dfLLX = dfNewLLX;
     3716        psImage->dfLLY = dfNewLLY;
     3717   
     3718        CPLDebug( "NITF",
     3719                  "IGEOLO corners have been reoriented by NITFPossibleIGEOLOReorientation()." );
     3720    }
     3721}
Note: See TracChangeset for help on using the changeset viewer.