2014-04-25 15:50:21 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
RandomPointsPolygonsVariable.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$'
|
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
import os
|
2014-04-25 15:50:21 +03:00
|
|
|
import random
|
|
|
|
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtGui import QIcon
|
|
|
|
from qgis.PyQt.QtCore import QVariant
|
2017-06-23 14:34:38 +10:00
|
|
|
from qgis.core import (QgsFields, QgsFeatureSink, QgsField, QgsFeature, QgsPointXY, QgsWkbTypes,
|
2017-04-25 17:53:32 +10:00
|
|
|
QgsGeometry, QgsSpatialIndex, QgsDistanceArea,
|
2017-04-24 14:35:50 +10:00
|
|
|
QgsMessageLog,
|
2017-04-25 17:53:32 +10:00
|
|
|
QgsProcessingUtils)
|
2014-04-25 15:50:21 +03:00
|
|
|
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.parameters import ParameterTableField
|
|
|
|
from processing.core.parameters import ParameterNumber
|
|
|
|
from processing.core.parameters import ParameterSelection
|
|
|
|
from processing.core.outputs import OutputVector
|
2014-04-25 15:50:21 +03:00
|
|
|
from processing.tools import dataobjects, vector
|
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
|
|
|
|
2014-04-25 15:50:21 +03:00
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class RandomPointsPolygonsVariable(QgisAlgorithm):
|
2014-04-25 15:50:21 +03:00
|
|
|
|
|
|
|
VECTOR = 'VECTOR'
|
|
|
|
FIELD = 'FIELD'
|
|
|
|
MIN_DISTANCE = 'MIN_DISTANCE'
|
|
|
|
STRATEGY = 'STRATEGY'
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
def icon(self):
|
2016-01-25 15:42:11 +02:00
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'random_points.png'))
|
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Vector creation tools')
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2015-08-31 16:59:11 +02:00
|
|
|
self.strategies = [self.tr('Points count'),
|
|
|
|
self.tr('Points density')]
|
|
|
|
|
2014-04-25 15:50:21 +03:00
|
|
|
self.addParameter(ParameterVector(self.VECTOR,
|
2016-08-23 19:33:42 +03:00
|
|
|
self.tr('Input layer'), [dataobjects.TYPE_VECTOR_POLYGON]))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterSelection(self.STRATEGY,
|
2015-08-31 16:59:11 +02:00
|
|
|
self.tr('Sampling strategy'), self.strategies, 0))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterTableField(self.FIELD,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Number field'),
|
|
|
|
self.VECTOR, ParameterTableField.DATA_TYPE_NUMBER))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterNumber(self.MIN_DISTANCE,
|
2015-10-07 08:59:52 +02:00
|
|
|
self.tr('Minimum distance'), 0.0, None, 0.0))
|
2016-08-23 19:33:42 +03:00
|
|
|
self.addOutput(OutputVector(self.OUTPUT, self.tr('Random points'), datatype=[dataobjects.TYPE_VECTOR_POINT]))
|
2014-04-25 15:50:21 +03:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'randompointsinsidepolygonsvariable'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Random points inside polygons (variable)')
|
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2017-05-02 13:40:49 +10:00
|
|
|
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.VECTOR), context)
|
2014-04-25 15:50:21 +03:00
|
|
|
fieldName = self.getParameterValue(self.FIELD)
|
|
|
|
minDistance = float(self.getParameterValue(self.MIN_DISTANCE))
|
|
|
|
strategy = self.getParameterValue(self.STRATEGY)
|
|
|
|
|
|
|
|
fields = QgsFields()
|
|
|
|
fields.append(QgsField('id', QVariant.Int, '', 10, 0))
|
2017-04-26 14:33:53 +10:00
|
|
|
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, QgsWkbTypes.Point, layer.crs(), context)
|
2014-04-25 15:50:21 +03:00
|
|
|
|
|
|
|
da = QgsDistanceArea()
|
2014-04-26 12:19:50 +03:00
|
|
|
|
2017-04-25 17:59:35 +10:00
|
|
|
features = QgsProcessingUtils.getFeatures(layer, context)
|
2014-04-25 15:50:21 +03:00
|
|
|
for current, f in enumerate(features):
|
2016-08-01 16:25:46 +10:00
|
|
|
fGeom = f.geometry()
|
2014-04-25 15:50:21 +03:00
|
|
|
bbox = fGeom.boundingBox()
|
|
|
|
if strategy == 0:
|
|
|
|
pointCount = int(f[fieldName])
|
|
|
|
else:
|
2016-09-15 18:26:43 +10:00
|
|
|
pointCount = int(round(f[fieldName] * da.measureArea(fGeom)))
|
2014-04-25 15:50:21 +03:00
|
|
|
|
2017-04-19 14:06:16 +03:00
|
|
|
if pointCount == 0:
|
|
|
|
feedback.pushInfo("Skip feature {} as number of points for it is 0.")
|
2014-09-19 20:05:01 +03:00
|
|
|
continue
|
|
|
|
|
2014-04-25 15:50:21 +03:00
|
|
|
index = QgsSpatialIndex()
|
|
|
|
points = dict()
|
|
|
|
|
|
|
|
nPoints = 0
|
|
|
|
nIterations = 0
|
|
|
|
maxIterations = pointCount * 200
|
2017-06-23 13:49:32 +10:00
|
|
|
total = 100.0 / pointCount if pointCount else 1
|
2014-04-25 15:50:21 +03:00
|
|
|
|
2014-04-26 12:19:50 +03:00
|
|
|
random.seed()
|
|
|
|
|
2014-04-25 15:50:21 +03:00
|
|
|
while nIterations < maxIterations and nPoints < pointCount:
|
|
|
|
rx = bbox.xMinimum() + bbox.width() * random.random()
|
|
|
|
ry = bbox.yMinimum() + bbox.height() * random.random()
|
|
|
|
|
2017-06-01 12:18:43 +02:00
|
|
|
pnt = QgsPointXY(rx, ry)
|
2014-04-25 15:50:21 +03:00
|
|
|
geom = QgsGeometry.fromPoint(pnt)
|
|
|
|
if geom.within(fGeom) and \
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
vector.checkMinDistance(pnt, index, minDistance, points):
|
2014-04-25 15:50:21 +03:00
|
|
|
f = QgsFeature(nPoints)
|
|
|
|
f.initAttributes(1)
|
|
|
|
f.setFields(fields)
|
|
|
|
f.setAttribute('id', nPoints)
|
|
|
|
f.setGeometry(geom)
|
2017-06-23 14:34:38 +10:00
|
|
|
writer.addFeature(f, QgsFeatureSink.FastInsert)
|
2014-04-25 15:50:21 +03:00
|
|
|
index.insertFeature(f)
|
|
|
|
points[nPoints] = pnt
|
|
|
|
nPoints += 1
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(nPoints * total))
|
2014-04-25 15:50:21 +03:00
|
|
|
nIterations += 1
|
|
|
|
|
|
|
|
if nPoints < pointCount:
|
2017-04-26 12:52:23 +10:00
|
|
|
QgsMessageLog.logMessage(self.tr('Can not generate requested number of random '
|
|
|
|
'points. Maximum number of attempts exceeded.'), self.tr('Processing'), QgsMessageLog.INFO)
|
2014-04-25 15:50:21 +03:00
|
|
|
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(0)
|
2014-04-25 15:50:21 +03:00
|
|
|
|
|
|
|
del writer
|