Skip to content

Commit f1590aa

Browse files
author
wonder
committed
Improvements to snapping speed: use cached geometries where possible
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@9974 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 1c02154 commit f1590aa

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

src/core/qgsvectorlayer.cpp

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,8 @@ bool QgsVectorLayer::draw( QgsRenderContext& rendererContext )
697697
{
698698
// Destroy all cached geometries and clear the references to them
699699
deleteCachedGeometries();
700+
701+
mCachedGeometriesRect = rendererContext.extent();
700702
}
701703

702704
updateFeatureCount();
@@ -783,13 +785,19 @@ bool QgsVectorLayer::draw( QgsRenderContext& rendererContext )
783785
QgsLogger::warning( "QgsRenderer is null in QgsVectorLayer::draw()" );
784786
}
785787

788+
if ( mEditable )
789+
{
790+
QgsDebugMsg(QString("Cached %1 geometries.").arg(mCachedGeometries.count()));
791+
}
792+
786793
return TRUE; // Assume success always
787794
}
788795

789796
void QgsVectorLayer::deleteCachedGeometries()
790797
{
791798
// Destroy any cached geometries
792799
mCachedGeometries.clear();
800+
mCachedGeometriesRect = QgsRectangle();
793801
}
794802

795803
void QgsVectorLayer::drawVertexMarker( int x, int y, QPainter& p, QgsVectorLayer::VertexMarkerType type )
@@ -3135,15 +3143,36 @@ int QgsVectorLayer::snapWithContext( const QgsPoint& startPoint, double snapping
31353143
QgsRectangle searchRect( startPoint.x() - snappingTolerance, startPoint.y() - snappingTolerance,
31363144
startPoint.x() + snappingTolerance, startPoint.y() + snappingTolerance );
31373145
double sqrSnappingTolerance = snappingTolerance * snappingTolerance;
3138-
3139-
select( QgsAttributeList(), searchRect, true, true );
3140-
3146+
31413147
int n = 0;
31423148
QgsFeature f;
3143-
while ( nextFeature( f ) )
3149+
3150+
if (mCachedGeometriesRect.contains(searchRect))
3151+
{
3152+
QgsDebugMsg("Using cached geometries for snapping.");
3153+
3154+
QgsGeometryMap::iterator it = mCachedGeometries.begin();
3155+
for ( ; it != mCachedGeometries.end() ; ++it)
3156+
{
3157+
QgsGeometry* g = &(it.value());
3158+
if (g->boundingBox().intersects(searchRect))
3159+
{
3160+
snapToGeometry( startPoint, it.key(), g, sqrSnappingTolerance, snappingResults, snap_to );
3161+
++n;
3162+
}
3163+
}
3164+
}
3165+
else
31443166
{
3145-
snapToGeometry( startPoint, f.id(), f.geometry(), sqrSnappingTolerance, snappingResults, snap_to );
3146-
++n;
3167+
// snapping outside cached area
3168+
3169+
select( QgsAttributeList(), searchRect, true, true );
3170+
3171+
while ( nextFeature( f ) )
3172+
{
3173+
snapToGeometry( startPoint, f.id(), f.geometry(), sqrSnappingTolerance, snappingResults, snap_to );
3174+
++n;
3175+
}
31473176
}
31483177

31493178
return n == 0 ? 2 : 0;

src/core/qgsvectorlayer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,9 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
576576

577577
/** cache of the committed geometries retrieved *for the current display* */
578578
QgsGeometryMap mCachedGeometries;
579+
580+
/** extent for which there are cached geometries */
581+
QgsRectangle mCachedGeometriesRect;
579582

580583
/** Set holding the feature IDs that are activated. Note that if a feature
581584
subsequently gets deleted (i.e. by its addition to mDeletedFeatureIds),

0 commit comments

Comments
 (0)