Skip to content
Permalink
Browse files

Port rectangles, ovals, diamonds to new API

  • Loading branch information
nyalldawson committed Aug 18, 2017
1 parent 4820216 commit 076fdc9449c4508a044bc69dd3dcc8effcfe2e22
@@ -119,6 +119,8 @@
from .RandomSelectionWithinSubsets import RandomSelectionWithinSubsets
from .Rasterize import RasterizeAlgorithm
from .RasterLayerStatistics import RasterLayerStatistics
from .RectanglesOvalsDiamondsFixed import RectanglesOvalsDiamondsFixed
from .RectanglesOvalsDiamondsVariable import RectanglesOvalsDiamondsVariable
from .RegularPoints import RegularPoints
from .Relief import Relief
from .ReverseLineDirection import ReverseLineDirection
@@ -166,8 +168,6 @@
# from .FieldPyculator import FieldsPyculator
# from .SelectByAttributeSum import SelectByAttributeSum
# from .DefineProjection import DefineProjection
# from .RectanglesOvalsDiamondsVariable import RectanglesOvalsDiamondsVariable
# from .RectanglesOvalsDiamondsFixed import RectanglesOvalsDiamondsFixed
# from .IdwInterpolation import IdwInterpolation
# from .TinInterpolation import TinInterpolation
# from .RasterCalculator import RasterCalculator
@@ -194,8 +194,6 @@ def getAlgs(self):
# FieldsPyculator(),
# FieldsMapper(), SelectByAttributeSum()
# DefineProjection(),
# RectanglesOvalsDiamondsVariable(),
# RectanglesOvalsDiamondsFixed(),
# IdwInterpolation(), TinInterpolation(),
# RasterCalculator(),
# ExecuteSQL(), FindProjection(),
@@ -279,6 +277,8 @@ def getAlgs(self):
RandomSelectionWithinSubsets(),
RasterizeAlgorithm(),
RasterLayerStatistics(),
RectanglesOvalsDiamondsFixed(),
RectanglesOvalsDiamondsVariable(),
RegularPoints(),
Relief(),
ReverseLineDirection(),
@@ -28,31 +28,29 @@

import math

from qgis.core import (QgsApplication,
from qgis.core import (QgsProcessingParameterFeatureSource,
QgsProcessingParameterEnum,
QgsProcessingParameterNumber,
QgsProcessingParameterFeatureSink,
QgsFeature,
QgsFeatureSink,
QgsGeometry,
QgsPointXY,
QgsWkbTypes,
QgsProcessingUtils)
QgsProcessing)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterSelection
from processing.core.parameters import ParameterNumber
from processing.core.outputs import OutputVector
from processing.tools import dataobjects


class RectanglesOvalsDiamondsFixed(QgisAlgorithm):

INPUT_LAYER = 'INPUT_LAYER'
INPUT = 'INPUT'
SHAPE = 'SHAPE'
WIDTH = 'WIDTH'
HEIGHT = 'HEIGHT'
ROTATION = 'ROTATION'
SEGMENTS = 'SEGMENTS'
OUTPUT_LAYER = 'OUTPUT_LAYER'
OUTPUT = 'OUTPUT'

def group(self):
return self.tr('Vector geometry tools')
@@ -63,26 +61,26 @@ def __init__(self):
def initAlgorithm(self, config=None):
self.shapes = [self.tr('Rectangles'), self.tr('Diamonds'), self.tr('Ovals')]

self.addParameter(ParameterVector(self.INPUT_LAYER,
self.tr('Input layer'),
[dataobjects.TYPE_VECTOR_POINT]))
self.addParameter(ParameterSelection(self.SHAPE,
self.tr('Buffer shape'), self.shapes))
self.addParameter(ParameterNumber(self.WIDTH, self.tr('Width'),
0.0000001, 999999999.0, 1.0))
self.addParameter(ParameterNumber(self.HEIGHT, self.tr('Height'),
0.0000001, 999999999.0, 1.0))
self.addParameter(ParameterNumber(self.ROTATION, self.tr('Rotation'),
0.0, 360.0, optional=True))
self.addParameter(ParameterNumber(self.SEGMENTS,
self.tr('Number of segments'),
1,
999999999,
36))

