|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + RandomPointsPolygonsVariable.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.ParameterTableField import ParameterTableField |
| 40 | +from processing.parameters.ParameterNumber import ParameterNumber |
| 41 | +from processing.parameters.ParameterSelection import ParameterSelection |
| 42 | +from processing.outputs.OutputVector import OutputVector |
| 43 | +from processing.tools import dataobjects, vector |
| 44 | + |
| 45 | + |
| 46 | +class RandomPointsPolygonsVariable(GeoAlgorithm): |
| 47 | + |
| 48 | + VECTOR = 'VECTOR' |
| 49 | + FIELD = 'FIELD' |
| 50 | + MIN_DISTANCE = 'MIN_DISTANCE' |
| 51 | + STRATEGY = 'STRATEGY' |
| 52 | + OUTPUT = 'OUTPUT' |
| 53 | + |
| 54 | + STRATEGIES = ['Points count', |
| 55 | + 'Points density' |
| 56 | + ] |
| 57 | + |
| 58 | + def defineCharacteristics(self): |
| 59 | + self.name = 'Random points inside polygons (variable)' |
| 60 | + self.group = 'Vector creation tools' |
| 61 | + self.addParameter(ParameterVector(self.VECTOR, |
| 62 | + 'Input layer',[ParameterVector.VECTOR_TYPE_POLYGON])) |
| 63 | + self.addParameter(ParameterSelection( |
| 64 | + self.STRATEGY, 'Sampling strategy', self.STRATEGIES, 0)) |
| 65 | + self.addParameter( |
| 66 | + ParameterTableField(self.FIELD, 'Number field', |
| 67 | + self.VECTOR, ParameterTableField.DATA_TYPE_NUMBER)) |
| 68 | + self.addParameter(ParameterNumber( |
| 69 | + self.MIN_DISTANCE, 'Minimum distance', 0.0, 9999999, 0.0)) |
| 70 | + self.addOutput(OutputVector(self.OUTPUT, 'Random points')) |
| 71 | + |
| 72 | + def processAlgorithm(self, progress): |
| 73 | + layer = dataobjects.getObjectFromUri( |
| 74 | + self.getParameterValue(self.VECTOR)) |
| 75 | + fieldName = self.getParameterValue(self.FIELD) |
| 76 | + minDistance = float(self.getParameterValue(self.MIN_DISTANCE)) |
| 77 | + strategy = self.getParameterValue(self.STRATEGY) |
| 78 | + |
| 79 | + fields = QgsFields() |
| 80 | + fields.append(QgsField('id', QVariant.Int, '', 10, 0)) |
| 81 | + writer = self.getOutputFromName(self.OUTPUT).getVectorWriter( |
| 82 | + fields, QGis.WKBPoint, layer.dataProvider().crs()) |
| 83 | + |
| 84 | + request = QgsFeatureRequest() |
| 85 | + |
| 86 | + da = QgsDistanceArea() |
| 87 | + features = vector.features(layer) |
| 88 | + for current, f in enumerate(features): |
| 89 | + fGeom = QgsGeometry(f.geometry()) |
| 90 | + bbox = fGeom.boundingBox() |
| 91 | + if strategy == 0: |
| 92 | + pointCount = int(f[fieldName]) |
| 93 | + else: |
| 94 | + pointCount = int(round(f[fieldName] * da.measure(fGeom))) |
| 95 | + |
| 96 | + index = QgsSpatialIndex() |
| 97 | + points = dict() |
| 98 | + |
| 99 | + nPoints = 0 |
| 100 | + nIterations = 0 |
| 101 | + maxIterations = pointCount * 200 |
| 102 | + total = 100.0 / pointCount |
| 103 | + |
| 104 | + while nIterations < maxIterations and nPoints < pointCount: |
| 105 | + rx = bbox.xMinimum() + bbox.width() * random.random() |
| 106 | + ry = bbox.yMinimum() + bbox.height() * random.random() |
| 107 | + |
| 108 | + pnt = QgsPoint(rx, ry) |
| 109 | + geom = QgsGeometry.fromPoint(pnt) |
| 110 | + if geom.within(fGeom) and \ |
| 111 | + vector.checkMinDistance(pnt, index, minDistance, points): |
| 112 | + f = QgsFeature(nPoints) |
| 113 | + f.initAttributes(1) |
| 114 | + f.setFields(fields) |
| 115 | + f.setAttribute('id', nPoints) |
| 116 | + f.setGeometry(geom) |
| 117 | + writer.addFeature(f) |
| 118 | + index.insertFeature(f) |
| 119 | + points[nPoints] = pnt |
| 120 | + nPoints += 1 |
| 121 | + progress.setPercentage(int(nPoints * total)) |
| 122 | + nIterations += 1 |
| 123 | + |
| 124 | + if nPoints < pointCount: |
| 125 | + ProcessingLog.addToLog( |
| 126 | + ProcessingLog.LOG_INFO, |
| 127 | + 'Can not generate requested number of random points. Maximum ' |
| 128 | + 'number of attempts exceeded.') |
| 129 | + |
| 130 | + progress.setPercentage(0) |
| 131 | + |
| 132 | + del writer |
0 commit comments