📄 ogrgeometryfactory.cpp
字号:
*
* This is equivelent to allocating the desired geometry with new, but
* the allocation is guaranteed to take place in the context of the
* GDAL/OGR heap.
*
* This function is the same as the CPP method
* OGRGeometryFactory::createGeometry.
*
* @param eGeometryType the type code of the geometry to be created.
*
* @return handle to the newly create geometry or NULL on failure.
*/
OGRGeometryH OGR_G_CreateGeometry( OGRwkbGeometryType eGeometryType )
{
return (OGRGeometryH) OGRGeometryFactory::createGeometry( eGeometryType );
}
/************************************************************************/
/* destroyGeometry() */
/************************************************************************/
/**
* Destroy geometry object.
*
* Equivalent to invoking delete on a geometry, but it guaranteed to take
* place within the context of the GDAL/OGR heap.
*
* This method is the same as the C function OGR_G_DestroyGeometry().
*
* @param poGeom the geometry to deallocate.
*/
void OGRGeometryFactory::destroyGeometry( OGRGeometry *poGeom )
{
delete poGeom;
}
/************************************************************************/
/* OGR_G_DestroyGeometry() */
/************************************************************************/
/**
* Destroy geometry object.
*
* Equivalent to invoking delete on a geometry, but it guaranteed to take
* place within the context of the GDAL/OGR heap.
*
* This function is the same as the CPP method
* OGRGeometryFactory::destroyGeometry.
*
* @param hGeom handle to the geometry to delete.
*/
void OGR_G_DestroyGeometry( OGRGeometryH hGeom )
{
OGRGeometryFactory::destroyGeometry( (OGRGeometry *) hGeom );
}
/************************************************************************/
/* forceToPolygon() */
/************************************************************************/
/**
* Convert to polygon.
*
* Tries to force the provided geometry to be a polygon. Currently
* this just effects a change on multipolygons. The passed in geometry is
* consumed and a new one returned (or potentially the same one).
*
* @return new geometry.
*/
OGRGeometry *OGRGeometryFactory::forceToPolygon( OGRGeometry *poGeom )
{
if( poGeom == NULL )
return NULL;
if( wkbFlatten(poGeom->getGeometryType()) != wkbGeometryCollection
|| wkbFlatten(poGeom->getGeometryType()) != wkbMultiPolygon )
return poGeom;
// build an aggregated polygon from all the polygon rings in the container.
OGRPolygon *poPolygon = new OGRPolygon();
OGRGeometryCollection *poGC = (OGRGeometryCollection *) poGeom;
int iGeom;
for( iGeom = 0; iGeom < poGC->getNumGeometries(); iGeom++ )
{
if( wkbFlatten(poGC->getGeometryRef(iGeom)->getGeometryType())
!= wkbPolygon )
continue;
OGRPolygon *poOldPoly = (OGRPolygon *) poGC->getGeometryRef(iGeom);
int iRing;
poPolygon->addRing( poOldPoly->getExteriorRing() );
for( iRing = 0; iRing < poOldPoly->getNumInteriorRings(); iRing++ )
poPolygon->addRing( poOldPoly->getInteriorRing( iRing ) );
}
delete poGC;
return poPolygon;
}
/************************************************************************/
/* forceToMultiPolygon() */
/************************************************************************/
/**
* Convert to multipolygon.
*
* Tries to force the provided geometry to be a multipolygon. Currently
* this just effects a change on polygons. The passed in geometry is
* consumed and a new one returned (or potentially the same one).
*
* @return new geometry.
*/
OGRGeometry *OGRGeometryFactory::forceToMultiPolygon( OGRGeometry *poGeom )
{
if( poGeom == NULL )
return NULL;
/* -------------------------------------------------------------------- */
/* Check for the case of a geometrycollection that can be */
/* promoted to MultiPolygon. */
/* -------------------------------------------------------------------- */
if( wkbFlatten(poGeom->getGeometryType()) == wkbGeometryCollection )
{
int iGeom;
int bAllPoly = TRUE;
OGRGeometryCollection *poGC = (OGRGeometryCollection *) poGeom;
for( iGeom = 0; iGeom < poGC->getNumGeometries(); iGeom++ )
{
if( wkbFlatten(poGC->getGeometryRef(iGeom)->getGeometryType())
!= wkbPolygon )
bAllPoly = FALSE;
}
if( !bAllPoly )
return poGeom;
OGRMultiPolygon *poMP = new OGRMultiPolygon();
while( poGC->getNumGeometries() > 0 )
{
poMP->addGeometryDirectly( poGC->getGeometryRef(0) );
poGC->removeGeometry( 0, FALSE );
}
delete poGC;
return poMP;
}
/* -------------------------------------------------------------------- */
/* Eventually we should try to split the polygon into component */
/* island polygons. But thats alot of work and can be put off. */
/* -------------------------------------------------------------------- */
if( wkbFlatten(poGeom->getGeometryType()) != wkbPolygon )
return poGeom;
OGRMultiPolygon *poMP = new OGRMultiPolygon();
poMP->addGeometryDirectly( poGeom );
return poMP;
}
/************************************************************************/
/* forceToMultiPoint() */
/************************************************************************/
/**
* Convert to multipoint.
*
* Tries to force the provided geometry to be a multipoint. Currently
* this just effects a change on points. The passed in geometry is
* consumed and a new one returned (or potentially the same one).
*
* @return new geometry.
*/
OGRGeometry *OGRGeometryFactory::forceToMultiPoint( OGRGeometry *poGeom )
{
if( poGeom == NULL )
return NULL;
/* -------------------------------------------------------------------- */
/* Check for the case of a geometrycollection that can be */
/* promoted to MultiPoint. */
/* -------------------------------------------------------------------- */
if( wkbFlatten(poGeom->getGeometryType()) == wkbGeometryCollection )
{
int iGeom;
int bAllPoint = TRUE;
OGRGeometryCollection *poGC = (OGRGeometryCollection *) poGeom;
for( iGeom = 0; iGeom < poGC->getNumGeometries(); iGeom++ )
{
if( wkbFlatten(poGC->getGeometryRef(iGeom)->getGeometryType())
!= wkbPoint )
bAllPoint = FALSE;
}
if( !bAllPoint )
return poGeom;
OGRMultiPoint *poMP = new OGRMultiPoint();
while( poGC->getNumGeometries() > 0 )
{
poMP->addGeometryDirectly( poGC->getGeometryRef(0) );
poGC->removeGeometry( 0, FALSE );
}
delete poGC;
return poMP;
}
if( wkbFlatten(poGeom->getGeometryType()) != wkbPoint )
return poGeom;
OGRMultiPoint *poMP = new OGRMultiPoint();
poMP->addGeometryDirectly( poGeom );
return poMP;
}
/************************************************************************/
/* forceToMultiLinestring() */
/************************************************************************/
/**
* Convert to multilinestring.
*
* Tries to force the provided geometry to be a multilinestring. Currently
* this just effects a change on linestrings. The passed in geometry is
* consumed and a new one returned (or potentially the same one).
*
* @return new geometry.
*/
OGRGeometry *OGRGeometryFactory::forceToMultiLineString( OGRGeometry *poGeom )
{
if( poGeom == NULL )
return NULL;
/* -------------------------------------------------------------------- */
/* Check for the case of a geometrycollection that can be */
/* promoted to MultiLineString. */
/* -------------------------------------------------------------------- */
if( wkbFlatten(poGeom->getGeometryType()) == wkbGeometryCollection )
{
int iGeom;
int bAllLines = TRUE;
OGRGeometryCollection *poGC = (OGRGeometryCollection *) poGeom;
for( iGeom = 0; iGeom < poGC->getNumGeometries(); iGeom++ )
{
if( wkbFlatten(poGC->getGeometryRef(iGeom)->getGeometryType())
!= wkbLineString )
bAllLines = FALSE;
}
if( !bAllLines )
return poGeom;
OGRMultiLineString *poMP = new OGRMultiLineString();
while( poGC->getNumGeometries() > 0 )
{
poMP->addGeometryDirectly( poGC->getGeometryRef(0) );
poGC->removeGeometry( 0, FALSE );
}
delete poGC;
return poMP;
}
if( wkbFlatten(poGeom->getGeometryType()) != wkbLineString )
return poGeom;
OGRMultiLineString *poMP = new OGRMultiLineString();
poMP->addGeometryDirectly( poGeom );
return poMP;
}
/************************************************************************/
/* createFromGML() */
/************************************************************************/
/**
* Create geometry from GML.
*
* This method translates a fragment of GML containing only the geometry
* portion into a corresponding OGRGeometry. There are many limitations
* on the forms of GML geometries supported by this parser, but they are
* too numerous to list here.
*
* The C function OGR_G_CreateFromGML() is the same as this method.
*
* @param pszData The GML fragment for the geometry.
*
* @return a geometry on succes, or NULL on error.
*/
OGRGeometry *OGRGeometryFactory::createFromGML( const char *pszData )
{
OGRGeometryH hGeom;
hGeom = OGR_G_CreateFromGML( pszData );
return (OGRGeometry *) hGeom;
}
/************************************************************************/
/* createFromGEOS() */
/************************************************************************/
OGRGeometry *
OGRGeometryFactory::createFromGEOS( GEOSGeom geosGeom )
{
#ifndef HAVE_GEOS
CPLError( CE_Failure, CPLE_NotSupported,
"GEOS support not enabled." );
return NULL;
#else
size_t nSize = 0;
unsigned char *pabyBuf = NULL;
OGRGeometry *poGeometry = NULL;
pabyBuf = GEOSGeomToWKB_buf( geosGeom, &nSize );
if( pabyBuf == NULL || nSize == 0 )
{
return NULL;
}
if( OGRGeometryFactory::createFromWkb( (unsigned char *) pabyBuf,
NULL, &poGeometry, (int) nSize )
!= OGRERR_NONE )
{
poGeometry = NULL;
}
if( pabyBuf != NULL )
{
free( pabyBuf );
}
return poGeometry;
#endif /* HAVE_GEOS */
}
/************************************************************************/
/* getGEOSGeometryFactory() */
/************************************************************************/
void *OGRGeometryFactory::getGEOSGeometryFactory()
{
// XXX - mloskot - What to do with this call
// after GEOS C++ API has been stripped?
return NULL;
}
/************************************************************************/
/* haveGEOS() */
/************************************************************************/
/**
* Test if GEOS enabled.
*
* This static method returns TRUE if GEOS support is built into OGR,
* otherwise it returns FALSE.
*
* @return TRUE if available, otherwise FALSE.
*/
int OGRGeometryFactory::haveGEOS()
{
#ifndef HAVE_GEOS
return FALSE;
#else
return TRUE;
#endif
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -