Skip to content

Commit

Permalink
Call prepare method on geometry checks
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Aug 8, 2019
1 parent fd64cbb commit c8b277b
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 7 deletions.
Expand Up @@ -174,6 +174,11 @@ Create a new geometry check.
%End
virtual ~QgsGeometryCheck();

virtual void prepare( const QgsGeometryCheckContext *context, const QVariantMap &configuration );
%Docstring
Will be run in the main thread before collectErrors is called (which may be run from a background thread).
%End


virtual bool isCompatible( QgsVectorLayer *layer ) const;
%Docstring
Expand Down
Expand Up @@ -12,7 +12,8 @@ struct QgsGeometryCheckContext
{
QgsGeometryCheckContext( int precision,
const QgsCoordinateReferenceSystem &mapCrs,
const QgsCoordinateTransformContext &transformContext );
const QgsCoordinateTransformContext &transformContext,
const QgsProject *project );

const double tolerance;

Expand All @@ -22,6 +23,8 @@ struct QgsGeometryCheckContext

const QgsCoordinateTransformContext transformContext;

const QgsProject *project;

private:
QgsGeometryCheckContext( const QgsGeometryCheckContext &rh );
};
Expand Down
5 changes: 5 additions & 0 deletions src/analysis/vector/geometry_checker/qgsgeometrycheck.cpp
Expand Up @@ -31,6 +31,11 @@ QgsGeometryCheck::QgsGeometryCheck( const QgsGeometryCheckContext *context, cons
, mConfiguration( configuration )
{}

void QgsGeometryCheck::prepare( const QgsGeometryCheckContext *context, const QVariantMap &configuration )
{

}

bool QgsGeometryCheck::isCompatible( QgsVectorLayer *layer ) const
{
return compatibleGeometryTypes().contains( layer->geometryType() );
Expand Down
5 changes: 5 additions & 0 deletions src/analysis/vector/geometry_checker/qgsgeometrycheck.h
Expand Up @@ -253,6 +253,11 @@ class ANALYSIS_EXPORT QgsGeometryCheck
QgsGeometryCheck( const QgsGeometryCheckContext *context, const QVariantMap &configuration );
virtual ~QgsGeometryCheck() = default;

/**
* Will be run in the main thread before collectErrors is called (which may be run from a background thread).
*/
virtual void prepare( const QgsGeometryCheckContext *context, const QVariantMap &configuration );

#ifndef SIP_RUN

/**
Expand Down
Expand Up @@ -15,10 +15,11 @@

#include "qgsgeometrycheckcontext.h"

QgsGeometryCheckContext::QgsGeometryCheckContext( int precision, const QgsCoordinateReferenceSystem &mapCrs, const QgsCoordinateTransformContext &transformContext )
QgsGeometryCheckContext::QgsGeometryCheckContext( int precision, const QgsCoordinateReferenceSystem &mapCrs, const QgsCoordinateTransformContext &transformContext, const QgsProject *project )
: tolerance( std::pow( 10, -precision ) )
, reducedTolerance( std::pow( 10, -precision / 2 ) )
, mapCrs( mapCrs )
, transformContext( transformContext )
, project( project )
{
}
Expand Up @@ -31,7 +31,8 @@ struct ANALYSIS_EXPORT QgsGeometryCheckContext
{
QgsGeometryCheckContext( int precision,
const QgsCoordinateReferenceSystem &mapCrs,
const QgsCoordinateTransformContext &transformContext );
const QgsCoordinateTransformContext &transformContext,
const QgsProject *project );

/**
* The tolerance to allow for in geometry checks.
Expand All @@ -58,6 +59,12 @@ struct ANALYSIS_EXPORT QgsGeometryCheckContext
*/
const QgsCoordinateTransformContext transformContext;

/**
* The project ... blablabla
* Only to be used in the main thread (prepare method)
*/
const QgsProject *project;

private:
#ifdef SIP_RUN
QgsGeometryCheckContext( const QgsGeometryCheckContext &rh )
Expand Down
29 changes: 29 additions & 0 deletions src/analysis/vector/geometry_checker/qgsgeometrygapcheck.cpp
Expand Up @@ -21,15 +21,30 @@
#include "qgsvectorlayer.h"
#include "qgsfeedback.h"
#include "qgsapplication.h"
#include "qgsproject.h"

#include "geos_c.h"

QgsGeometryGapCheck::QgsGeometryGapCheck( const QgsGeometryCheckContext *context, const QVariantMap &configuration )
: QgsGeometryCheck( context, configuration )
, mGapThresholdMapUnits( configuration.value( QStringLiteral( "gapThreshold" ) ).toDouble() )
{

}

void QgsGeometryGapCheck::prepare( const QgsGeometryCheckContext *context, const QVariantMap &configuration )
{
if ( configuration.value( QStringLiteral( "allowedGapsEnabled" ) ).toBool() )
{
QgsVectorLayer *layer = context->project->mapLayer<QgsVectorLayer *>( configuration.value( "allowedGapsLayer" ).toString() );
mAllowedGapsSource = qgis::make_unique<QgsVectorLayerFeatureSource>( layer );

mAllowedGapsBuffer = configuration.value( QStringLiteral( "allowedGapsBuffer" ) ).toDouble();
}
else
{
mAllowedGapsSource.reset();
}
}

void QgsGeometryGapCheck::collectErrors( const QMap<QString, QgsFeaturePool *> &featurePools, QList<QgsGeometryCheckError *> &errors, QStringList &messages, QgsFeedback *feedback, const LayerFeatureIds &ids ) const
Expand All @@ -39,6 +54,20 @@ void QgsGeometryGapCheck::collectErrors( const QMap<QString, QgsFeaturePool *> &

QVector<QgsGeometry> geomList;

if ( mAllowedGapsSource )
{
QgsFeatureRequest request;
request.setSubsetOfAttributes( QgsAttributeList() );
QgsFeatureIterator iterator = mAllowedGapsSource->getFeatures( request );
QgsFeature feature;

while ( iterator.nextFeature( feature ) )
{
QgsGeometry geom = feature.geometry();
QgsGeometry gg = geom.buffer( mAllowedGapsBuffer, 20 );
geomList.append( gg );
}
}

QMap<QString, QgsFeatureIds> featureIds = ids.isEmpty() ? allLayerFeatureIds( featurePools ) : ids.toMap();
const QgsGeometryCheckerUtils::LayerFeatures layerFeatures( featurePools, featureIds, compatibleGeometryTypes(), nullptr, mContext, true );
Expand Down
4 changes: 4 additions & 0 deletions src/analysis/vector/geometry_checker/qgsgeometrygapcheck.h
Expand Up @@ -106,6 +106,8 @@ class ANALYSIS_EXPORT QgsGeometryGapCheck : public QgsGeometryCheck
*/
explicit QgsGeometryGapCheck( const QgsGeometryCheckContext *context, const QVariantMap &configuration );

void prepare( const QgsGeometryCheckContext *context, const QVariantMap &configuration ) override;

QList<QgsWkbTypes::GeometryType> compatibleGeometryTypes() const override { return factoryCompatibleGeometryTypes(); }
void collectErrors( const QMap<QString, QgsFeaturePool *> &featurePools, QList<QgsGeometryCheckError *> &errors, QStringList &messages, QgsFeedback *feedback, const LayerFeatureIds &ids = LayerFeatureIds() ) const override;
void fixError( const QMap<QString, QgsFeaturePool *> &featurePools, QgsGeometryCheckError *error, int method, const QMap<QString, int> &mergeAttributeIndices, Changes &changes ) const override;
Expand All @@ -115,6 +117,8 @@ class ANALYSIS_EXPORT QgsGeometryGapCheck : public QgsGeometryCheck
QString id() const override;
QgsGeometryCheck::Flags flags() const override;
QgsGeometryCheck::CheckType checkType() const override { return factoryCheckType(); }
std::unique_ptr<QgsVectorLayerFeatureSource> mAllowedGapsSource;
double mAllowedGapsBuffer;

///@cond private
static QString factoryDescription() SIP_SKIP;
Expand Down
5 changes: 4 additions & 1 deletion src/app/qgsgeometryvalidationservice.cpp
Expand Up @@ -223,7 +223,7 @@ void QgsGeometryValidationService::enableLayerChecks( QgsVectorLayer *layer )
precision = 8;
}

checkInformation.context = qgis::make_unique<QgsGeometryCheckContext>( precision, mProject->crs(), mProject->transformContext() );
checkInformation.context = qgis::make_unique<QgsGeometryCheckContext>( precision, mProject->crs(), mProject->transformContext(), mProject );

QList<QgsGeometryCheck *> layerChecks;

Expand Down Expand Up @@ -424,7 +424,10 @@ void QgsGeometryValidationService::triggerTopologyChecks( QgsVectorLayer *layer

QHash<const QgsGeometryCheck *, QgsFeedback *> feedbacks;
for ( QgsGeometryCheck *check : checks )
{
feedbacks.insert( check, new QgsFeedback() );
check->prepare( mLayerChecks[layer].context.get(), layer->geometryOptions()->checkConfiguration( check->id() ) );
}

mLayerChecks[layer].topologyCheckFeedbacks = feedbacks.values();

Expand Down
Expand Up @@ -427,7 +427,7 @@ void QgsGeometryCheckerSetupTab::runChecks()
featurePools.insert( layer->id(), new QgsVectorDataProviderFeaturePool( layer, selectedOnly ) );
}

QgsGeometryCheckContext *context = new QgsGeometryCheckContext( ui.spinBoxTolerance->value(), QgsProject::instance()->crs(), QgsProject::instance()->transformContext() );
QgsGeometryCheckContext *context = new QgsGeometryCheckContext( ui.spinBoxTolerance->value(), QgsProject::instance()->crs(), QgsProject::instance()->transformContext(), QgsProject::instance() );

QList<QgsGeometryCheck *> checks;
for ( const QgsGeometryCheckFactory *factory : QgsGeometryCheckFactoryRegistry::getCheckFactories() )
Expand Down
4 changes: 2 additions & 2 deletions tests/src/geometry_checker/testqgsgeometrychecks.cpp
Expand Up @@ -936,7 +936,7 @@ void TestQgsGeometryChecks::testSelfContactCheck()

// https://github.com/qgis/QGIS/issues/28228
// test with a linestring which collapses to an empty linestring
QgsGeometryCheckContext context( 1, QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3857" ) ), QgsCoordinateTransformContext() );
QgsGeometryCheckContext context( 1, QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3857" ) ), QgsCoordinateTransformContext(), nullptr );
QgsGeometrySelfContactCheck check2( &context, QVariantMap() );
QgsGeometry g = QgsGeometry::fromWkt( QStringLiteral( "MultiLineString ((2988987 10262483, 2988983 10262480, 2988991 10262432, 2988990 10262419, 2988977 10262419, 2988976 10262420, 2988967 10262406, 2988970 10262421, 2988971 10262424),(2995620 10301368))" ) );
QList<QgsSingleGeometryCheckError *> errors = check2.processGeometry( g );
Expand Down Expand Up @@ -1128,7 +1128,7 @@ QPair<QgsGeometryCheckContext *, QMap<QString, QgsFeaturePool *> > TestQgsGeomet
layer->dataProvider()->enterUpdateMode();
featurePools.insert( layer->id(), createFeaturePool( layer ) );
}
return qMakePair( new QgsGeometryCheckContext( prec, mapCrs, QgsProject::instance()->transformContext() ), featurePools );
return qMakePair( new QgsGeometryCheckContext( prec, mapCrs, QgsProject::instance()->transformContext(), nullptr ), featurePools );
}

void TestQgsGeometryChecks::cleanupTestContext( QPair<QgsGeometryCheckContext *, QMap<QString, QgsFeaturePool *> > ctx ) const
Expand Down

0 comments on commit c8b277b

Please sign in to comment.