31 : mSimplifyFlags( simplifyFlags )
32 , mSimplifyAlgorithm( simplifyAlgorithm )
33 , mTolerance( tolerance )
42 float vx =
static_cast< float >( x2 - x1 );
43 float vy =
static_cast< float >( y2 - y1 );
45 return ( vx * vx ) + ( vy * vy );
50 int grid_x1 = std::round( ( x1 - gridOriginX ) * gridInverseSizeXY );
51 int grid_x2 = std::round( ( x2 - gridOriginX ) * gridInverseSizeXY );
52 if ( grid_x1 != grid_x2 )
return false;
54 int grid_y1 = std::round( ( y1 - gridOriginY ) * gridInverseSizeXY );
55 int grid_y2 = std::round( ( y2 - gridOriginY ) * gridInverseSizeXY );
56 return grid_y1 == grid_y2;
66 #include "simplify/effectivearea.h" 71 static std::unique_ptr< QgsAbstractGeometry > generalizeWkbGeometryByBoundingBox(
84 return std::unique_ptr< QgsAbstractGeometry >( geometry.
clone() );
87 const double x1 = envelope.
xMinimum();
88 const double y1 = envelope.
yMinimum();
89 const double x2 = envelope.
xMaximum();
90 const double y2 = envelope.
yMaximum();
95 return qgis::make_unique< QgsLineString >( QVector<double>() << x1 << x2, QVector<double>() << y1 << y2 );
99 std::unique_ptr< QgsLineString > ext = qgis::make_unique< QgsLineString >(
100 QVector< double >() << x1
105 QVector< double >() << y1
111 return std::move( ext );
114 std::unique_ptr< QgsPolygon > polygon = qgis::make_unique< QgsPolygon >();
115 polygon->setExteriorRing( ext.release() );
116 return std::move( polygon );
121 std::unique_ptr< QgsAbstractGeometry > QgsMapToPixelSimplifier::simplifyGeometry(
int simplifyFlags,
126 bool isGeneralizable =
true;
134 return generalizeWkbGeometryByBoundingBox( wkbType, geometry, envelope, isaLinearRing );
138 isGeneralizable =
false;
146 const int numPoints = srcCurve.
numPoints();
148 std::unique_ptr<QgsCurve> output;
150 QVector< double > lineStringX;
151 QVector< double > lineStringY;
156 lineStringX.reserve( numPoints );
157 lineStringY.reserve( numPoints );
164 double x = 0.0, y = 0.0, lastX = 0.0, lastY = 0.0;
168 if ( numPoints <= ( isaLinearRing ? 4 : 2 ) )
169 isGeneralizable =
false;
172 bool hasLongSegments =
false;
186 double gridOriginX = envelope.
xMinimum();
187 double gridOriginY = envelope.
yMinimum();
190 float gridInverseSizeXY = map2pixelTol != 0 ? ( float )( 1.0f / ( 0.8 * map2pixelTol ) ) : 0.0f;
192 const double *xData =
nullptr;
193 const double *yData =
nullptr;
200 for (
int i = 0; i < numPoints; ++i )
202 if ( xData && yData )
209 x = srcCurve.
xAt( i );
210 y = srcCurve.
yAt( i );
215 !
equalSnapToGrid( x, y, lastX, lastY, gridOriginX, gridOriginY, gridInverseSizeXY ) ||
216 ( !isaLinearRing && ( i == 1 || i >= numPoints - 2 ) ) )
222 lineStringX.append( x );
223 lineStringY.append( y );
236 map2pixelTol *= map2pixelTol;
238 EFFECTIVE_AREAS ea( srcCurve );
241 ptarray_calc_areas( &ea, isaLinearRing ? 4 : 2, set_area, map2pixelTol );
243 for (
int i = 0; i < numPoints; ++i )
245 if ( ea.res_arealist[ i ] > map2pixelTol )
248 output->insertVertex(
QgsVertexId( 0, 0, output->numPoints() ), ea.inpts.at( i ) );
251 lineStringX.append( ea.inpts.at( i ).x() );
252 lineStringY.append( ea.inpts.at( i ).y() );
261 map2pixelTol *= map2pixelTol;
263 const double *xData =
nullptr;
264 const double *yData =
nullptr;
271 for (
int i = 0; i < numPoints; ++i )
273 if ( xData && yData )
280 x = srcCurve.
xAt( i );
281 y = srcCurve.
yAt( i );
284 isLongSegment =
false;
289 ( !isaLinearRing && ( i == 1 || i >= numPoints - 2 ) ) )
295 lineStringX.append( x );
296 lineStringY.append( y );
301 hasLongSegments |= isLongSegment;
311 output = qgis::make_unique< QgsLineString >( lineStringX, lineStringY );
313 if ( output->numPoints() < ( isaLinearRing ? 4 : 2 ) )
316 if ( !hasLongSegments )
320 return generalizeWkbGeometryByBoundingBox( wkbType, geometry, r, isaLinearRing );
327 return std::unique_ptr< QgsAbstractGeometry >( geometry.
clone() );
336 output->insertVertex(
QgsVertexId( 0, 0, output->numPoints() ),
QgsPoint( output->xAt( 0 ), output->yAt( 0 ) ) );
340 return std::move( output );
345 std::unique_ptr<QgsPolygon> polygon(
new QgsPolygon() );
347 polygon->setExteriorRing( qgsgeometry_cast<QgsCurve *>( extRing.release() ) );
352 polygon->addInteriorRing( qgsgeometry_cast<QgsCurve *>( ring.release() ) );
354 return std::move( polygon );
361 for (
int i = 0; i < numGeoms; ++i )
365 collection->addGeometry( part.release() );
367 return std::move( collection );
369 return std::unique_ptr< QgsAbstractGeometry >( geometry.
clone() );
377 return envelope.
width() < map2pixelTol && envelope.
height() < map2pixelTol;
402 if ( numPoints <= ( isaLinearRing ? 6 : 3 ) )
A rectangle specified with double values.
static Type singleType(Type type)
Returns the single type for a WKB type.
void setMinimal()
Set a rectangle so that min corner is at max and max corner is at min.
bool isNull() const
Returns true if the geometry is null (ie, contains no underlying geometry accessible via geometry() )...
static bool isMultiType(Type type)
Returns true if the WKB type is a multi type.
QgsWkbTypes::Type wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
const QgsCurve * interiorRing(int i) const
A geometry is the spatial representation of a feature.
No simplification can be applied.
virtual QgsAbstractGeometry * createEmptyWithSameType() const =0
Creates a new geometry with the same class and same WKB type as the original and transfers ownership...
The simplification uses a grid (similar to ST_SnapToGrid) to remove duplicate points.
virtual QgsRectangle boundingBox() const =0
Returns the minimal bounding box for the geometry.
SimplifyAlgorithm
Types of simplification algorithms that can be used.
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
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 numInteriorRings() const
Type
The WKB type describes the number of dimensions a geometry has.
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.
SimplifyAlgorithm simplifyAlgorithm() const
Gets the local simplification algorithm of the vector layer managed.
Utility class for identifying a unique vertex within a geometry.
The geometries can be fully simplified by its BoundingBox.
double width() const
Returns the width of the rectangle.
QgsGeometryCollection * createEmptyWithSameType() const override
Creates a new geometry with the same class and same WKB type as the original and transfers ownership...
int simplifyFlags() const
Gets the simplification hints of the vector layer managed.
The simplification gives each point in a line an importance weighting, so that least important points...
T qgsgeometry_cast(const QgsAbstractGeometry *geom)
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.
virtual double xAt(int index) const =0
Returns the x-coordinate of the specified node in the line string.
Abstract base class for curved geometry type.
Abstract base class for all geometries.
QgsWkbTypes::Type wkbType() const
Returns the WKB type of the geometry.
Point geometry type, with support for z-dimension and m-values.
double mTolerance
Distance tolerance for the simplification.
int numGeometries() const
Returns the number of geometries within the collection.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
double xMaximum() const
Returns the x maximum value (right side of rectangle).
void combineExtentWith(const QgsRectangle &rect)
Expands the rectangle so that it covers both the original rectangle and the given rectangle...
QgsGeometry simplify(const QgsGeometry &geometry) const override
Returns a simplified version the specified geometry.
QgsMapToPixelSimplifier(int simplifyFlags, double tolerance, SimplifyAlgorithm simplifyAlgorithm=Distance)
Constructor.
Line string geometry type, with support for z-dimension and m-values.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
const QgsAbstractGeometry * geometryN(int n) const
Returns a const reference to a geometry from within the collection.
virtual double yAt(int index) const =0
Returns the y-coordinate of the specified node in the line string.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
double yMaximum() const
Returns the y maximum value (top side of rectangle).
int mSimplifyFlags
Current simplification flags.
SimplifyAlgorithm mSimplifyAlgorithm
Current algorithm.
The geometries can be simplified using the current map2pixel context state.
const QgsCurve * exteriorRing() const
virtual int nCoordinates() const
Returns the number of nodes contained in the geometry.
static Type flatType(Type type)
Returns the flat type for a WKB type.
virtual int numPoints() const =0
Returns the number of points in the curve.
The simplification uses the distance between points to remove duplicate points.
double height() const
Returns the height of the rectangle.