Skip to content

Commit a2b8efa

Browse files
committed
#9360R: mix CPP/C code - experimental
1 parent b7ee396 commit a2b8efa

File tree

2 files changed

+66
-29
lines changed

2 files changed

+66
-29
lines changed

src/providers/ogr/qgsogrgeometrysimplifier.cpp

+62-27
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,25 @@
1818
#include "qgsogrprovider.h"
1919
#include "qgsapplication.h"
2020

21-
#include <ogr_geometry.h>
21+
/***************************************************************************/
22+
// Use OgrGeometry class to speed up simplification if possible
23+
24+
#ifdef __cplusplus
25+
#define USE_OGR_GEOMETRY_CLASS
26+
#endif
27+
28+
#ifdef USE_OGR_GEOMETRY_CLASS
29+
#include <ogr_geometry.h>
30+
#else
31+
class OGRRawPoint
32+
{
33+
public:
34+
double x;
35+
double y;
36+
};
37+
#endif
38+
39+
/***************************************************************************/
2240

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

6482
QgsOgrMapToPixelSimplifier::QgsOgrMapToPixelSimplifier( int simplifyFlags, double map2pixelTol )
6583
: QgsMapToPixelSimplifier( simplifyFlags, map2pixelTol )
84+
, mPointBufferPtr( NULL )
85+
, mPointBufferCount( 0 )
6686
{
67-
mPointBufferCount = 64;
68-
mPointBufferPtr = ( OGRRawPoint* )OGRMalloc( mPointBufferCount * sizeof( OGRRawPoint ) );
6987
}
7088

7189
QgsOgrMapToPixelSimplifier::~QgsOgrMapToPixelSimplifier()
@@ -85,7 +103,7 @@ OGRRawPoint* QgsOgrMapToPixelSimplifier::mallocPoints( int numPoints )
85103
OGRFree( mPointBufferPtr );
86104
mPointBufferPtr = NULL;
87105
}
88-
if ( mPointBufferPtr == NULL )
106+
if ( !mPointBufferPtr )
89107
{
90108
mPointBufferCount = numPoints;
91109
mPointBufferPtr = ( OGRRawPoint* )OGRMalloc( mPointBufferCount * sizeof( OGRRawPoint ) );
@@ -135,28 +153,26 @@ bool QgsOgrMapToPixelSimplifier::simplifyOgrGeometry( QGis::GeometryType geometr
135153
}
136154

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

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

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

151168
OGREnvelope env;
152-
geometry->getEnvelope( &env );
169+
OGR_G_GetEnvelope( geometry, &env );
153170
QgsRectangle envelope( env.MinX, env.MinY, env.MaxX, env.MaxY );
154171

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

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

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

198214
if ( simplifyOgrGeometry( geometryType, xptr, 16, yptr, 16, numPoints, numSimplifiedPoints ) )
199215
{
200-
lineString->setPoints( numSimplifiedPoints, points );
201-
lineString->flattenTo2D();
216+
setGeometryPoints( geometry, points, numSimplifiedPoints, isaLinearRing );
217+
OGR_G_FlattenTo2D( geometry );
202218
}
203219
return numSimplifiedPoints != numPoints;
204220
}
205221
}
206222
else if ( wkbGeometryType == wkbPolygon )
207223
{
208-
OGRPolygon* polygon = ( OGRPolygon* )geometry;
209-
bool result = simplifyOgrGeometry( polygon->getExteriorRing(), true );
224+
bool result = simplifyOgrGeometry( OGR_G_GetGeometryRef( geometry, 0 ), true );
210225

211-
for ( int i = 0, numInteriorRings = polygon->getNumInteriorRings(); i < numInteriorRings; ++i )
226+
for ( int i = 1, numInteriorRings = OGR_G_GetGeometryCount( geometry ); i < numInteriorRings; ++i )
212227
{
213-
result |= simplifyOgrGeometry( polygon->getInteriorRing( i ), true );
228+
result |= simplifyOgrGeometry( OGR_G_GetGeometryRef( geometry, i ), true );
214229
}
215230

216-
if ( result )
217-
polygon->flattenTo2D();
231+
if ( result )
232+
OGR_G_FlattenTo2D( geometry );
218233

219234
return result;
220235
}
221236
else if ( wkbGeometryType == wkbMultiLineString || wkbGeometryType == wkbMultiPolygon )
222237
{
223-
OGRGeometryCollection* collection = ( OGRGeometryCollection* )geometry;
224238
bool result = false;
225239

226-
for ( int i = 0, numGeometries = collection->getNumGeometries(); i < numGeometries; ++i )
240+
for ( int i = 0, numGeometries = OGR_G_GetGeometryCount( geometry ); i < numGeometries; ++i )
227241
{
228-
result |= simplifyOgrGeometry( collection->getGeometryRef( i ), wkbGeometryType == wkbMultiPolygon );
242+
result |= simplifyOgrGeometry( OGR_G_GetGeometryRef( geometry, i ), wkbGeometryType == wkbMultiPolygon );
229243
}
230244

231-
if ( result )
232-
collection->flattenTo2D();
245+
if ( result )
246+
OGR_G_FlattenTo2D( geometry );
233247

234248
return result;
235249
}
236250

237251
return false;
238252
}
239253

254+
//! Load a point array to the specified LineString geometry
255+
void QgsOgrMapToPixelSimplifier::setGeometryPoints( OGRGeometryH geometry, OGRRawPoint* points, int numPoints, bool isaLinearRing )
256+
{
257+
#ifdef USE_OGR_GEOMETRY_CLASS
258+
OGRLineString* lineString = ( OGRLineString* )geometry;
259+
lineString->setPoints( numPoints, points );
260+
#else
261+
OGRGeometryH g = OGR_G_CreateGeometry( isaLinearRing ? wkbLinearRing : wkbLineString );
262+
263+
for (int i = 0; i < numPoints; i++, points++)
264+
OGR_G_SetPoint( g, i, points->x, points->y, 0);
265+
266+
size_t wkbSize = OGR_G_WkbSize( g );
267+
unsigned char *wkb = new unsigned char[ wkbSize ];
268+
OGR_G_ExportToWkb( g, ( OGRwkbByteOrder ) QgsApplication::endian(), wkb );
269+
OGR_G_ImportFromWkb( geometry, wkb, wkbSize );
270+
delete [] wkb;
271+
OGR_G_DestroyGeometry( g );
272+
#endif
273+
}
274+
240275
//////////////////////////////////////////////////////////////////////////////////////////////
241276

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

247282
if ( wkbGeometryType == wkbLineString || wkbGeometryType == wkbPolygon )
248283
{
249-
return simplifyOgrGeometry( (OGRGeometry*) geometry, wkbGeometryType == wkbPolygon );
284+
return simplifyOgrGeometry( geometry, wkbGeometryType == wkbPolygon );
250285
}
251286

252287
return false;

src/providers/ogr/qgsogrgeometrysimplifier.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include <ogr_api.h>
2222

2323
class OGRRawPoint;
24-
class OGRGeometry;
2524

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

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

81+
//! Load a point array to the specified LineString geometry
82+
static void setGeometryPoints( OGRGeometryH geometry, OGRRawPoint* points, int numPoints, bool isaLinearRing );
83+
8284
public:
8385
//! Simplifies the specified geometry
8486
virtual bool simplifyGeometry( OGRGeometryH geometry );

0 commit comments

Comments
 (0)