2014-04-24 21:02:04 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
RandomPointsPolygonsFixed.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-24 21:02:04 +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-03-04 19:41:23 +01:00
|
|
|
from qgis.core import (QgsFields, QgsField, QgsDistanceArea, QgsGeometry, QgsWkbTypes,
|
2016-02-17 09:36:59 +02:00
|
|
|
QgsSpatialIndex, QgsPoint, QgsFeature)
|
2014-04-24 21:02:04 +03:00
|
|
|
|
|
|
|
from processing.core.GeoAlgorithm import GeoAlgorithm
|
|
|
|
from processing.core.ProcessingLog import ProcessingLog
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.parameters import ParameterNumber
|
|
|
|
from processing.core.parameters import ParameterSelection
|
|
|
|
from processing.core.outputs import OutputVector
|
2014-04-24 21:02:04 +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-24 21:02:04 +03:00
|
|
|
|
|
|
|
class RandomPointsPolygonsFixed(GeoAlgorithm):
|
|
|
|
|
|
|
|
VECTOR = 'VECTOR'
|
|
|
|
VALUE = 'VALUE'
|
|
|
|
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-03-29 12:51:59 +10:00
|
|
|
def name(self):
|
2017-03-29 17:09:39 +10:00
|
|
|
return 'randompointsinsidepolygonsfixed'
|
2017-03-29 12:51:59 +10:00
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Random points inside polygons (fixed)')
|
2015-08-31 16:59:11 +02:00
|
|
|
|
2017-03-29 12:51:59 +10:00
|
|
|
def defineCharacteristics(self):
|
2015-08-31 16:59:11 +02:00
|
|
|
self.strategies = [self.tr('Points count'),
|
|
|
|
self.tr('Points density')]
|
|
|
|
|
2014-04-24 21:02:04 +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(ParameterNumber(self.VALUE,
|
2015-10-07 08:59:52 +02:00
|
|
|
self.tr('Number or density of points'), 0.0001, None, 1.0))
|
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))
|
2015-01-15 20:41:15 +02:00
|
|
|
|
2016-08-23 19:33:42 +03:00
|
|
|
self.addOutput(OutputVector(self.OUTPUT, self.tr('Random points'), datatype=[dataobjects.TYPE_VECTOR_POINT]))
|
2014-04-24 21:02:04 +03:00
|
|
|
|
2017-04-25 14:55:38 +10:00
|
|
|
def processAlgorithm(self, context, feedback):
|
2017-04-05 18:35:55 +10:00
|
|
|
layer = dataobjects.getLayerFromString(
|
2014-04-24 21:02:04 +03:00
|
|
|
self.getParameterValue(self.VECTOR))
|
|
|
|
value = float(self.getParameterValue(self.VALUE))
|
|
|
|
minDistance = float(self.getParameterValue(self.MIN_DISTANCE))
|
|
|
|
strategy = self.getParameterValue(self.STRATEGY)
|
|
|
|
|
|
|
|
fields = QgsFields()
|
|
|
|
fields.append(QgsField('id', QVariant.Int, '', 10, 0))
|
|
|
|
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
|
2016-08-04 07:33:49 +10:00
|
|
|
fields, QgsWkbTypes.Point, layer.crs())
|
2014-04-24 21:02:04 +03:00
|
|
|
|
|
|
|
da = QgsDistanceArea()
|
2014-04-26 12:19:50 +03:00
|
|
|
|
2017-04-25 15:57:24 +10:00
|
|
|
features = vector.features(layer, context)
|
2014-04-24 21:02:04 +03:00
|
|
|
for current, f in enumerate(features):
|
2016-08-01 16:25:46 +10:00
|
|
|
fGeom = f.geometry()
|
2014-04-24 21:02:04 +03:00
|
|
|
bbox = fGeom.boundingBox()
|
|
|
|
if strategy == 0:
|
|
|
|
pointCount = int(value)
|
|
|
|
else:
|
2016-09-15 18:26:43 +10:00
|
|
|
pointCount = int(round(value * da.measureArea(fGeom)))
|
2014-04-24 21:02:04 +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.")
|
2017-04-19 14:01:41 +03:00
|
|
|
continue
|
|
|
|
|
2014-04-24 21:02:04 +03:00
|
|
|
index = QgsSpatialIndex()
|
|
|
|
points = dict()
|
|
|
|
|
|
|
|
nPoints = 0
|
|
|
|
nIterations = 0
|
|
|
|
maxIterations = pointCount * 200
|
|
|
|
total = 100.0 / pointCount
|
|
|
|
|
2014-04-26 12:19:50 +03:00
|
|
|
random.seed()
|
|
|
|
|
2014-04-24 21:02:04 +03:00
|
|
|
while nIterations < maxIterations and nPoints < pointCount:
|
|
|
|
rx = bbox.xMinimum() + bbox.width() * random.random()
|
|
|
|
ry = bbox.yMinimum() + bbox.height() * random.random()
|
|
|
|
|
|
|
|
pnt = QgsPoint(rx, ry)
|
|
|
|
geom = QgsGeometry.fromPoint(pnt)
|
|
|
|
if geom.within(fGeom) and \
|
2014-04-25 12:39:06 +03:00
|
|
|
vector.checkMinDistance(pnt, index, minDistance, points):
|
2014-04-24 21:02:04 +03:00
|
|
|
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
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(nPoints * total))
|
2014-04-24 21:02:04 +03:00
|
|
|
nIterations += 1
|
|
|
|
|
|
|
|
if nPoints < pointCount:
|
2015-08-22 14:29:41 +02:00
|
|
|
ProcessingLog.addToLog(ProcessingLog.LOG_INFO,
|
|
|
|
self.tr('Can not generate requested number of random '
|
|
|
|
'points. Maximum number of attempts exceeded.'))
|
2014-04-24 21:02:04 +03:00
|
|
|
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(0)
|
2014-04-24 21:02:04 +03:00
|
|
|
|
|
|
|
del writer
|