Skip to content

Commit

Permalink
[processing] Fix "Random points along line" alg
Browse files Browse the repository at this point in the history
Avoid various errors in case of empty input layer, empty/null geometries, invalid geometries, linestrings with zero-length segments.
  • Loading branch information
agiudiceandrea authored Mar 4, 2024
1 parent b17714c commit 44ce1b2
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions python/plugins/processing/algs/qgis/RandomPointsAlongLines.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,22 @@ def processAlgorithm(self, parameters, context, feedback):

ids = source.allFeatureIds()

while nIterations < maxIterations and nPoints < pointCount:
while ids and nIterations < maxIterations and nPoints < pointCount:
if feedback.isCanceled():
break

# pick random feature
fid = random.choice(ids)
f = next(source.getFeatures(request.setFilterFid(fid).setSubsetOfAttributes([])))
try:
f = next(source.getFeatures(request.setFilterFid(fid).setSubsetOfAttributes([])))
except:
ids.remove(fid)
continue

fGeom = f.geometry()
if fGeom.isEmpty():
ids.remove(fid)
continue

if fGeom.isMultipart():
lines = fGeom.asMultiPolyline()
Expand All @@ -141,6 +149,9 @@ def processAlgorithm(self, parameters, context, feedback):
startPoint = vertices[vid]
endPoint = vertices[vid + 1]
length = da.measureLine(startPoint, endPoint)
if length == 0:
nIterations += 1
continue
dist = length * random.random()

d = dist / (length - dist)
Expand Down

0 comments on commit 44ce1b2

Please sign in to comment.