Skip to content
Permalink
Browse files

Restore regular points alg

  • Loading branch information
nyalldawson committed Jun 22, 2017
1 parent c33f940 commit 66d1a588ae6608f4ea13e8b58819f23e0f812bfe
@@ -58,8 +58,8 @@
from .ImportIntoPostGIS import ImportIntoPostGIS
from .Merge import Merge
from .PostGISExecuteSQL import PostGISExecuteSQL
from .RegularPoints import RegularPoints

# from .RegularPoints import RegularPoints
# from .SymmetricalDifference import SymmetricalDifference
# from .VectorSplit import VectorSplit
# from .RandomExtract import RandomExtract
@@ -200,7 +200,7 @@ def getAlgs(self):
# RandomSelection(), RandomSelectionWithinSubsets(),
# SelectByLocation(), RandomExtract(),
# RandomExtractWithinSubsets(), ExtractByLocation(),
# SpatialJoin(), RegularPoints(), SymmetricalDifference(),
# SpatialJoin(), SymmetricalDifference(),
# VectorSplit(),
# DeleteDuplicateGeometries(), TextToFloat(),
# SelectByAttribute(),
@@ -261,7 +261,8 @@ def getAlgs(self):
GridPolygon(),
ImportIntoPostGIS(),
Merge(),
PostGISExecuteSQL()
PostGISExecuteSQL(),
RegularPoints()
]

if hasPlotly:
@@ -33,15 +33,16 @@
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtCore import QVariant
from qgis.core import (QgsRectangle, QgsFields, QgsField, QgsFeature, QgsWkbTypes,
QgsGeometry, QgsPointXY, QgsCoordinateReferenceSystem)
QgsGeometry, QgsPointXY, QgsCoordinateReferenceSystem,
QgsProcessingParameterExtent,
QgsProcessingParameterNumber,
QgsProcessingParameterBoolean,
QgsProcessingParameterCrs,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterDefinition,
QgsProcessingOutputVectorLayer)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.parameters import ParameterExtent
from processing.core.parameters import ParameterNumber
from processing.core.parameters import ParameterBoolean
from processing.core.parameters import ParameterCrs
from processing.core.outputs import OutputVector
from processing.tools import dataobjects

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]

@@ -64,19 +65,21 @@ def group(self):

def __init__(self):
super().__init__()
self.addParameter(ParameterExtent(self.EXTENT,
self.tr('Input extent'), optional=False))
self.addParameter(ParameterNumber(self.SPACING,
self.tr('Point spacing/count'), 100, 999999999.999999999, 100))
self.addParameter(ParameterNumber(self.INSET,
self.tr('Initial inset from corner (LH side)'), 0.0, 9999.9999, 0.0))
self.addParameter(ParameterBoolean(self.RANDOMIZE,
self.tr('Apply random offset to point spacing'), False))
self.addParameter(ParameterBoolean(self.IS_SPACING,
self.tr('Use point spacing'), True))
self.addParameter(ParameterCrs(self.CRS,
self.tr('Output layer CRS'), 'ProjectCrs'))
self.addOutput(OutputVector(self.OUTPUT, self.tr('Regular points'), datatype=[dataobjects.TYPE_VECTOR_POINT]))
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'), QgsProcessingParameterDefinition.TypeVectorPoint))
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr('Regular points'), QgsProcessingParameterDefinition.TypeVectorPoint))

def name(self):
return 'regularpoints'
@@ -85,23 +88,19 @@ def displayName(self):
return self.tr('Regular points')

def processAlgorithm(self, parameters, context, feedback):
extent = str(self.getParameterValue(self.EXTENT)).split(',')
extent = self.parameterAsExtent(parameters, self.EXTENT, context)

