@@ -25,6 +25,7 @@ QgsOgrAbstractGeometrySimplifier::~QgsOgrAbstractGeometrySimplifier()
25
25
/* **************************************************************************/
26
26
27
27
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1900
28
+
28
29
QgsOgrTopologyPreservingSimplifier::QgsOgrTopologyPreservingSimplifier ( double tolerance )
29
30
: QgsTopologyPreservingSimplifier( tolerance )
30
31
{
@@ -59,10 +60,7 @@ bool QgsOgrTopologyPreservingSimplifier::simplifyGeometry( OGRGeometryH geometry
59
60
60
61
/* **************************************************************************/
61
62
62
- #if defined(HAVE_OGR_GEOMETRY_CLASS)
63
-
64
- // Use OgrGeometry class to speed up simplification on provider side
65
- #include < ogr_geometry.h>
63
+ #if defined(GDAL_VERSION_NUM) && defined(GDAL_COMPUTE_VERSION) && GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(1,11,0)
66
64
67
65
QgsOgrMapToPixelSimplifier::QgsOgrMapToPixelSimplifier ( int simplifyFlags, double map2pixelTol )
68
66
: QgsMapToPixelSimplifier( simplifyFlags, map2pixelTol )
@@ -81,7 +79,7 @@ QgsOgrMapToPixelSimplifier::~QgsOgrMapToPixelSimplifier()
81
79
}
82
80
83
81
// ! Returns a point buffer of the specified size
84
- OGRRawPoint * QgsOgrMapToPixelSimplifier::mallocPoints ( int numPoints )
82
+ QgsPoint * QgsOgrMapToPixelSimplifier::mallocPoints ( int numPoints )
85
83
{
86
84
if ( mPointBufferPtr && mPointBufferCount < numPoints )
87
85
{
@@ -91,11 +89,41 @@ OGRRawPoint* QgsOgrMapToPixelSimplifier::mallocPoints( int numPoints )
91
89
if ( !mPointBufferPtr )
92
90
{
93
91
mPointBufferCount = numPoints;
94
- mPointBufferPtr = ( OGRRawPoint * )OGRMalloc ( mPointBufferCount * sizeof ( OGRRawPoint ) );
92
+ mPointBufferPtr = ( QgsPoint * )OGRMalloc ( mPointBufferCount * sizeof ( QgsPoint ) );
95
93
}
96
94
return mPointBufferPtr ;
97
95
}
98
96
97
+ // ! Returns a point buffer of the specified envelope
98
+ QgsPoint* QgsOgrMapToPixelSimplifier::getEnvelopePoints ( const QgsRectangle& envelope, int & numPoints, bool isaLinearRing )
99
+ {
100
+ QgsPoint* points = NULL ;
101
+
102
+ double x1 = envelope.xMinimum ();
103
+ double y1 = envelope.yMinimum ();
104
+ double x2 = envelope.xMaximum ();
105
+ double y2 = envelope.yMaximum ();
106
+
107
+ if ( isaLinearRing )
108
+ {
109
+ numPoints = 5 ;
110
+ points = mallocPoints ( numPoints );
111
+ points[0 ].set ( x1, y1 );
112
+ points[1 ].set ( x2, y1 );
113
+ points[2 ].set ( x2, y2 );
114
+ points[3 ].set ( x1, y2 );
115
+ points[4 ].set ( x1, y1 );
116
+ }
117
+ else
118
+ {
119
+ numPoints = 2 ;
120
+ points = mallocPoints ( numPoints );
121
+ points[0 ].set ( x1, y1 );
122
+ points[1 ].set ( x2, y2 );
123
+ }
124
+ return points;
125
+ }
126
+
99
127
// ////////////////////////////////////////////////////////////////////////////////////////////
100
128
// Helper simplification methods
101
129
@@ -138,52 +166,29 @@ bool QgsOgrMapToPixelSimplifier::simplifyOgrGeometry( QGis::GeometryType geometr
138
166
}
139
167
140
168
// ! Simplifies the OGR-geometry (Removing duplicated points) when is applied the specified map2pixel context
141
- bool QgsOgrMapToPixelSimplifier::simplifyOgrGeometry ( OGRGeometry* geometry, bool isaLinearRing )
169
+ bool QgsOgrMapToPixelSimplifier::simplifyOgrGeometry ( OGRGeometryH geometry, bool isaLinearRing )
142
170
{
143
- OGRwkbGeometryType wkbGeometryType = wkbFlatten ( geometry-> getGeometryType ( ) );
171
+ OGRwkbGeometryType wkbGeometryType = wkbFlatten ( OGR_G_GetGeometryType ( geometry ) );
144
172
145
173
// Simplify the geometry rewriting temporally its WKB-stream for saving calloc's.
146
174
if ( wkbGeometryType == wkbLineString )
147
175
{
148
- OGRLineString* lineString = ( OGRLineString* )geometry ;
176
+ int numPoints = OGR_G_GetPointCount ( geometry ) ;
149
177
150
- int numPoints = lineString->getNumPoints ();
151
178
if (( isaLinearRing && numPoints <= 5 ) || ( !isaLinearRing && numPoints <= 4 ) )
152
179
return false ;
153
180
154
181
OGREnvelope env;
155
- lineString-> getEnvelope ( &env );
182
+ OGR_G_GetEnvelope ( geometry, &env );
156
183
QgsRectangle envelope ( env.MinX , env.MinY , env.MaxX , env.MaxY );
157
184
158
185
// Can replace the geometry by its BBOX ?
159
186
if (( mSimplifyFlags & QgsMapToPixelSimplifier::SimplifyEnvelope ) && canbeGeneralizedByMapBoundingBox ( envelope ) )
160
187
{
161
- OGRRawPoint * points = NULL ;
188
+ QgsPoint * points = getEnvelopePoints ( envelope, numPoints, isaLinearRing ) ;
162
189
163
- double x1 = envelope.xMinimum ();
164
- double y1 = envelope.yMinimum ();
165
- double x2 = envelope.xMaximum ();
166
- double y2 = envelope.yMaximum ();
167
-
168
- if ( isaLinearRing )
169
- {
170
- numPoints = 5 ;
171
- points = mallocPoints ( numPoints );
172
- points[0 ].x = x1; points[0 ].y = y1 ;
173
- points[1 ].x = x2; points[1 ].y = y1 ;
174
- points[2 ].x = x2; points[2 ].y = y2;
175
- points[3 ].x = x1; points[3 ].y = y2;
176
- points[4 ].x = x1; points[4 ].y = y1 ;
177
- }
178
- else
179
- {
180
- numPoints = 2 ;
181
- points = mallocPoints ( numPoints );
182
- points[0 ].x = x1; points[0 ].y = y1 ;
183
- points[1 ].x = x2; points[1 ].y = y2;
184
- }
185
- lineString->setPoints ( numPoints, points );
186
- lineString->flattenTo2D ();
190
+ setGeometryPoints ( geometry, points, numPoints );
191
+ OGR_G_FlattenTo2D ( geometry );
187
192
188
193
return true ;
189
194
}
@@ -192,27 +197,29 @@ bool QgsOgrMapToPixelSimplifier::simplifyOgrGeometry( OGRGeometry* geometry, boo
192
197
QGis::GeometryType geometryType = isaLinearRing ? QGis::Polygon : QGis::Line;
193
198
int numSimplifiedPoints = 0 ;
194
199
195
- OGRRawPoint * points = mallocPoints ( numPoints );
200
+ QgsPoint * points = mallocPoints ( numPoints );
196
201
double * xptr = ( double * )points;
197
202
double * yptr = xptr + 1 ;
198
- lineString-> getPoints ( points );
203
+ OGR_G_GetPoints ( geometry, xptr, 16 , yptr, 16 , NULL , 0 );
199
204
200
205
if ( simplifyOgrGeometry ( geometryType, xptr, 16 , yptr, 16 , numPoints, numSimplifiedPoints ) )
201
206
{
202
- lineString->setPoints ( numSimplifiedPoints, points );
203
- lineString->flattenTo2D ();
207
+ if (( isaLinearRing && numSimplifiedPoints <= 4 ) || ( !isaLinearRing && numSimplifiedPoints <= 1 ) )
208
+ points = getEnvelopePoints ( envelope, numSimplifiedPoints, isaLinearRing );
209
+
210
+ setGeometryPoints ( geometry, points, numSimplifiedPoints );
211
+ OGR_G_FlattenTo2D ( geometry );
204
212
}
205
213
return numSimplifiedPoints != numPoints;
206
214
}
207
215
}
208
216
else if ( wkbGeometryType == wkbPolygon )
209
217
{
210
- OGRPolygon* polygon = ( OGRPolygon* )geometry;
211
- bool result = simplifyOgrGeometry ( polygon->getExteriorRing (), true );
218
+ bool result = simplifyOgrGeometry ( OGR_G_GetGeometryRef ( geometry, 0 ), true );
212
219
213
- for ( int i = 0 , numInteriorRings = polygon-> getNumInteriorRings ( ); i < numInteriorRings; ++i )
220
+ for ( int i = 1 , numInteriorRings = OGR_G_GetGeometryCount ( geometry ); i < numInteriorRings; ++i )
214
221
{
215
- result |= simplifyOgrGeometry ( polygon-> getInteriorRing ( i ), true );
222
+ result |= simplifyOgrGeometry ( OGR_G_GetGeometryRef ( geometry, i ), true );
216
223
}
217
224
218
225
if ( result )
@@ -222,12 +229,11 @@ bool QgsOgrMapToPixelSimplifier::simplifyOgrGeometry( OGRGeometry* geometry, boo
222
229
}
223
230
else if ( wkbGeometryType == wkbMultiLineString || wkbGeometryType == wkbMultiPolygon )
224
231
{
225
- OGRGeometryCollection* collection = ( OGRGeometryCollection* )geometry;
226
232
bool result = false ;
227
233
228
- for ( int i = 0 , numGeometries = collection-> getNumGeometries ( ); i < numGeometries; ++i )
234
+ for ( int i = 0 , numGeometries = OGR_G_GetGeometryCount ( geometry ); i < numGeometries; ++i )
229
235
{
230
- result |= simplifyOgrGeometry ( collection-> getGeometryRef ( i ), wkbGeometryType == wkbMultiPolygon );
236
+ result |= simplifyOgrGeometry ( OGR_G_GetGeometryRef ( geometry, i ), wkbGeometryType == wkbMultiPolygon );
231
237
}
232
238
233
239
if ( result )
@@ -239,6 +245,15 @@ bool QgsOgrMapToPixelSimplifier::simplifyOgrGeometry( OGRGeometry* geometry, boo
239
245
return false ;
240
246
}
241
247
248
+ // ! Load a point array to the specified LineString geometry
249
+ void QgsOgrMapToPixelSimplifier::setGeometryPoints ( OGRGeometryH geometry, QgsPoint* points, int numPoints )
250
+ {
251
+ double * xptr = ( double * )points;
252
+ double * yptr = xptr + 1 ;
253
+
254
+ OGR_G_SetPoints ( geometry, numPoints, xptr, 16 , yptr, 16 , NULL , 0 );
255
+ }
256
+
242
257
// ////////////////////////////////////////////////////////////////////////////////////////////
243
258
244
259
// ! Simplifies the specified geometry
@@ -248,7 +263,7 @@ bool QgsOgrMapToPixelSimplifier::simplifyGeometry( OGRGeometryH geometry )
248
263
249
264
if ( wkbGeometryType == wkbLineString || wkbGeometryType == wkbPolygon )
250
265
{
251
- return simplifyOgrGeometry ( (OGRGeometry*) geometry, wkbGeometryType == wkbPolygon );
266
+ return simplifyOgrGeometry ( geometry, wkbGeometryType == wkbPolygon );
252
267
}
253
268
254
269
return false ;
0 commit comments