Skip to content

Commit 2374c80

Browse files
committed
[processing] new tool: random points along lines
Work done for Faunalia funded by Prof. António Mira (University of Évora, Portugal, Unidade de Biologia da Conservação) and Dr. Rosana Peixoto
1 parent 9b4b1df commit 2374c80

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed

python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py

+2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
from RandomPointsLayer import RandomPointsLayer
9595
from RandomPointsPolygonsFixed import RandomPointsPolygonsFixed
9696
from RandomPointsPolygonsVariable import RandomPointsPolygonsVariable
97+
from RandomPointsAlongLines import RandomPointsAlongLines
9798

9899
# from VectorLayerHistogram import VectorLayerHistogram
99100
# from VectorLayerScatterplot import VectorLayerScatterplot
@@ -149,6 +150,7 @@ def __init__(self):
149150
PointsFromLines(), RandomPointsExtent(),
150151
RandomPointsLayer(), RandomPointsPolygonsFixed(),
151152
RandomPointsPolygonsVariable(),
153+
RandomPointsAlongLines(),
152154
# ------ raster ------
153155
# CreateConstantRaster(),
154156
# ------ graphics ------
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
RandomPointsAlongLines.py
6+
---------------------
7+
Date : April 2014
8+
Copyright : (C) 2014 by Alexander Bruy
9+
Email : alexander dot bruy at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Alexander Bruy'
21+
__date__ = 'April 2014'
22+
__copyright__ = '(C) 2014, Alexander Bruy'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
import math
29+
import random
30+
31+
from PyQt4.QtCore import *
32+
33+
from qgis.core import *
34+
35+
from processing import interface
36+
from processing.core.GeoAlgorithm import GeoAlgorithm
37+
from processing.core.ProcessingLog import ProcessingLog
38+
from processing.parameters.ParameterVector import ParameterVector
39+
from processing.parameters.ParameterNumber import ParameterNumber
40+
from processing.parameters.ParameterSelection import ParameterSelection
41+
from processing.outputs.OutputVector import OutputVector
42+
from processing.tools import dataobjects, vector
43+
44+
45+
class RandomPointsAlongLines(GeoAlgorithm):
46+
47+
VECTOR = 'VECTOR'
48+
POINT_NUMBER = 'POINT_NUMBER'
49+
MIN_DISTANCE = 'MIN_DISTANCE'
50+
OUTPUT = 'OUTPUT'
51+
52+
def defineCharacteristics(self):
53+
self.name = 'Random points along line'
54+
self.group = 'Vector creation tools'
55+
self.addParameter(ParameterVector(self.VECTOR,
56+
'Input layer',[ParameterVector.VECTOR_TYPE_LINE]))
57+
self.addParameter(ParameterNumber
58+
(self.POINT_NUMBER, 'Number of points', 1, 9999999, 1))
59+
self.addParameter(ParameterNumber(
60+
self.MIN_DISTANCE, 'Minimum distance', 0.0, 9999999.0, 0.0))
61+
self.addOutput(OutputVector(self.OUTPUT, 'Random points'))
62+
63+
def processAlgorithm(self, progress):
64+
layer = dataobjects.getObjectFromUri(
65+
self.getParameterValue(self.VECTOR))
66+
pointCount = float(self.getParameterValue(self.POINT_NUMBER))
67+
minDistance = float(self.getParameterValue(self.MIN_DISTANCE))
68+
69+
fields = QgsFields()
70+
fields.append(QgsField('id', QVariant.Int, '', 10, 0))
71+
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
72+
fields, QGis.WKBPoint, layer.dataProvider().crs())
73+
74+
nPoints = 0
75+
nIterations = 0
76+
maxIterations = pointCount * 200
77+
featureCount = layer.featureCount()
78+
total = 100.0 / pointCount
79+
80+
index = QgsSpatialIndex()
81+
points = dict()
82+
83+
da = QgsDistanceArea()
84+
request = QgsFeatureRequest()
85+
86+
random.seed()
87+
88+
while nIterations < maxIterations and nPoints < pointCount:
89+
# pick random feature
90+
fid = random.randint(0, featureCount - 1)
91+
f = layer.getFeatures(request.setFilterFid(fid)).next()
92+
fGeom = QgsGeometry(f.geometry())
93+
94+
if fGeom.isMultipart():
95+
lines = fGeom.asMultiPolyline()
96+
# pick random line
97+
lineId = random.randint(0, len(lines) - 1)
98+
vertices = lines[lineId]
99+
else:
100+
vertices = fGeom.asPolyline()
101+
102+
# pick random segment
103+
if len(vertices) == 2:
104+
vid = 0
105+
else:
106+
vid = random.randint(0, len(vertices) - 2)
107+
startPoint = vertices[vid]
108+
endPoint = vertices[vid + 1]
109+
length = da.measureLine(startPoint, endPoint)
110+
dist = length * random.random()
111+
112+
if dist > minDistance:
113+
d = dist / (length - dist)
114+
rx = (startPoint.x() + d * endPoint.x()) / (1 + d)
115+
ry = (startPoint.y() + d * endPoint.y()) / (1 + d)
116+
117+
# generate random point
118+
pnt = QgsPoint(rx, ry)
119+
geom = QgsGeometry.fromPoint(pnt)
120+
if vector.checkMinDistance(pnt, index, minDistance, points):
121+
f = QgsFeature(nPoints)
122+
f.initAttributes(1)
123+
f.setFields(fields)
124+
f.setAttribute('id', nPoints)
125+
f.setGeometry(geom)
126+
writer.addFeature(f)
127+
index.insertFeature(f)
128+
points[nPoints] = pnt
129+
nPoints += 1
130+
progress.setPercentage(int(nPoints * total))
131+
nIterations += 1
132+
133+
if nPoints < pointCount:
134+
ProcessingLog.addToLog(
135+
ProcessingLog.LOG_INFO,
136+
'Can not generate requested number of random points. Maximum '
137+
'number of attempts exceeded.')
138+
139+
del writer

0 commit comments

Comments
 (0)