Skip to content

Commit

Permalink
move common function to tools package
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed May 16, 2014
1 parent 72997dc commit 6e4fbf8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 52 deletions.
19 changes: 2 additions & 17 deletions python/plugins/processing/algs/qgis/RandomPointsExtent.py
Expand Up @@ -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):

Expand Down Expand Up @@ -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)
Expand All @@ -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
17 changes: 1 addition & 16 deletions python/plugins/processing/algs/qgis/RandomPointsLayer.py
Expand Up @@ -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())
Expand All @@ -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
22 changes: 4 additions & 18 deletions python/plugins/processing/algs/qgis/RandomPointsPolygonsFixed.py
Expand Up @@ -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'))
Expand Down Expand Up @@ -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)
Expand All @@ -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
20 changes: 19 additions & 1 deletion python/plugins/processing/tools/vector.py
Expand Up @@ -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.