18
18
#include " qgsogrprovider.h"
19
19
#include " qgsapplication.h"
20
20
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
+ /* **************************************************************************/
22
40
23
41
QgsOgrAbstractGeometrySimplifier::~QgsOgrAbstractGeometrySimplifier ()
24
42
{
@@ -63,9 +81,9 @@ bool QgsOgrTopologyPreservingSimplifier::simplifyGeometry( OGRGeometryH geometry
63
81
64
82
QgsOgrMapToPixelSimplifier::QgsOgrMapToPixelSimplifier ( int simplifyFlags, double map2pixelTol )
65
83
: QgsMapToPixelSimplifier( simplifyFlags, map2pixelTol )
84
+ , mPointBufferPtr( NULL )
85
+ , mPointBufferCount( 0 )
66
86
{
67
- mPointBufferCount = 64 ;
68
- mPointBufferPtr = ( OGRRawPoint* )OGRMalloc ( mPointBufferCount * sizeof ( OGRRawPoint ) );
69
87
}
70
88
71
89
QgsOgrMapToPixelSimplifier::~QgsOgrMapToPixelSimplifier ()
@@ -85,7 +103,7 @@ OGRRawPoint* QgsOgrMapToPixelSimplifier::mallocPoints( int numPoints )
85
103
OGRFree ( mPointBufferPtr );
86
104
mPointBufferPtr = NULL ;
87
105
}
88
- if ( mPointBufferPtr == NULL )
106
+ if ( ! mPointBufferPtr )
89
107
{
90
108
mPointBufferCount = numPoints;
91
109
mPointBufferPtr = ( OGRRawPoint* )OGRMalloc ( mPointBufferCount * sizeof ( OGRRawPoint ) );
@@ -135,28 +153,26 @@ bool QgsOgrMapToPixelSimplifier::simplifyOgrGeometry( QGis::GeometryType geometr
135
153
}
136
154
137
155
// ! 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 )
139
157
{
140
- OGRwkbGeometryType wkbGeometryType = wkbFlatten ( geometry-> getGeometryType ( ) );
158
+ OGRwkbGeometryType wkbGeometryType = wkbFlatten ( OGR_G_GetGeometryType ( geometry ) );
141
159
142
160
// Simplify the geometry rewriting temporally its WKB-stream for saving calloc's.
143
161
if ( wkbGeometryType == wkbLineString )
144
162
{
145
- OGRLineString* lineString = ( OGRLineString* )geometry ;
163
+ int numPoints = OGR_G_GetPointCount ( geometry ) ;
146
164
147
- int numPoints = lineString->getNumPoints ();
148
165
if (( isaLinearRing && numPoints <= 5 ) || ( !isaLinearRing && numPoints <= 4 ) )
149
166
return false ;
150
167
151
168
OGREnvelope env;
152
- geometry-> getEnvelope ( &env );
169
+ OGR_G_GetEnvelope ( geometry, &env );
153
170
QgsRectangle envelope ( env.MinX , env.MinY , env.MaxX , env.MaxY );
154
171
155
172
// Can replace the geometry by its BBOX ?
156
173
if (( mSimplifyFlags & QgsMapToPixelSimplifier::SimplifyEnvelope ) && canbeGeneralizedByMapBoundingBox ( envelope ) )
157
174
{
158
175
OGRRawPoint* points = NULL ;
159
- int numPoints = 0 ;
160
176
161
177
double x1 = envelope.xMinimum ();
162
178
double y1 = envelope.yMinimum ();
@@ -180,8 +196,8 @@ bool QgsOgrMapToPixelSimplifier::simplifyOgrGeometry( OGRGeometry* geometry, boo
180
196
points[0 ].x = x1; points[0 ].y = y1 ;
181
197
points[1 ].x = x2; points[1 ].y = y2;
182
198
}
183
- lineString-> setPoints ( numPoints , points );
184
- lineString-> flattenTo2D ( );
199
+ setGeometryPoints ( geometry , points, numPoints, isaLinearRing );
200
+ OGR_G_FlattenTo2D ( geometry );
185
201
186
202
return true ;
187
203
}
@@ -193,50 +209,69 @@ bool QgsOgrMapToPixelSimplifier::simplifyOgrGeometry( OGRGeometry* geometry, boo
193
209
OGRRawPoint* points = mallocPoints ( numPoints );
194
210
double * xptr = ( double * )points;
195
211
double * yptr = xptr + 1 ;
196
- lineString-> getPoints ( points );
212
+ OGR_G_GetPoints ( geometry, xptr, 16 , yptr, 16 , NULL , 0 );
197
213
198
214
if ( simplifyOgrGeometry ( geometryType, xptr, 16 , yptr, 16 , numPoints, numSimplifiedPoints ) )
199
215
{
200
- lineString-> setPoints ( numSimplifiedPoints , points );
201
- lineString-> flattenTo2D ( );
216
+ setGeometryPoints ( geometry , points, numSimplifiedPoints, isaLinearRing );
217
+ OGR_G_FlattenTo2D ( geometry );
202
218
}
203
219
return numSimplifiedPoints != numPoints;
204
220
}
205
221
}
206
222
else if ( wkbGeometryType == wkbPolygon )
207
223
{
208
- OGRPolygon* polygon = ( OGRPolygon* )geometry;
209
- bool result = simplifyOgrGeometry ( polygon->getExteriorRing (), true );
224
+ bool result = simplifyOgrGeometry ( OGR_G_GetGeometryRef ( geometry, 0 ), true );
210
225
211
- for ( int i = 0 , numInteriorRings = polygon-> getNumInteriorRings ( ); i < numInteriorRings; ++i )
226
+ for ( int i = 1 , numInteriorRings = OGR_G_GetGeometryCount ( geometry ); i < numInteriorRings; ++i )
212
227
{
213
- result |= simplifyOgrGeometry ( polygon-> getInteriorRing ( i ), true );
228
+ result |= simplifyOgrGeometry ( OGR_G_GetGeometryRef ( geometry, i ), true );
214
229
}
215
230
216
- if ( result )
217
- polygon-> flattenTo2D ( );
231
+ if ( result )
232
+ OGR_G_FlattenTo2D ( geometry );
218
233
219
234
return result;
220
235
}
221
236
else if ( wkbGeometryType == wkbMultiLineString || wkbGeometryType == wkbMultiPolygon )
222
237
{
223
- OGRGeometryCollection* collection = ( OGRGeometryCollection* )geometry;
224
238
bool result = false ;
225
239
226
- for ( int i = 0 , numGeometries = collection-> getNumGeometries ( ); i < numGeometries; ++i )
240
+ for ( int i = 0 , numGeometries = OGR_G_GetGeometryCount ( geometry ); i < numGeometries; ++i )
227
241
{
228
- result |= simplifyOgrGeometry ( collection-> getGeometryRef ( i ), wkbGeometryType == wkbMultiPolygon );
242
+ result |= simplifyOgrGeometry ( OGR_G_GetGeometryRef ( geometry, i ), wkbGeometryType == wkbMultiPolygon );
229
243
}
230
244
231
- if ( result )
232
- collection-> flattenTo2D ( );
245
+ if ( result )
246
+ OGR_G_FlattenTo2D ( geometry );
233
247
234
248
return result;
235
249
}
236
250
237
251
return false ;
238
252
}
239
253
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
+
240
275
// ////////////////////////////////////////////////////////////////////////////////////////////
241
276
242
277
// ! Simplifies the specified geometry
@@ -246,7 +281,7 @@ bool QgsOgrMapToPixelSimplifier::simplifyGeometry( OGRGeometryH geometry )
246
281
247
282
if ( wkbGeometryType == wkbLineString || wkbGeometryType == wkbPolygon )
248
283
{
249
- return simplifyOgrGeometry ( (OGRGeometry*) geometry, wkbGeometryType == wkbPolygon );
284
+ return simplifyOgrGeometry ( geometry, wkbGeometryType == wkbPolygon );
250
285
}
251
286
252
287
return false ;
0 commit comments