Changeset 14776
- Timestamp:
- 06/27/08 23:37:52 (2 months ago)
- Files:
-
- trunk/gdal/alg/gdalproximity.cpp (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/gdal/alg/gdalproximity.cpp
r14774 r14776 36 36 static CPLErr 37 37 ProcessProximityLine( GInt32 *panSrcScanline, int *panNearX, int *panNearY, 38 int bForward, int iLine, int nXSize, intnMaxDist,38 int bForward, int iLine, int nXSize, double nMaxDist, 39 39 float *pafProximity, 40 40 int nTargetValues, int *panTargetValues ); … … 82 82 The NODATA value to use on the output band for pixels that are 83 83 beyond MAXDIST. If not provided, the hProximityBand will be 84 queried for a nodata value. If one is not found, 255 will be used.84 queried for a nodata value. If one is not found, 65535 will be used. 85 85 86 86 FIXED_BUF_VAL=n … … 99 99 100 100 { 101 int n MaxDist, nXSize, nYSize, i;101 int nXSize, nYSize, i, bFixedBufVal = FALSE; 102 102 const char *pszOpt; 103 double dfMaxDist; 104 double dfFixedBufVal; 103 105 104 106 VALIDATE_POINTER1( hSrcBand, "GDALComputeProximity", CE_Failure ); … … 109 111 110 112 /* -------------------------------------------------------------------- */ 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 /* -------------------------------------------------------------------- */ 111 143 /* What is our maxdist value? */ 112 144 /* -------------------------------------------------------------------- */ 113 145 pszOpt = CSLFetchNameValue( papszOptions, "MAXDIST" ); 114 146 if( pszOpt ) 115 nMaxDist = atoi(pszOpt);147 dfMaxDist = atof(pszOpt) / dfDistMult; 116 148 else 117 nMaxDist = GDALGetRasterXSize(hSrcBand) + GDALGetRasterYSize(hSrcBand);149 dfMaxDist = GDALGetRasterXSize(hSrcBand) + GDALGetRasterYSize(hSrcBand); 118 150 119 151 /* -------------------------------------------------------------------- */ … … 128 160 "Source and proximity bands are not the same size." ); 129 161 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; 130 188 } 131 189 … … 184 242 185 243 /* -------------------------------------------------------------------- */ 186 /* Loop f orward over all the lines.*/244 /* Loop from top to bottom of the image. */ 187 245 /* -------------------------------------------------------------------- */ 188 246 int iLine; … … 194 252 for( iLine = 0; eErr == CE_None && iLine < nYSize; iLine++ ) 195 253 { 254 // Read for target values. 196 255 eErr = GDALRasterIO( hSrcBand, GF_Read, 0, iLine, nXSize, 1, 197 256 panSrcScanline, nXSize, 1, GDT_Int32, 0, 0 ); … … 202 261 pafProximity[i] = -1.0; 203 262 263 // Left to right 204 264 ProcessProximityLine( panSrcScanline, panNearX, panNearY, 205 TRUE, iLine, nXSize, nMaxDist,265 TRUE, iLine, nXSize, dfMaxDist, 206 266 pafProximity, nTargetValues, panTargetValues ); 207 267 268 // Right to Left 208 269 ProcessProximityLine( panSrcScanline, panNearX, panNearY, 209 FALSE, iLine, nXSize, nMaxDist,270 FALSE, iLine, nXSize, dfMaxDist, 210 271 pafProximity, nTargetValues, panTargetValues ); 211 272 … … 227 288 228 289 /* -------------------------------------------------------------------- */ 229 /* Loop backward over all the lines.*/290 /* Loop from bottom to top of the image. */ 230 291 /* -------------------------------------------------------------------- */ 231 292 for( i = 0; i < nXSize; i++ ) … … 249 310 break; 250 311 251 // Process backwards.312 // Right to left 252 313 ProcessProximityLine( panSrcScanline, panNearX, panNearY, 253 FALSE, iLine, nXSize, nMaxDist,314 FALSE, iLine, nXSize, dfMaxDist, 254 315 pafProximity, nTargetValues, panTargetValues ); 255 316 256 // Process backwards.317 // Left to right 257 318 ProcessProximityLine( panSrcScanline, panNearX, panNearY, 258 TRUE, iLine, nXSize, nMaxDist,319 TRUE, iLine, nXSize, dfMaxDist, 259 320 pafProximity, nTargetValues, panTargetValues ); 260 321 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 261 336 // Write out results. 262 337 eErr = … … 292 367 static CPLErr 293 368 ProcessProximityLine( GInt32 *panSrcScanline, int *panNearX, int *panNearY, 294 int bForward, int iLine, int nXSize, int nMaxDist,369 int bForward, int iLine, int nXSize, double dfMaxDist, 295 370 float *pafProximity, 296 371 int nTargetValues, int *panTargetValues ) … … 314 389 for( iPixel = iStart; iPixel != iEnd; iPixel += iStep ) 315 390 { 316 int bIsTarget ;391 int bIsTarget = FALSE; 317 392 318 393 /* -------------------------------------------------------------------- */ … … 324 399 { 325 400 int i; 401 326 402 for( i = 0; i < nTargetValues; i++ ) 327 403 { … … 407 483 /* -------------------------------------------------------------------- */ 408 484 if( panNearX[iPixel] != -1 409 && fNearDistSq <= nMaxDist * nMaxDist485 && fNearDistSq <= dfMaxDist * dfMaxDist 410 486 && (pafProximity[iPixel] < 0 411 487 || fNearDistSq < pafProximity[iPixel] * pafProximity[iPixel]) )
