mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-28 00:05:04 -04:00
Algorithms and other processing code should use this method (instead of dataobjects.getLayerFromString) to retrieve layers from a string, as it considers the processing context and allows resolving strings to temporarily stored layers. This permits processing models to function correctly when intermediate results are stored as memory layers. Subsequent model algorithms can then access these temporary layers as inputs. All temporary layers will be removed when the context object is destroyed after the model algorithm is run.
288 lines
12 KiB
Python
288 lines
12 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
***************************************************************************
|
|
RectanglesOvalsDiamondsVariable.py
|
|
---------------------
|
|
Date : April 2016
|
|
Copyright : (C) 2016 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. *
|
|
* *
|
|
***************************************************************************
|
|
"""
|
|
from builtins import range
|
|
|
|
__author__ = 'Alexander Bruy'
|
|
__date__ = 'August 2012'
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive323
|
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
import math
|
|
|
|
from qgis.core import (QgsApplication,
|
|
QgsWkbTypes,
|
|
QgsFeature,
|
|
QgsGeometry,
|
|
QgsPoint,
|
|
QgsMessageLog,
|
|
QgsProcessingUtils)
|
|
|
|
from processing.core.GeoAlgorithm import GeoAlgorithm
|
|
from processing.core.parameters import ParameterVector
|
|
from processing.core.parameters import ParameterSelection
|
|
from processing.core.parameters import ParameterTableField
|
|
from processing.core.parameters import ParameterNumber
|
|
from processing.core.outputs import OutputVector
|
|
from processing.tools import dataobjects, vector
|
|
|
|
|
|
class RectanglesOvalsDiamondsVariable(GeoAlgorithm):
|
|
|
|
INPUT_LAYER = 'INPUT_LAYER'
|
|
SHAPE = 'SHAPE'
|
|
WIDTH = 'WIDTH'
|
|
HEIGHT = 'HEIGHT'
|
|
ROTATION = 'ROTATION'
|
|
SEGMENTS = 'SEGMENTS'
|
|
OUTPUT_LAYER = 'OUTPUT_LAYER'
|
|
|
|
def icon(self):
|
|
return QgsApplication.getThemeIcon("/providerQgis.svg")
|
|
|
|
def svgIconPath(self):
|
|
return QgsApplication.iconPath("providerQgis.svg")
|
|
|
|
def group(self):
|
|
return self.tr('Vector geometry tools')
|
|
|
|
def name(self):
|
|
return 'rectanglesovalsdiamondsvariable'
|
|
|
|
def displayName(self):
|
|
return self.tr('Rectangles, ovals, diamonds (variable)')
|
|
|
|
def defineCharacteristics(self):
|
|
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(ParameterTableField(self.WIDTH,
|
|
self.tr('Width field'),
|
|
self.INPUT_LAYER,
|
|
ParameterTableField.DATA_TYPE_NUMBER))
|
|
self.addParameter(ParameterTableField(self.HEIGHT,
|
|
self.tr('Height field'),
|
|
self.INPUT_LAYER,
|
|
ParameterTableField.DATA_TYPE_NUMBER))
|
|
self.addParameter(ParameterTableField(self.ROTATION,
|
|
self.tr('Rotation field'),
|
|
self.INPUT_LAYER,
|
|
ParameterTableField.DATA_TYPE_NUMBER,
|
|
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]))
|
|
|
|
def processAlgorithm(self, context, feedback):
|
|
layer = dataobjects.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)
|
|
|
|
writer = self.getOutputFromName(
|
|
self.OUTPUT_LAYER).getVectorWriter(layer.fields().toList(), QgsWkbTypes.Polygon, layer.crs(), context)
|
|
|
|
features = QgsProcessingUtils.getFeatures(layer, context)
|
|
|
|
if shape == 0:
|
|
self.rectangles(writer, features, width, height, rotation)
|
|
elif shape == 1:
|
|
self.diamonds(writer, features, width, height, rotation)
|
|
else:
|
|
self.ovals(writer, features, width, height, rotation, segments)
|
|
|
|
del writer
|
|
|
|
def rectangles(self, writer, features, width, height, rotation):
|
|
ft = QgsFeature()
|
|
|
|
if rotation is not None:
|
|
for current, feat in enumerate(features):
|
|
w = feat[width]
|
|
h = feat[height]
|
|
angle = feat[rotation]
|
|
if not w or not h or not angle:
|
|
QgsMessageLog.logMessage(self.tr('Feature {} has empty '
|
|
'width, height or angle. '
|
|
'Skipping...'.format(feat.id())),
|
|
self.tr('Processing'), QgsMessageLog.WARNING)
|
|
continue
|
|
|
|
xOffset = w / 2.0
|
|
yOffset = h / 2.0
|
|
phi = angle * math.pi / 180
|
|
|
|
point = feat.geometry().asPoint()
|
|
x = point.x()
|
|
y = point.y()
|
|
points = [(-xOffset, -yOffset), (-xOffset, yOffset), (xOffset, yOffset), (xOffset, -yOffset)]
|
|
polygon = [[QgsPoint(i[0] * math.cos(phi) + i[1] * math.sin(phi) + x,
|
|
-i[0] * math.sin(phi) + i[1] * math.cos(phi) + y) for i in points]]
|
|
|
|
ft.setGeometry(QgsGeometry.fromPolygon(polygon))
|
|
ft.setAttributes(feat.attributes())
|
|
writer.addFeature(ft)
|
|
else:
|
|
for current, feat in enumerate(features):
|
|
w = feat[width]
|
|
h = feat[height]
|
|
if not w or not h:
|
|
QgsMessageLog.logMessage(self.tr('Feature {} has empty '
|
|
'width or height. '
|
|
'Skipping...'.format(feat.id())),
|
|
self.tr('Processing'), QgsMessageLog.WARNING)
|
|
continue
|
|
|
|
xOffset = w / 2.0
|
|
yOffset = h / 2.0
|
|
|
|
point = feat.geometry().asPoint()
|
|
x = point.x()
|
|
y = point.y()
|
|
points = [(-xOffset, -yOffset), (-xOffset, yOffset), (xOffset, yOffset), (xOffset, -yOffset)]
|
|
polygon = [[QgsPoint(i[0] + x, i[1] + y) for i in points]]
|
|
|
|
ft.setGeometry(QgsGeometry.fromPolygon(polygon))
|
|
ft.setAttributes(feat.attributes())
|
|
writer.addFeature(ft)
|
|
|
|
def diamonds(self, writer, features, width, height, rotation):
|
|
ft = QgsFeature()
|
|
|
|
if rotation is not None:
|
|
for current, feat in enumerate(features):
|
|
w = feat[width]
|
|
h = feat[height]
|
|
angle = feat[rotation]
|
|
if not w or not h or not angle:
|
|
QgsMessageLog.logMessage(self.tr('Feature {} has empty '
|
|
'width, height or angle. '
|
|
'Skipping...'.format(feat.id())),
|
|
self.tr('Processing'), QgsMessageLog.WARNING)
|
|
continue
|
|
|
|
xOffset = w / 2.0
|
|
yOffset = h / 2.0
|
|
phi = angle * math.pi / 180
|
|
|
|
point = feat.geometry().asPoint()
|
|
x = point.x()
|
|
y = point.y()
|
|
points = [(0.0, -yOffset), (-xOffset, 0.0), (0.0, yOffset), (xOffset, 0.0)]
|
|
polygon = [[QgsPoint(i[0] * math.cos(phi) + i[1] * math.sin(phi) + x,
|
|
-i[0] * math.sin(phi) + i[1] * math.cos(phi) + y) for i in points]]
|
|
|
|
ft.setGeometry(QgsGeometry.fromPolygon(polygon))
|
|
ft.setAttributes(feat.attributes())
|
|
writer.addFeature(ft)
|
|
else:
|
|
for current, feat in enumerate(features):
|
|
w = feat[width]
|
|
h = feat[height]
|
|
if not w or not h:
|
|
QgsMessageLog.logMessage(self.tr('Feature {} has empty '
|
|
'width or height. '
|
|
'Skipping...'.format(feat.id())),
|
|
self.tr('Processing'), QgsMessageLog.WARNING)
|
|
continue
|
|
|
|
xOffset = w / 2.0
|
|
yOffset = h / 2.0
|
|
|
|
point = feat.geometry().asPoint()
|
|
x = point.x()
|
|
y = point.y()
|
|
points = [(0.0, -yOffset), (-xOffset, 0.0), (0.0, yOffset), (xOffset, 0.0)]
|
|
polygon = [[QgsPoint(i[0] + x, i[1] + y) for i in points]]
|
|
|
|
ft.setGeometry(QgsGeometry.fromPolygon(polygon))
|
|
ft.setAttributes(feat.attributes())
|
|
writer.addFeature(ft)
|
|
|
|
def ovals(self, writer, features, width, height, rotation, segments):
|
|
ft = QgsFeature()
|
|
|
|
if rotation is not None:
|
|
for current, feat in enumerate(features):
|
|
w = feat[width]
|
|
h = feat[height]
|
|
angle = feat[rotation]
|
|
if not w or not h or not angle:
|
|
QgsMessageLog.logMessage(self.tr('Feature {} has empty '
|
|
'width, height or angle. '
|
|
'Skipping...'.format(feat.id())),
|
|
self.tr('Processing'), QgsMessageLog.WARNING)
|
|
continue
|
|
|
|
xOffset = w / 2.0
|
|
yOffset = h / 2.0
|
|
phi = angle * math.pi / 180
|
|
|
|
point = feat.geometry().asPoint()
|
|
x = point.x()
|
|
y = point.y()
|
|
points = []
|
|
for t in [(2 * math.pi) / segments * i for i in range(segments)]:
|
|
points.append((xOffset * math.cos(t), yOffset * math.sin(t)))
|
|
polygon = [[QgsPoint(i[0] * math.cos(phi) + i[1] * math.sin(phi) + x,
|
|
-i[0] * math.sin(phi) + i[1] * math.cos(phi) + y) for i in points]]
|
|
|
|
ft.setGeometry(QgsGeometry.fromPolygon(polygon))
|
|
ft.setAttributes(feat.attributes())
|
|
writer.addFeature(ft)
|
|
else:
|
|
for current, feat in enumerate(features):
|
|
w = feat[width]
|
|
h = feat[height]
|
|
if not w or not h:
|
|
QgsMessageLog.logMessage(self.tr('Feature {} has empty '
|
|
'width or height. '
|
|
'Skipping...'.format(feat.id())),
|
|
self.tr('Processing'), QgsMessageLog.WARNING)
|
|
continue
|
|
|
|
xOffset = w / 2.0
|
|
yOffset = h / 2.0
|
|
|
|
point = feat.geometry().asPoint()
|
|
x = point.x()
|
|
y = point.y()
|
|
points = []
|
|
for t in [(2 * math.pi) / segments * i for i in range(segments)]:
|
|
points.append((xOffset * math.cos(t), yOffset * math.sin(t)))
|
|
polygon = [[QgsPoint(i[0] + x, i[1] + y) for i in points]]
|
|
|
|
ft.setGeometry(QgsGeometry.fromPolygon(polygon))
|
|
ft.setAttributes(feat.attributes())
|
|
writer.addFeature(ft)
|