QGIS API Documentation
3.14.0-Pi (9f7028fd23)
|
Go to the documentation of this file.
35 , mGapThresholdMapUnits( configuration.value( QStringLiteral(
"gapThreshold" ) ).toDouble() )
41 if ( configuration.value( QStringLiteral(
"allowedGapsEnabled" ) ).toBool() )
46 mAllowedGapsLayer = layer;
47 mAllowedGapsSource = qgis::make_unique<QgsVectorLayerFeatureSource>( layer );
49 mAllowedGapsBuffer = configuration.value( QStringLiteral(
"allowedGapsBuffer" ) ).toDouble();
54 mAllowedGapsSource.reset();
63 std::unique_ptr<QgsAbstractGeometry> allowedGapsGeom;
64 std::unique_ptr<QgsGeometryEngine> allowedGapsGeomEngine;
66 if ( mAllowedGapsSource )
68 QVector<QgsGeometry> allowedGaps;
78 allowedGaps.append( gg );
85 allowedGapsGeom.reset( allowedGapsEngine->combine( allowedGaps, &errMsg ) );
87 allowedGapsGeomEngine->prepareGeometry();
90 QVector<QgsGeometry> geomList;
95 geomList.append( layerFeature.geometry() );
104 if ( geomList.isEmpty() )
113 std::unique_ptr<QgsAbstractGeometry> unionGeom( geomEngine->combine( geomList, &errMsg ) );
116 messages.append( tr(
"Gap check: %1" ).arg( errMsg ) );
122 geomEngine->prepareGeometry();
123 std::unique_ptr<QgsAbstractGeometry> envelope( geomEngine->envelope( &errMsg ) );
126 messages.append( tr(
"Gap check: %1" ).arg( errMsg ) );
132 geomEngine->prepareGeometry();
133 QgsAbstractGeometry *bufEnvelope = geomEngine->buffer( 2, 0, GEOSBUF_CAP_SQUARE, GEOSBUF_JOIN_MITRE, 4. );
134 envelope.reset( bufEnvelope );
138 geomEngine->prepareGeometry();
139 std::unique_ptr<QgsAbstractGeometry> diffGeom( geomEngine->difference( unionGeom.get(), &errMsg ) );
142 messages.append( tr(
"Gap check: %1" ).arg( errMsg ) );
167 QMap<QString, QgsFeatureIds> neighboringIds;
174 neighboringIds[layerFeature.layer()->id()].insert( layerFeature.feature().id() );
179 if ( neighboringIds.isEmpty() )
184 if ( allowedGapsGeomEngine && allowedGapsGeomEngine->contains( gapGeom ) )
190 double area = gapGeom->
area();
198 QMetaEnum metaEnum = QMetaEnum::fromType<QgsGeometryGapCheck::ResolutionMethod>();
199 if ( !metaEnum.isValid() || !metaEnum.valueToKey( method ) )
206 switch ( methodValue )
215 if ( mergeWithNeighbor( featurePools,
static_cast<QgsGeometryGapCheckError *
>( error ), changes, errMsg, LongestSharedEdge ) )
221 error->
setFixFailed( tr(
"Failed to merge with neighbor: %1" ).arg( errMsg ) );
228 QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( mAllowedGapsLayer.data() );
233 error->
setFixFailed( tr(
"Could not start editing layer %1" ).arg( layer->
name() ) );
241 error->
setFixFailed( tr(
"Could not add feature to layer %1" ).arg( layer->
name() ) );
251 error->
setFixFailed( tr(
"Allowed gaps layer could not be resolved" ) );
277 error->
setFixFailed( tr(
"Could not resolve target layer %1 to add feature" ).arg( error->
layerId() ) );
285 if ( mergeWithNeighbor( featurePools,
static_cast<QgsGeometryGapCheckError *
>( error ), changes, errMsg, LargestArea ) )
291 error->
setFixFailed( tr(
"Failed to merge with neighbor: %1" ).arg( errMsg ) );
299 bool QgsGeometryGapCheck::mergeWithNeighbor(
const QMap<QString, QgsFeaturePool *> &featurePools,
QgsGeometryGapCheckError *err, Changes &changes, QString &errMsg, Condition condition )
const
302 QString mergeLayerId;
304 int mergePartIdx = -1;
309 const auto layerIds = err->
neighbors().keys();
310 QList<QgsFeature> neighbours;
313 for (
const QString &layerId : layerIds )
316 std::unique_ptr<QgsAbstractGeometry> errLayerGeom( errGeometry->
clone() );
320 const auto featureIds = err->
neighbors().value( layerId );
325 if ( !featurePool->
getFeature( testId, feature ) )
333 neighbours.append( feature );
336 for (
const QgsFeature &testFeature : neighbours )
338 const QgsGeometry featureGeom = testFeature.geometry();
340 for (
int iPart = 0, nParts = testGeom->
partCount(); iPart < nParts; ++iPart )
345 case LongestSharedEdge:
357 mergeFeature = testFeature;
358 mergePartIdx = iPart;
359 mergeLayerId = layerId;
370 QgsSpatialIndex neighbourIndex( QgsSpatialIndex::Flag::FlagStoreFeatureGeometries );
371 neighbourIndex.addFeatures( neighbours );
379 QgsGeometry closestGeom = neighbourIndex.geometry( neighbourIndex.nearestNeighbor(
QgsPointXY( pt ) ).first() );
383 snappedRing.append( closestPoint );
387 std::unique_ptr<QgsPolygon> snappedErrGeom = qgis::make_unique<QgsPolygon>();
388 snappedErrGeom->setExteriorRing(
new QgsLineString( snappedRing ) );
392 std::unique_ptr<QgsAbstractGeometry> errLayerGeom( snappedErrGeom->clone() );
413 QStringList methods = QStringList()
414 << tr(
"Add gap area to neighboring polygon with longest shared edge" )
415 << tr(
"No action" );
416 if ( mAllowedGapsSource )
417 methods << tr(
"Add gap to allowed exceptions" );
424 QList<QgsGeometryCheckResolutionMethod> fixes
431 if ( mAllowedGapsSource )
441 return factoryDescription();
451 return factoryFlags();
455 QString QgsGeometryGapCheck::factoryDescription()
460 QString QgsGeometryGapCheck::factoryId()
462 return QStringLiteral(
"QgsGeometryGapCheck" );
465 QgsGeometryCheck::Flags QgsGeometryGapCheck::factoryFlags()
470 QList<QgsWkbTypes::GeometryType> QgsGeometryGapCheck::factoryCompatibleGeometryTypes()
477 return factoryCompatibleGeometryTypes().contains( layer->geometryType() );
488 return mContextBoundingBox;
508 mNeighbors = err->mNeighbors;
509 mGapAreaBBox = err->mGapAreaBBox;
bool hasNext() const
Find out whether there are more parts.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setFixFailed(const QString &reason)
Set the error status to failed and specify the reason for failure.
bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) FINAL
Adds a list of features to the sink.
bool handleChanges(const QgsGeometryCheck::Changes &) override
Apply a list of changes.
void setProgress(double progress)
Sets the current progress for the feedback object.
OperationResult transform(const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection direction=QgsCoordinateTransform::ForwardTransform, bool transformZ=false) SIP_THROW(QgsCsException)
Transforms this geometry as described by the coordinate transform ct.
static QgsAbstractGeometry * getGeomPart(QgsAbstractGeometry *geom, int partIdx)
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
QList< QgsGeometryCheckResolutionMethod > availableResolutionMethods() const override
Returns a list of available resolution methods.
const QMap< QString, QgsFeatureIds > & neighbors() const
A map of layers and feature ids of the neighbors of the gap.
void combineExtentWith(const QgsRectangle &rect)
Expands the rectangle so that it covers both the original rectangle and the given rectangle.
@ AddToAllowedGaps
Add gap geometry to allowed gaps layer.
QList< QgsWkbTypes::GeometryType > compatibleGeometryTypes() const override
A list of geometry types for which this check can be performed.
Point geometry type, with support for z-dimension and m-values.
@ StatusFixed
The error is fixed.
QMap< QString, QMap< QgsFeatureId, QList< QgsGeometryCheck::Change > > > Changes
A collection of changes.
double progress() const
Returns the current progress reported by the feedback object.
bool isEqual(QgsGeometryCheckError *other) const override
Check if this error is equal to other.
void replaceFeatureGeometryPart(const QMap< QString, QgsFeaturePool * > &featurePools, const QString &layerId, QgsFeature &feature, int partIdx, QgsAbstractGeometry *newPartGeom, Changes &changes) const
Replaces a part in a feature geometry.
QMap< QString, QgsFeatureIds > allLayerFeatureIds(const QMap< QString, QgsFeaturePool * > &featurePools) const
Returns all layers and feature ids.
@ LayerCheck
The check controls a whole layer (topology checks)
static QgsProject * instance()
Returns the QgsProject singleton instance.
const QgsGeometryCheckContext * context() const
Returns the context.
QgsRectangle contextBoundingBox() const override
The context of the error.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Q_INVOKABLE bool startEditing()
Makes the layer editable.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
virtual int partCount() const =0
Returns count of parts contained in the geometry.
Line string geometry type, with support for z-dimension and m-values.
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
QList< int > QgsAttributeList
QgsGeometry buffer(double distance, int segments) const
Returns a buffer region around this geometry having the given width and with a specified number of se...
bool getFeature(QgsFeatureId id, QgsFeature &feature)
Retrieves the feature with the specified id into feature.
QMap< QString, QgsFeatureIds > toMap() const
@ CreateNewFeature
Create a new feature with the gap geometry.
bool isEditable() const FINAL
Returns true if the provider is in editing mode.
virtual double area() const
Returns the planar, 2-dimensional area of the geometry.
@ NoChange
Do not handle the error.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
static double sharedEdgeLength(const QgsAbstractGeometry *geom1, const QgsAbstractGeometry *geom2, double tol)
void setFixed(int method)
Set the status to fixed and specify the method that has been used to fix the error.
const QgsPointXY & location() const
The location of the error in map units.
QgsRectangle snappedToGrid(double spacing) const
Returns a copy of this rectangle that is snapped to a grid with the specified spacing between the gri...
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
@ MergeLongestEdge
Merge the gap with the polygon with the longest shared edge.
const QgsCoordinateReferenceSystem mapCrs
The coordinate system in which calculations should be done.
QList< QgsFeature > QgsFeatureList
QMap< int, QVariant > QgsAttributeMap
Status status() const
The status of the error.
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
const double reducedTolerance
The tolerance to allow for in geometry checks.
A list of layers and feature ids for each of these layers.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Q_DECL_DEPRECATED QStringList resolutionMethods() const override
Returns a list of descriptions for available resolutions for errors.
QgsPoint next()
Returns next vertex of the geometry (undefined behavior if hasNext() returns false before calling nex...
bool closeMatch(QgsGeometryCheckError *other) const override
Check if this error is almost equal to other.
static bool isSingleType(Type type)
Returns true if the WKB type is a single type.
QString id() const override
Returns an id for this check.
QgsCoordinateReferenceSystem crs() const
The coordinate reference system of this layer.
static QgsPoint closestVertex(const QgsAbstractGeometry &geom, const QgsPoint &pt, QgsVertexId &id)
Returns the closest vertex to a geometry for a specified point.
Abstract base class for all geometries.
static QgsFeature createFeature(const QgsVectorLayer *layer, const QgsGeometry &geometry=QgsGeometry(), const QgsAttributeMap &attributes=QgsAttributeMap(), QgsExpressionContext *context=nullptr)
Creates a new feature ready for insertion into a layer.
const QgsGeometryCheckContext * mContext
QgsPointSequence QgsPolyline
Polyline as represented as a vector of points.
@ AvailableInValidation
This geometry check should be available in layer validation on the vector layer peroperties.
void update(const QgsGeometryCheckError *other) override
Update this error with the information from other.
bool isCanceled() const
Tells whether the operation has been canceled already.
virtual QgsRectangle boundingBox() const =0
Returns the minimal bounding box for the geometry.
void prepare(const QgsGeometryCheckContext *context, const QVariantMap &configuration) override
Will be run in the main thread before collectErrors is called (which may be run from a background thr...
QIcon icon() const override
Returns an icon that should be shown for this kind of error.
@ MergeLargestArea
Merge with neighbouring polygon with largest area.
bool nextFeature(QgsFeature &f)
static QgsFeatureList makeFeatureCompatible(const QgsFeature &feature, const QgsVectorLayer *layer)
Converts input feature to be compatible with the given layer.
Java-style iterator for traversal of parts of a geometry.
const QgsProject * project() const
The project can be used to resolve additional layers.
QgsVertexIterator vertices() const
Returns a read-only, Java-style iterator for traversal of vertices of all the geometry,...
CheckType
The type of a check.
Utility class for identifying a unique vertex within a geometry.
QgsAbstractGeometry * next()
Returns next part of the geometry (undefined behavior if hasNext() returns false before calling next(...
virtual void update(const QgsGeometryCheckError *other)
Update this error with the information from other.
const double tolerance
The tolerance to allow for in geometry checks.
QgsGeometryCheck::Flags flags() const override
Flags for this geometry check.
const QgsGeometryCheck * mCheck
Java-style iterator for traversal of vertices of a geometry.
static std::unique_ptr< QgsGeometryEngine > createGeomEngine(const QgsAbstractGeometry *geometry, double tolerance)
bool hasNext() const
Find out whether there are more vertices.
QgsRectangle affectedAreaBBox() const override
The bounding box of the affected area of the error.
const QString & layerId() const
The id of the layer on which this error has been detected.
ResolutionMethod
Resolution methods for geometry gap checks.
void fixError(const QMap< QString, QgsFeaturePool * > &featurePools, QgsGeometryCheckError *error, int method, const QMap< QString, int > &mergeAttributeIndices, Changes &changes) const override
Fixes the error error with the specified method.
void collectErrors(const QMap< QString, QgsFeaturePool * > &featurePools, QList< QgsGeometryCheckError * > &errors, QStringList &messages, QgsFeedback *feedback, const LayerFeatureIds &ids=LayerFeatureIds()) const override
The main worker method.
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) FINAL
Adds a single feature to the sink.
QMap< QString, QgsFeatureIds > involvedFeatures() const override
Returns a list of involved features.
QString description() const override
Returns a human readable description for this check.
const QgsCoordinateTransformContext transformContext
The coordinate transform context with which transformations will be done.
QgsGeometry geometry() const
The geometry of the error in map units.
QgsGeometryGapCheck(const QgsGeometryCheckContext *context, const QVariantMap &configuration)
The configuration accepts a "gapThreshold" key which specifies the maximum gap size in squared map un...
static bool pointsFuzzyEqual(const QgsPointXY &p1, const QgsPointXY &p2, double tol)
Determine whether two points are equal up to the specified tolerance.