QgsGeos class

Does vector analysis using the geos library and handles import, export, exception handling*.

Base classes

class QgsGeometryEngine
Contains geometry relation and modification algorithms.

Public static functions

static auto addPart(QgsGeometry& geometry, GEOSGeometry* newPart) -> QgsGeometry::OperationResult
Adds a new island polygon to a multipolygon feature.
static auto asGeos(const QgsGeometry& geometry, double precision = 0) -> geos::unique_ptr
Returns a geos geometry - caller takes ownership of the object (should be deleted with GEOSGeom_destroy_r)
static auto asGeos(const QgsAbstractGeometry* geometry, double precision = 0) -> geos::unique_ptr
Returns a geos geometry - caller takes ownership of the object (should be deleted with GEOSGeom_destroy_r)
static auto fromGeos(const GEOSGeometry* geos) -> std::unique_ptr<QgsAbstractGeometry>
Create a geometry from a GEOSGeometry.
static auto geometryFromGeos(GEOSGeometry* geos) -> QgsGeometry
Creates a new QgsGeometry object, feeding in a geometry in GEOS format.
static auto geometryFromGeos(const geos::unique_ptr& geos) -> QgsGeometry
Creates a new QgsGeometry object, feeding in a geometry in GEOS format.
static auto polygonize(const QVector<const QgsAbstractGeometry*>& geometries, QString* errorMsg = nullptr) -> QgsGeometry
Creates a GeometryCollection geometry containing possible polygons formed from the constituent linework of a set of geometries.

Constructors, destructors, conversion operators

QgsGeos(const QgsAbstractGeometry* geometry, double precision = 0)
GEOS geometry engine constructor.

Public functions

