[QGIS Commit] r13860 - trunk/qgis/python/plugins/fTools/tools

svn_qgis at osgeo.org svn_qgis at osgeo.org
Wed Jun 30 18:50:17 EDT 2010


Author: cfarmer
Date: 2010-06-30 22:50:17 +0000 (Wed, 30 Jun 2010)
New Revision: 13860

Modified:
   trunk/qgis/python/plugins/fTools/tools/doRandPoints.py
Log:
fixes #2430 and speeds up random point generation on top of vectors significantly

Modified: trunk/qgis/python/plugins/fTools/tools/doRandPoints.py
===================================================================
--- trunk/qgis/python/plugins/fTools/tools/doRandPoints.py	2010-06-30 22:49:20 UTC (rev 13859)
+++ trunk/qgis/python/plugins/fTools/tools/doRandPoints.py	2010-06-30 22:50:17 UTC (rev 13860)
@@ -146,14 +146,14 @@
         provider.select(allAttrs)
         feat = QgsFeature()
         geom = QgsGeometry()
-        geom2 = QgsGeometry()
+        #geom2 = QgsGeometry()
         provider.nextFeature(feat)
-        geom = feat.geometry()
+        geom = QgsGeometry(feat.geometry())
         count = 10.00
         add = ( 40.00 - 10.00 ) / provider.featureCount()
-        provider.rewind()
-        provider.nextFeature(feat)
-        geom = QgsGeometry(feat.geometry())
+        #provider.rewind()
+        #provider.nextFeature(feat)
+        #geom = QgsGeometry(feat.geometry())
         while provider.nextFeature(feat):
             geom = geom.combine(QgsGeometry( feat.geometry() ))
             count = count + add
@@ -176,13 +176,41 @@
                 self.progressBar.setValue(count)
         return points
 
+    def vectorRandom(self, n, layer, xmin, xmax, ymin, ymax):
+        provider = layer.dataProvider()
+        provider.select([])
+        index = ftools_utils.createIndex(provider)
+        seed()
+        points = []
+        feat = QgsFeature()
+        i = 1
+        count = 40.00
+        add = ( 70.00 - 40.00 ) / n
+        while i <= n:
+            point = QgsPoint(xmin + (xmax-xmin) * random(), ymin + (ymax-ymin) * random())
+            pGeom = QgsGeometry().fromPoint(point)
+            ids = index.intersects(pGeom.buffer(5,5).boundingBox())
+            for id in ids:
+                provider.featureAtId(int(id),feat,True)
+                tGeom = QgsGeometry(feat.geometry())
+                if pGeom.intersects(tGeom):
+                    points.append(pGeom)
+                    i = i + 1
+                    count = count + add
+                    self.progressBar.setValue(count)
+                    break
+        return points
+
     def randomize(self, inLayer, outPath, minimum, design, value):
         outFeat = QgsFeature()
         if design == self.tr("unstratified"):
             ext = inLayer.extent()
-            if inLayer.type() == inLayer.RasterLayer: bound = ext
-            else: bound = self.createSinglePolygon(inLayer)
-            points = self.simpleRandom(int(value), bound, ext.xMinimum(), ext.xMaximum(), ext.yMinimum(), ext.yMaximum())
+            if inLayer.type() == inLayer.RasterLayer:
+                points = self.simpleRandom(int(value), ext, ext.xMinimum(),
+                ext.xMaximum(), ext.yMinimum(), ext.yMaximum())
+            else:
+                points = self.vectorRandom(int(value), inLayer,
+                ext.xMinimum(), ext.xMaximum(), ext.yMinimum(), ext.yMaximum())
         else: points = self.loopThruPolygons(inLayer, value, design)
         crs = self.mapCanvas.mapRenderer().destinationSrs()
         if not crs.isValid(): crs = None



More information about the QGIS-commit mailing list