Skip to content
Permalink
Browse files

#9360R: mix CPP/C code - experimental

  • Loading branch information
ahuarte47 committed Jan 19, 2014
1 parent b7ee396 commit a2b8efaa1aa6593279af512073c29dedb137219a
Showing with 66 additions and 29 deletions.
  1. +62 −27 src/providers/ogr/qgsogrgeometrysimplifier.cpp
  2. +4 −2 src/providers/ogr/qgsogrgeometrysimplifier.h
@@ -18,7 +18,25 @@
#include "qgsogrprovider.h"
#include "qgsapplication.h"

#include <ogr_geometry.h>
/***************************************************************************/
// Use OgrGeometry class to speed up simplification if possible

#ifdef __cplusplus
#define USE_OGR_GEOMETRY_CLASS
#endif

#ifdef USE_OGR_GEOMETRY_CLASS
#include <ogr_geometry.h>
#else
class OGRRawPoint
{
public:
double x;
double y;
};
#endif

/***************************************************************************/

QgsOgrAbstractGeometrySimplifier::~QgsOgrAbstractGeometrySimplifier()
{
@@ -63,9 +81,9 @@ bool QgsOgrTopologyPreservingSimplifier::simplifyGeometry( OGRGeometryH geometry

QgsOgrMapToPixelSimplifier::QgsOgrMapToPixelSimplifier( int simplifyFlags, double map2pixelTol )
: QgsMapToPixelSimplifier( simplifyFlags, map2pixelTol )
, mPointBufferPtr( NULL )
, mPointBufferCount( 0 )
{
mPointBufferCount = 64;
mPointBufferPtr = ( OGRRawPoint* )OGRMalloc( mPointBufferCount * sizeof( OGRRawPoint ) );
}

QgsOgrMapToPixelSimplifier::~QgsOgrMapToPixelSimplifier()
@@ -85,7 +103,7 @@ OGRRawPoint* QgsOgrMapToPixelSimplifier::mallocPoints( int numPoints )
OGRFree( mPointBufferPtr );
mPointBufferPtr = NULL;
}
if ( mPointBufferPtr == NULL )
if ( !mPointBufferPtr )
{
mPointBufferCount = numPoints;
mPointBufferPtr = ( OGRRawPoint* )OGRMalloc( mPointBufferCount * sizeof( OGRRawPoint ) );
@@ -135,28 +153,26 @@ bool QgsOgrMapToPixelSimplifier::simplifyOgrGeometry( QGis::GeometryType geometr
}

//! Simplifies the OGR-geometry (Removing duplicated points) when is applied the specified map2pixel context
bool QgsOgrMapToPixelSimplifier::simplifyOgrGeometry( OGRGeometry* geometry, bool isaLinearRing )
bool QgsOgrMapToPixelSimplifier::simplifyOgrGeometry( OGRGeometryH geometry, bool isaLinearRing )
{
OGRwkbGeometryType wkbGeometryType = wkbFlatten( geometry->getGeometryType() );
OGRwkbGeometryType wkbGeometryType = wkbFlatten( OGR_G_GetGeometryType( geometry ) );

// Simplify the geometry rewriting temporally its WKB-stream for saving calloc's.
if ( wkbGeometryType == wkbLineString )
{
OGRLineString* lineString = ( OGRLineString* )geometry;
int numPoints = OGR_G_GetPointCount( geometry );

int numPoints = lineString->getNumPoints();
if (( isaLinearRing && numPoints <= 5 ) || ( !isaLinearRing && numPoints <= 4 ) )
return false;

OGREnvelope env;
geometry->getEnvelope( &env );
OGR_G_GetEnvelope( geometry, &env );
QgsRectangle envelope( env.MinX, env.MinY, env.MaxX, env.MaxY );

// Can replace the geometry by its BBOX ?
if (( mSimplifyFlags & QgsMapToPixelSimplifier::SimplifyEnvelope ) && canbeGeneralizedByMapBoundingBox( envelope ) )
{
OGRRawPoint* points = NULL;
int numPoints = 0;

double x1 = envelope.xMinimum();
double y1 = envelope.yMinimum();
@@ -180,8 +196,8 @@ bool QgsOgrMapToPixelSimplifier::simplifyOgrGeometry( OGRGeometry* geometry, boo
points[0].x = x1; points[0].y = y1;
points[1].x = x2; points[1].y = y2;
}
lineString->setPoints( numPoints, points );
lineString->flattenTo2D();
setGeometryPoints( geometry, points, numPoints, isaLinearRing );
OGR_G_FlattenTo2D( geometry );

return true;
}
@@ -193,50 +209,69 @@ bool QgsOgrMapToPixelSimplifier::simplifyOgrGeometry( OGRGeometry* geometry, boo
OGRRawPoint* points = mallocPoints( numPoints );
double* xptr = ( double* )points;
double* yptr = xptr + 1;
lineString->getPoints( points );
OGR_G_GetPoints( geometry, xptr, 16, yptr, 16, NULL, 0 );

if ( simplifyOgrGeometry( geometryType, xptr, 16, yptr, 16, numPoints, numSimplifiedPoints ) )
{
lineString->setPoints( numSimplifiedPoints, points );
lineString->flattenTo2D();
setGeometryPoints( geometry, points, numSimplifiedPoints, isaLinearRing );
OGR_G_FlattenTo2D( geometry );
}
return numSimplifiedPoints != numPoints;
}
}
else if ( wkbGeometryType == wkbPolygon )
{
OGRPolygon* polygon = ( OGRPolygon* )geometry;
bool result = simplifyOgrGeometry( polygon->getExteriorRing(), true );
bool result = simplifyOgrGeometry( OGR_G_GetGeometryRef( geometry, 0 ), true );

for ( int i = 0, numInteriorRings = polygon->getNumInteriorRings(); i < numInteriorRings; ++i )
for ( int i = 1, numInteriorRings = OGR_G_GetGeometryCount( geometry ); i < numInteriorRings; ++i )
{
result |= simplifyOgrGeometry( polygon->getInteriorRing( i ), true );
result |= simplifyOgrGeometry( OGR_G_GetGeometryRef( geometry, i ), true );
}

if ( result )
polygon->flattenTo2D();
if ( result )
OGR_G_FlattenTo2D( geometry );

return result;
}
else if ( wkbGeometryType == wkbMultiLineString || wkbGeometryType == wkbMultiPolygon )
{
OGRGeometryCollection* collection = ( OGRGeometryCollection* )geometry;
bool result = false;

for ( int i = 0, numGeometries = collection->getNumGeometries(); i < numGeometries; ++i )
for ( int i = 0, numGeometries = OGR_G_GetGeometryCount( geometry ); i < numGeometries; ++i )
{
result |= simplifyOgrGeometry( collection->getGeometryRef( i ), wkbGeometryType == wkbMultiPolygon );
result |= simplifyOgrGeometry( OGR_G_GetGeometryRef( geometry, i ), wkbGeometryType == wkbMultiPolygon );
}

if ( result )
collection->flattenTo2D();
if ( result )
OGR_G_FlattenTo2D( geometry );

return result;
}

return false;
}

//! Load a point array to the specified LineString geometry
void QgsOgrMapToPixelSimplifier::setGeometryPoints( OGRGeometryH geometry, OGRRawPoint* points, int numPoints, bool isaLinearRing )
{
#ifdef USE_OGR_GEOMETRY_CLASS
OGRLineString* lineString = ( OGRLineString* )geometry;
lineString->setPoints( numPoints, points );
#else
OGRGeometryH g = OGR_G_CreateGeometry( isaLinearRing ? wkbLinearRing : wkbLineString );

for (int i = 0; i < numPoints; i++, points++)
OGR_G_SetPoint( g, i, points->x, points->y, 0);

size_t wkbSize = OGR_G_WkbSize( g );
unsigned char *wkb = new unsigned char[ wkbSize ];
OGR_G_ExportToWkb( g, ( OGRwkbByteOrder ) QgsApplication::endian(), wkb );
OGR_G_ImportFromWkb( geometry, wkb, wkbSize );
delete [] wkb;
OGR_G_DestroyGeometry( g );
#endif
}

//////////////////////////////////////////////////////////////////////////////////////////////

//! Simplifies the specified geometry
@@ -246,7 +281,7 @@ bool QgsOgrMapToPixelSimplifier::simplifyGeometry( OGRGeometryH geometry )

if ( wkbGeometryType == wkbLineString || wkbGeometryType == wkbPolygon )
{
return simplifyOgrGeometry( (OGRGeometry*) geometry, wkbGeometryType == wkbPolygon );
return simplifyOgrGeometry( geometry, wkbGeometryType == wkbPolygon );
}

return false;
@@ -21,7 +21,6 @@
#include <ogr_api.h>

class OGRRawPoint;
class OGRGeometry;

/**
* Abstract base class for simplify OGR-geometries using a specific algorithm
@@ -74,11 +73,14 @@ class QgsOgrMapToPixelSimplifier : public QgsOgrAbstractGeometrySimplifier, QgsM
//! Simplifies the OGR-geometry (Removing duplicated points) when is applied the specified map2pixel context
bool simplifyOgrGeometry( QGis::GeometryType geometryType, double* xptr, int xStride, double* yptr, int yStride, int pointCount, int& pointSimplifiedCount );
//! Simplifies the OGR-geometry (Removing duplicated points) when is applied the specified map2pixel context
bool simplifyOgrGeometry( OGRGeometry* geometry, bool isaLinearRing );
bool simplifyOgrGeometry( OGRGeometryH geometry, bool isaLinearRing );

//! Returns a point buffer of the specified size
OGRRawPoint* mallocPoints( int numPoints );

//! Load a point array to the specified LineString geometry
static void setGeometryPoints( OGRGeometryH geometry, OGRRawPoint* points, int numPoints, bool isaLinearRing );

public:
//! Simplifies the specified geometry
virtual bool simplifyGeometry( OGRGeometryH geometry );

0 comments on commit a2b8efa

Please sign in to comment.
You can’t perform that action at this time.