[QGIS Commit] r13901 - branches/threading-branch/python/plugins/osm

svn_qgis at osgeo.org svn_qgis at osgeo.org
Thu Jul 8 08:53:32 EDT 2010


Author: wonder
Date: 2010-07-08 12:53:32 +0000 (Thu, 08 Jul 2010)
New Revision: 13901

Modified:
   branches/threading-branch/python/plugins/osm/OsmDatabaseManager.py
Log:
OSM plugin - identification - fully iterate through the layers (and retrieve the closest feature, not first) to avoid deadlocks.


Modified: branches/threading-branch/python/plugins/osm/OsmDatabaseManager.py
===================================================================
--- branches/threading-branch/python/plugins/osm/OsmDatabaseManager.py	2010-07-08 12:37:11 UTC (rev 13900)
+++ branches/threading-branch/python/plugins/osm/OsmDatabaseManager.py	2010-07-08 12:53:32 UTC (rev 13901)
@@ -287,23 +287,37 @@
         # finds out tolerance for snapping
         tolerance=self.getTolerance()
         area=QgsRectangle(mapPoint.x()-tolerance,mapPoint.y()-tolerance,mapPoint.x()+tolerance,mapPoint.y()+tolerance)
+        
+        mapPointGeom = QgsGeometry.fromPoint(mapPoint)
 
         feat=QgsFeature()
         lay=self.pointLayers[self.currentKey]
         lay.select([],area,True,True)
-        result=lay.nextFeature(feat)
-        lay.dataProvider().rewind()
+        minDist = 1e10
+        minFeat = None
+        while lay.nextFeature(feat):
+            dist = feat.geometry().distance(mapPointGeom)
+            if dist < minDist:
+                minDist = dist
+                minFeat = QgsFeature(feat)
+            
+        if minFeat:
+            return (minFeat,'Point')
 
-        if result:
-            return (feat,'Point')
-
         feat=QgsFeature()
         lay=self.lineLayers[self.currentKey]
         lay.select([],area,True,True)
-        result=lay.nextFeature(feat)
-        lay.dataProvider().rewind()
 
-        if result:
+        minDist = 1e10
+        minFeat = None
+        while lay.nextFeature(feat):
+            dist = feat.geometry().distance(mapPointGeom)
+            if dist < minDist:
+                minDist = dist
+                minFeat = QgsFeature(feat)
+
+        if minFeat:
+            feat = minFeat
             # line vertices
             c=self.getConnection().cursor()
             c.execute("select n.id,n.lat,n.lon from node n,way_member wm where n.u=1 and wm.u=1 and wm.way_id=:lineId and wm.node_id=n.id and n.status<>'R' and n.lat>=:minLat and n.lat<=:maxLat and n.lon>=:minLon and n.lon<=:maxLon"
@@ -322,10 +336,17 @@
         feat=QgsFeature()
         lay=self.polygonLayers[self.currentKey]
         lay.select([],area,True,True)
-        result=lay.nextFeature(feat)
-        lay.dataProvider().rewind()
 
-        if result:
+        minDist = 1e10
+        minFeat = None
+        while lay.nextFeature(feat):
+            dist = feat.geometry().distance(mapPointGeom)
+            if dist < minDist:
+                minDist = dist
+                minFeat = QgsFeature(feat)
+
+        if minFeat:
+            feat = minFeat
             # polygon vertices
             c=self.getConnection().cursor()
             c.execute("select n.id,n.lat,n.lon from node n,way_member wm where n.u=1 and wm.u=1 and wm.way_id=:polygonId and wm.node_id=n.id and n.status<>'R' and n.lat>=:minLat and n.lat<=:maxLat and n.lon>=:minLon and n.lon<=:maxLon"
@@ -365,20 +386,16 @@
         lay=self.pointLayers[self.currentKey]
         lay.select([],area,True,True)
         feat=QgsFeature()
-        result=lay.nextFeature(feat)
         featMap={}
 
-        while result:
+        while lay.nextFeature(feat):
             foundPoints.append((feat,'Point'))
-            feat=QgsFeature()
-            result=lay.nextFeature(feat)
 
         lay=self.lineLayers[self.currentKey]
         lay.select([],area,True,True)
         feat=QgsFeature()
-        result=lay.nextFeature(feat)
 
-        while result:
+        while lay.nextFeature(feat):
             # line vertices
             c=self.getConnection().cursor()
             c.execute("select n.id,n.lat,n.lon from node n,way_member wm where n.u=1 and wm.u=1 and wm.way_id=:lineId and wm.node_id=n.id and n.status<>'R' and n.lat>=:minLat and n.lat<=:maxLat and n.lon>=:minLon and n.lon<=:maxLon"
@@ -393,15 +410,12 @@
             c.close()
 
             foundLines.append((feat,'Line'))
-            feat=QgsFeature()
-            result=lay.nextFeature(feat)
 
         lay=self.polygonLayers[self.currentKey]
         lay.select([],area,True,True)
         feat=QgsFeature()
-        result=lay.nextFeature(feat)
 
-        while result:
+        while lay.nextFeature(feat):
             # polygon vertices
             c=self.getConnection().cursor()
             c.execute("select n.id,n.lat,n.lon from node n,way_member wm where n.u=1 and wm.u=1 and wm.way_id=:polygonId and wm.node_id=n.id and n.status<>'R' and n.lat>=:minLat and n.lat<=:maxLat and n.lon>=:minLon and n.lon<=:maxLon"
@@ -416,8 +430,6 @@
             c.close()
 
             foundPolygons.append((feat,'Polygon'))
-            feat=QgsFeature()
-            result=lay.nextFeature(feat)
 
         res=foundPoints
         for key in featMap.keys():



More information about the QGIS-commit mailing list