self.addOutput(OutputVector(self.OUTPUT_LAYER,
self.tr('Output'),
datatype=[dataobjects.TYPE_VECTOR_POLYGON]))
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
self.tr('Input layer'),
[QgsProcessing.TypeVectorPoint]))
self.addParameter(QgsProcessingParameterEnum(self.SHAPE,
self.tr('Buffer shape'), options=self.shapes))
self.addParameter(QgsProcessingParameterNumber(self.WIDTH, self.tr('Width'), type=QgsProcessingParameterNumber.Double,
minValue=0.0000001, maxValue=999999999.0, defaultValue=1.0))
self.addParameter(QgsProcessingParameterNumber(self.HEIGHT, self.tr('Height'), type=QgsProcessingParameterNumber.Double,
minValue=0.0000001, maxValue=999999999.0, defaultValue=1.0))
self.addParameter(QgsProcessingParameterNumber(self.ROTATION, self.tr('Rotation'), type=QgsProcessingParameterNumber.Double,
minValue=0.0, maxValue=360.0, optional=True))
self.addParameter(QgsProcessingParameterNumber(self.SEGMENTS,
self.tr('Number of segments'),
minValue=1,
maxValue=999999999,
defaultValue=36))

self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT,
self.tr('Output'),
type=QgsProcessing.TypeVectorPolygon))

def name(self):
return 'rectanglesovalsdiamondsfixed'
@@ -91,36 +89,44 @@ def displayName(self):
return self.tr('Rectangles, ovals, diamonds (fixed)')

def processAlgorithm(self, parameters, context, feedback):
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT_LAYER), context)
shape = self.getParameterValue(self.SHAPE)
width = self.getParameterValue(self.WIDTH)
height = self.getParameterValue(self.HEIGHT)
rotation = self.getParameterValue(self.ROTATION)
segments = self.getParameterValue(self.SEGMENTS)
source = self.parameterAsSource(parameters, self.INPUT, context)
shape = self.parameterAsEnum(parameters, self.SHAPE, context)
width = self.parameterAsDouble(parameters, self.WIDTH, context)
height = self.parameterAsDouble(parameters, self.HEIGHT, context)
rotation = self.parameterAsDouble(parameters, self.ROTATION, context)
segments = self.parameterAsInt(parameters, self.SEGMENTS, context)

writer = self.getOutputFromName(
self.OUTPUT_LAYER).getVectorWriter(layer.fields(), QgsWkbTypes.Polygon, layer.crs(), context)

features = QgsProcessingUtils.getFeatures(layer, context)
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
source.fields(), QgsWkbTypes.Polygon, source.sourceCrs())

if shape == 0:
self.rectangles(writer, features, width, height, rotation)
self.rectangles(sink, source, width, height, rotation, feedback)
elif shape == 1:
self.diamonds(writer, features, width, height, rotation)
self.diamonds(sink, source, width, height, rotation, feedback)
else:
self.ovals(writer, features, width, height, rotation, segments)
self.ovals(sink, source, width, height, rotation, segments, feedback)

return {self.OUTPUT: dest_id}

del writer
def rectangles(self, sink, source, width, height, rotation, feedback):

def rectangles(self, writer, features, width, height, rotation):
features = source.getFeatures()
ft = QgsFeature()

xOffset = width / 2.0
yOffset = height / 2.0

total = 100.0 / source.featureCount() if source.featureCount() else 0

if rotation is not None:
phi = rotation * math.pi / 180
for current, feat in enumerate(features):
if feedback.isCanceled():
break

if not feat.hasGeometry():
continue

