Skip to content

Commit

Permalink
QgsVectorLayerFeaturePool needs to be aware of geometry changes
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Oct 15, 2018
1 parent 76700cd commit 6df73d6
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 22 deletions.
Expand Up @@ -90,6 +90,7 @@ To be used by implementations of ``deleteFeature``.
%End



private:
QgsFeaturePool( const QgsFeaturePool &other );
};
Expand Down
2 changes: 1 addition & 1 deletion src/analysis/CMakeLists.txt
Expand Up @@ -186,6 +186,7 @@ SET(QGIS_ANALYSIS_MOC_HDRS

vector/geometry_checker/qgsgeometrychecker.h
vector/geometry_checker/qgsgeometrycheck.h
vector/geometry_checker/qgsvectorlayerfeaturepool.h
)

INCLUDE_DIRECTORIES(SYSTEM ${SPATIALITE_INCLUDE_DIR})
Expand Down Expand Up @@ -252,7 +253,6 @@ SET(QGIS_ANALYSIS_HDRS
vector/geometry_checker/qgsgeometrycheckerutils.h
vector/geometry_checker/qgsfeaturepool.h
vector/geometry_checker/qgsvectordataproviderfeaturepool.h
vector/geometry_checker/qgsvectorlayerfeaturepool.h

interpolation/qgsinterpolator.h
interpolation/qgsgridfilewriter.h
Expand Down
5 changes: 5 additions & 0 deletions src/analysis/vector/geometry_checker/qgsfeaturepool.cpp
Expand Up @@ -140,6 +140,11 @@ void QgsFeaturePool::setFeatureIds( const QgsFeatureIds &ids )
mFeatureIds = ids;
}

bool QgsFeaturePool::isFeatureCached( QgsFeatureId fid )
{
return mFeatureCache.contains( fid );
}

QgsCoordinateReferenceSystem QgsFeaturePool::crs() const
{
return mCrs;
Expand Down
8 changes: 8 additions & 0 deletions src/analysis/vector/geometry_checker/qgsfeaturepool.h
Expand Up @@ -145,6 +145,14 @@ class ANALYSIS_EXPORT QgsFeaturePool : public QgsFeatureSink SIP_ABSTRACT
*/
void setFeatureIds( const QgsFeatureIds &ids ) SIP_SKIP;

/**
* Checks if the feature \a fid is cached.
*
* \since QGIS 3.4
* \note not available in Python bindings
*/
bool isFeatureCached( QgsFeatureId fid ) SIP_SKIP;

private:
#ifdef SIP_RUN
QgsFeaturePool( const QgsFeaturePool &other )
Expand Down
41 changes: 21 additions & 20 deletions src/analysis/vector/geometry_checker/qgsvectorlayerfeaturepool.cpp
Expand Up @@ -17,29 +17,14 @@ email : matthias@opengis.ch
#include "qgsthreadingutils.h"

#include "qgsfeaturerequest.h"
#include "qgsvectorlayer.h"

QgsVectorLayerFeaturePool::QgsVectorLayerFeaturePool( QgsVectorLayer *layer )
: QgsFeaturePool( layer )
: QObject()
, QgsFeaturePool( layer )
{
// Build spatial index
QgsFeature feature;
QgsFeatureRequest req;
QgsFeatureIds featureIds;

QgsFeatureIterator it = layer->getFeatures( req );
while ( it.nextFeature( feature ) )
{
if ( feature.geometry() )
{
insertFeature( feature );
featureIds.insert( feature.id() );
}
else
{
featureIds.remove( feature.id() );
}
}
setFeatureIds( featureIds );
connect( layer, &QgsVectorLayer::featureDeleted, this, &QgsVectorLayerFeaturePool::onFeatureDeleted );
connect( layer, &QgsVectorLayer::geometryChanged, this, &QgsVectorLayerFeaturePool::onGeometryChanged );
}

bool QgsVectorLayerFeaturePool::addFeature( QgsFeature &feature, Flags flags )
Expand Down Expand Up @@ -147,3 +132,19 @@ void QgsVectorLayerFeaturePool::deleteFeature( QgsFeatureId fid )
}
} );
}

void QgsVectorLayerFeaturePool::onGeometryChanged( QgsFeatureId fid, const QgsGeometry &geometry )
{
if ( isFeatureCached( fid ) )
{
QgsFeature feature;
getFeature( fid, feature );
feature.setGeometry( geometry );
updateFeature( feature );
}
}

void QgsVectorLayerFeaturePool::onFeatureDeleted( QgsFeatureId fid )
{
deleteFeature( fid );
}
Expand Up @@ -27,15 +27,21 @@ email : matthias@opengis.ch
*
* \since QGIS 3.4
*/
class ANALYSIS_EXPORT QgsVectorLayerFeaturePool : public QgsFeaturePool
class ANALYSIS_EXPORT QgsVectorLayerFeaturePool : public QObject, public QgsFeaturePool
{
Q_OBJECT

public:
QgsVectorLayerFeaturePool( QgsVectorLayer *layer );

bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = nullptr ) override;
bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = nullptr ) override;
void updateFeature( QgsFeature &feature ) override;
void deleteFeature( QgsFeatureId fid ) override;

private slots:
void onGeometryChanged( QgsFeatureId fid, const QgsGeometry &geometry );
void onFeatureDeleted( QgsFeatureId fid );
};

#endif // QGSVECTORLAYERFEATUREPOOL_H

0 comments on commit 6df73d6

Please sign in to comment.