Skip to content
Permalink
Browse files
move common function to tools package
  • Loading branch information
alexbruy committed May 16, 2014
1 parent 72997dc commit 6e4fbf8d1016db576d6aaec3de8ac0666bc5a1a3
@@ -38,7 +38,7 @@
from processing.parameters.ParameterExtent import ParameterExtent
from processing.parameters.ParameterNumber import ParameterNumber
from processing.outputs.OutputVector import OutputVector

from processing.tools import vector

class RandomPointsExtent(GeoAlgorithm):

@@ -90,7 +90,7 @@ def processAlgorithm(self, progress):
pnt = QgsPoint(rx, ry)
geom = QgsGeometry.fromPoint(pnt)
if geom.within(extent) and \
self.checkMinDistance(pnt, index, minDistance, points):
vector.checkMinDistance(pnt, index, minDistance, points):
f = QgsFeature(nPoints)
f.initAttributes(1)
f.setFields(fields)
@@ -110,18 +110,3 @@ def processAlgorithm(self, progress):
'number of attempts exceeded.')

del writer

def checkMinDistance(self, point, index, distance, points):
if distance == 0:
return True

neighbors = index.nearestNeighbor(point, 1)
if len(neighbors) == 0:
return True

if neighbors[0] in points:
np = points[neighbors[0]]
if np.sqrDist(point) < (distance * distance):
return False

return True
@@ -92,7 +92,7 @@ def processAlgorithm(self, progress):
geom = QgsGeometry.fromPoint(pnt)
ids = idxLayer.intersects(geom.buffer(5, 5).boundingBox())
if len(ids) > 0 and \
self.checkMinDistance(pnt, index, minDistance, points):
vector.checkMinDistance(pnt, index, minDistance, points):
for i in ids:
f = layer.getFeatures(request.setFilterFid(i)).next()
tmpGeom = QgsGeometry(f.geometry())
@@ -116,18 +116,3 @@ def processAlgorithm(self, progress):
'number of attempts exceeded.')

del writer

def checkMinDistance(self, point, index, distance, points):
if distance == 0:
return True

neighbors = index.nearestNeighbor(point, 1)
if len(neighbors) == 0:
return True

if neighbors[0] in points:
np = points[neighbors[0]]
if np.sqrDist(point) < (distance * distance):
return False

return True
@@ -55,14 +55,15 @@ class RandomPointsPolygonsFixed(GeoAlgorithm):
]

def defineCharacteristics(self):
self.name = 'Random points inside polygons'
self.name = 'Random points inside polygons (fixed)'
self.group = 'Vector creation tools'
self.addParameter(ParameterVector(self.VECTOR,
'Input layer',[ParameterVector.VECTOR_TYPE_POLYGON]))
self.addParameter(ParameterSelection(
self.STRATEGY, 'Sampling strategy', self.STRATEGIES, 0))
self.addParameter(
ParameterNumber(self.VALUE, 'Number or density of points', 0.0001, 9999999.0, 1.0))
ParameterNumber(self.VALUE, 'Number or density of points',
0.0001, 9999999.0, 1.0))
self.addParameter(ParameterNumber(
self.MIN_DISTANCE, 'Minimum distance', 0.0, 9999999, 0.0))
self.addOutput(OutputVector(self.OUTPUT, 'Random points'))
@@ -107,7 +108,7 @@ def processAlgorithm(self, progress):
pnt = QgsPoint(rx, ry)
geom = QgsGeometry.fromPoint(pnt)
if geom.within(fGeom) and \
self.checkMinDistance(pnt, index, minDistance, points):
vector.checkMinDistance(pnt, index, minDistance, points):
f = QgsFeature(nPoints)
f.initAttributes(1)
f.setFields(fields)
@@ -129,18 +130,3 @@ def processAlgorithm(self, progress):
progress.setPercentage(0)

del writer

def checkMinDistance(self, point, index, distance, points):
if distance == 0:
return True

neighbors = index.nearestNeighbor(point, 1)
if len(neighbors) == 0:
return True

if neighbors[0] in points:
np = points[neighbors[0]]
if np.sqrDist(point) < (distance * distance):
return False

return True
@@ -323,4 +323,22 @@ def duplicateInMemory(layer, newName='', addToRegistry=False):
else:
raise RuntimeError('Layer invalid')

return memLayer
return memLayer

def checkMinDistance(point, index, distance, points):
"""Check if distance from given point to all other points is greater
than given value.
"""
if distance == 0:
return True

neighbors = index.nearestNeighbor(point, 1)
if len(neighbors) == 0:
return True

if neighbors[0] in points:
np = points[neighbors[0]]
if np.sqrDist(point) < (distance * distance):
return False

return True

0 comments on commit 6e4fbf8

Please sign in to comment.