mirror of
https://github.com/qgis/QGIS.git
synced 2025-10-15 00:02:52 -04:00
[processing] Fix "Random points along line" alg
Avoid various errors in case of empty input layer, empty/null geometries, invalid geometries, linestrings with zero-length segments...
This commit is contained in:
parent
a4f1707d46
commit
9c32e69f88
@ -100,7 +100,6 @@ class RandomPointsAlongLines(QgisAlgorithm):
|
|||||||
nPoints = 0
|
nPoints = 0
|
||||||
nIterations = 0
|
nIterations = 0
|
||||||
maxIterations = pointCount * 200
|
maxIterations = pointCount * 200
|
||||||
featureCount = source.featureCount()
|
|
||||||
total = 100.0 / pointCount if pointCount else 1
|
total = 100.0 / pointCount if pointCount else 1
|
||||||
|
|
||||||
index = QgsSpatialIndex()
|
index = QgsSpatialIndex()
|
||||||
@ -116,14 +115,22 @@ class RandomPointsAlongLines(QgisAlgorithm):
|
|||||||
|
|
||||||
ids = source.allFeatureIds()
|
ids = source.allFeatureIds()
|
||||||
|
|
||||||
while nIterations < maxIterations and nPoints < pointCount:
|
while ids and nIterations < maxIterations and nPoints < pointCount:
|
||||||
if feedback.isCanceled():
|
if feedback.isCanceled():
|
||||||
break
|
break
|
||||||
|
|
||||||
# pick random feature
|
# pick random feature
|
||||||
fid = random.choice(ids)
|
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()
|
fGeom = f.geometry()
|
||||||
|
if fGeom.isEmpty():
|
||||||
|
ids.remove(fid)
|
||||||
|
continue
|
||||||
|
|
||||||
if fGeom.isMultipart():
|
if fGeom.isMultipart():
|
||||||
lines = fGeom.asMultiPolyline()
|
lines = fGeom.asMultiPolyline()
|
||||||
@ -134,13 +141,20 @@ class RandomPointsAlongLines(QgisAlgorithm):
|
|||||||
vertices = fGeom.asPolyline()
|
vertices = fGeom.asPolyline()
|
||||||
|
|
||||||
# pick random segment
|
# pick random segment
|
||||||
if len(vertices) == 2:
|
nVertices = len(vertices)
|
||||||
|
if nVertices < 2:
|
||||||
|
nIterations += 1
|
||||||
|
continue
|
||||||
|
if nVertices == 2:
|
||||||
vid = 0
|
vid = 0
|
||||||
else:
|
else:
|
||||||
vid = random.randint(0, len(vertices) - 2)
|
vid = random.randint(0, nVertices - 2)
|
||||||
startPoint = vertices[vid]
|
startPoint = vertices[vid]
|
||||||
endPoint = vertices[vid + 1]
|
endPoint = vertices[vid + 1]
|
||||||
length = da.measureLine(startPoint, endPoint)
|
length = da.measureLine(startPoint, endPoint)
|
||||||
|
if length == 0:
|
||||||
|
nIterations += 1
|
||||||
|
continue
|
||||||
dist = length * random.random()
|
dist = length * random.random()
|
||||||
|
|
||||||
d = dist / (length - dist)
|
d = dist / (length - dist)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user