move common function to tools package

This commit is contained in:
Alexander Bruy 2014-04-25 12:39:06 +03:00
parent 72997dca6d
commit 6e4fbf8d10
4 changed files with 26 additions and 52 deletions

View File

@ -38,7 +38,7 @@ from processing.core.ProcessingLog import ProcessingLog
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 @@ class RandomPointsExtent(GeoAlgorithm):
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 @@ class RandomPointsExtent(GeoAlgorithm):
'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

View File

@ -92,7 +92,7 @@ class RandomPointsLayer(GeoAlgorithm):
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 @@ class RandomPointsLayer(GeoAlgorithm):
'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

View File

@ -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 @@ class RandomPointsPolygonsFixed(GeoAlgorithm):
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 @@ class RandomPointsPolygonsFixed(GeoAlgorithm):
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

View File

@ -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