QGIS/python/plugins/processing/algs/qgis/RegularPoints.py

150 lines
6.1 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
"""
***************************************************************************
RegularPoints.py
---------------------
Date : September 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. *
* *
***************************************************************************
"""
2016-09-21 18:24:26 +02:00
from builtins import str
__author__ = 'Alexander Bruy'
__date__ = 'September 2014'
__copyright__ = '(C) 2014, Alexander Bruy'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import os
from random import seed, uniform
from math import sqrt
2016-04-22 10:38:48 +02:00
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtCore import QVariant
from qgis.core import (QgsRectangle, QgsFields, QgsFeatureSink, QgsField, QgsFeature, QgsWkbTypes,
2017-06-22 18:20:56 +10:00
QgsGeometry, QgsPointXY, QgsCoordinateReferenceSystem,
QgsProcessing,
2017-06-22 18:20:56 +10:00
QgsProcessingParameterExtent,
QgsProcessingParameterNumber,
QgsProcessingParameterBoolean,
QgsProcessingParameterCrs,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterDefinition,
QgsProcessingOutputVectorLayer)
2017-06-06 13:41:42 +10:00
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
class RegularPoints(QgisAlgorithm):
EXTENT = 'EXTENT'
SPACING = 'SPACING'
INSET = 'INSET'
RANDOMIZE = 'RANDOMIZE'
IS_SPACING = 'IS_SPACING'
OUTPUT = 'OUTPUT'
CRS = 'CRS'
def icon(self):
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'regular_points.png'))
def group(self):
return self.tr('Vector creation tools')
def __init__(self):
super().__init__()
2017-06-22 18:20:56 +10:00
self.addParameter(QgsProcessingParameterExtent(self.EXTENT,
self.tr('Input extent'), optional=False))
self.addParameter(QgsProcessingParameterNumber(self.SPACING,
self.tr('Point spacing/count'), QgsProcessingParameterNumber.Double, 100, False, 0.000001, 999999999.999999999))
self.addParameter(QgsProcessingParameterNumber(self.INSET,
self.tr('Initial inset from corner (LH side)'), QgsProcessingParameterNumber.Double, 0.0, False, 0.0, 9999.9999))
self.addParameter(QgsProcessingParameterBoolean(self.RANDOMIZE,
self.tr('Apply random offset to point spacing'), False))
self.addParameter(QgsProcessingParameterBoolean(self.IS_SPACING,
self.tr('Use point spacing'), True))
self.addParameter(QgsProcessingParameterCrs(self.CRS,
self.tr('Output layer CRS'), 'ProjectCrs'))
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Regular points'), QgsProcessing.TypeVectorPoint))
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr('Regular points'), QgsProcessing.TypeVectorPoint))
def name(self):
return 'regularpoints'
def displayName(self):
return self.tr('Regular points')
def processAlgorithm(self, parameters, context, feedback):
2017-06-22 18:20:56 +10:00
extent = self.parameterAsExtent(parameters, self.EXTENT, context)
2017-06-22 18:20:56 +10:00
spacing = self.parameterAsDouble(parameters, self.SPACING, context)
inset = self.parameterAsDouble(parameters, self.INSET, context)
randomize = self.parameterAsBool(parameters, self.RANDOMIZE, context)
isSpacing = self.parameterAsBool(parameters, self.IS_SPACING, context)
crs = self.parameterAsCrs(parameters, self.CRS, context)
fields = QgsFields()
fields.append(QgsField('id', QVariant.Int, '', 10, 0))
2017-06-22 18:20:56 +10:00
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
fields, QgsWkbTypes.Point, crs)
if randomize:
seed()
area = extent.width() * extent.height()
if isSpacing:
pSpacing = spacing
else:
pSpacing = sqrt(area / spacing)
f = QgsFeature()
f.initAttributes(1)
f.setFields(fields)
count = 0
total = 100.0 / (area / pSpacing)
y = extent.yMaximum() - inset
extent_geom = QgsGeometry.fromRect(extent)
extent_engine = QgsGeometry.createGeometryEngine(extent_geom.geometry())
extent_engine.prepareGeometry()
while y >= extent.yMinimum():
x = extent.xMinimum() + inset
while x <= extent.xMaximum():
2017-06-22 18:20:56 +10:00
if feedback.isCanceled():
break
if randomize:
geom = QgsGeometry().fromPoint(QgsPointXY(
uniform(x - (pSpacing / 2.0), x + (pSpacing / 2.0)),
uniform(y - (pSpacing / 2.0), y + (pSpacing / 2.0))))
else:
geom = QgsGeometry().fromPoint(QgsPointXY(x, y))
if extent_engine.intersects(geom.geometry()):
f.setAttribute('id', count)
f.setGeometry(geom)
sink.addFeature(f, QgsFeatureSink.FastInsert)
x += pSpacing
count += 1
feedback.setProgress(int(count * total))
y = y - pSpacing
2017-06-22 18:20:56 +10:00
return {self.OUTPUT: dest_id}