Skip to content

Commit 8f03681

Browse files
committedAug 22, 2018
Deduplicate code
1 parent 26261ca commit 8f03681

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed
 

‎src/core/qgsvectorlayer.cpp

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,20 @@ void QgsVectorLayer::setExtent( const QgsRectangle &r )
757757
mValidExtent = true;
758758
}
759759

760+
void QgsVectorLayer::applyGeometryFixes( QgsGeometry &geom ) const
761+
{
762+
if ( mGeometryOptions.geometryPrecision != 0.0 )
763+
geom = geom.snappedToGrid( mGeometryOptions.geometryPrecision, mGeometryOptions.geometryPrecision );
764+
765+
if ( mGeometryOptions.removeDuplicateNodes )
766+
geom.removeDuplicateNodes();
767+
}
768+
769+
bool QgsVectorLayer::geometryFixesEnabled() const
770+
{
771+
return mGeometryOptions.geometryPrecision != 0.0 || mGeometryOptions.removeDuplicateNodes;
772+
}
773+
760774
void QgsVectorLayer::updateDefaultValues( QgsFeatureId fid, QgsFeature feature )
761775
{
762776
if ( !mDefaultValueOnUpdateFields.isEmpty() )
@@ -945,16 +959,10 @@ bool QgsVectorLayer::addFeature( QgsFeature &feature, Flags )
945959
return false;
946960

947961

948-
if ( mGeometryOptions.geometryPrecision != 0.0 || mGeometryOptions.removeDuplicateNodes )
962+
if ( geometryFixesEnabled() )
949963
{
950964
QgsGeometry geom = feature.geometry();
951-
952-
if ( mGeometryOptions.geometryPrecision != 0.0 )
953-
geom = geom.snappedToGrid( mGeometryOptions.geometryPrecision, mGeometryOptions.geometryPrecision );
954-
955-
if ( mGeometryOptions.removeDuplicateNodes )
956-
geom.removeDuplicateNodes();
957-
965+
applyGeometryFixes( geom );
958966
feature.setGeometry( geom );
959967
}
960968

@@ -2518,11 +2526,7 @@ bool QgsVectorLayer::changeGeometry( QgsFeatureId fid, QgsGeometry &geom, bool s
25182526
return false;
25192527
}
25202528

2521-
if ( mGeometryOptions.geometryPrecision != 0.0 )
2522-
geom = geom.snappedToGrid( mGeometryOptions.geometryPrecision, mGeometryOptions.geometryPrecision );
2523-
2524-
if ( mGeometryOptions.removeDuplicateNodes )
2525-
geom.removeDuplicateNodes();
2529+
applyGeometryFixes( geom );
25262530

25272531
updateExtents();
25282532

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

2995-
if ( mGeometryOptions.geometryPrecision != 0.0 || mGeometryOptions.removeDuplicateNodes )
2999+
if ( geometryFixesEnabled() )
29963000
{
29973001
for ( auto feature = features.begin(); feature != features.end(); ++feature )
29983002
{
29993003
QgsGeometry geom = feature->geometry();
3000-
3001-
if ( mGeometryOptions.geometryPrecision != 0.0 )
3002-
geom = geom.snappedToGrid( mGeometryOptions.geometryPrecision, mGeometryOptions.geometryPrecision );
3003-
3004-
if ( mGeometryOptions.removeDuplicateNodes )
3005-
geom.removeDuplicateNodes();
3006-
3004+
applyGeometryFixes( geom );
30073005
feature->setGeometry( geom );
30083006
}
30093007
}

‎src/core/qgsvectorlayer.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2387,6 +2387,15 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
23872387
void setExtent( const QgsRectangle &rect ) override;
23882388

23892389
private: // Private methods
2390+
/**
2391+
* Applies automatic fixes to geometries added to or edited on this layer.
2392+
*/
2393+
void applyGeometryFixes( QgsGeometry &geom ) const;
2394+
2395+
/**
2396+
* Check if geometry fixes are enabled and `applyGeometryFixes` needs to be called.
2397+
*/
2398+
bool geometryFixesEnabled() const;
23902399

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

0 commit comments

Comments
 (0)
Please sign in to comment.