40 const float vx =
static_cast< float >( x2 - x1 );
41 const float vy =
static_cast< float >( y2 - y1 );
43 return ( vx * vx ) + ( vy * vy );
48 const int grid_x1 = std::round( ( x1 - gridOriginX ) * gridInverseSizeXY );
49 const int grid_x2 = std::round( ( x2 - gridOriginX ) * gridInverseSizeXY );
50 if ( grid_x1 != grid_x2 )
53 const int grid_y1 = std::round( ( y1 - gridOriginY ) * gridInverseSizeXY );
54 const int grid_y2 = std::round( ( y2 - gridOriginY ) * gridInverseSizeXY );
55 return grid_y1 == grid_y2;
65#include "simplify/effectivearea.h"
79 return std::unique_ptr< QgsAbstractGeometry >( geometry.
clone() );
82 const double x1 = envelope.
xMinimum();
83 const double y1 = envelope.
yMinimum();
84 const double x2 = envelope.
xMaximum();
85 const double y2 = envelope.
yMaximum();
90 return std::make_unique< QgsLineString >( QVector<double>() << x1 << x2, QVector<double>() << y1 << y2 );
94 auto ext = std::make_unique< QgsLineString >( QVector< double >() << x1 << x2 << x2 << x1 << x1, QVector< double >() << y1 << y1 << y2 << y2 << y1 );
96 return std::move( ext );
99 auto polygon = std::make_unique< QgsPolygon >();
100 polygon->setExteriorRing( ext.release() );
101 return std::move( polygon );
106std::unique_ptr< QgsAbstractGeometry > QgsMapToPixelSimplifier::simplifyGeometry(
110 bool isGeneralizable =
true;
114 const QgsRectangle envelope = geometry.
boundingBox();
117 return generalizeWkbGeometryByBoundingBox( wkbType, geometry, envelope, isaLinearRing );
121 isGeneralizable =
false;
128 const QgsCurve &srcCurve =
dynamic_cast<const QgsCurve &
>( geometry );
129 const int numPoints = srcCurve.
numPoints();
131 std::unique_ptr<QgsCurve> output;
133 QVector< double > lineStringX;
134 QVector< double > lineStringY;
135 QVector< double > lineStringZ;
136 QVector< double > lineStringM;
141 lineStringX.reserve( numPoints );
142 lineStringY.reserve( numPoints );
144 if ( geometry.
is3D() )
145 lineStringZ.reserve( numPoints );
148 lineStringM.reserve( numPoints );
155 double x = 0.0, y = 0.0, z = 0.0, m = 0.0, lastX = 0.0, lastY = 0.0;
157 if ( numPoints <= ( isaLinearRing ? 4 : 2 ) )
158 isGeneralizable =
false;
161 bool hasLongSegments =
false;
162 const bool is3D = geometry.
is3D();
163 const bool isMeasure = geometry.
isMeasure();
176 const double gridOriginX = envelope.
xMinimum();
177 const double gridOriginY = envelope.
yMinimum();
180 const float gridInverseSizeXY = map2pixelTol != 0 ? ( float ) ( 1.0f / ( 0.8 * map2pixelTol ) ) : 0.0f;
182 const double *xData =
nullptr;
183 const double *yData =
nullptr;
184 const double *zData =
nullptr;
185 const double *mData =
nullptr;
198 for (
int i = 0; i < numPoints; ++i )
200 if ( xData && yData )
207 x = srcCurve.
xAt( i );
208 y = srcCurve.
yAt( i );
212 z = zData ? *zData++ : srcCurve.
zAt( i );
215 m = mData ? *mData++ : srcCurve.
mAt( i );
217 if ( i == 0 || !isGeneralizable || !
equalSnapToGrid( x, y, lastX, lastY, gridOriginX, gridOriginY, gridInverseSizeXY ) || ( !isaLinearRing && ( i == 1 || i >= numPoints - 2 ) ) )
220 output->insertVertex( QgsVertexId( 0, 0, output->numPoints() ), QgsPoint( x, y, z, m ) );
223 lineStringX.append( x );
224 lineStringY.append( y );
227 lineStringZ.append( z );
230 lineStringM.append( m );
247 map2pixelTol *= map2pixelTol;
249 EFFECTIVE_AREAS ea( srcCurve );
251 const int set_area = 0;
252 ptarray_calc_areas( &ea, isaLinearRing ? 4 : 2, set_area, map2pixelTol );
254 for (
int i = 0; i < numPoints; ++i )
256 if ( ea.res_arealist[i] > map2pixelTol )
259 output->insertVertex( QgsVertexId( 0, 0, output->numPoints() ), ea.inpts.at( i ) );
262 lineStringX.append( ea.inpts.at( i ).x() );
263 lineStringY.append( ea.inpts.at( i ).y() );
266 lineStringZ.append( ea.inpts.at( i ).z() );
269 lineStringM.append( ea.inpts.at( i ).m() );
278 map2pixelTol *= map2pixelTol;
280 const double *xData =
nullptr;
281 const double *yData =
nullptr;
288 for (
int i = 0; i < numPoints; ++i )
290 if ( xData && yData )
297 x = srcCurve.
xAt( i );
298 y = srcCurve.
yAt( i );
301 isLongSegment =
false;
303 if ( i == 0 || !isGeneralizable || ( isLongSegment = (
calculateLengthSquared2D( x, y, lastX, lastY ) > map2pixelTol ) ) || ( !isaLinearRing && ( i == 1 || i >= numPoints - 2 ) ) )
306 output->insertVertex( QgsVertexId( 0, 0, output->numPoints() ), QgsPoint( x, y ) );
309 lineStringX.append( x );
310 lineStringY.append( y );
315 hasLongSegments |= isLongSegment;
323 output = std::make_unique< QgsLineString >( lineStringX, lineStringY, lineStringZ, lineStringM );
325 if ( output->numPoints() < ( isaLinearRing ? 4 : 2 ) )
328 if ( !hasLongSegments )
332 return generalizeWkbGeometryByBoundingBox( wkbType, geometry, envelope, isaLinearRing );
339 return std::unique_ptr< QgsAbstractGeometry >( geometry.
clone() );
348 output->insertVertex( QgsVertexId( 0, 0, output->numPoints() ), QgsPoint( output->xAt( 0 ), output->yAt( 0 ) ) );
352 return std::move( output );
356 const QgsPolygon &srcPolygon =
dynamic_cast<const QgsPolygon &
>( geometry );
357 auto polygon = std::make_unique<QgsPolygon>();
359 polygon->setExteriorRing( qgis::down_cast<QgsCurve *>( extRing.release() ) );
364 polygon->addInteriorRing( qgis::down_cast<QgsCurve *>( ring.release() ) );
366 return std::move( polygon );
370 const QgsGeometryCollection &srcCollection =
dynamic_cast<const QgsGeometryCollection &
>( geometry );
373 collection->reserve( numGeoms );
374 for (
int i = 0; i < numGeoms; ++i )
376 const QgsAbstractGeometry *sub = srcCollection.
geometryN( i );
378 collection->addGeometry( part.release() );
380 return std::move( collection );
382 return std::unique_ptr< QgsAbstractGeometry >( geometry.
clone() );
390 return envelope.
width() < map2pixelTol && envelope.
height() < map2pixelTol;
415 if ( numPoints <= ( isaLinearRing ? 6 : 3 ) )
459 if ( numPoints <= ( isaLinearRing ? 6 : 3 ) )
VectorSimplificationAlgorithm
Simplification algorithms for vector features.
@ Distance
The simplification uses the distance between points to remove duplicate points.
@ SnappedToGridGlobal
Snap to a global grid based on the tolerance. Good for consistent results for incoming vertices,...
@ SnapToGrid
The simplification uses a grid (similar to ST_SnapToGrid) to remove duplicate points.
@ Visvalingam
The simplification gives each point in a line an importance weighting, so that least important points...
WkbType
The WKB type describes the number of dimensions a geometry has.
@ CircularString
CircularString.
Abstract base class for all geometries.
virtual QgsAbstractGeometry * snappedToGrid(double hSpacing, double vSpacing, double dSpacing=0, double mSpacing=0, bool removeRedundantPoints=false) const =0
Makes a new geometry with all the points or vertices snapped to the closest point of the grid.
bool isMeasure() const
Returns true if the geometry contains m values.
virtual QgsRectangle boundingBox() const
Returns the minimal bounding box for the geometry.
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
virtual QgsAbstractGeometry * createEmptyWithSameType() const =0
Creates a new geometry with the same class and same WKB type as the original and transfers ownership.
virtual int nCoordinates() const
Returns the number of nodes contained in the geometry.
Qgis::WkbType wkbType() const
Returns the WKB type of the geometry.
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
int numInteriorRings() const
Returns the number of interior rings contained with the curve polygon.
const QgsCurve * exteriorRing() const
Returns the curve polygon's exterior ring.
const QgsCurve * interiorRing(int i) const
Retrieves an interior ring from the curve polygon.
virtual int numPoints() const =0
Returns the number of points in the curve.
virtual double xAt(int index) const =0
Returns the x-coordinate of the specified node in the line string.
virtual double zAt(int index) const =0
Returns the z-coordinate of the specified node in the line string.
virtual double mAt(int index) const =0
Returns the m-coordinate of the specified node in the line string.
virtual double yAt(int index) const =0
Returns the y-coordinate of the specified node in the line string.
QgsGeometryCollection * createEmptyWithSameType() const override
Creates a new geometry with the same class and same WKB type as the original and transfers ownership.
int numGeometries() const
Returns the number of geometries within the collection.
const QgsAbstractGeometry * geometryN(int n) const
Returns a const reference to a geometry from within the collection.
A geometry is the spatial representation of a feature.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
Qgis::WkbType wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.).
double mTolerance
Distance tolerance for the simplification.
static float calculateLengthSquared2D(double x1, double y1, double x2, double y2)
Returns the squared 2D-distance of the vector defined by the two points specified.
static bool isGeneralizableByMapBoundingBox(const QgsRectangle &envelope, double map2pixelTol)
Returns whether the envelope can be replaced by its BBOX when is applied the specified map2pixel cont...
int simplifyFlags() const
Gets the simplification hints of the vector layer managed.
Qgis::VectorSimplificationAlgorithm mSimplifyAlgorithm
Current algorithm.
@ NoFlags
No simplification can be applied.
@ SimplifyEnvelope
The geometries can be fully simplified by its BoundingBox.
@ SimplifyGeometry
The geometries can be simplified using the current map2pixel context state.
int mSimplifyFlags
Current simplification flags.
static bool equalSnapToGrid(double x1, double y1, double x2, double y2, double gridOriginX, double gridOriginY, float gridInverseSizeXY)
Returns whether the points belong to the same grid.
QgsGeometry simplify(const QgsGeometry &geometry) const override
Returns a simplified version the specified geometry.
Qgis::VectorSimplificationAlgorithm simplifyAlgorithm() const
Gets the local simplification algorithm of the vector layer managed.
QgsMapToPixelSimplifier(int simplifyFlags, double tolerance, Qgis::VectorSimplificationAlgorithm simplifyAlgorithm=Qgis::VectorSimplificationAlgorithm::Distance)
Constructor.
A rectangle specified with double values.
static Qgis::WkbType singleType(Qgis::WkbType type)
Returns the single type for a WKB type.
static Qgis::WkbType flatType(Qgis::WkbType type)
Returns the flat type for a WKB type.
static Q_INVOKABLE bool isMultiType(Qgis::WkbType type)
Returns true if the WKB type is a multi type.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
T qgsgeometry_cast(QgsAbstractGeometry *geom)