Skip to content

Commit

Permalink
Fix "Random points along line" alg
Browse files Browse the repository at this point in the history
Fixes "Random points along line" (qgis:randompointsalongline) algorithm.
See #33156 (comment)
  • Loading branch information
agiudiceandrea authored and nyalldawson committed Feb 4, 2020
1 parent 5e3ba6c commit c589bde
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions python/plugins/processing/algs/qgis/RandomPointsAlongLines.py
Expand Up @@ -144,25 +144,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:
Expand Down

0 comments on commit c589bde

Please sign in to comment.