Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fixes #2430 and speeds up random point generation on top of vectors s…
…ignificantly

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13860 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
cfarmer committed Jun 30, 2010
1 parent bc166a8 commit 13cf72a
Showing 1 changed file with 36 additions and 8 deletions.
44 changes: 36 additions & 8 deletions python/plugins/fTools/tools/doRandPoints.py
Expand Up @@ -146,14 +146,14 @@ def createSinglePolygon(self, vlayer):
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
Expand All @@ -176,13 +176,41 @@ def simpleRandom(self, n, bound, xmin, xmax, ymin, ymax):
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
Expand Down

0 comments on commit 13cf72a

Please sign in to comment.