@@ -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,22 +60,76 @@ bool QgsOgrTopologyPreservingSimplifier::simplifyGeometry( OGRGeometryH geometry
59
60
60
61
/* **************************************************************************/
61
62
63
+ #if defined(GDAL_VERSION_NUM) && defined(GDAL_COMPUTE_VERSION) && GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(1,11,0)
64
+
62
65
QgsOgrMapToPixelSimplifier::QgsOgrMapToPixelSimplifier ( int simplifyFlags, double map2pixelTol )
63
66
: QgsMapToPixelSimplifier( simplifyFlags, map2pixelTol )
67
+ , mPointBufferPtr( NULL )
68
+ , mPointBufferCount( 0 )
64
69
{
65
70
}
66
71
67
72
QgsOgrMapToPixelSimplifier::~QgsOgrMapToPixelSimplifier ()
68
73
{
74
+ if ( mPointBufferPtr )
75
+ {
76
+ OGRFree ( mPointBufferPtr );
77
+ mPointBufferPtr = NULL ;
78
+ }
79
+ }
80
+
81
+ // ! Returns a point buffer of the specified size
82
+ QgsPoint* QgsOgrMapToPixelSimplifier::mallocPoints ( int numPoints )
83
+ {
84
+ if ( mPointBufferPtr && mPointBufferCount < numPoints )
85
+ {
86
+ OGRFree ( mPointBufferPtr );
87
+ mPointBufferPtr = NULL ;
88
+ }
89
+ if ( !mPointBufferPtr )
90
+ {
91
+ mPointBufferCount = numPoints;
92
+ mPointBufferPtr = ( QgsPoint* )OGRMalloc ( mPointBufferCount * sizeof ( QgsPoint ) );
93
+ }
94
+ return mPointBufferPtr ;
95
+ }
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;
69
125
}
70
126
71
127
// ////////////////////////////////////////////////////////////////////////////////////////////
72
128
// Helper simplification methods
73
129
74
130
// ! Simplifies the OGR-geometry (Removing duplicated points) when is applied the specified map2pixel context
75
- bool QgsOgrMapToPixelSimplifier::simplifyOgrGeometry ( QGis::GeometryType geometryType, const QgsRectangle& envelope, double *xptr , double * yptr, int pointCount, int & pointSimplifiedCount )
131
+ bool QgsOgrMapToPixelSimplifier::simplifyOgrGeometry ( QGis::GeometryType geometryType, double * xptr, int xStride , double * yptr, int yStride , int pointCount, int & pointSimplifiedCount )
76
132
{
77
- Q_UNUSED ( envelope )
78
133
bool canbeGeneralizable = ( mSimplifyFlags & QgsMapToPixelSimplifier::SimplifyGeometry );
79
134
80
135
pointSimplifiedCount = pointCount;
@@ -84,20 +139,20 @@ bool QgsOgrMapToPixelSimplifier::simplifyOgrGeometry( QGis::GeometryType geometr
84
139
double map2pixelTol = mMapToPixelTol * mMapToPixelTol ; // -> Use mappixelTol for 'LengthSquare' calculations.
85
140
double x, y, lastX = 0 , lastY = 0 ;
86
141
87
- double * xsourcePtr = xptr;
88
- double * ysourcePtr = yptr;
89
- double * xtargetPtr = xptr;
90
- double * ytargetPtr = yptr;
142
+ char * xsourcePtr = ( char * ) xptr;
143
+ char * ysourcePtr = ( char * ) yptr;
144
+ char * xtargetPtr = ( char * ) xptr;
145
+ char * ytargetPtr = ( char * ) yptr;
91
146
92
147
for ( int i = 0 , numPoints = geometryType == QGis::Polygon ? pointCount - 1 : pointCount; i < numPoints; ++i )
93
148
{
94
- memcpy ( &x, xsourcePtr++ , sizeof ( double ) );
95
- memcpy ( &y, ysourcePtr++ , sizeof ( double ) );
149
+ memcpy ( &x, xsourcePtr, sizeof ( double ) ); xsourcePtr += xStride ;
150
+ memcpy ( &y, ysourcePtr, sizeof ( double ) ); ysourcePtr += yStride ;
96
151
97
152
if ( i == 0 || !canbeGeneralizable || QgsMapToPixelSimplifier::calculateLengthSquared2D ( x, y, lastX, lastY ) > map2pixelTol || ( geometryType == QGis::Line && ( i == 1 || i >= numPoints - 2 ) ) )
98
153
{
99
- memcpy ( xtargetPtr++ , &x, sizeof ( double ) ); lastX = x;
100
- memcpy ( ytargetPtr++ , &y, sizeof ( double ) ); lastY = y;
154
+ memcpy ( xtargetPtr, &x, sizeof ( double ) ); lastX = x; xtargetPtr += xStride ;
155
+ memcpy ( ytargetPtr, &y, sizeof ( double ) ); lastY = y; ytargetPtr += yStride ;
101
156
pointSimplifiedCount++;
102
157
}
103
158
}
@@ -107,7 +162,6 @@ bool QgsOgrMapToPixelSimplifier::simplifyOgrGeometry( QGis::GeometryType geometr
107
162
memcpy ( ytargetPtr, yptr, sizeof ( double ) );
108
163
pointSimplifiedCount++;
109
164
}
110
-
111
165
return pointSimplifiedCount != pointCount;
112
166
}
113
167
@@ -120,7 +174,8 @@ bool QgsOgrMapToPixelSimplifier::simplifyOgrGeometry( OGRGeometryH geometry, boo
120
174
if ( wkbGeometryType == wkbLineString )
121
175
{
122
176
int numPoints = OGR_G_GetPointCount ( geometry );
123
- if (( isaLinearRing && numPoints <= 5 ) || ( !isaLinearRing && numPoints <= 4 ) )
177
+
178
+ if (( isaLinearRing && numPoints <= 5 ) || ( !isaLinearRing && numPoints <= 4 ) )
124
179
return false ;
125
180
126
181
OGREnvelope env;
@@ -130,25 +185,9 @@ bool QgsOgrMapToPixelSimplifier::simplifyOgrGeometry( OGRGeometryH geometry, boo
130
185
// Can replace the geometry by its BBOX ?
131
186
if (( mSimplifyFlags & QgsMapToPixelSimplifier::SimplifyEnvelope ) && canbeGeneralizedByMapBoundingBox ( envelope ) )
132
187
{
133
- double x1 = envelope.xMinimum ();
134
- double y1 = envelope.yMinimum ();
135
- double x2 = envelope.xMaximum ();
136
- double y2 = envelope.yMaximum ();
137
-
138
- if ( isaLinearRing )
139
- {
140
- OGR_G_SetPoint ( geometry, 0 , x1, y1 , 0.0 );
141
- OGR_G_SetPoint ( geometry, 1 , x2, y1 , 0.0 );
142
- OGR_G_SetPoint ( geometry, 2 , x2, y2, 0.0 );
143
- OGR_G_SetPoint ( geometry, 3 , x1, y2, 0.0 );
144
- OGR_G_SetPoint ( geometry, 4 , x1, y1 , 0.0 );
145
- }
146
- else
147
- {
148
- OGR_G_SetPoint ( geometry, 0 , x1, y1 , 0.0 );
149
- OGR_G_SetPoint ( geometry, 1 , x2, y2, 0.0 );
150
- }
188
+ QgsPoint* points = getEnvelopePoints ( envelope, numPoints, isaLinearRing );
151
189
190
+ setGeometryPoints ( geometry, points, numPoints );
152
191
OGR_G_FlattenTo2D ( geometry );
153
192
154
193
return true ;
@@ -158,22 +197,19 @@ bool QgsOgrMapToPixelSimplifier::simplifyOgrGeometry( OGRGeometryH geometry, boo
158
197
QGis::GeometryType geometryType = isaLinearRing ? QGis::Polygon : QGis::Line;
159
198
int numSimplifiedPoints = 0 ;
160
199
161
- QVector<double > x ( numPoints ), y ( numPoints );
162
- for ( int i = 0 ; i < numPoints; i++ )
163
- {
164
- double z;
165
- OGR_G_GetPoint ( geometry, i, &x[i], &y[i], &z );
166
- }
200
+ QgsPoint* points = mallocPoints ( numPoints );
201
+ double * xptr = ( double * )points;
202
+ double * yptr = xptr + 1 ;
203
+ OGR_G_GetPoints ( geometry, xptr, 16 , yptr, 16 , NULL , 0 );
167
204
168
- if ( simplifyOgrGeometry ( geometryType, envelope, x. data (), y. data () , numPoints, numSimplifiedPoints ) )
205
+ if ( simplifyOgrGeometry ( geometryType, xptr, 16 , yptr, 16 , numPoints, numSimplifiedPoints ) )
169
206
{
170
- for ( int i = 0 ; i < numSimplifiedPoints; i++ )
171
- {
172
- OGR_G_SetPoint ( geometry, i, x[i], y[i], 0.0 );
173
- }
207
+ if (( isaLinearRing && numSimplifiedPoints <= 4 ) || ( !isaLinearRing && numSimplifiedPoints <= 1 ) )
208
+ points = getEnvelopePoints ( envelope, numSimplifiedPoints, isaLinearRing );
209
+
210
+ setGeometryPoints ( geometry, points, numSimplifiedPoints );
174
211
OGR_G_FlattenTo2D ( geometry );
175
212
}
176
-
177
213
return numSimplifiedPoints != numPoints;
178
214
}
179
215
}
@@ -195,18 +231,29 @@ bool QgsOgrMapToPixelSimplifier::simplifyOgrGeometry( OGRGeometryH geometry, boo
195
231
{
196
232
bool result = false ;
197
233
198
- for ( int i = 1 , numGeometries = OGR_G_GetGeometryCount ( geometry ); i < numGeometries; ++i )
234
+ for ( int i = 0 , numGeometries = OGR_G_GetGeometryCount ( geometry ); i < numGeometries; ++i )
199
235
{
200
236
result |= simplifyOgrGeometry ( OGR_G_GetGeometryRef ( geometry, i ), wkbGeometryType == wkbMultiPolygon );
201
237
}
238
+
202
239
if ( result )
203
240
OGR_G_FlattenTo2D ( geometry );
241
+
204
242
return result;
205
243
}
206
244
207
245
return false ;
208
246
}
209
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
+
210
257
// ////////////////////////////////////////////////////////////////////////////////////////////
211
258
212
259
// ! Simplifies the specified geometry
@@ -221,3 +268,5 @@ bool QgsOgrMapToPixelSimplifier::simplifyGeometry( OGRGeometryH geometry )
221
268
222
269
return false ;
223
270
}
271
+
272
+ #endif
0 commit comments