auto centroid(QString* errorMsg = nullptr) const -> QgsPoint* override
Calculates the centroid of this.
auto clip(const QgsRectangle& rectangle, QString* errorMsg = nullptr) const -> std::unique_ptr<QgsAbstractGeometry>
Performs a fast, non-robust intersection between the geometry and a rectangle.
auto closestPoint(const QgsGeometry& other, QString* errorMsg = nullptr) const -> QgsGeometry
Returns the closest point on the geometry to the other geometry.
auto combine(const QgsAbstractGeometry* geom, QString* errorMsg = nullptr) const -> QgsAbstractGeometry* override
Calculate the combination of this and geom.
auto combine(const QVector<QgsAbstractGeometry*>& geomList, QString* errorMsg) const -> QgsAbstractGeometry* override
Calculate the combination of this and geometries.
auto combine(const QVector<QgsGeometry>& geometries, QString* errorMsg = nullptr) const -> QgsAbstractGeometry* override
Calculate the combination of this and geometries.
auto contains(const QgsAbstractGeometry* geom, QString* errorMsg = nullptr) const -> bool override
Checks if geom contains this.
auto convexHull(QString* errorMsg = nullptr) const -> QgsAbstractGeometry* override
Calculate the convex hull of this.
auto crosses(const QgsAbstractGeometry* geom, QString* errorMsg = nullptr) const -> bool override
Checks if geom crosses this.
auto delaunayTriangulation(double tolerance = 0.0, bool edgesOnly = false, QString* errorMsg = nullptr) const -> QgsGeometry
Returns the Delaunay triangulation for the vertices of the geometry.
auto difference(const QgsAbstractGeometry* geom, QString* errorMsg = nullptr) const -> QgsAbstractGeometry* override
Calculate the difference of this and geom.
auto disjoint(const QgsAbstractGeometry* geom, QString* errorMsg = nullptr) const -> bool override
Checks if geom is disjoint from this.
auto distance(const QgsAbstractGeometry* geom, QString* errorMsg = nullptr) const -> double override
Calculates the distance between this and geom.
void geometryChanged() override
Should be called whenever the geometry associated with the engine has been modified and the engine must be updated to suit.
auto hausdorffDistance(const QgsAbstractGeometry* geom, QString* errorMsg = nullptr) const -> double
Returns the Hausdorff distance between this geometry and geom.
auto hausdorffDistanceDensify(const QgsAbstractGeometry* geom, double densifyFraction, QString* errorMsg = nullptr) const -> double
Returns the Hausdorff distance between this geometry and geom.
auto intersection(const QgsAbstractGeometry* geom, QString* errorMsg = nullptr) const -> QgsAbstractGeometry* override
Calculate the intersection of this and geom.
auto intersects(const QgsAbstractGeometry* geom, QString* errorMsg = nullptr) const -> bool override
Checks if geom intersects this.
auto isEqual(const QgsAbstractGeometry* geom, QString* errorMsg = nullptr) const -> bool override
Checks if this is equal to geom.
auto isSimple(QString* errorMsg = nullptr) const -> bool override
Determines whether the geometry is simple (according to OGC definition).
auto lineLocatePoint(const QgsPoint& point, QString* errorMsg = nullptr) const -> double
Returns a distance representing the location along this linestring of the closest point on this linestring geometry to the specified point.
auto mergeLines(QString* errorMsg = nullptr) const -> QgsGeometry
Merges any connected lines in a LineString/MultiLineString geometry and converts them to single line strings.
auto overlaps(const QgsAbstractGeometry* geom, QString* errorMsg = nullptr) const -> bool override
Checks if geom overlaps this.
auto pointOnSurface(QString* errorMsg = nullptr) const -> QgsPoint* override
Calculate a point that is guaranteed to be on the surface of this.
void prepareGeometry() override
Prepares the geometry, so that subsequent calls to spatial relation methods are much faster.
auto relate(const QgsAbstractGeometry* geom, QString* errorMsg = nullptr) const -> QString override
Returns the Dimensional Extended 9 Intersection Model (DE-9IM) representation of the relationship between the geometries.
auto relatePattern(const QgsAbstractGeometry* geom, const QString& pattern, QString* errorMsg = nullptr) const -> bool override
Tests whether two geometries are related by a specified Dimensional Extended 9 Intersection Model (DE-9IM) pattern.
auto reshapeGeometry(const QgsLineString& reshapeWithLine, EngineOperationResult* errorCode, QString* errorMsg = nullptr) const -> std::unique_ptr<QgsAbstractGeometry>
Reshapes the geometry using a line.
auto shortestLine(const QgsGeometry& other, QString* errorMsg = nullptr) const -> QgsGeometry
Returns the shortest line joining this geometry to the other geometry.
auto singleSidedBuffer(double distance, int segments, int side, int joinStyle, double miterLimit, QString* errorMsg = nullptr) const -> std::unique_ptr<QgsAbstractGeometry>
Returns a single sided buffer for a geometry.
auto splitGeometry(const QgsLineString& splitLine, QVector<QgsGeometry>& newGeometries, bool topological, QgsPointSequence& topologyTestPoints, QString* errorMsg = nullptr) const -> EngineOperationResult override
Splits this geometry according to a given line.
auto subdivide(int maxNodes, QString* errorMsg = nullptr) const -> std::unique_ptr<QgsAbstractGeometry>
Subdivides the geometry.
auto symDifference(const QgsAbstractGeometry* geom, QString* errorMsg = nullptr) const -> QgsAbstractGeometry* override
Calculate the symmetric difference of this and geom.
auto touches(const QgsAbstractGeometry* geom, QString* errorMsg = nullptr) const -> bool override
Checks if geom touches this.
auto voronoiDiagram(const QgsAbstractGeometry* extent = nullptr, double tolerance = 0.0, bool edgesOnly = false, QString* errorMsg = nullptr) const -> QgsGeometry
Creates a Voronoi diagram for the nodes contained within the geometry.
auto within(const QgsAbstractGeometry* geom, QString* errorMsg = nullptr) const -> bool override
Checks if geom is within this.

Function documentation

static QgsGeometry::OperationResult QgsGeos::addPart(QgsGeometry& geometry, GEOSGeometry* newPart)

Adds a new island polygon to a multipolygon feature.

Parameters
geometry geometry to add part to
newPart part to add. Ownership is NOT transferred.
Returns OperationResult a result code: success or reason of failure