spacing = float(self.getParameterValue(self.SPACING))
inset = float(self.getParameterValue(self.INSET))
randomize = self.getParameterValue(self.RANDOMIZE)
isSpacing = self.getParameterValue(self.IS_SPACING)
crsId = self.getParameterValue(self.CRS)
crs = QgsCoordinateReferenceSystem()
crs.createFromUserInput(crsId)

extent = QgsRectangle(float(extent[0]), float(extent[2]),
float(extent[1]), float(extent[3]))
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))

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, QgsWkbTypes.Point, crs, context)
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
fields, QgsWkbTypes.Point, crs)

if randomize:
seed()
@@ -127,6 +126,9 @@ def processAlgorithm(self, parameters, context, feedback):
while y >= extent.yMinimum():
x = extent.xMinimum() + inset
while x <= extent.xMaximum():
if feedback.isCanceled():
break

if randomize:
geom = QgsGeometry().fromPoint(QgsPointXY(
uniform(x - (pSpacing / 2.0), x + (pSpacing / 2.0)),
@@ -137,9 +139,10 @@ def processAlgorithm(self, parameters, context, feedback):
if extent_engine.intersects(geom.geometry()):
f.setAttribute('id', count)
f.setGeometry(geom)
writer.addFeature(f)
sink.addFeature(f)
x += pSpacing
count += 1
feedback.setProgress(int(count * total))
y = y - pSpacing
del writer

return {self.OUTPUT: dest_id}
@@ -1566,15 +1566,15 @@ tests:
name: expected/extract_by_attribute_greater.gml
type: vector

# - algorithm: qgis:createattributeindex
# name: Create Attribute Index (only tests for python errors, does not check result)
# params:
# FIELD: fid
# INPUT:
# name: lines.gml
# type: vector
# results: {}
#
- algorithm: qgis:createattributeindex
name: Create Attribute Index (only tests for python errors, does not check result)
params:
FIELD: fid
INPUT:
name: lines.gml
type: vector
results: {}

- algorithm: qgis:deletecolumn
name: Delete columns (multiple)
params:
@@ -2133,20 +2133,20 @@ tests:
# # name: expected/servicearea_bounds.gml
# # type: vector
#
# - algorithm: qgis:createattributeindex
# name: Create attribute index
# params:
# FIELD: id
# INPUT:
# name: custom/points.shp
# type: vector
# in_place: true
# results:
# INPUT:
# name: expected/create_attr_index_points.shp
# type: vector
# in_place_result: true
#
- algorithm: qgis:createattributeindex
name: Create attribute index
params:
FIELD: id
INPUT:
name: custom/points.shp
type: vector
in_place: true
results:
INPUT:
name: expected/create_attr_index_points.shp
type: vector
in_place_result: true

# - algorithm: qgis:createspatialindex
# name: Create spatial index
# params:
@@ -2375,23 +2375,23 @@ tests:
# name: expected/topocolor_polys_min_dist.gml
# type: vector
#
# - algorithm: qgis:regularpoints
# name: Regular point with standard extent
# params:
# EXTENT: -0.9182432432432436,10.208108108108108,-3.1266891891891904,5.480067567567567
# INSET: 0.0
# IS_SPACING: false
# RANDOMIZE: false
# SPACING: 100
# CRS: EPSG:4326
# results:
# OUTPUT:
# name: expected/regular_points.gml
# type: vector
# compare:
# geometry:
# precision: 5
#
- algorithm: qgis:regularpoints
name: Regular point with standard extent
params:
EXTENT: -0.9182432432432436,10.208108108108108,-3.1266891891891904,5.480067567567567
INSET: 0.0
IS_SPACING: false
RANDOMIZE: false
SPACING: 100
CRS: EPSG:4326
results:
OUTPUT:
name: expected/regular_points.gml
type: vector
compare:
geometry:
precision: 5

# - algorithm: qgis:rectanglesovalsdiamondsvariable
# name: Rectangular buffer shape
# params:

0 comments on commit 66d1a58

Please sign in to comment.
You can’t perform that action at this time.