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 QgsGeometry generalizeWkbGeometryByBoundingBox(
86 const double x1 = envelope.
xMinimum();
87 const double y1 = envelope.
yMinimum();
88 const double x2 = envelope.
xMaximum();
89 const double y2 = envelope.
yMaximum();
94 return QgsGeometry( qgis::make_unique< QgsLineString >( QVector<double>() << x1 << x2, QVector<double>() << y1 << y2 ) );
102 QgsGeometry QgsMapToPixelSimplifier::simplifyGeometry(
110 bool isGeneralizable =
true;
116 return generalizeWkbGeometryByBoundingBox( wkbType, geometry, envelope );
120 isGeneralizable =
false;
128 const int numPoints = srcCurve.
numPoints();
130 std::unique_ptr<QgsCurve> output;
132 QVector< double > lineStringX;
133 QVector< double > lineStringY;
138 lineStringX.reserve( numPoints );
139 lineStringY.reserve( numPoints );
146 double x = 0.0, y = 0.0, lastX = 0.0, lastY = 0.0;
150 if ( numPoints <= ( isaLinearRing ? 4 : 2 ) )
151 isGeneralizable =
false;
154 bool hasLongSegments =
false;
168 double gridOriginX = envelope.
xMinimum();
169 double gridOriginY = envelope.
yMinimum();
172 float gridInverseSizeXY = map2pixelTol != 0 ? ( float )( 1.0
f / ( 0.8 * map2pixelTol ) ) : 0.0
f;
174 for (
int i = 0; i < numPoints; ++i )
176 x = srcCurve.
xAt( i );
177 y = srcCurve.
yAt( i );
181 !
equalSnapToGrid( x, y, lastX, lastY, gridOriginX, gridOriginY, gridInverseSizeXY ) ||
182 ( !isaLinearRing && ( i == 1 || i >= numPoints - 2 ) ) )
188 lineStringX.append( x );
189 lineStringY.append( y );
202 map2pixelTol *= map2pixelTol;
204 EFFECTIVE_AREAS ea( srcCurve );
207 ptarray_calc_areas( &ea, isaLinearRing ? 4 : 2, set_area, map2pixelTol );
209 for (
int i = 0; i < numPoints; ++i )
211 if ( ea.res_arealist[ i ] > map2pixelTol )
214 output->insertVertex(
QgsVertexId( 0, 0, output->numPoints() ), ea.inpts.at( i ) );
217 lineStringX.append( ea.inpts.at( i ).x() );
218 lineStringY.append( ea.inpts.at( i ).y() );
227 map2pixelTol *= map2pixelTol;
229 for (
int i = 0; i < numPoints; ++i )
231 x = srcCurve.
xAt( i );
232 y = srcCurve.
yAt( i );
234 isLongSegment =
false;
239 ( !isaLinearRing && ( i == 1 || i >= numPoints - 2 ) ) )
245 lineStringX.append( x );
246 lineStringY.append( y );
251 hasLongSegments |= isLongSegment;
261 output = qgis::make_unique< QgsLineString >( lineStringX, lineStringY );
263 if ( output->numPoints() < ( isaLinearRing ? 4 : 2 ) )
266 if ( !hasLongSegments )
270 return generalizeWkbGeometryByBoundingBox( wkbType, geometry, r );
295 std::unique_ptr<QgsPolygon> polygon(
new QgsPolygon() );
300 polygon->addInteriorRing( qgsgeometry_cast<QgsCurve *>( simplifyGeometry(
simplifyFlags,
simplifyAlgorithm, sub->
wkbType(), *sub, envelope, map2pixelTol, true ).constGet()->clone() ) );
309 for (
int i = 0; i < numGeoms; ++i )
324 return envelope.
width() < map2pixelTol && envelope.
height() < map2pixelTol;
349 if ( numPoints <= ( isaLinearRing ? 6 : 3 ) )
const QgsCurve * interiorRing(int i) const
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.)
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.
bool insertVertex(double x, double y, int beforeVertex)
Insert a new vertex before the given vertex index, ring and item (first number is index 0) If the req...
SimplifyAlgorithm
Types of simplification algorithms that can be used.
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
static QgsGeometry fromRect(const QgsRectangle &rect)
Creates a new geometry from a QgsRectangle.
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...
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.
const QgsCurve * exteriorRing() const
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)
Expand the rectangle so that 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.
const QgsAbstractGeometry * geometryN(int n) const
Returns a const reference to a geometry from within the collection.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
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.
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.