Skip to content
Permalink
Browse files

Deduplicate code

  • Loading branch information
m-kuhn committed Aug 22, 2018
1 parent 26261ca commit 8f03681e65e11340b1f36ee49bbbc6c33309e65a
Showing with 28 additions and 21 deletions.
  1. +19 −21 src/core/qgsvectorlayer.cpp
  2. +9 −0 src/core/qgsvectorlayer.h
@@ -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() )
@@ -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 );
}

@@ -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();

@@ -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 );
}
}
@@ -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() );

0 comments on commit 8f03681

Please sign in to comment.
You can’t perform that action at this time.