Skip to content

Commit

Permalink
gdalwarp: fix failure with cutline on a layer of touching polygons (f…
Browse files Browse the repository at this point in the history
…ixes OSGeo#6694)

git-svn-id: https://svn.osgeo.org/gdal/branches/2.1@35888 f0d54148-0727-0410-94bb-9a71ac55c965
  • Loading branch information
rouault committed Oct 24, 2016
1 parent 9336261 commit f5a4a74
Showing 1 changed file with 53 additions and 21 deletions.
74 changes: 53 additions & 21 deletions gdal/apps/gdalwarp_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,50 @@ GDALDatasetH GDALWarp( const char *pszDest, GDALDatasetH hDstDS, int nSrcCount,
return (bHasGotErr) ? NULL : hDstDS;
}

/************************************************************************/
/* ValidateCutline() */
/************************************************************************/

static bool ValidateCutline(OGRGeometryH hGeom)
{
OGRwkbGeometryType eType = wkbFlatten(OGR_G_GetGeometryType( hGeom ));
if( eType == wkbMultiPolygon )
{
for( int iGeom = 0; iGeom < OGR_G_GetGeometryCount( hGeom ); iGeom++ )
{
OGRGeometryH hPoly = OGR_G_GetGeometryRef(hGeom,iGeom);
if( !ValidateCutline(hPoly) )
return false;
}
}
else if( eType == wkbPolygon )
{
if( OGRGeometryFactory::haveGEOS() && !OGR_G_IsValid(hGeom) )
{
char *pszWKT = NULL;
OGR_G_ExportToWkt( hGeom, &pszWKT );
CPLDebug("GDALWARP", "WKT = \"%s\"", pszWKT ? pszWKT : "(null)");
//fprintf(stderr, "WKT = \"%s\"\n", pszWKT ? pszWKT : "(null)");
CPLFree( pszWKT );

if( CPLTestBool(CPLGetConfigOption("GDALWARP_IGNORE_BAD_CUTLINE", "NO")) )
CPLError(CE_Warning, CPLE_AppDefined, "Cutline polygon is invalid.");
else
{
CPLError(CE_Failure, CPLE_AppDefined, "Cutline polygon is invalid.");
return false;
}
}
}
else
{
CPLError( CE_Failure, CPLE_AppDefined, "Cutline not of polygon type." );
return false;
}

return true;
}

/************************************************************************/
/* LoadCutline() */
/* */
Expand Down Expand Up @@ -1475,6 +1519,12 @@ LoadCutline( const char *pszCutlineDSName, const char *pszCLayer,
goto error;
}

if( !ValidateCutline(hGeom) )
{
OGR_F_Destroy( hFeat );
goto error;
}

OGRwkbGeometryType eType = wkbFlatten(OGR_G_GetGeometryType( hGeom ));

if( eType == wkbPolygon )
Expand All @@ -1489,12 +1539,6 @@ LoadCutline( const char *pszCutlineDSName, const char *pszCLayer,
OGR_G_GetGeometryRef(hGeom,iGeom) );
}
}
else
{
CPLError( CE_Failure, CPLE_AppDefined, "Cutline not of polygon type." );
OGR_F_Destroy( hFeat );
goto error;
}

OGR_F_Destroy( hFeat );
}
Expand Down Expand Up @@ -2339,22 +2383,10 @@ TransformCutlineToSource( GDALDatasetH hSrcDS, void *hCutline,
return CE_Failure;
}
}
else if( OGRGeometryFactory::haveGEOS() && !OGR_G_IsValid(hMultiPolygon) )
else if( !ValidateCutline(hMultiPolygon) )
{
char *pszWKT = NULL;
OGR_G_ExportToWkt( hMultiPolygon, &pszWKT );
CPLDebug("GDALWARP", "WKT = \"%s\"", pszWKT ? pszWKT : "(null)");
//fprintf(stderr, "WKT = \"%s\"\n", pszWKT ? pszWKT : "(null)");
CPLFree( pszWKT );

if( CPLTestBool(CPLGetConfigOption("GDALWARP_IGNORE_BAD_CUTLINE", "NO")) )
CPLError(CE_Warning, CPLE_AppDefined, "Cutline is not valid after transformation");
else
{
CPLError(CE_Failure, CPLE_AppDefined, "Cutline is not valid after transformation");
OGR_G_DestroyGeometry( hMultiPolygon );
return CE_Failure;
}
OGR_G_DestroyGeometry( hMultiPolygon );
return CE_Failure;
}

/* -------------------------------------------------------------------- */
Expand Down

0 comments on commit f5a4a74

Please sign in to comment.