Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
wonder committed Jan 15, 2009
1 parent 1c02154 commit f1590aa
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
41 changes: 35 additions & 6 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -697,6 +697,8 @@ bool QgsVectorLayer::draw( QgsRenderContext& rendererContext )
{
// Destroy all cached geometries and clear the references to them
deleteCachedGeometries();

mCachedGeometriesRect = rendererContext.extent();
}

updateFeatureCount();
Expand Down Expand Up @@ -783,13 +785,19 @@ bool QgsVectorLayer::draw( QgsRenderContext& rendererContext )
QgsLogger::warning( "QgsRenderer is null in QgsVectorLayer::draw()" );
}

if ( mEditable )
{
QgsDebugMsg(QString("Cached %1 geometries.").arg(mCachedGeometries.count()));
}

return TRUE; // Assume success always
}

void QgsVectorLayer::deleteCachedGeometries()
{
// Destroy any cached geometries
mCachedGeometries.clear();
mCachedGeometriesRect = QgsRectangle();
}

void QgsVectorLayer::drawVertexMarker( int x, int y, QPainter& p, QgsVectorLayer::VertexMarkerType type )
Expand Down Expand Up @@ -3135,15 +3143,36 @@ int QgsVectorLayer::snapWithContext( const QgsPoint& startPoint, double snapping
QgsRectangle searchRect( startPoint.x() - snappingTolerance, startPoint.y() - snappingTolerance,
startPoint.x() + snappingTolerance, startPoint.y() + snappingTolerance );
double sqrSnappingTolerance = snappingTolerance * snappingTolerance;

select( QgsAttributeList(), searchRect, true, true );


int n = 0;
QgsFeature f;
while ( nextFeature( f ) )

if (mCachedGeometriesRect.contains(searchRect))
{
QgsDebugMsg("Using cached geometries for snapping.");

QgsGeometryMap::iterator it = mCachedGeometries.begin();
for ( ; it != mCachedGeometries.end() ; ++it)
{
QgsGeometry* g = &(it.value());
if (g->boundingBox().intersects(searchRect))
{
snapToGeometry( startPoint, it.key(), g, sqrSnappingTolerance, snappingResults, snap_to );
++n;
}
}
}
else
{
snapToGeometry( startPoint, f.id(), f.geometry(), sqrSnappingTolerance, snappingResults, snap_to );
++n;
// snapping outside cached area

select( QgsAttributeList(), searchRect, true, true );

while ( nextFeature( f ) )
{
snapToGeometry( startPoint, f.id(), f.geometry(), sqrSnappingTolerance, snappingResults, snap_to );
++n;
}
}

return n == 0 ? 2 : 0;
Expand Down
3 changes: 3 additions & 0 deletions src/core/qgsvectorlayer.h
Expand Up @@ -576,6 +576,9 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer

/** cache of the committed geometries retrieved *for the current display* */
QgsGeometryMap mCachedGeometries;

/** extent for which there are cached geometries */
QgsRectangle mCachedGeometriesRect;

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

0 comments on commit f1590aa

Please sign in to comment.