static geos::unique_ptr QgsGeos::asGeos(const QgsGeometry& geometry, double precision = 0)

Returns a geos geometry - caller takes ownership of the object (should be deleted with GEOSGeom_destroy_r)

Parameters
geometry geometry to convert to GEOS representation
precision The precision of the grid to which to snap the geometry vertices. If 0, no snapping is performed.

static geos::unique_ptr QgsGeos::asGeos(const QgsAbstractGeometry* geometry, double precision = 0)

Returns a geos geometry - caller takes ownership of the object (should be deleted with GEOSGeom_destroy_r)

Parameters
geometry geometry to convert to GEOS representation
precision The precision of the grid to which to snap the geometry vertices. If 0, no snapping is performed.

static std::unique_ptr<QgsAbstractGeometry> QgsGeos::fromGeos(const GEOSGeometry* geos)

Create a geometry from a GEOSGeometry.

Parameters
geos GEOSGeometry. Ownership is NOT transferred.

static QgsGeometry QgsGeos::geometryFromGeos(GEOSGeometry* geos)

Creates a new QgsGeometry object, feeding in a geometry in GEOS format.

This class will take ownership of the buffer.

static QgsGeometry QgsGeos::polygonize(const QVector<const QgsAbstractGeometry*>& geometries, QString* errorMsg = nullptr)

Creates a GeometryCollection geometry containing possible polygons formed from the constituent linework of a set of geometries.

The input geometries must be fully noded (i.e. nodes exist at every common intersection of the geometries). The easiest way to ensure this is to first unary union these geometries by calling combine() on the set of input geometries and then pass the result to polygonize(). An empty geometry will be returned in the case of errors.

QgsGeos::QgsGeos(const QgsAbstractGeometry* geometry, double precision = 0)

GEOS geometry engine constructor.

Parameters
geometry The geometry
precision The precision of the grid to which to snap the geometry vertices. If 0, no snapping is performed.

QgsPoint* QgsGeos::centroid(QString* errorMsg = nullptr) const override

Calculates the centroid of this.

May return a nullptr.

std::unique_ptr<QgsAbstractGeometry> QgsGeos::clip(const QgsRectangle& rectangle, QString* errorMsg = nullptr) const

Performs a fast, non-robust intersection between the geometry and a rectangle.

The returned geometry may be invalid.

QgsGeometry QgsGeos::closestPoint(const QgsGeometry& other, QString* errorMsg = nullptr) const

Returns the closest point on the geometry to the other geometry.

QgsAbstractGeometry* QgsGeos::combine(const QgsAbstractGeometry* geom, QString* errorMsg = nullptr) const override

Calculate the combination of this and geom.

QgsAbstractGeometry* QgsGeos::combine(const QVector<QgsAbstractGeometry*>& geomList, QString* errorMsg) const override

Calculate the combination of this and geometries.

QgsAbstractGeometry* QgsGeos::combine(const QVector<QgsGeometry>& geometries, QString* errorMsg = nullptr) const override

Calculate the combination of this and geometries.

bool QgsGeos::contains(const QgsAbstractGeometry* geom, QString* errorMsg = nullptr) const override

Checks if geom contains this.

bool QgsGeos::crosses(const QgsAbstractGeometry* geom, QString* errorMsg = nullptr) const override

Checks if geom crosses this.

QgsGeometry QgsGeos::delaunayTriangulation(double tolerance = 0.0, bool edgesOnly = false, QString* errorMsg = nullptr) const

Returns the Delaunay triangulation for the vertices of the geometry.

The tolerance parameter specifies an optional snapping tolerance which can be used to improve the robustness of the triangulation. If edgesOnly is true than line string boundary geometries will be returned instead of polygons. An empty geometry will be returned if the diagram could not be calculated.

QgsAbstractGeometry* QgsGeos::difference(const QgsAbstractGeometry* geom, QString* errorMsg = nullptr) const override

Calculate the difference of this and geom.

