-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
qgsmaptopixelgeometrysimplifier.cpp
401 lines (335 loc) · 15.1 KB
/
qgsmaptopixelgeometrysimplifier.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/***************************************************************************
qgsmaptopixelgeometrysimplifier.cpp
---------------------
begin : December 2013
copyright : (C) 2013 by Alvaro Huarte
email : http://wiki.osgeo.org/wiki/Alvaro_Huarte
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <limits>
#include "qgsmaptopixelgeometrysimplifier.h"
#include "qgsapplication.h"
QgsMapToPixelSimplifier::QgsMapToPixelSimplifier( int simplifyFlags, const QgsCoordinateTransform* coordinateTransform, const QgsMapToPixel* mapTolPixel, float mapToPixelTol )
: mSimplifyFlags( simplifyFlags )
, mMapCoordTransform( coordinateTransform )
, mMapToPixel( mapTolPixel )
, mMapToPixelTol( mapToPixelTol )
{
}
QgsMapToPixelSimplifier::~QgsMapToPixelSimplifier()
{
}
//////////////////////////////////////////////////////////////////////////////////////////////
// Helper simplification methods
//! Returns the squared 2D-distance of the vector defined by the two points specified
float QgsMapToPixelSimplifier::calculateLengthSquared2D( double x1, double y1, double x2, double y2 )
{
float vx = (float)( x2 - x1 );
float vy = (float)( y2 - y1 );
return vx*vx + vy*vy;
}
//! Returns the MapTolerance for transform between map coordinates and device coordinates
float QgsMapToPixelSimplifier::calculateViewPixelTolerance( const QgsRectangle& boundingRect, const QgsCoordinateTransform* ct, const QgsMapToPixel* mapToPixel )
{
double mapUnitsPerPixel = mapToPixel ? mapToPixel->mapUnitsPerPixel() : 1.0;
double mapUnitsFactor = 1;
// Calculate one aprox factor of the size of the BBOX from the source CoordinateSystem to the target CoordinateSystem
if ( ct && !((QgsCoordinateTransform*)ct)->isShortCircuited() )
{
QgsRectangle sourceRect = boundingRect;
QgsRectangle targetRect = ct->transform(sourceRect);
QgsPoint minimumSrcPoint( sourceRect.xMinimum(), sourceRect.yMinimum() );
QgsPoint maximumSrcPoint( sourceRect.xMaximum(), sourceRect.yMaximum() );
QgsPoint minimumDstPoint( targetRect.xMinimum(), targetRect.yMinimum() );
QgsPoint maximumDstPoint( targetRect.xMaximum(), targetRect.yMaximum() );
double sourceHypothenuse = sqrt( calculateLengthSquared2D( minimumSrcPoint.x(), minimumSrcPoint.y(), maximumSrcPoint.x(), maximumSrcPoint.y() ) );
double targetHypothenuse = sqrt( calculateLengthSquared2D( minimumDstPoint.x(), minimumDstPoint.y(), maximumDstPoint.x(), maximumDstPoint.y() ) );
if (targetHypothenuse!=0)
mapUnitsFactor = sourceHypothenuse/targetHypothenuse;
}
return (float)( mapUnitsPerPixel * mapUnitsFactor );
}
//! Returns the BBOX of the specified WKB-point stream
inline static QgsRectangle calculateBoundingBox( QGis::WkbType wkbType, unsigned char* wkb, size_t numPoints )
{
unsigned char* wkb2 = wkb;
double xmin = std::numeric_limits<double>::max(), x,y;
double ymin = std::numeric_limits<double>::max();
double xmax = -std::numeric_limits<double>::max();
double ymax = -std::numeric_limits<double>::max();
int sizeOfDoubleX = sizeof(double);
int sizeOfDoubleY = QGis::wkbDimensions(wkbType)==3 /*hasZValue*/ ? 2*sizeof(double) : sizeof(double);
for ( size_t i = 0; i < numPoints; ++i )
{
memcpy( &x, wkb, sizeof( double ) ); wkb += sizeOfDoubleX;
memcpy( &y, wkb, sizeof( double ) ); wkb += sizeOfDoubleY;
if ( xmin > x ) xmin = x;
if ( ymin > y ) ymin = y;
if ( xmax < x ) xmax = x;
if ( ymax < y ) ymax = y;
}
wkb = wkb2;
return QgsRectangle( xmin, ymin, xmax, ymax );
}
//! Generalize the WKB-geometry using the BBOX of the original geometry
inline static bool generalizeWkbGeometry( QGis::WkbType wkbType, unsigned char* sourceWkb, size_t sourceWkbSize, unsigned char* targetWkb, size_t& targetWkbSize, const QgsRectangle& envelope, bool writeHeader )
{
unsigned char* wkb2 = targetWkb;
unsigned int geometryType = QGis::singleType( QGis::flatType( wkbType ) );
int sizeOfDoubleX = sizeof(double);
int sizeOfDoubleY = QGis::wkbDimensions(wkbType)==3 /*hasZValue*/ ? 2*sizeof(double) : sizeof(double);
// Skip the unnecesary generalization because of is a very single geometry
size_t minimumSize = ( geometryType==QGis::WKBLineString ? 4 + 2*(sizeOfDoubleX+sizeOfDoubleY) : 8 + 5*(sizeOfDoubleX+sizeOfDoubleY) );
if ( writeHeader ) minimumSize += 5;
if ( sourceWkbSize <= minimumSize )
{
targetWkbSize = 0;
return false;
}
double x1 = envelope.xMinimum();
double y1 = envelope.yMinimum();
double x2 = envelope.xMaximum();
double y2 = envelope.yMaximum();
// Write the main header of the geometry
if ( writeHeader )
{
char byteOrder = QgsApplication::endian(); // byteOrder
memcpy( targetWkb, &byteOrder, 1 );
targetWkb += 1;
memcpy( targetWkb, &geometryType, 4 ); // type
targetWkb += 4;
if ( geometryType == QGis::WKBPolygon ) // numRings
{
int numRings = 1;
memcpy( targetWkb, &numRings, 4 );
targetWkb += 4;
}
}
// Write the generalized geometry
if ( geometryType == QGis::WKBLineString )
{
int numPoints = 2;
memcpy( targetWkb, &numPoints, 4 ); // numPoints;
targetWkb += 4;
memcpy( targetWkb, &x1, sizeof( double ) ); targetWkb += sizeof( double );
memcpy( targetWkb, &y1, sizeof( double ) ); targetWkb += sizeof( double );
memcpy( targetWkb, &x2, sizeof( double ) ); targetWkb += sizeof( double );
memcpy( targetWkb, &y2, sizeof( double ) ); targetWkb += sizeof( double );
}
else
{
int numPoints = 5;
memcpy( targetWkb, &numPoints, 4 ); // numPoints;
targetWkb += 4;
memcpy( targetWkb, &x1, sizeof( double ) ); targetWkb += sizeof( double );
memcpy( targetWkb, &y1, sizeof( double ) ); targetWkb += sizeof( double );
memcpy( targetWkb, &x2, sizeof( double ) ); targetWkb += sizeof( double );
memcpy( targetWkb, &y1, sizeof( double ) ); targetWkb += sizeof( double );
memcpy( targetWkb, &x2, sizeof( double ) ); targetWkb += sizeof( double );
memcpy( targetWkb, &y2, sizeof( double ) ); targetWkb += sizeof( double );
memcpy( targetWkb, &x1, sizeof( double ) ); targetWkb += sizeof( double );
memcpy( targetWkb, &y2, sizeof( double ) ); targetWkb += sizeof( double );
memcpy( targetWkb, &x1, sizeof( double ) ); targetWkb += sizeof( double );
memcpy( targetWkb, &y1, sizeof( double ) ); targetWkb += sizeof( double );
}
targetWkbSize += targetWkb - wkb2;
targetWkb = wkb2;
return true;
}
//! Simplify the WKB-geometry using the specified tolerance
bool QgsMapToPixelSimplifier::simplifyWkbGeometry( int simplifyFlags, QGis::WkbType wkbType, unsigned char* sourceWkb, size_t sourceWkbSize, unsigned char* targetWkb, size_t& targetWkbSize, const QgsRectangle& envelope, float map2pixelTol, bool writeHeader, bool isaLinearRing )
{
bool canbeGeneralizable = true;
bool hasZValue = QGis::wkbDimensions(wkbType)==3;
bool result = false;
// Can replace the geometry by its BBOX ?
if ( ( simplifyFlags & QgsMapToPixelSimplifier::SimplifyEnvelope ) && (envelope.xMaximum()-envelope.xMinimum()) < map2pixelTol && (envelope.yMaximum()-envelope.yMinimum()) < map2pixelTol )
{
canbeGeneralizable = generalizeWkbGeometry( wkbType, sourceWkb, sourceWkbSize, targetWkb, targetWkbSize, envelope, writeHeader );
if (canbeGeneralizable) return true;
}
if (!( simplifyFlags & QgsMapToPixelSimplifier::SimplifyGeometry ) ) canbeGeneralizable = false;
// Write the main header of the geometry
if ( writeHeader )
{
memcpy( targetWkb, sourceWkb, 1 ); // byteOrder
sourceWkb += 1;
targetWkb += 1;
int geometryType;
memcpy( &geometryType, sourceWkb, 4 );
int flatType = QGis::flatType( (QGis::WkbType)geometryType );
memcpy( targetWkb, &flatType, 4 ); // type
sourceWkb += 4;
targetWkb += 4;
targetWkbSize += 5;
}
unsigned char* wkb1 = sourceWkb;
unsigned char* wkb2 = targetWkb;
unsigned int flatType = QGis::flatType( wkbType );
// Write the geometry
if ( flatType == QGis::WKBLineString || isaLinearRing )
{
double x,y, lastX=0,lastY=0;
int sizeOfDoubleX = sizeof(double);
int sizeOfDoubleY = QGis::wkbDimensions(wkbType)==3 /*hasZValue*/ ? 2*sizeof(double) : sizeof(double);
int numPoints;
memcpy( &numPoints, sourceWkb, 4);
sourceWkb += 4;
if (numPoints <= (isaLinearRing ? 5 : 2)) canbeGeneralizable = false;
int numTargetPoints = 0;
memcpy( targetWkb, &numTargetPoints, 4 );
targetWkb += 4;
targetWkbSize += 4;
double* ptr = (double*)targetWkb;
map2pixelTol *= map2pixelTol; //-> Use mappixelTol for 'LengthSquare' calculations.
// Process each vertex...
for ( int i = 0, numPoints_i = (isaLinearRing ? numPoints-1 : numPoints); i < numPoints_i; ++i )
{
memcpy( &x, sourceWkb, sizeof( double ) ); sourceWkb += sizeOfDoubleX;
memcpy( &y, sourceWkb, sizeof( double ) ); sourceWkb += sizeOfDoubleY;
if ( i==0 || !canbeGeneralizable || QgsMapToPixelSimplifier::calculateLengthSquared2D(x,y,lastX,lastY)>map2pixelTol )
{
memcpy( ptr, &x, sizeof( double ) ); lastX = x; ptr++;
memcpy( ptr, &y, sizeof( double ) ); lastY = y; ptr++;
numTargetPoints++;
}
}
targetWkb = wkb2+4;
// Fix the topology of the geometry
if ( isaLinearRing )
{
memcpy( &x, targetWkb+0, sizeof( double ) );
memcpy( &y, targetWkb+8, sizeof( double ) );
memcpy( ptr, &x, sizeof( double ) ); ptr++;
memcpy( ptr, &y, sizeof( double ) ); ptr++;
numTargetPoints++;
}
targetWkbSize += numTargetPoints * 16;
targetWkb = wkb2;
memcpy( targetWkb, &numTargetPoints, 4 );
result = numPoints!=numTargetPoints;
}
else
if ( flatType == QGis::WKBPolygon )
{
int numRings;
memcpy( &numRings, sourceWkb, 4 );
sourceWkb += 4;
memcpy( targetWkb, &numRings, 4 );
targetWkb += 4;
targetWkbSize += 4;
for ( int i = 0; i < numRings; ++i )
{
int numPoints_i;
memcpy( &numPoints_i, sourceWkb, 4 );
QgsRectangle envelope_i = numRings==1 ? envelope : calculateBoundingBox( wkbType, sourceWkb+4, numPoints_i );
size_t sourceWkbSize_i = 4 + numPoints_i * (hasZValue ? 3 : 2) * sizeof(double);
size_t targetWkbSize_i = 0;
result |= simplifyWkbGeometry( simplifyFlags, wkbType, sourceWkb, sourceWkbSize_i, targetWkb, targetWkbSize_i, envelope_i, map2pixelTol, false, true );
sourceWkb += sourceWkbSize_i;
targetWkb += targetWkbSize_i;
targetWkbSize += targetWkbSize_i;
}
}
else
if ( flatType == QGis::WKBMultiLineString || flatType == QGis::WKBMultiPolygon )
{
int numGeoms;
memcpy( &numGeoms, sourceWkb, 4 );
sourceWkb += 4;
wkb1 += 4;
memcpy( targetWkb, &numGeoms, 4 );
targetWkb += 4;
targetWkbSize += 4;
for ( int i = 0; i < numGeoms; ++i )
{
size_t sourceWkbSize_i = 0;
size_t targetWkbSize_i = 0;
// ... calculate the wkb-size of the current child complex geometry
if ( flatType == QGis::WKBMultiLineString )
{
int numPoints_i;
memcpy( &numPoints_i, wkb1+5, 4 );
int wkbSize_i = 4 + numPoints_i * (hasZValue ? 3 : 2) * sizeof(double);
sourceWkbSize_i += 5 + wkbSize_i;
wkb1 += 5 + wkbSize_i;
}
else
{
int numPrings_i;
memcpy( &numPrings_i, wkb1+5, 4 );
sourceWkbSize_i = 9;
wkb1 += 9;
for (int j = 0; j < numPrings_i; ++j)
{
int numPoints_i;
memcpy( &numPoints_i, wkb1, 4);
int wkbSize_i = 4 + numPoints_i * (hasZValue ? 3 : 2) * sizeof(double);
sourceWkbSize_i += wkbSize_i;
wkb1 += wkbSize_i;
}
}
result |= simplifyWkbGeometry( simplifyFlags, QGis::singleType(wkbType), sourceWkb, sourceWkbSize_i, targetWkb, targetWkbSize_i, envelope, map2pixelTol, true, false );
sourceWkb += sourceWkbSize_i;
targetWkb += targetWkbSize_i;
targetWkbSize += targetWkbSize_i;
}
}
return result;
}
//////////////////////////////////////////////////////////////////////////////////////////////
//! Returns whether the envelope can be replaced by its BBOX when is applied the specified map2pixel context
bool QgsMapToPixelSimplifier::canbeGeneralizedByMapBoundingBox( const QgsRectangle& envelope, const QgsCoordinateTransform* coordinateTransform, const QgsMapToPixel* mapToPixel, float mapToPixelTol )
{
double map2pixelTol = mapToPixelTol * calculateViewPixelTolerance( envelope, coordinateTransform, mapToPixel );
// Can replace the geometry by its BBOX ?
if ( (envelope.xMaximum()-envelope.xMinimum()) < map2pixelTol && (envelope.yMaximum()-envelope.yMinimum()) < map2pixelTol )
{
return true;
}
return false;
}
//! Returns a simplified version the specified geometry (Removing duplicated points) when is applied the specified map2pixel context
QgsGeometry* QgsMapToPixelSimplifier::simplify( QgsGeometry* geometry )
{
QgsGeometry* g = new QgsGeometry();
size_t wkbSize = geometry->wkbSize();
unsigned char* wkb = (unsigned char*)malloc( wkbSize );
memcpy( wkb, geometry->asWkb(), wkbSize );
g->fromWkb( wkb, wkbSize );
simplifyGeometry( g, mSimplifyFlags, mMapCoordTransform, mMapToPixel, mMapToPixelTol );
return g;
}
//! Simplifies the geometry (Removing duplicated points) when is applied the specified map2pixel context
bool QgsMapToPixelSimplifier::simplifyGeometry( QgsGeometry* geometry, int simplifyFlags, const QgsCoordinateTransform* coordinateTransform, const QgsMapToPixel* mapToPixel, float mapToPixelTol )
{
size_t targetWkbSize = 0;
// Check whether the geometry can be simplified using the map2pixel context
QGis::GeometryType geometryType = geometry->type();
if ( !(geometryType==QGis::Line || geometryType==QGis::Polygon) ) return false;
QgsRectangle envelope = geometry->boundingBox();
QGis::WkbType wkbType = geometry->wkbType();
double map2pixelTol = mapToPixelTol * calculateViewPixelTolerance( envelope, coordinateTransform, mapToPixel );
unsigned char* wkb = (unsigned char*)geometry->asWkb( );
size_t wkbSize = geometry->wkbSize( );
// Simplify the geometry rewriting temporally its WKB-stream for saving calloc's.
if ( simplifyWkbGeometry( simplifyFlags, wkbType, wkb, wkbSize, wkb, targetWkbSize, envelope, map2pixelTol ) )
{
unsigned char* targetWkb = (unsigned char*)malloc( targetWkbSize );
memcpy( targetWkb, wkb, targetWkbSize );
geometry->fromWkb( targetWkb, targetWkbSize );
return true;
}
return false;
}
//! Simplifies the geometry (Removing duplicated points) when is applied the specified map2pixel context
bool QgsMapToPixelSimplifier::simplifyGeometry( QgsGeometry* geometry )
{
return simplifyGeometry( geometry, mSimplifyFlags, mMapCoordTransform, mMapToPixel, mMapToPixelTol );
}