1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
|
/******************************************************************************
* $Id: gdalcutline.cpp 33715 2016-03-13 08:52:06Z goatbar $
*
* Project: High Performance Image Reprojector
* Purpose: Implement cutline/blend mask generator.
* Author: Frank Warmerdam, warmerdam@pobox.com
*
******************************************************************************
* Copyright (c) 2008, Frank Warmerdam <warmerdam@pobox.com>
* Copyright (c) 2008-2013, Even Rouault <even dot rouault at mines-paris dot org>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "gdalwarper.h"
#include "gdal_alg.h"
#include "ogr_api.h"
#include "ogr_geos.h"
#include "ogr_geometry.h"
#include "cpl_string.h"
CPL_CVSID("$Id: gdalcutline.cpp 33715 2016-03-13 08:52:06Z goatbar $");
/************************************************************************/
/* BlendMaskGenerator() */
/************************************************************************/
static CPLErr
BlendMaskGenerator(
#ifndef HAVE_GEOS
CPL_UNUSED int nXOff, CPL_UNUSED int nYOff,
CPL_UNUSED int nXSize, CPL_UNUSED int nYSize,
CPL_UNUSED GByte *pabyPolyMask,
CPL_UNUSED float *pafValidityMask,
CPL_UNUSED OGRGeometryH hPolygon,
CPL_UNUSED double dfBlendDist
#else
int nXOff, int nYOff, int nXSize, int nYSize,
GByte *pabyPolyMask, float *pafValidityMask,
OGRGeometryH hPolygon, double dfBlendDist
#endif
)
{
#ifndef HAVE_GEOS
CPLError( CE_Failure, CPLE_AppDefined,
"Blend distance support not available without the GEOS library.");
return CE_Failure;
#else /* HAVE_GEOS */
/* -------------------------------------------------------------------- */
/* Convert the polygon into a collection of lines so that we */
/* measure distance from the edge even on the inside. */
/* -------------------------------------------------------------------- */
OGRGeometry *poLines
= OGRGeometryFactory::forceToMultiLineString(
((OGRGeometry *) hPolygon)->clone() );
/* -------------------------------------------------------------------- */
/* Prepare a clipping polygon a bit bigger than the area of */
/* interest in the hopes of simplifying the cutline down to */
/* stuff that will be relevant for this area of interest. */
/* -------------------------------------------------------------------- */
CPLString osClipRectWKT;
osClipRectWKT.Printf( "POLYGON((%g %g,%g %g,%g %g,%g %g,%g %g))",
nXOff - (dfBlendDist+1),
nYOff - (dfBlendDist+1),
nXOff + nXSize + (dfBlendDist+1),
nYOff - (dfBlendDist+1),
nXOff + nXSize + (dfBlendDist+1),
nYOff + nYSize + (dfBlendDist+1),
nXOff - (dfBlendDist+1),
nYOff + nYSize + (dfBlendDist+1),
nXOff - (dfBlendDist+1),
nYOff - (dfBlendDist+1) );
OGRPolygon *poClipRect = NULL;
char *pszWKT = (char *) osClipRectWKT.c_str();
OGRGeometryFactory::createFromWkt( &pszWKT, NULL,
(OGRGeometry**) (&poClipRect) );
if( poClipRect )
{
// If it does not intersect the polym zero the mask and return.
if ( ! ((OGRGeometry *) hPolygon)->Intersects( poClipRect ) )
{
memset( pafValidityMask, 0, sizeof(float) * nXSize * nYSize );
delete poLines;
delete poClipRect;
return CE_None;
}
// If it does not intersect the line at all just return.
else if ( ! ((OGRGeometry *) poLines)->Intersects( poClipRect ) )
{
delete poLines;
delete poClipRect;
return CE_None;
}
OGRGeometry *poClippedLines =
poLines->Intersection( poClipRect );
delete poLines;
poLines = poClippedLines;
delete poClipRect;
}
/* -------------------------------------------------------------------- */
/* Convert our polygon into GEOS format, and compute an */
/* envelope to accelerate later distance operations. */
/* -------------------------------------------------------------------- */
OGREnvelope sEnvelope;
int iXMin, iYMin, iXMax, iYMax;
GEOSContextHandle_t hGEOSCtxt = OGRGeometry::createGEOSContext();
GEOSGeom poGEOSPoly;
poGEOSPoly = poLines->exportToGEOS(hGEOSCtxt);
OGR_G_GetEnvelope( hPolygon, &sEnvelope );
delete poLines;
/***** this check was already done in the calling *****/
/***** function and should never be true *****/
/*if( sEnvelope.MinY - dfBlendDist > nYOff+nYSize
|| sEnvelope.MaxY + dfBlendDist < nYOff
|| sEnvelope.MinX - dfBlendDist > nXOff+nXSize
|| sEnvelope.MaxX + dfBlendDist < nXOff )
return CE_None;
*/
iXMin = MAX(0,(int) floor(sEnvelope.MinX - dfBlendDist - nXOff));
iXMax = MIN(nXSize, (int) ceil(sEnvelope.MaxX + dfBlendDist - nXOff));
iYMin = MAX(0,(int) floor(sEnvelope.MinY - dfBlendDist - nYOff));
iYMax = MIN(nYSize, (int) ceil(sEnvelope.MaxY + dfBlendDist - nYOff));
/* -------------------------------------------------------------------- */
/* Loop over potential area within blend line distance, */
/* processing each pixel. */
/* -------------------------------------------------------------------- */
int iY, iX;
double dfLastDist;
for( iY = 0; iY < nYSize; iY++ )
{
dfLastDist = 0.0;
for( iX = 0; iX < nXSize; iX++ )
{
if( iX < iXMin || iX >= iXMax
|| iY < iYMin || iY > iYMax
|| dfLastDist > dfBlendDist + 1.5 )
{
if( pabyPolyMask[iX + iY * nXSize] == 0 )
pafValidityMask[iX + iY * nXSize] = 0.0;
dfLastDist -= 1.0;
continue;
}
double dfDist, dfRatio;
CPLString osPointWKT;
GEOSGeom poGEOSPoint;
osPointWKT.Printf( "POINT(%d.5 %d.5)", iX + nXOff, iY + nYOff );
poGEOSPoint = GEOSGeomFromWKT_r( hGEOSCtxt, osPointWKT );
GEOSDistance_r( hGEOSCtxt, poGEOSPoly, poGEOSPoint, &dfDist );
GEOSGeom_destroy_r( hGEOSCtxt, poGEOSPoint );
dfLastDist = dfDist;
if( dfDist > dfBlendDist )
{
if( pabyPolyMask[iX + iY * nXSize] == 0 )
pafValidityMask[iX + iY * nXSize] = 0.0;
continue;
}
if( pabyPolyMask[iX + iY * nXSize] == 0 )
{
/* outside */
dfRatio = 0.5 - (dfDist / dfBlendDist) * 0.5;
}
else
{
/* inside */
dfRatio = 0.5 + (dfDist / dfBlendDist) * 0.5;
}
pafValidityMask[iX + iY * nXSize] *= (float)dfRatio;
}
}
/* -------------------------------------------------------------------- */
/* Cleanup */
/* -------------------------------------------------------------------- */
GEOSGeom_destroy_r( hGEOSCtxt, poGEOSPoly );
OGRGeometry::freeGEOSContext( hGEOSCtxt );
return CE_None;
#endif /* HAVE_GEOS */
}
/************************************************************************/
/* CutlineTransformer() */
/* */
/* A simple transformer for the cutline that just offsets */
/* relative to the current chunk. */
/************************************************************************/
static int CutlineTransformer( void *pTransformArg,
int bDstToSrc,
int nPointCount,
double *x,
double *y,
CPL_UNUSED double *z,
CPL_UNUSED int *panSuccess )
{
int nXOff = ((int *) pTransformArg)[0];
int nYOff = ((int *) pTransformArg)[1];
int i;
if( bDstToSrc )
{
nXOff *= -1;
nYOff *= -1;
}
for( i = 0; i < nPointCount; i++ )
{
x[i] -= nXOff;
y[i] -= nYOff;
}
return TRUE;
}
/************************************************************************/
/* GDALWarpCutlineMasker() */
/* */
/* This function will generate a source mask based on a */
/* provided cutline, and optional blend distance. */
/************************************************************************/
CPLErr
GDALWarpCutlineMasker( void *pMaskFuncArg,
CPL_UNUSED int nBandCount,
CPL_UNUSED GDALDataType eType,
int nXOff, int nYOff, int nXSize, int nYSize,
GByte ** /*ppImageData */,
int bMaskIsFloat, void *pValidityMask )
{
GDALWarpOptions *psWO = (GDALWarpOptions *) pMaskFuncArg;
float *pafMask = (float *) pValidityMask;
CPLErr eErr;
GDALDriverH hMemDriver;
if( nXSize < 1 || nYSize < 1 )
return CE_None;
/* -------------------------------------------------------------------- */
/* Do some minimal checking. */
/* -------------------------------------------------------------------- */
if( !bMaskIsFloat )
{
CPLAssert( FALSE );
return CE_Failure;
}
if( psWO == NULL || psWO->hCutline == NULL )
{
CPLAssert( FALSE );
return CE_Failure;
}
hMemDriver = GDALGetDriverByName("MEM");
if (hMemDriver == NULL)
{
CPLError(CE_Failure, CPLE_AppDefined, "GDALWarpCutlineMasker needs MEM driver");
return CE_Failure;
}
/* -------------------------------------------------------------------- */
/* Check the polygon. */
/* -------------------------------------------------------------------- */
OGRGeometryH hPolygon = (OGRGeometryH) psWO->hCutline;
OGREnvelope sEnvelope;
if( wkbFlatten(OGR_G_GetGeometryType(hPolygon)) != wkbPolygon
&& wkbFlatten(OGR_G_GetGeometryType(hPolygon)) != wkbMultiPolygon )
{
CPLAssert( FALSE );
return CE_Failure;
}
OGR_G_GetEnvelope( hPolygon, &sEnvelope );
if( sEnvelope.MaxX + psWO->dfCutlineBlendDist < nXOff
|| sEnvelope.MinX - psWO->dfCutlineBlendDist > nXOff + nXSize
|| sEnvelope.MaxY + psWO->dfCutlineBlendDist < nYOff
|| sEnvelope.MinY - psWO->dfCutlineBlendDist > nYOff + nYSize )
{
// We are far from the blend line - everything is masked to zero.
// It would be nice to realize no work is required for this whole
// chunk!
memset( pafMask, 0, sizeof(float) * nXSize * nYSize );
return CE_None;
}
/* -------------------------------------------------------------------- */
/* Create a byte buffer into which we can burn the */
/* mask polygon and wrap it up as a memory dataset. */
/* -------------------------------------------------------------------- */
GByte *pabyPolyMask = (GByte *) CPLCalloc( nXSize, nYSize );
GDALDatasetH hMemDS;
double adfGeoTransform[6] = { 0.0, 1.0, 0.0, 0.0, 0.0, 1.0 };
char szDataPointer[100];
char *apszOptions[] = { szDataPointer, NULL };
memset( szDataPointer, 0, sizeof(szDataPointer) );
snprintf( szDataPointer, sizeof(szDataPointer), "DATAPOINTER=" );
CPLPrintPointer( szDataPointer+strlen(szDataPointer),
pabyPolyMask,
static_cast<int>(sizeof(szDataPointer) - strlen(szDataPointer)) );
hMemDS = GDALCreate( hMemDriver, "warp_temp",
nXSize, nYSize, 0, GDT_Byte, NULL );
GDALAddBand( hMemDS, GDT_Byte, apszOptions );
GDALSetGeoTransform( hMemDS, adfGeoTransform );
/* -------------------------------------------------------------------- */
/* Burn the polygon into the mask with 1.0 values. */
/* -------------------------------------------------------------------- */
int nTargetBand = 1;
double dfBurnValue = 255.0;
int anXYOff[2];
char **papszRasterizeOptions = NULL;
if( CSLFetchBoolean( psWO->papszWarpOptions, "CUTLINE_ALL_TOUCHED", FALSE ))
papszRasterizeOptions =
CSLSetNameValue( papszRasterizeOptions, "ALL_TOUCHED", "TRUE" );
anXYOff[0] = nXOff;
anXYOff[1] = nYOff;
eErr =
GDALRasterizeGeometries( hMemDS, 1, &nTargetBand,
1, &hPolygon,
CutlineTransformer, anXYOff,
&dfBurnValue, papszRasterizeOptions,
NULL, NULL );
CSLDestroy( papszRasterizeOptions );
// Close and ensure data flushed to underlying array.
GDALClose( hMemDS );
/* -------------------------------------------------------------------- */
/* In the case with no blend distance, we just apply this as a */
/* mask, zeroing out everything outside the polygon. */
/* -------------------------------------------------------------------- */
if( psWO->dfCutlineBlendDist == 0.0 )
{
int i;
for( i = nXSize * nYSize - 1; i >= 0; i-- )
{
if( pabyPolyMask[i] == 0 )
((float *) pValidityMask)[i] = 0.0;
}
}
else
{
eErr = BlendMaskGenerator( nXOff, nYOff, nXSize, nYSize,
pabyPolyMask, (float *) pValidityMask,
hPolygon, psWO->dfCutlineBlendDist );
}
/* -------------------------------------------------------------------- */
/* Clean up. */
/* -------------------------------------------------------------------- */
CPLFree( pabyPolyMask );
return eErr;
}
|