Skip to content
Permalink
Browse files

Fix "Random points along line" alg

Fixes "Random points along line" (qgis:randompointsalongline) algorithm.
See #33156 (comment)
  • Loading branch information
agiudiceandrea authored and nyalldawson committed Feb 3, 2020
1 parent e82ba52 commit 02a6fd40cd0383adbda4bf4929a0b69ef29e3690
Showing with 18 additions and 19 deletions.
  1. +18 −19 python/plugins/processing/algs/qgis/RandomPointsAlongLines.py
@@ -146,25 +146,24 @@ def processAlgorithm(self, parameters, context, feedback):
length = da.measureLine(startPoint, endPoint)
dist = length * random.random()

if dist > minDistance:
d = dist / (length - dist)
rx = (startPoint.x() + d * endPoint.x()) / (1 + d)
ry = (startPoint.y() + d * endPoint.y()) / (1 + d)

# generate random point
p = QgsPointXY(rx, ry)
geom = QgsGeometry.fromPointXY(p)
if vector.checkMinDistance(p, index, minDistance, points):
f = QgsFeature(nPoints)
f.initAttributes(1)
f.setFields(fields)
f.setAttribute('id', nPoints)
f.setGeometry(geom)
sink.addFeature(f, QgsFeatureSink.FastInsert)
index.addFeature(f)
points[nPoints] = p
nPoints += 1
feedback.setProgress(int(nPoints * total))
d = dist / (length - dist)
rx = (startPoint.x() + d * endPoint.x()) / (1 + d)
ry = (startPoint.y() + d * endPoint.y()) / (1 + d)

# generate random point
p = QgsPointXY(rx, ry)
geom = QgsGeometry.fromPointXY(p)
if vector.checkMinDistance(p, index, minDistance, points):
f = QgsFeature(nPoints)
f.initAttributes(1)
f.setFields(fields)
f.setAttribute('id', nPoints)
f.setGeometry(geom)
sink.addFeature(f, QgsFeatureSink.FastInsert)
index.addFeature(f)
points[nPoints] = p
nPoints += 1
feedback.setProgress(int(nPoints * total))
nIterations += 1

if nPoints < pointCount:

0 comments on commit 02a6fd4

Please sign in to comment.
You can’t perform that action at this time.