Skip to content
Permalink
Browse files
[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
  • Loading branch information
alexbruy committed May 16, 2014
1 parent 9b4b1df commit 2374c80e3a519d0873dc6aebab86309a074f2ff7
Showing with 141 additions and 0 deletions.
  1. +2 −0 python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
  2. +139 −0 python/plugins/processing/algs/qgis/RandomPointsAlongLines.py
@@ -94,6 +94,7 @@
from RandomPointsLayer import RandomPointsLayer
from RandomPointsPolygonsFixed import RandomPointsPolygonsFixed
from RandomPointsPolygonsVariable import RandomPointsPolygonsVariable
from RandomPointsAlongLines import RandomPointsAlongLines

# from VectorLayerHistogram import VectorLayerHistogram
# from VectorLayerScatterplot import VectorLayerScatterplot
@@ -149,6 +150,7 @@ def __init__(self):
PointsFromLines(), RandomPointsExtent(),
RandomPointsLayer(), RandomPointsPolygonsFixed(),
RandomPointsPolygonsVariable(),
RandomPointsAlongLines(),
# ------ raster ------
# CreateConstantRaster(),
# ------ graphics ------
@@ -0,0 +1,139 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
RandomPointsAlongLines.py
---------------------
Date : April 2014
Copyright : (C) 2014 by Alexander Bruy
Email : alexander dot bruy at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""

__author__ = 'Alexander Bruy'
__date__ = 'April 2014'
__copyright__ = '(C) 2014, Alexander Bruy'

# This will get replaced with a git SHA1 when you do a git archive

__revision__ = '$Format:%H$'

import math
import random

from PyQt4.QtCore import *

from qgis.core import *

from processing import interface
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.ProcessingLog import ProcessingLog
from processing.parameters.ParameterVector import ParameterVector
from processing.parameters.ParameterNumber import ParameterNumber
from processing.parameters.ParameterSelection import ParameterSelection
from processing.outputs.OutputVector import OutputVector
from processing.tools import dataobjects, vector


class RandomPointsAlongLines(GeoAlgorithm):

VECTOR = 'VECTOR'
POINT_NUMBER = 'POINT_NUMBER'
MIN_DISTANCE = 'MIN_DISTANCE'
OUTPUT = 'OUTPUT'

def defineCharacteristics(self):
self.name = 'Random points along line'
self.group = 'Vector creation tools'
self.addParameter(ParameterVector(self.VECTOR,
'Input layer',[ParameterVector.VECTOR_TYPE_LINE]))
self.addParameter(ParameterNumber
(self.POINT_NUMBER, 'Number of points', 1, 9999999, 1))
self.addParameter(ParameterNumber(
self.MIN_DISTANCE, 'Minimum distance', 0.0, 9999999.0, 0.0))
self.addOutput(OutputVector(self.OUTPUT, 'Random points'))

def processAlgorithm(self, progress):
layer = dataobjects.getObjectFromUri(
self.getParameterValue(self.VECTOR))
pointCount = float(self.getParameterValue(self.POINT_NUMBER))
minDistance = float(self.getParameterValue(self.MIN_DISTANCE))

fields = QgsFields()
fields.append(QgsField('id', QVariant.Int, '', 10, 0))
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
fields, QGis.WKBPoint, layer.dataProvider().crs())

nPoints = 0
nIterations = 0
maxIterations = pointCount * 200
featureCount = layer.featureCount()
total = 100.0 / pointCount

index = QgsSpatialIndex()
points = dict()

da = QgsDistanceArea()
request = QgsFeatureRequest()

random.seed()

while nIterations < maxIterations and nPoints < pointCount:
# pick random feature
fid = random.randint(0, featureCount - 1)
f = layer.getFeatures(request.setFilterFid(fid)).next()
fGeom = QgsGeometry(f.geometry())

if fGeom.isMultipart():
lines = fGeom.asMultiPolyline()
# pick random line
lineId = random.randint(0, len(lines) - 1)
vertices = lines[lineId]
else:
vertices = fGeom.asPolyline()

# pick random segment
if len(vertices) == 2:
vid = 0
else:
vid = random.randint(0, len(vertices) - 2)
startPoint = vertices[vid]
endPoint = vertices[vid + 1]
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
pnt = QgsPoint(rx, ry)
geom = QgsGeometry.fromPoint(pnt)
if vector.checkMinDistance(pnt, index, minDistance, points):
f = QgsFeature(nPoints)
f.initAttributes(1)
f.setFields(fields)
f.setAttribute('id', nPoints)
f.setGeometry(geom)
writer.addFeature(f)
index.insertFeature(f)
points[nPoints] = pnt
nPoints += 1
progress.setPercentage(int(nPoints * total))
nIterations += 1

if nPoints < pointCount:
ProcessingLog.addToLog(
ProcessingLog.LOG_INFO,
'Can not generate requested number of random points. Maximum '
'number of attempts exceeded.')

del writer

0 comments on commit 2374c80

Please sign in to comment.