Skip to content

Commit

Permalink
Deduplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Aug 22, 2018
1 parent 26261ca commit 8f03681
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
40 changes: 19 additions & 21 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -757,6 +757,20 @@ void QgsVectorLayer::setExtent( const QgsRectangle &r )
mValidExtent = true;
}

void QgsVectorLayer::applyGeometryFixes( QgsGeometry &geom ) const
{
if ( mGeometryOptions.geometryPrecision != 0.0 )
geom = geom.snappedToGrid( mGeometryOptions.geometryPrecision, mGeometryOptions.geometryPrecision );

if ( mGeometryOptions.removeDuplicateNodes )
geom.removeDuplicateNodes();
}

bool QgsVectorLayer::geometryFixesEnabled() const
{
return mGeometryOptions.geometryPrecision != 0.0 || mGeometryOptions.removeDuplicateNodes;
}

void QgsVectorLayer::updateDefaultValues( QgsFeatureId fid, QgsFeature feature )
{
if ( !mDefaultValueOnUpdateFields.isEmpty() )
Expand Down Expand Up @@ -945,16 +959,10 @@ bool QgsVectorLayer::addFeature( QgsFeature &feature, Flags )
return false;


if ( mGeometryOptions.geometryPrecision != 0.0 || mGeometryOptions.removeDuplicateNodes )
if ( geometryFixesEnabled() )
{
QgsGeometry geom = feature.geometry();

if ( mGeometryOptions.geometryPrecision != 0.0 )
geom = geom.snappedToGrid( mGeometryOptions.geometryPrecision, mGeometryOptions.geometryPrecision );

if ( mGeometryOptions.removeDuplicateNodes )
geom.removeDuplicateNodes();

applyGeometryFixes( geom );
feature.setGeometry( geom );
}

Expand Down Expand Up @@ -2518,11 +2526,7 @@ bool QgsVectorLayer::changeGeometry( QgsFeatureId fid, QgsGeometry &geom, bool s
return false;
}

if ( mGeometryOptions.geometryPrecision != 0.0 )
geom = geom.snappedToGrid( mGeometryOptions.geometryPrecision, mGeometryOptions.geometryPrecision );

if ( mGeometryOptions.removeDuplicateNodes )
geom.removeDuplicateNodes();
applyGeometryFixes( geom );

updateExtents();

Expand Down Expand Up @@ -2992,18 +2996,12 @@ bool QgsVectorLayer::addFeatures( QgsFeatureList &features, Flags )
if ( !mEditBuffer || !mDataProvider )
return false;

if ( mGeometryOptions.geometryPrecision != 0.0 || mGeometryOptions.removeDuplicateNodes )
if ( geometryFixesEnabled() )
{
for ( auto feature = features.begin(); feature != features.end(); ++feature )
{
QgsGeometry geom = feature->geometry();

if ( mGeometryOptions.geometryPrecision != 0.0 )
geom = geom.snappedToGrid( mGeometryOptions.geometryPrecision, mGeometryOptions.geometryPrecision );

if ( mGeometryOptions.removeDuplicateNodes )
geom.removeDuplicateNodes();

applyGeometryFixes( geom );
feature->setGeometry( geom );
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/core/qgsvectorlayer.h
Expand Up @@ -2387,6 +2387,15 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
void setExtent( const QgsRectangle &rect ) override;

private: // Private methods
/**
* Applies automatic fixes to geometries added to or edited on this layer.
*/
void applyGeometryFixes( QgsGeometry &geom ) const;

/**
* Check if geometry fixes are enabled and `applyGeometryFixes` needs to be called.
*/
bool geometryFixesEnabled() const;

void updateDefaultValues( QgsFeatureId fid, QgsFeature feature = QgsFeature() );

Expand Down

0 comments on commit 8f03681

Please sign in to comment.