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$'
|
|
|
|
|
|
|
|
import random
|
|
|
|
|
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
|
|
|
from PyQt4.QtCore import QVariant
|
|
|
|
from qgis.core import QGis, QgsFields, QgsField, QgsDistanceArea, QgsGeometry, 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
|
|
|
|
|
|
|
|
|
|
|
|
class RandomPointsPolygonsFixed(GeoAlgorithm):
|
|
|
|
|
|
|
|
VECTOR = 'VECTOR'
|
|
|
|
VALUE = 'VALUE'
|
|
|
|
MIN_DISTANCE = 'MIN_DISTANCE'
|
|
|
|
STRATEGY = 'STRATEGY'
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
|
|
|
|
STRATEGIES = ['Points count',
|
|
|
|
'Points density'
|
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
|
|
|
]
|
2014-04-24 21:02:04 +03:00
|
|
|
|
|
|
|
def defineCharacteristics(self):
|
2014-04-25 12:39:06 +03:00
|
|
|
self.name = 'Random points inside polygons (fixed)'
|
2014-04-24 21:02:04 +03:00
|
|
|
self.group = 'Vector creation tools'
|
|
|
|
self.addParameter(ParameterVector(self.VECTOR,
|
2015-01-15 20:41:15 +02:00
|
|
|
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_POLYGON]))
|
|
|
|
self.addParameter(ParameterSelection(self.STRATEGY,
|
|
|
|
self.tr('Sampling strategy'), self.STRATEGIES, 0))
|
|
|
|
self.addParameter(ParameterNumber(self.VALUE,
|
|
|
|
self.tr('Number or density of points'), 0.0001, 9999999.0, 1.0))
|
|
|
|
self.addParameter(ParameterNumber(self.MIN_DISTANCE,
|
|
|
|
self.tr('Minimum distance'), 0.0, 9999999, 0.0))
|
|
|
|
|
|
|
|
self.addOutput(OutputVector(self.OUTPUT, self.tr('Random points')))
|
2014-04-24 21:02:04 +03:00
|
|
|
|
|
|
|
def processAlgorithm(self, progress):
|
|
|
|
layer = dataobjects.getObjectFromUri(
|
|
|
|
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(
|
|
|
|
fields, QGis.WKBPoint, layer.dataProvider().crs())
|
|
|
|
|
|
|
|
da = QgsDistanceArea()
|
2014-04-26 12:19:50 +03:00
|
|
|
|
2014-04-24 21:02:04 +03:00
|
|
|
features = vector.features(layer)
|
|
|
|
for current, f in enumerate(features):
|
|
|
|
fGeom = QgsGeometry(f.geometry())
|
|
|
|
bbox = fGeom.boundingBox()
|
|
|
|
if strategy == 0:
|
|
|
|
pointCount = int(value)
|
|
|
|
else:
|
|
|
|
pointCount = int(round(value * da.measure(fGeom)))
|
|
|
|
|
|
|
|
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
|
|
|
|
progress.setPercentage(int(nPoints * total))
|
|
|
|
nIterations += 1
|
|
|
|
|
|
|
|
if nPoints < pointCount:
|
2015-01-15 20:41:15 +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
|
|
|
|
|
|
|
progress.setPercentage(0)
|
|
|
|
|
|
|
|
del writer
|