|
| 1 | +/*************************************************************************** |
| 2 | + qgsalgorithmrectanglesovalsdiamonds.cpp |
| 3 | + --------------------- |
| 4 | + begin : January 2020 |
| 5 | + copyright : (C) 2020 by Alexander Bruy |
| 6 | + email : alexander dot bruy at gmail dot com |
| 7 | + ***************************************************************************/ |
| 8 | + |
| 9 | +/*************************************************************************** |
| 10 | + * * |
| 11 | + * This program is free software; you can redistribute it and/or modify * |
| 12 | + * it under the terms of the GNU General Public License as published by * |
| 13 | + * the Free Software Foundation; either version 2 of the License, or * |
| 14 | + * (at your option) any later version. * |
| 15 | + * * |
| 16 | + ***************************************************************************/ |
| 17 | + |
| 18 | +#include "qgsalgorithmrectanglesovalsdiamonds.h" |
| 19 | +#include "qgsapplication.h" |
| 20 | + |
| 21 | +///@cond PRIVATE |
| 22 | + |
| 23 | +QString QgsRectanglesOvalsDiamondsAlgorithm::name() const |
| 24 | +{ |
| 25 | + return QStringLiteral( "rectanglesovalsdiamonds" ); |
| 26 | +} |
| 27 | + |
| 28 | +QString QgsRectanglesOvalsDiamondsAlgorithm::displayName() const |
| 29 | +{ |
| 30 | + return QObject::tr( "Rectangles, ovals, diamonds" ); |
| 31 | +} |
| 32 | + |
| 33 | +QStringList QgsRectanglesOvalsDiamondsAlgorithm::tags() const |
| 34 | +{ |
| 35 | + return QObject::tr( "buffer,grow,fixed,variable,distance,rectangle,oval,diamond,point" ).split( ',' ); |
| 36 | +} |
| 37 | + |
| 38 | +QString QgsRectanglesOvalsDiamondsAlgorithm::group() const |
| 39 | +{ |
| 40 | + return QObject::tr( "Vector geometry" ); |
| 41 | +} |
| 42 | + |
| 43 | +QString QgsRectanglesOvalsDiamondsAlgorithm::groupId() const |
| 44 | +{ |
| 45 | + return QStringLiteral( "vectorgeometry" ); |
| 46 | +} |
| 47 | + |
| 48 | +QString QgsRectanglesOvalsDiamondsAlgorithm::shortHelpString() const |
| 49 | +{ |
| 50 | + return QObject::tr( "Creates a rectangle, oval or diamond-shaped polygons from the input point layer using " |
| 51 | + "specified width, height and (optional) rotation values." ); |
| 52 | +} |
| 53 | + |
| 54 | +QString QgsRectanglesOvalsDiamondsAlgorithm::outputName() const |
| 55 | +{ |
| 56 | + return QObject::tr( "Polygon" ); |
| 57 | +} |
| 58 | + |
| 59 | +QList<int> QgsRectanglesOvalsDiamondsAlgorithm::inputLayerTypes() const |
| 60 | +{ |
| 61 | + return QList<int>() << QgsProcessing::TypeVectorPoint; |
| 62 | +} |
| 63 | + |
| 64 | +QgsProcessing::SourceType QgsRectanglesOvalsDiamondsAlgorithm::outputLayerType() const |
| 65 | +{ |
| 66 | + return QgsProcessing::TypeVectorPolygon; |
| 67 | +} |
| 68 | + |
| 69 | +QgsWkbTypes::Type QgsRectanglesOvalsDiamondsAlgorithm::outputWkbType( QgsWkbTypes::Type inputWkbType ) const |
| 70 | +{ |
| 71 | + QgsWkbTypes::Type outputWkbType = QgsWkbTypes::Polygon; |
| 72 | + if ( QgsWkbTypes::hasM( inputWkbType ) ) |
| 73 | + { |
| 74 | + outputWkbType = QgsWkbTypes::addM( outputWkbType ); |
| 75 | + } |
| 76 | + if ( QgsWkbTypes::hasZ( inputWkbType ) ) |
| 77 | + { |
| 78 | + outputWkbType = QgsWkbTypes::addZ( outputWkbType ); |
| 79 | + } |
| 80 | + |
| 81 | + return outputWkbType; |
| 82 | +} |
| 83 | + |
| 84 | +QgsRectanglesOvalsDiamondsAlgorithm *QgsRectanglesOvalsDiamondsAlgorithm::createInstance() const |
| 85 | +{ |
| 86 | + return new QgsRectanglesOvalsDiamondsAlgorithm(); |
| 87 | +} |
| 88 | + |
| 89 | +void QgsRectanglesOvalsDiamondsAlgorithm::initParameters( const QVariantMap & ) |
| 90 | +{ |
| 91 | + addParameter( new QgsProcessingParameterEnum( QStringLiteral( "SHAPE" ), QObject::tr( "Shape" ), QStringList() << QObject::tr( "Rectangle" ) << QObject::tr( "Diamond" ) << QObject::tr( "Oval" ), false, 0 ) ); |
| 92 | + |
| 93 | + auto widthParam = qgis::make_unique < QgsProcessingParameterDistance >( QStringLiteral( "WIDTH" ), QObject::tr( "Width" ), 1.0, QStringLiteral( "INPUT" ), false, 0.0 ); |
| 94 | + widthParam->setIsDynamic( true ); |
| 95 | + widthParam->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Width" ), QObject::tr( "Width" ), QgsPropertyDefinition::Double ) ); |
| 96 | + widthParam->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) ); |
| 97 | + addParameter( widthParam.release() ); |
| 98 | + |
| 99 | + auto heightParam = qgis::make_unique < QgsProcessingParameterDistance >( QStringLiteral( "HEIGHT" ), QObject::tr( "Height" ), 1.0, QStringLiteral( "INPUT" ), false, 0.0 ); |
| 100 | + heightParam->setIsDynamic( true ); |
| 101 | + heightParam->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Height" ), QObject::tr( "Height" ), QgsPropertyDefinition::Double ) ); |
| 102 | + heightParam->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) ); |
| 103 | + addParameter( heightParam.release() ); |
| 104 | + |
| 105 | + auto rotationParam = qgis::make_unique < QgsProcessingParameterNumber >( QStringLiteral( "ROTATION" ), QObject::tr( "Rotation" ), QgsProcessingParameterNumber::Double, 0.0, true, 0.0, 360.0 ); |
| 106 | + rotationParam->setIsDynamic( true ); |
| 107 | + rotationParam->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Rotation" ), QObject::tr( "Rotation" ), QgsPropertyDefinition::Double ) ); |
| 108 | + rotationParam->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) ); |
| 109 | + addParameter( rotationParam.release() ); |
| 110 | + |
| 111 | + addParameter( new QgsProcessingParameterNumber( QStringLiteral( "SEGMENTS" ), QObject::tr( "Segments" ), QgsProcessingParameterNumber::Integer, 5, false, 1 ) ); |
| 112 | +} |
| 113 | + |
| 114 | +bool QgsRectanglesOvalsDiamondsAlgorithm::prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * ) |
| 115 | +{ |
| 116 | + mShape = parameterAsEnum( parameters, QStringLiteral( "SHAPE" ), context ); |
| 117 | + |
| 118 | + mWidth = parameterAsDouble( parameters, QStringLiteral( "WIDTH" ), context ); |
| 119 | + mDynamicWidth = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "WIDTH" ) ); |
| 120 | + if ( mDynamicWidth ) |
| 121 | + mWidthProperty = parameters.value( QStringLiteral( "WIDTH" ) ).value< QgsProperty >(); |
| 122 | + |
| 123 | + mHeight = parameterAsDouble( parameters, QStringLiteral( "HEIGHT" ), context ); |
| 124 | + mDynamicHeight = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "HEIGHT" ) ); |
| 125 | + if ( mDynamicHeight ) |
| 126 | + mHeightProperty = parameters.value( QStringLiteral( "HEIGHT" ) ).value< QgsProperty >(); |
| 127 | + |
| 128 | + mRotation = parameterAsDouble( parameters, QStringLiteral( "ROTATION" ), context ); |
| 129 | + mDynamicRotation = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "ROTATION" ) ); |
| 130 | + if ( mDynamicRotation ) |
| 131 | + mRotationProperty = parameters.value( QStringLiteral( "ROTATION" ) ).value< QgsProperty >(); |
| 132 | + |
| 133 | + mSegments = parameterAsDouble( parameters, QStringLiteral( "SEGMENTS" ), context ); |
| 134 | + |
| 135 | + return true; |
| 136 | +} |
| 137 | + |
| 138 | +QgsFeatureList QgsRectanglesOvalsDiamondsAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * ) |
| 139 | +{ |
| 140 | + QgsFeature outFeature = feature; |
| 141 | + if ( outFeature.hasGeometry() ) |
| 142 | + { |
| 143 | + double width = mWidth; |
| 144 | + if ( mDynamicWidth ) |
| 145 | + width = mWidthProperty.valueAsDouble( context.expressionContext(), width ); |
| 146 | + |
| 147 | + double height = mHeight; |
| 148 | + if ( mDynamicHeight ) |
| 149 | + height = mHeightProperty.valueAsDouble( context.expressionContext(), height ); |
| 150 | + |
| 151 | + double rotation = mRotation; |
| 152 | + if ( mDynamicRotation ) |
| 153 | + rotation = mRotationProperty.valueAsDouble( context.expressionContext(), rotation ); |
| 154 | + |
| 155 | + if ( width == 0 || height == 0 ) |
| 156 | + { |
| 157 | + throw QgsProcessingException( QObject::tr( "Width and height should be greater than 0." ) ); |
| 158 | + } |
| 159 | + |
| 160 | + double phi = rotation * M_PI / 180; |
| 161 | + double xOffset = width / 2.0; |
| 162 | + double yOffset = height / 2.0; |
| 163 | + QgsPointXY point = outFeature.geometry().asPoint(); |
| 164 | + double x = point.x(); |
| 165 | + double y = point.y(); |
| 166 | + |
| 167 | + QgsPolylineXY ring; |
| 168 | + switch ( mShape ) |
| 169 | + { |
| 170 | + case 0: |
| 171 | + // rectangle |
| 172 | + ring << QgsPointXY( -xOffset, -yOffset ) |
| 173 | + << QgsPointXY( -xOffset, yOffset ) |
| 174 | + << QgsPointXY( xOffset, yOffset ) |
| 175 | + << QgsPointXY( xOffset, -yOffset ); |
| 176 | + break; |
| 177 | + case 1: |
| 178 | + // diamond |
| 179 | + ring << QgsPointXY( 0.0, -yOffset ) |
| 180 | + << QgsPointXY( -xOffset, 0.0 ) |
| 181 | + << QgsPointXY( 0.0, yOffset ) |
| 182 | + << QgsPointXY( xOffset, 0.0 ); |
| 183 | + break; |
| 184 | + case 2: |
| 185 | + // oval |
| 186 | + for ( int i = 0; i < mSegments; i ++ ) |
| 187 | + { |
| 188 | + double t = ( 2 * M_PI ) / mSegments * i; |
| 189 | + ring << QgsPointXY( xOffset * cos( t ), yOffset * sin( t ) ); |
| 190 | + } |
| 191 | + break; |
| 192 | + } |
| 193 | + |
| 194 | + for ( int i = 0; i < ring.size(); ++i ) |
| 195 | + { |
| 196 | + QgsPointXY p = ring[ i ]; |
| 197 | + double px = p.x(); |
| 198 | + double py = p.y(); |
| 199 | + ring[ i ] = QgsPointXY( px * cos( phi ) + py * sin( phi ) + x, |
| 200 | + -px * sin( phi ) + py * cos( phi ) + y ); |
| 201 | + } |
| 202 | + |
| 203 | + outFeature.setGeometry( QgsGeometry::fromPolygonXY( QgsPolygonXY() << ring ) ); |
| 204 | + } |
| 205 | + |
| 206 | + return QgsFeatureList() << outFeature; |
| 207 | +} |
| 208 | + |
| 209 | +///@endcond |
0 commit comments