bool QgsGeos::disjoint(const QgsAbstractGeometry* geom, QString* errorMsg = nullptr) const override

Checks if geom is disjoint from this.

double QgsGeos::distance(const QgsAbstractGeometry* geom, QString* errorMsg = nullptr) const override

Calculates the distance between this and geom.

double QgsGeos::hausdorffDistance(const QgsAbstractGeometry* geom, QString* errorMsg = nullptr) const

Returns the Hausdorff distance between this geometry and geom.

This is basically a measure of how similar or dissimilar 2 geometries are.

This algorithm is an approximation to the standard Hausdorff distance. This approximation is exact or close enough for a large subset of useful cases. Examples of these are:

  • computing distance between Linestrings that are roughly parallel to each other, and roughly equal in length. This occurs in matching linear networks.
  • Testing similarity of geometries.

If the default approximate provided by this method is insufficient, use hausdorffDistanceDensify() instead.

double QgsGeos::hausdorffDistanceDensify(const QgsAbstractGeometry* geom, double densifyFraction, QString* errorMsg = nullptr) const

Returns the Hausdorff distance between this geometry and geom.

This is basically a measure of how similar or dissimilar 2 geometries are.

This function accepts a densifyFraction argument. The function performs a segment densification before computing the discrete Hausdorff distance. The densifyFraction parameter sets the fraction by which to densify each segment. Each segment will be split into a number of equal-length subsegments, whose fraction of the total length is closest to the given fraction.

This method can be used when the default approximation provided by hausdorffDistance() is not sufficient. Decreasing the densifyFraction parameter will make the distance returned approach the true Hausdorff distance for the geometries.

QgsAbstractGeometry* QgsGeos::intersection(const QgsAbstractGeometry* geom, QString* errorMsg = nullptr) const override

Calculate the intersection of this and geom.

bool QgsGeos::intersects(const QgsAbstractGeometry* geom, QString* errorMsg = nullptr) const override

Checks if geom intersects this.

bool QgsGeos::isEqual(const QgsAbstractGeometry* geom, QString* errorMsg = nullptr) const override

Checks if this is equal to geom.

If both are Null geometries, false is returned.

bool QgsGeos::isSimple(QString* errorMsg = nullptr) const override

Determines whether the geometry is simple (according to OGC definition).

double QgsGeos::lineLocatePoint(const QgsPoint& point, QString* errorMsg = nullptr) const

Returns a distance representing the location along this linestring of the closest point on this linestring geometry to the specified point.

Parameters
point point to seek proximity to
errorMsg error messages emitted, if any
Returns distance along line, or -1 on error

Ie, the returned value indicates how far along this linestring you need to traverse to get to the closest location where this linestring comes to the specified point.

QgsGeometry QgsGeos::mergeLines(QString* errorMsg = nullptr) const

Merges any connected lines in a LineString/MultiLineString geometry and converts them to single line strings.

Parameters
errorMsg if specified, will be set to any reported GEOS errors
Returns a LineString or MultiLineString geometry, with any connected lines joined. An empty geometry will be returned if the input geometry was not a LineString/MultiLineString geometry.

bool QgsGeos::overlaps(const QgsAbstractGeometry* geom, QString* errorMsg = nullptr) const override

Checks if geom overlaps this.

QgsPoint* QgsGeos::pointOnSurface(QString* errorMsg = nullptr) const override

Calculate a point that is guaranteed to be on the surface of this.

May return a nullptr.

void QgsGeos::prepareGeometry() override

Prepares the geometry, so that subsequent calls to spatial relation methods are much faster.

This should be called for any geometry which is used for multiple relation tests against other geometries.

QString QgsGeos::relate(const QgsAbstractGeometry* geom, QString* errorMsg = nullptr) const override

Returns the Dimensional Extended 9 Intersection Model (DE-9IM) representation of the relationship between the geometries.

Parameters
geom geometry to relate to
errorMsg destination storage for any error message
Returns DE-9IM string for relationship, or an empty string if an error occurred

bool QgsGeos::relatePattern(const QgsAbstractGeometry* geom, const QString& pattern, QString* errorMsg = nullptr) const override

