📄 ogrgeometry.cpp
字号:
/* */
/* This is a special entry point to enable the hack for */
/* generating DB2 V7.2 style WKB. DB2 seems to have placed */
/* (and require) an extra 0x30 or'ed with the byte order in */
/* WKB. This entry point is used to turn on or off the */
/* generation of such WKB. */
/************************************************************************/
OGRErr OGRSetGenerate_DB2_V72_BYTE_ORDER( int bGenerate_DB2_V72_BYTE_ORDER )
{
#if defined(HACK_FOR_IBM_DB2_V72)
OGRGeometry::bGenerate_DB2_V72_BYTE_ORDER = bGenerate_DB2_V72_BYTE_ORDER;
return OGRERR_NONE;
#else
if( bGenerate_DB2_V72_BYTE_ORDER )
return OGRERR_FAILURE;
else
return OGRERR_NONE;
#endif
}
/************************************************************************/
/* OGRGetGenerate_DB2_V72_BYTE_ORDER() */
/* */
/* This is a special entry point to get the value of static flag */
/* OGRGeometry::bGenerate_DB2_V72_BYTE_ORDER. */
/************************************************************************/
int OGRGetGenerate_DB2_V72_BYTE_ORDER()
{
return OGRGeometry::bGenerate_DB2_V72_BYTE_ORDER;
}
/************************************************************************/
/* exportToGEOS() */
/************************************************************************/
GEOSGeom OGRGeometry::exportToGEOS() const
{
#ifndef HAVE_GEOS
CPLError( CE_Failure, CPLE_NotSupported,
"GEOS support not enabled." );
return NULL;
#else
static int bGEOSInitialized = FALSE;
if( !bGEOSInitialized )
{
bGEOSInitialized = TRUE;
initGEOS( _GEOSWarningHandler, _GEOSErrorHandler );
}
GEOSGeom hGeom = NULL;
size_t nDataSize;
unsigned char *pabyData = NULL;
nDataSize = WkbSize();
pabyData = (unsigned char *) CPLMalloc(nDataSize);
if( exportToWkb( wkbNDR, pabyData ) == OGRERR_NONE )
hGeom = GEOSGeomFromWKB_buf( pabyData, nDataSize );
CPLFree( pabyData );
return hGeom;
#endif /* HAVE_GEOS */
}
/************************************************************************/
/* Distance() */
/************************************************************************/
/**
* Compute distance between two geometries.
*
* Returns the shortest distance between the two geometries.
*
* This method is the same as the C function OGR_G_Distance().
*
* This method is built on the GEOS library, check it for the definition
* of the geometry operation.
* If OGR is built without the GEOS library, this method will always fail,
* issuing a CPLE_NotSupported error.
*
* @param poOtherGeom the other geometry to compare against.
*
* @return the distance between the geometries or -1 if an error occurs.
*/
double OGRGeometry::Distance( const OGRGeometry *poOtherGeom ) const
{
if( NULL == poOtherGeom )
{
CPLDebug( "OGR", "OGRGeometry::Distance called with NULL geometry pointer" );
return -1.0;
}
#ifndef HAVE_GEOS
CPLError( CE_Failure, CPLE_NotSupported,
"GEOS support not enabled." );
return -1.0;
#else
// GEOSGeom is a pointer
GEOSGeom hThis = NULL;
GEOSGeom hOther = NULL;
hOther = poOtherGeom->exportToGEOS();
hThis = exportToGEOS();
int bIsErr = 0;
double dfDistance = 0.0;
if( hThis != NULL && hOther != NULL )
{
bIsErr = GEOSDistance( hThis, hOther, &dfDistance );
}
GEOSGeom_destroy( hThis );
GEOSGeom_destroy( hOther );
if ( bIsErr > 0 )
{
return dfDistance;
}
/* Calculations error */
return -1.0;
#endif /* HAVE_GEOS */
}
/************************************************************************/
/* OGR_G_Distance() */
/************************************************************************/
double OGR_G_Distance( OGRGeometryH hFirst, OGRGeometryH hOther )
{
return ((OGRGeometry *) hFirst)->Distance( (OGRGeometry *) hOther );
}
/************************************************************************/
/* ConvexHull() */
/************************************************************************/
/**
* Compute convex hull.
*
* A new geometry object is created and returned containing the convex
* hull of the geometry on which the method is invoked.
*
* This method is the same as the C function OGR_G_ConvexHull().
*
* This method is built on the GEOS library, check it for the definition
* of the geometry operation.
* If OGR is built without the GEOS library, this method will always fail,
* issuing a CPLE_NotSupported error.
*
* @return a newly allocated geometry now owned by the caller, or NULL on failure.
*/
OGRGeometry *OGRGeometry::ConvexHull() const
{
#ifndef HAVE_GEOS
CPLError( CE_Failure, CPLE_NotSupported,
"GEOS support not enabled." );
return NULL;
#else
GEOSGeom hGeosGeom = NULL;
GEOSGeom hGeosHull = NULL;
OGRGeometry *poHullOGRGeom = NULL;
hGeosGeom = exportToGEOS();
if( hGeosGeom != NULL )
{
hGeosHull = GEOSConvexHull( hGeosGeom );
GEOSGeom_destroy( hGeosGeom );
if( hGeosHull != NULL )
{
poHullOGRGeom = OGRGeometryFactory::createFromGEOS(hGeosHull);
GEOSGeom_destroy( hGeosHull);
}
}
return poHullOGRGeom;
#endif /* HAVE_GEOS */
}
/************************************************************************/
/* OGR_G_ConvexHull() */
/************************************************************************/
OGRGeometryH OGR_G_ConvexHull( OGRGeometryH hTarget )
{
return (OGRGeometryH) ((OGRGeometry *) hTarget)->ConvexHull();
}
/************************************************************************/
/* getBoundary() */
/************************************************************************/
/**
* Compute boundary.
*
* A new geometry object is created and returned containing the boundary
* of the geometry on which the method is invoked.
*
* This method is the same as the C function OGR_G_GetBoundary().
*
* This method is built on the GEOS library, check it for the definition
* of the geometry operation.
* If OGR is built without the GEOS library, this method will always fail,
* issuing a CPLE_NotSupported error.
*
* @return a newly allocated geometry now owned by the caller, or NULL on failure.
*/
OGRGeometry *OGRGeometry::getBoundary() const
{
#ifndef HAVE_GEOS
CPLError( CE_Failure, CPLE_NotSupported,
"GEOS support not enabled." );
return NULL;
#else
GEOSGeom hGeosGeom = NULL;
GEOSGeom hGeosProduct = NULL;
OGRGeometry *poOGRProduct = NULL;
hGeosGeom = exportToGEOS();
if( hGeosGeom != NULL )
{
hGeosProduct = GEOSBoundary( hGeosGeom );
GEOSGeom_destroy( hGeosGeom );
if( hGeosProduct != NULL )
{
poOGRProduct = OGRGeometryFactory::createFromGEOS(hGeosProduct);
GEOSGeom_destroy( hGeosProduct );
}
}
return poOGRProduct;
#endif /* HAVE_GEOS */
}
/************************************************************************/
/* OGR_G_GetBoundary() */
/************************************************************************/
OGRGeometryH OGR_G_GetBoundary( OGRGeometryH hTarget )
{
return (OGRGeometryH) ((OGRGeometry *) hTarget)->getBoundary();
}
/************************************************************************/
/* Buffer() */
/************************************************************************/
/**
* Compute buffer of geometry.
*
* Builds a new geometry containing the buffer region around the geometry
* on which it is invoked. The buffer is a polygon containing the region within
* the buffer distance of the original geometry.
*
* Some buffer sections are properly described as curves, but are converted to
* approximate polygons. The nQuadSegs parameter can be used to control how many
* segements should be used to define a 90 degree curve - a quadrant of a circle.
* A value of 30 is a reasonable default. Large values result in large numbers
* of vertices in the resulting buffer geometry while small numbers reduce the
* accuracy of the result.
*
* This method is the same as the C function OGR_G_Buffer().
*
* This method is built on the GEOS library, check it for the definition
* of the geometry operation.
* If OGR is built without the GEOS library, this method will always fail,
* issuing a CPLE_NotSupported error.
*
* @param dfDist the buffer distance to be applied.
*
* @param nQuadSegs the number of segments used to approximate a 90 degree (quadrant) of
* curvature.
*
* @return the newly created geometry, or NULL if an error occurs.
*/
OGRGeometry *OGRGeometry::Buffer( double dfDist, int nQuadSegs ) const
{
#ifndef HAVE_GEOS
CPLError( CE_Failure, CPLE_NotSupported,
"GEOS support not enabled." );
return NULL;
#else
GEOSGeom hGeosGeom = NULL;
GEOSGeom hGeosProduct = NULL;
OGRGeometry *poOGRProduct = NULL;
hGeosGeom = exportToGEOS();
if( hGeosGeom != NULL )
{
hGeosProduct = GEOSBuffer( hGeosGeom, dfDist, nQuadSegs );
GEOSGeom_destroy( hGeosGeom );
if( hGeosProduct != NULL )
{
poOGRProduct = OGRGeometryFactory::createFromGEOS(hGeosProduct);
GEOSGeom_destroy( hGeosProduct );
}
}
return poOGRProduct;
#endif /* HAVE_GEOS */
}
/************************************************************************/
/* OGR_G_Buffer() */
/************************************************************************/
OGRGeometryH OGR_G_Buffer( OGRGeometryH hTarget, double dfDist, int nQuadSegs )
{
return (OGRGeometryH) ((OGRGeometry *) hTarget)->Buffer( dfDist, nQuadSegs );
}
/************************************************************************/
/* Intersection() */
/************************************************************************/
/**
* Compute intersection.
*
* Generates a new geometry which is the region of intersection of the
* two geometries operated on. The Intersect() method can be used to test if
* two geometries intersect.
*
* This method is the same as the C function OGR_G_Intersection().
*
* This method is built on the GEOS library, check it for the definition
* of the geometry operation.
* If OGR is built without the GEOS library, this method will always fail,
* issuing a CPLE_NotSupported error.
*
* @param poOtherGeom the other geometry intersected with "this" geometry.
*
* @return a new geometry representing the intersection or NULL if there is
* no intersection or an error occurs.
*/
OGRGeometry *OGRGeometry::Intersection( const OGRGeometry *poOtherGeom ) const
{
#ifndef HAVE_GEOS
CPLError( CE_Failure, CPLE_NotSupported,
"GEOS support not enabled." );
return NULL;
#else
GEOSGeom hThisGeosGeom = NULL;
GEOSGeom hOtherGeosGeom = NULL;
GEOSGeom hGeosProduct = NULL;
OGRGeometry *poOGRProduct = NULL;
hThisGeosGeom = exportToGEOS();
hOtherGeosGeom = poOtherGeom->exportToGEOS();
if( hThisGeosGeom != NULL && hOtherGeosGeom != NULL )
{
hGeosProduct = GEOSIntersection( hThisGeosGeom, hOtherGeosGeom );
GEOSGeom_destroy( hThisGeosGeom );
GEOSGeom_destroy( hOtherGeosGeom );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -