[QGIS Commit] r9974 - trunk/qgis/src/core

svn_qgis at osgeo.org svn_qgis at osgeo.org
Thu Jan 15 15:10:32 EST 2009


Author: wonder
Date: 2009-01-15 15:10:32 -0500 (Thu, 15 Jan 2009)
New Revision: 9974

Modified:
   trunk/qgis/src/core/qgsvectorlayer.cpp
   trunk/qgis/src/core/qgsvectorlayer.h
Log:
Improvements to snapping speed: use cached geometries where possible


Modified: trunk/qgis/src/core/qgsvectorlayer.cpp
===================================================================
--- trunk/qgis/src/core/qgsvectorlayer.cpp	2009-01-15 18:02:58 UTC (rev 9973)
+++ trunk/qgis/src/core/qgsvectorlayer.cpp	2009-01-15 20:10:32 UTC (rev 9974)
@@ -697,6 +697,8 @@
     {
       // Destroy all cached geometries and clear the references to them
       deleteCachedGeometries();
+      
+      mCachedGeometriesRect = rendererContext.extent();
     }
 
     updateFeatureCount();
@@ -783,6 +785,11 @@
     QgsLogger::warning( "QgsRenderer is null in QgsVectorLayer::draw()" );
   }
 
+  if ( mEditable )
+  {
+    QgsDebugMsg(QString("Cached %1 geometries.").arg(mCachedGeometries.count()));
+  }
+  
   return TRUE; // Assume success always
 }
 
@@ -790,6 +797,7 @@
 {
   // Destroy any cached geometries
   mCachedGeometries.clear();
+  mCachedGeometriesRect = QgsRectangle();
 }
 
 void QgsVectorLayer::drawVertexMarker( int x, int y, QPainter& p, QgsVectorLayer::VertexMarkerType type )
@@ -3135,16 +3143,37 @@
   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))
   {
-    snapToGeometry( startPoint, f.id(), f.geometry(), sqrSnappingTolerance, snappingResults, snap_to );
-    ++n;
+    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
+  {
+    // 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;
 }

Modified: trunk/qgis/src/core/qgsvectorlayer.h
===================================================================
--- trunk/qgis/src/core/qgsvectorlayer.h	2009-01-15 18:02:58 UTC (rev 9973)
+++ trunk/qgis/src/core/qgsvectorlayer.h	2009-01-15 20:10:32 UTC (rev 9974)
@@ -576,6 +576,9 @@
 
     /** 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),



More information about the QGIS-commit mailing list