point = feat.geometry().asPoint()
x = point.x()
y = point.y()
@@ -130,9 +136,17 @@ def rectangles(self, writer, features, width, height, rotation):

ft.setGeometry(QgsGeometry.fromPolygon(polygon))
ft.setAttributes(feat.attributes())
writer.addFeature(ft, QgsFeatureSink.FastInsert)
sink.addFeature(ft, QgsFeatureSink.FastInsert)

feedback.setProgress(int(current * total))
else:
for current, feat in enumerate(features):
if feedback.isCanceled():
break

if not feat.hasGeometry():
continue

point = feat.geometry().asPoint()
x = point.x()
y = point.y()
@@ -141,17 +155,27 @@ def rectangles(self, writer, features, width, height, rotation):

ft.setGeometry(QgsGeometry.fromPolygon(polygon))
ft.setAttributes(feat.attributes())
writer.addFeature(ft, QgsFeatureSink.FastInsert)
sink.addFeature(ft, QgsFeatureSink.FastInsert)

feedback.setProgress(int(current * total))

def diamonds(self, writer, features, width, height, rotation):
def diamonds(self, sink, source, width, height, rotation, feedback):
features = source.getFeatures()
ft = QgsFeature()

xOffset = width / 2.0
yOffset = height / 2.0

total = 100.0 / source.featureCount() if source.featureCount() else 0
if rotation is not None:
phi = rotation * math.pi / 180
for current, feat in enumerate(features):
if feedback.isCanceled():
break

if not feat.hasGeometry():
continue

point = feat.geometry().asPoint()
x = point.x()
y = point.y()
@@ -161,9 +185,16 @@ def diamonds(self, writer, features, width, height, rotation):

ft.setGeometry(QgsGeometry.fromPolygon(polygon))
ft.setAttributes(feat.attributes())
writer.addFeature(ft, QgsFeatureSink.FastInsert)
sink.addFeature(ft, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))
else:
for current, feat in enumerate(features):
if feedback.isCanceled():
break

if not feat.hasGeometry():
continue

point = feat.geometry().asPoint()
x = point.x()
y = point.y()
@@ -172,17 +203,26 @@ def diamonds(self, writer, features, width, height, rotation):

ft.setGeometry(QgsGeometry.fromPolygon(polygon))
ft.setAttributes(feat.attributes())
writer.addFeature(ft, QgsFeatureSink.FastInsert)
sink.addFeature(ft, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))

def ovals(self, writer, features, width, height, rotation, segments):
def ovals(self, sink, source, width, height, rotation, segments, feedback):
features = source.getFeatures()
ft = QgsFeature()

xOffset = width / 2.0
yOffset = height / 2.0

total = 100.0 / source.featureCount() if source.featureCount() else 0
if rotation is not None:
phi = rotation * math.pi / 180
for current, feat in enumerate(features):
if feedback.isCanceled():
break

if not feat.hasGeometry():
continue

point = feat.geometry().asPoint()
x = point.x()
y = point.y()
@@ -194,9 +234,16 @@ def ovals(self, writer, features, width, height, rotation, segments):

ft.setGeometry(QgsGeometry.fromPolygon(polygon))
ft.setAttributes(feat.attributes())
writer.addFeature(ft, QgsFeatureSink.FastInsert)
sink.addFeature(ft, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))
else:
for current, feat in enumerate(features):
if feedback.isCanceled():
break

if not feat.hasGeometry():
continue

point = feat.geometry().asPoint()
x = point.x()
y = point.y()
@@ -207,4 +254,5 @@ def ovals(self, writer, features, width, height, rotation, segments):

ft.setGeometry(QgsGeometry.fromPolygon(polygon))
ft.setAttributes(feat.attributes())
writer.addFeature(ft, QgsFeatureSink.FastInsert)
sink.addFeature(ft, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))

0 comments on commit 076fdc9

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