31 , mGapThresholdMapUnits( configuration.value( QStringLiteral(
"gapThreshold" ) ).toDouble() )
37 if ( configuration.value( QStringLiteral(
"allowedGapsEnabled" ) ).toBool() )
42 mAllowedGapsLayer = layer;
43 mAllowedGapsSource = qgis::make_unique<QgsVectorLayerFeatureSource>( layer );
45 mAllowedGapsBuffer = configuration.value( QStringLiteral(
"allowedGapsBuffer" ) ).toDouble();
50 mAllowedGapsSource.reset();
59 std::unique_ptr<QgsAbstractGeometry> allowedGapsGeom;
60 std::unique_ptr<QgsGeometryEngine> allowedGapsGeomEngine;
62 if ( mAllowedGapsSource )
64 QVector<QgsGeometry> allowedGaps;
74 allowedGaps.append( gg );
81 allowedGapsGeom.reset( allowedGapsEngine->combine( allowedGaps, &errMsg ) );
83 allowedGapsGeomEngine->prepareGeometry();
86 QVector<QgsGeometry> geomList;
91 geomList.append( layerFeature.geometry() );
100 if ( geomList.isEmpty() )
109 std::unique_ptr<QgsAbstractGeometry> unionGeom( geomEngine->combine( geomList, &errMsg ) );
112 messages.append( tr(
"Gap check: %1" ).arg( errMsg ) );
118 geomEngine->prepareGeometry();
119 std::unique_ptr<QgsAbstractGeometry> envelope( geomEngine->envelope( &errMsg ) );
122 messages.append( tr(
"Gap check: %1" ).arg( errMsg ) );
128 geomEngine->prepareGeometry();
129 QgsAbstractGeometry *bufEnvelope = geomEngine->buffer( 2, 0, GEOSBUF_CAP_SQUARE, GEOSBUF_JOIN_MITRE, 4. );
130 envelope.reset( bufEnvelope );
134 geomEngine->prepareGeometry();
135 std::unique_ptr<QgsAbstractGeometry> diffGeom( geomEngine->difference( unionGeom.get(), &errMsg ) );
138 messages.append( tr(
"Gap check: %1" ).arg( errMsg ) );
163 QMap<QString, QgsFeatureIds> neighboringIds;
170 neighboringIds[layerFeature.layer()->id()].insert( layerFeature.feature().id() );
175 if ( neighboringIds.isEmpty() )
180 if ( allowedGapsGeomEngine && allowedGapsGeomEngine->contains( gapGeom ) )
186 double area = gapGeom->
area();
194 QMetaEnum metaEnum = QMetaEnum::fromType<QgsGeometryGapCheck::ResolutionMethod>();
195 if ( !metaEnum.isValid() || !metaEnum.valueToKey( method ) )
202 switch ( methodValue )
211 if ( mergeWithNeighbor( featurePools, static_cast<QgsGeometryGapCheckError *>( error ), changes, errMsg ) )
217 error->
setFixFailed( tr(
"Failed to merge with neighbor: %1" ).arg( errMsg ) );
229 error->
setFixFailed( tr(
"Could not start editing layer %1" ).arg( layer->
name() ) );
237 error->
setFixFailed( tr(
"Could not add feature to layer %1" ).arg( layer->
name() ) );
247 error->
setFixFailed( tr(
"Allowed gaps layer could not be resolved" ) );
255 bool QgsGeometryGapCheck::mergeWithNeighbor(
const QMap<QString, QgsFeaturePool *> &featurePools,
257 Changes &changes, QString &errMsg )
const 260 QString mergeLayerId;
262 int mergePartIdx = -1;
267 const auto layerIds = err->
neighbors().keys();
269 for (
const QString &layerId : layerIds )
272 std::unique_ptr<QgsAbstractGeometry> errLayerGeom( errGeometry->
clone() );
276 const auto featureIds = err->
neighbors().value( layerId );
281 if ( !featurePool->
getFeature( testId, testFeature ) )
287 for (
int iPart = 0, nParts = testGeom->
partCount(); iPart < nParts; ++iPart )
293 mergeFeature = testFeature;
294 mergePartIdx = iPart;
295 mergeLayerId = layerId;
308 std::unique_ptr<QgsAbstractGeometry> errLayerGeom( errGeometry->
clone() );
329 QStringList methods = QStringList()
330 << tr(
"Add gap area to neighboring polygon with longest shared edge" )
331 << tr(
"No action" );
332 if ( mAllowedGapsSource )
333 methods << tr(
"Add gap to allowed exceptions" );
340 return factoryDescription();
350 return factoryFlags();
354 QString QgsGeometryGapCheck::factoryDescription()
359 QString QgsGeometryGapCheck::factoryId()
361 return QStringLiteral(
"QgsGeometryGapCheck" );
364 QgsGeometryCheck::Flags QgsGeometryGapCheck::factoryFlags()
369 QList<QgsWkbTypes::GeometryType> QgsGeometryGapCheck::factoryCompatibleGeometryTypes()
376 return factoryCompatibleGeometryTypes().contains( layer->
geometryType() );
387 return mContextBoundingBox;
399 return err && err->
layerId() == layerId() && err->
neighbors() == neighbors();
407 mNeighbors = err->mNeighbors;
408 mGapAreaBBox = err->mGapAreaBBox;
Wrapper for iterator of features from vector data provider or vector layer.
A rectangle specified with double values.
Java-style iterator for traversal of parts of a geometry.
static bool pointsFuzzyEqual(const QgsPointXY &p1, const QgsPointXY &p2, double tol)
Determine whether two points are equal up to the specified tolerance.
QList< QgsWkbTypes::GeometryType > compatibleGeometryTypes() const override
A list of geometry types for which this check can be performed.
virtual void update(const QgsGeometryCheckError *other)
Update this error with the information from other.
bool closeMatch(QgsGeometryCheckError *other) const override
Check if this error is almost equal to other.
static QgsAbstractGeometry * getGeomPart(QgsAbstractGeometry *geom, int partIdx)
double progress() const
Returns the current progress reported by the feedback object.
QList< QgsFeature > QgsFeatureList
QString description() const override
Returns a human readable description for this check.
const QgsPointXY & location() const
The location of the error in map units.
void setProgress(double progress)
Sets the current progress for the feedback object.
bool startEditing()
Makes the layer editable.
QMap< QString, QgsFeatureIds > toMap() const
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
Base configuration for geometry checks.
Contains a set of layers and feature ids in those layers to pass to a geometry check.
CheckType
The type of a check.
QgsWkbTypes::GeometryType geometryType() const
Returns point, line or polygon.
A geometry is the spatial representation of a feature.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
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...
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
virtual QgsRectangle boundingBox() const =0
Returns the minimal bounding box for the geometry.
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.
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.
void setFixFailed(const QString &reason)
Set the error status to failed and specify the reason for failure.
QIcon icon() const override
Returns an icon that should be shown for this kind of error.
Base class for feedback objects to be used for cancellation of something running in a worker thread...
An error produced by a QgsGeometryGapCheck.
bool isEditable() const FINAL
Returns true if the provider is in editing mode.
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...
static double sharedEdgeLength(const QgsAbstractGeometry *geom1, const QgsAbstractGeometry *geom2, double tol)
void update(const QgsGeometryCheckError *other) override
Update this error with the information from other.
QStringList resolutionMethods() const override
Returns a list of descriptions for available resolutions for errors.
bool getFeature(QgsFeatureId id, QgsFeature &feature)
Retrieves the feature with the specified id into feature.
A layer feature combination to uniquely identify and access a feature in a set of layers...
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.
QgsRectangle contextBoundingBox() const override
The context of the error.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=nullptr) FINAL
Adds a list of features to the sink.
const double reducedTolerance
The tolerance to allow for in geometry checks.
This class implements a geometry check.
virtual double area() const
Returns the planar, 2-dimensional area of the geometry.
QgsGeometryCheck::Flags flags() const override
Flags for this geometry check.
Abstract base class for all geometries.
QMap< QString, QgsFeatureIds > allLayerFeatureIds(const QMap< QString, QgsFeaturePool *> &featurePools) const
Returns all layers and feature ids.
const QString & layerId() const
The id of the layer on which this error has been detected.
ResolutionMethod
Resolution methods for geometry gap checks.
const QgsGeometryCheckContext * mContext
const QgsCoordinateReferenceSystem mapCrs
The coordinate system in which calculations should be done.
A list of layers and feature ids for each of these layers.
const QMap< QString, QgsFeatureIds > & neighbors() const
A map of layers and feature ids of the neighbors of the gap.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
QgsGeometry geometry() const
The geometry of the error in map units.
static bool isSingleType(Type type)
Returns true if the WKB type is a single type.
void combineExtentWith(const QgsRectangle &rect)
Expands the rectangle so that it covers both the original rectangle and the given rectangle...
QgsGeometryGapCheck(const QgsGeometryCheckContext *context, const QVariantMap &configuration)
The configuration accepts a "gapThreshold" key which specifies the maximum gap size in squared map un...
QMap< QString, QgsFeatureIds > involvedFeatures() const override
Returns a list of involved features.
QMap< QString, QMap< QgsFeatureId, QList< QgsGeometryCheck::Change > > > Changes
A collection of changes.
bool isCanceled() const
Tells whether the operation has been canceled already.
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.
QgsAbstractGeometry * next()
Returns next part of the geometry (undefined behavior if hasNext() returns false before calling next(...
bool handleChanges(const QgsGeometryCheck::Changes &) override
Apply a list of changes.
const double tolerance
The tolerance to allow for in geometry checks.
A feature pool is based on a vector layer and caches features.
QgsRectangle affectedAreaBBox() const override
The bounding box of the affected area of the error.
void setFixed(int method)
Set the status to fixed and specify the method that has been used to fix the error.
const QgsProject * project() const
The project can be used to resolve additional layers.
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 isEqual(QgsGeometryCheckError *other) const override
Check if this error is equal to other.
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
QgsCoordinateReferenceSystem crs() const
The coordinate reference system of this layer.
const QgsGeometryCheckContext * context() const
Returns the context.
QList< int > QgsAttributeList
This represents an error reported by a geometry check.
static std::unique_ptr< QgsGeometryEngine > createGeomEngine(const QgsAbstractGeometry *geometry, double tolerance)
bool nextFeature(QgsFeature &f)
static QgsFeatureList makeFeatureCompatible(const QgsFeature &feature, const QgsVectorLayer *layer)
Converts input feature to be compatible with the given layer.
Represents a vector layer which manages a vector based data sets.
QString id() const override
Returns an id for this check.
Merge the gap with the polygon with the longest shared edge.
bool hasNext() const
Find out whether there are more parts.
This geometry check should be available in layer validation on the vector layer peroperties.
virtual int partCount() const =0
Returns count of parts contained in the geometry.
The check controls a whole layer (topology checks)
const QgsCoordinateTransformContext transformContext
The coordinate transform context with which transformations will be done.