Tests whether two geometries are related by a specified Dimensional Extended 9 Intersection Model (DE-9IM) pattern.

Parameters
geom geometry to relate to
pattern DE-9IM pattern for match
errorMsg destination storage for any error message
Returns true if geometry relationship matches with pattern

std::unique_ptr<QgsAbstractGeometry> QgsGeos::reshapeGeometry(const QgsLineString& reshapeWithLine, EngineOperationResult* errorCode, QString* errorMsg = nullptr) const

Reshapes the geometry using a line.

Parameters
reshapeWithLine the line used to reshape lines or polygons
errorCode if specified, provides result of operation (success or reason of failure)
errorMsg if specified, provides more details about failure
Returns the reshaped geometry

QgsGeometry QgsGeos::shortestLine(const QgsGeometry& other, QString* errorMsg = nullptr) const

Returns the shortest line joining this geometry to the other geometry.

std::unique_ptr<QgsAbstractGeometry> QgsGeos::singleSidedBuffer(double distance, int segments, int side, int joinStyle, double miterLimit, QString* errorMsg = nullptr) const

Returns a single sided buffer for a geometry.

Parameters
distance buffer distance
segments for round joins, number of segments to approximate quarter-circle
side side of geometry to buffer (0 = left, 1 = right)
joinStyle join style for corners ( Round (1) / Miter (2) / Bevel (3) )
miterLimit limit on the miter ratio used for very sharp corners
errorMsg error messages emitted, if any
Returns buffered geometry, or an nullptr if buffer could not be calculated

The buffer is only applied to one side of the geometry.

EngineOperationResult QgsGeos::splitGeometry(const QgsLineString& splitLine, QVector<QgsGeometry>& newGeometries, bool topological, QgsPointSequence& topologyTestPoints, QString* errorMsg = nullptr) const override

Splits this geometry according to a given line.

Parameters
splitLine the line that splits the geometry
newGeometries out list of new geometries that have been created with the split
topological true if topological editing is enabled
topologyTestPoints out points that need to be tested for topological completeness in the dataset
errorMsg out error messages emitted, if any
Returns 0 in case of success, 1 if geometry has not been split, error else

std::unique_ptr<QgsAbstractGeometry> QgsGeos::subdivide(int maxNodes, QString* errorMsg = nullptr) const

Subdivides the geometry.

The returned geometry will be a collection containing subdivided parts from the original geometry, where no part has more then the specified maximum number of nodes (maxNodes).

This is useful for dividing a complex geometry into less complex parts, which are better able to be spatially indexed and faster to perform further operations such as intersects on. The returned geometry parts may not be valid and may contain self-intersections.

The minimum allowed value for maxNodes is 8.

Curved geometries are not supported.

QgsAbstractGeometry* QgsGeos::symDifference(const QgsAbstractGeometry* geom, QString* errorMsg = nullptr) const override

Calculate the symmetric difference of this and geom.

bool QgsGeos::touches(const QgsAbstractGeometry* geom, QString* errorMsg = nullptr) const override

Checks if geom touches this.

QgsGeometry QgsGeos::voronoiDiagram(const QgsAbstractGeometry* extent = nullptr, double tolerance = 0.0, bool edgesOnly = false, QString* errorMsg = nullptr) const

Creates a Voronoi diagram for the nodes contained within the geometry.

Returns the Voronoi polygons for the nodes contained within the geometry. If extent is specified then it will be used as a clipping envelope for the diagram. If no extent is set then the clipping envelope will be automatically calculated. In either case the diagram will be clipped to the larger of the provided envelope OR the envelope surrounding all input nodes. The tolerance parameter specifies an optional snapping tolerance which can be used to improve the robustness of the diagram calculation. If edgesOnly is true than line string boundary geometries will be returned instead of polygons. An empty geometry will be returned if the diagram could not be calculated.

bool QgsGeos::within(const QgsAbstractGeometry* geom, QString* errorMsg = nullptr) const override

Checks if geom is within this.