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;
166 if ( numPoints <= ( isaLinearRing ? 4 : 2 ) )
167 isGeneralizable =
false;
170 bool hasLongSegments =
false;
184 double gridOriginX = envelope.
xMinimum();
185 double gridOriginY = envelope.
yMinimum();
188 float gridInverseSizeXY = map2pixelTol != 0 ? ( float )( 1.0f / ( 0.8 * map2pixelTol ) ) : 0.0f;
190 const double *xData =
nullptr;
191 const double *yData =
nullptr;
198 for (
int i = 0; i < numPoints; ++i )
200 if ( xData && yData )
207 x = srcCurve.
xAt( i );
208 y = srcCurve.
yAt( i );
213 !
equalSnapToGrid( x, y, lastX, lastY, gridOriginX, gridOriginY, gridInverseSizeXY ) ||
214 ( !isaLinearRing && ( i == 1 || i >= numPoints - 2 ) ) )
220 lineStringX.append( x );
221 lineStringY.append( y );
232 map2pixelTol *= map2pixelTol;
234 EFFECTIVE_AREAS ea( srcCurve );
237 ptarray_calc_areas( &ea, isaLinearRing ? 4 : 2, set_area, map2pixelTol );
239 for (
int i = 0; i < numPoints; ++i )
241 if ( ea.res_arealist[ i ] > map2pixelTol )
244 output->insertVertex(
QgsVertexId( 0, 0, output->numPoints() ), ea.inpts.at( i ) );
247 lineStringX.append( ea.inpts.at( i ).x() );
248 lineStringY.append( ea.inpts.at( i ).y() );
257 map2pixelTol *= map2pixelTol;
259 const double *xData =
nullptr;
260 const double *yData =
nullptr;
267 for (
int i = 0; i < numPoints; ++i )
269 if ( xData && yData )
276 x = srcCurve.
xAt( i );
277 y = srcCurve.
yAt( i );
280 isLongSegment =
false;
285 ( !isaLinearRing && ( i == 1 || i >= numPoints - 2 ) ) )
291 lineStringX.append( x );
292 lineStringY.append( y );
297 hasLongSegments |= isLongSegment;
305 output = qgis::make_unique< QgsLineString >( lineStringX, lineStringY );
307 if ( output->numPoints() < ( isaLinearRing ? 4 : 2 ) )
310 if ( !hasLongSegments )
314 return generalizeWkbGeometryByBoundingBox( wkbType, geometry, envelope, isaLinearRing );
321 return std::unique_ptr< QgsAbstractGeometry >( geometry.
clone() );
330 output->insertVertex(
QgsVertexId( 0, 0, output->numPoints() ),
QgsPoint( output->xAt( 0 ), output->yAt( 0 ) ) );
334 return std::move( output );
339 std::unique_ptr<QgsPolygon> polygon(
new QgsPolygon() );
341 polygon->setExteriorRing( qgsgeometry_cast<QgsCurve *>( extRing.release() ) );
346 polygon->addInteriorRing( qgsgeometry_cast<QgsCurve *>( ring.release() ) );
348 return std::move( polygon );
355 for (
int i = 0; i < numGeoms; ++i )
359 collection->addGeometry( part.release() );
361 return std::move( collection );
363 return std::unique_ptr< QgsAbstractGeometry >( geometry.
clone() );
371 return envelope.
width() < map2pixelTol && envelope.
height() < map2pixelTol;
396 if ( numPoints <= ( isaLinearRing ? 6 : 3 ) )
const QgsCurve * exteriorRing() const
Returns the curve polygon's exterior ring.
A rectangle specified with double values.
static Type singleType(Type type)
Returns the single type for a WKB type.
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.
double yMaximum() const
Returns the y maximum value (top side of rectangle).
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
SimplifyAlgorithm simplifyAlgorithm() const
Gets the local simplification algorithm of the vector layer managed.
QgsWkbTypes::Type wkbType() const
Returns the WKB type of the geometry.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
A geometry is the spatial representation of a feature.
No simplification can be applied.
const QgsAbstractGeometry * geometryN(int n) const
Returns a const reference to a geometry from within the collection.
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.
int numInteriorRings() const
Returns the number of interior rings contained with the curve polygon.
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...
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
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.
virtual int nCoordinates() const
Returns the number of nodes contained in the geometry.
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
double xMaximum() const
Returns the x maximum value (right side of rectangle).
Utility class for identifying a unique vertex within a geometry.
The geometries can be fully simplified by its BoundingBox.
QgsGeometryCollection * createEmptyWithSameType() const override
Creates a new geometry with the same class and same WKB type as the original and transfers ownership...
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.
int simplifyFlags() const
Gets the simplification hints of the vector layer managed.
Abstract base class for curved geometry type.
int numGeometries() const
Returns the number of geometries within the collection.
Abstract base class for all geometries.
Point geometry type, with support for z-dimension and m-values.
double mTolerance
Distance tolerance for the simplification.
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.
const QgsCurve * interiorRing(int i) const
Retrieves an interior ring from the curve polygon.
virtual double yAt(int index) const =0
Returns the y-coordinate of the specified node in the line string.
int mSimplifyFlags
Current simplification flags.
SimplifyAlgorithm mSimplifyAlgorithm
Current algorithm.
The geometries can be simplified using the current map2pixel context state.
double width() const
Returns the width of the rectangle.
static Type flatType(Type type)
Returns the flat type for a WKB type.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
QgsWkbTypes::Type wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
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.