Changeset 14776

Show
Ignore:
Timestamp:
06/27/08 23:37:52 (2 months ago)
Author:
warmerdam
Message:

fix up a few details

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/gdal/alg/gdalproximity.cpp

    r14774 r14776  
    3636static CPLErr 
    3737ProcessProximityLine( GInt32 *panSrcScanline, int *panNearX, int *panNearY,  
    38                       int bForward, int iLine, int nXSize, int nMaxDist, 
     38                      int bForward, int iLine, int nXSize, double nMaxDist, 
    3939                      float *pafProximity, 
    4040                      int nTargetValues, int *panTargetValues ); 
     
    8282The NODATA value to use on the output band for pixels that are 
    8383beyond MAXDIST.  If not provided, the hProximityBand will be 
    84 queried for a nodata value.  If one is not found, 255 will be used. 
     84queried for a nodata value.  If one is not found, 65535 will be used. 
    8585 
    8686  FIXED_BUF_VAL=n 
     
    9999 
    100100{ 
    101     int nMaxDist, nXSize, nYSize, i
     101    int nXSize, nYSize, i, bFixedBufVal = FALSE
    102102    const char *pszOpt; 
     103    double dfMaxDist; 
     104    double dfFixedBufVal; 
    103105 
    104106    VALIDATE_POINTER1( hSrcBand, "GDALComputeProximity", CE_Failure ); 
     
    109111 
    110112/* -------------------------------------------------------------------- */ 
     113/*      Are we using pixels or georeferenced coordinates for distances? */ 
     114/* -------------------------------------------------------------------- */ 
     115    double dfDistMult = 1.0; 
     116    pszOpt = CSLFetchNameValue( papszOptions, "DISTUNITS" ); 
     117    if( pszOpt ) 
     118    { 
     119        if( EQUAL(pszOpt,"GEO") ) 
     120        { 
     121            GDALDatasetH hSrcDS = GDALGetBandDataset( hSrcBand ); 
     122            if( hSrcDS ) 
     123            { 
     124                double adfGeoTransform[6]; 
     125 
     126                GDALGetGeoTransform( hSrcDS, adfGeoTransform ); 
     127                if( ABS(adfGeoTransform[1]) != ABS(adfGeoTransform[5]) ) 
     128                    CPLError( CE_Warning, CPLE_AppDefined, 
     129                              "Pixels not square, distances will be inaccurate." ); 
     130                dfDistMult = ABS(adfGeoTransform[1]); 
     131            } 
     132        } 
     133        else if( !EQUAL(pszOpt,"PIXEL") ) 
     134        { 
     135            CPLError( CE_Failure, CPLE_AppDefined, 
     136                      "Unrecognised DISTUNITS value '%s', should be GEO or PIXEL.", 
     137                      pszOpt ); 
     138            return CE_Failure; 
     139        } 
     140    } 
     141 
     142/* -------------------------------------------------------------------- */ 
    111143/*      What is our maxdist value?                                      */ 
    112144/* -------------------------------------------------------------------- */ 
    113145    pszOpt = CSLFetchNameValue( papszOptions, "MAXDIST" ); 
    114146    if( pszOpt ) 
    115         nMaxDist = atoi(pszOpt)
     147        dfMaxDist = atof(pszOpt) / dfDistMult
    116148    else 
    117         nMaxDist = GDALGetRasterXSize(hSrcBand) + GDALGetRasterYSize(hSrcBand); 
     149        dfMaxDist = GDALGetRasterXSize(hSrcBand) + GDALGetRasterYSize(hSrcBand); 
    118150 
    119151/* -------------------------------------------------------------------- */ 
     
    128160                  "Source and proximity bands are not the same size." ); 
    129161        return CE_Failure; 
     162    } 
     163 
     164/* -------------------------------------------------------------------- */ 
     165/*      Get output NODATA value.                                        */ 
     166/* -------------------------------------------------------------------- */ 
     167    float fNoDataValue; 
     168    pszOpt = CSLFetchNameValue( papszOptions, "NODATA" ); 
     169    if( pszOpt != NULL ) 
     170        fNoDataValue = atof(pszOpt); 
     171    else 
     172    { 
     173        int bSuccess; 
     174 
     175        fNoDataValue = GDALGetRasterNoDataValue( hProximityBand, &bSuccess ); 
     176        if( !bSuccess ) 
     177            fNoDataValue = 65535.0; 
     178    } 
     179     
     180/* -------------------------------------------------------------------- */ 
     181/*      Is there a fixed value we wish to force the buffer area to?     */ 
     182/* -------------------------------------------------------------------- */ 
     183    pszOpt = CSLFetchNameValue( papszOptions, "FIXED_BUF_VAL" ); 
     184    if( pszOpt ) 
     185    { 
     186        dfFixedBufVal = atof(pszOpt); 
     187        bFixedBufVal = TRUE; 
    130188    } 
    131189 
     
    184242 
    185243/* -------------------------------------------------------------------- */ 
    186 /*      Loop forward over all the lines.                                */ 
     244/*      Loop from top to bottom of the image.                           */ 
    187245/* -------------------------------------------------------------------- */ 
    188246    int iLine; 
     
    194252    for( iLine = 0; eErr == CE_None && iLine < nYSize; iLine++ ) 
    195253    { 
     254        // Read for target values. 
    196255        eErr = GDALRasterIO( hSrcBand, GF_Read, 0, iLine, nXSize, 1,  
    197256                             panSrcScanline, nXSize, 1, GDT_Int32, 0, 0 ); 
     
    202261            pafProximity[i] = -1.0; 
    203262 
     263        // Left to right 
    204264        ProcessProximityLine( panSrcScanline, panNearX, panNearY,  
    205                               TRUE, iLine, nXSize, nMaxDist, 
     265                              TRUE, iLine, nXSize, dfMaxDist, 
    206266                              pafProximity, nTargetValues, panTargetValues ); 
    207267 
     268        // Right to Left 
    208269        ProcessProximityLine( panSrcScanline, panNearX, panNearY,  
    209                               FALSE, iLine, nXSize, nMaxDist, 
     270                              FALSE, iLine, nXSize, dfMaxDist, 
    210271                              pafProximity, nTargetValues, panTargetValues ); 
    211272 
     
    227288 
    228289/* -------------------------------------------------------------------- */ 
    229 /*      Loop backward over all the lines.                               */ 
     290/*      Loop from bottom to top of the image.                           */ 
    230291/* -------------------------------------------------------------------- */ 
    231292    for( i = 0; i < nXSize; i++ ) 
     
    249310            break; 
    250311 
    251         // Process backwards. 
     312        // Right to left 
    252313        ProcessProximityLine( panSrcScanline, panNearX, panNearY,  
    253                               FALSE, iLine, nXSize, nMaxDist, 
     314                              FALSE, iLine, nXSize, dfMaxDist, 
    254315                              pafProximity, nTargetValues, panTargetValues ); 
    255316 
    256         // Process backwards. 
     317        // Left to right 
    257318        ProcessProximityLine( panSrcScanline, panNearX, panNearY,  
    258                               TRUE, iLine, nXSize, nMaxDist, 
     319                              TRUE, iLine, nXSize, dfMaxDist, 
    259320                              pafProximity, nTargetValues, panTargetValues ); 
    260321 
     322        // Final post processing of distances.  
     323        for( i = 0; i < nXSize; i++ ) 
     324        { 
     325            if( pafProximity[i] < 0.0 ) 
     326                pafProximity[i] = fNoDataValue; 
     327            else if( pafProximity[i] > 0.0 ) 
     328            { 
     329                if( bFixedBufVal ) 
     330                    pafProximity[i] = (float) dfFixedBufVal; 
     331                else  
     332                    pafProximity[i] *= dfDistMult; 
     333            } 
     334        } 
     335   
    261336        // Write out results. 
    262337        eErr =  
     
    292367static CPLErr 
    293368ProcessProximityLine( GInt32 *panSrcScanline, int *panNearX, int *panNearY,  
    294                       int bForward, int iLine, int nXSize, int nMaxDist, 
     369                      int bForward, int iLine, int nXSize, double dfMaxDist, 
    295370                      float *pafProximity, 
    296371                      int nTargetValues, int *panTargetValues ) 
     
    314389    for( iPixel = iStart; iPixel != iEnd; iPixel += iStep ) 
    315390    { 
    316         int bIsTarget
     391        int bIsTarget = FALSE
    317392 
    318393/* -------------------------------------------------------------------- */ 
     
    324399        { 
    325400            int i; 
     401 
    326402            for( i = 0; i < nTargetValues; i++ ) 
    327403            { 
     
    407483/* -------------------------------------------------------------------- */ 
    408484        if( panNearX[iPixel] != -1  
    409             && fNearDistSq <= nMaxDist * nMaxDist 
     485            && fNearDistSq <= dfMaxDist * dfMaxDist 
    410486            && (pafProximity[iPixel] < 0  
    411487                || fNearDistSq < pafProximity[iPixel] * pafProximity[iPixel]) )