|
| 1 | +/*************************************************************************** |
| 2 | + qgsalgorithmprojectpointcartesian.cpp |
| 3 | + --------------------- |
| 4 | + begin : April 2017 |
| 5 | + copyright : (C) 2017 by Nyall Dawson |
| 6 | + email : nyall dot dawson 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 "qgsalgorithmprojectpointcartesian.h" |
| 19 | +#include "qgsmultipoint.h" |
| 20 | + |
| 21 | +///@cond PRIVATE |
| 22 | + |
| 23 | +QString QgsProjectPointCartesianAlgorithm::name() const |
| 24 | +{ |
| 25 | + return QStringLiteral( "projectpointcartesian" ); |
| 26 | +} |
| 27 | + |
| 28 | +QString QgsProjectPointCartesianAlgorithm::displayName() const |
| 29 | +{ |
| 30 | + return QObject::tr( "Project points (Cartesian)" ); |
| 31 | +} |
| 32 | + |
| 33 | +QStringList QgsProjectPointCartesianAlgorithm::tags() const |
| 34 | +{ |
| 35 | + return QObject::tr( "bearing,azimuth,distance,angle" ).split( ',' ); |
| 36 | +} |
| 37 | + |
| 38 | +QString QgsProjectPointCartesianAlgorithm::group() const |
| 39 | +{ |
| 40 | + return QObject::tr( "Vector geometry" ); |
| 41 | +} |
| 42 | + |
| 43 | +QString QgsProjectPointCartesianAlgorithm::groupId() const |
| 44 | +{ |
| 45 | + return QStringLiteral( "vectorgeometry" ); |
| 46 | +} |
| 47 | + |
| 48 | +QString QgsProjectPointCartesianAlgorithm::outputName() const |
| 49 | +{ |
| 50 | + return QObject::tr( "Projected" ); |
| 51 | +} |
| 52 | + |
| 53 | +QString QgsProjectPointCartesianAlgorithm::shortHelpString() const |
| 54 | +{ |
| 55 | + return QObject::tr( "This algorithm projects point geometries by a specified distance and bearing (azimuth), creating a new point layer with the projected points.\n\n" |
| 56 | + "The distance is specified in layer units, and the bearing in degrees clockwise from North." ); |
| 57 | +} |
| 58 | + |
| 59 | +QList<int> QgsProjectPointCartesianAlgorithm::inputLayerTypes() const |
| 60 | +{ |
| 61 | + return QList<int>() << QgsProcessing::TypeVectorPoint; |
| 62 | +} |
| 63 | + |
| 64 | +QgsProcessing::SourceType QgsProjectPointCartesianAlgorithm::outputLayerType() const |
| 65 | +{ |
| 66 | + return QgsProcessing::TypeVectorPoint; |
| 67 | +} |
| 68 | + |
| 69 | +QgsProjectPointCartesianAlgorithm *QgsProjectPointCartesianAlgorithm::createInstance() const |
| 70 | +{ |
| 71 | + return new QgsProjectPointCartesianAlgorithm(); |
| 72 | +} |
| 73 | + |
| 74 | +void QgsProjectPointCartesianAlgorithm::initParameters( const QVariantMap & ) |
| 75 | +{ |
| 76 | + std::unique_ptr< QgsProcessingParameterNumber > bearing = qgis::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "BEARING" ), QObject::tr( "Bearing (degrees from North)" ), QgsProcessingParameterNumber::Double, false ); |
| 77 | + bearing->setIsDynamic( true ); |
| 78 | + bearing->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Bearing" ), QObject::tr( "Bearing (degrees from North)" ), QgsPropertyDefinition::Double ) ); |
| 79 | + bearing->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) ); |
| 80 | + addParameter( bearing.release() ); |
| 81 | + |
| 82 | + std::unique_ptr< QgsProcessingParameterNumber > distance = qgis::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "DISTANCE" ), QObject::tr( "Distance" ), QgsProcessingParameterNumber::Double, false ); |
| 83 | + distance->setIsDynamic( true ); |
| 84 | + distance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Distance" ), QObject::tr( "Projection distance" ), QgsPropertyDefinition::Double ) ); |
| 85 | + distance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) ); |
| 86 | + addParameter( distance.release() ); |
| 87 | +} |
| 88 | + |
| 89 | +bool QgsProjectPointCartesianAlgorithm::prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * ) |
| 90 | +{ |
| 91 | + mBearing = parameterAsDouble( parameters, QStringLiteral( "BEARING" ), context ); |
| 92 | + mDynamicBearing = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "BEARING" ) ); |
| 93 | + if ( mDynamicBearing ) |
| 94 | + mBearingProperty = parameters.value( QStringLiteral( "BEARING" ) ).value< QgsProperty >(); |
| 95 | + |
| 96 | + mDistance = parameterAsDouble( parameters, QStringLiteral( "DISTANCE" ), context ); |
| 97 | + mDynamicDistance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DISTANCE" ) ); |
| 98 | + if ( mDynamicDistance ) |
| 99 | + mDistanceProperty = parameters.value( QStringLiteral( "DISTANCE" ) ).value< QgsProperty >(); |
| 100 | + |
| 101 | + return true; |
| 102 | +} |
| 103 | + |
| 104 | +QgsFeatureList QgsProjectPointCartesianAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * ) |
| 105 | +{ |
| 106 | + QgsFeature f = feature; |
| 107 | + if ( f.hasGeometry() && QgsWkbTypes::geometryType( f.geometry().wkbType() ) == QgsWkbTypes::PointGeometry ) |
| 108 | + { |
| 109 | + double distance = mDistance; |
| 110 | + if ( mDynamicDistance ) |
| 111 | + distance = mDistanceProperty.valueAsDouble( context.expressionContext(), distance ); |
| 112 | + double bearing = mBearing; |
| 113 | + if ( mDynamicBearing ) |
| 114 | + bearing = mBearingProperty.valueAsDouble( context.expressionContext(), bearing ); |
| 115 | + |
| 116 | + QgsGeometry g = f.geometry(); |
| 117 | + if ( QgsWkbTypes::isMultiType( g.wkbType() ) ) |
| 118 | + { |
| 119 | + const QgsMultiPoint *mp = static_cast< const QgsMultiPoint * >( g.constGet() ); |
| 120 | + std::unique_ptr< QgsMultiPoint > result = qgis::make_unique< QgsMultiPoint >(); |
| 121 | + for ( int i = 0; i < mp->numGeometries(); ++i ) |
| 122 | + { |
| 123 | + const QgsPoint *p = static_cast< const QgsPoint * >( mp->geometryN( i ) ); |
| 124 | + result->addGeometry( p->project( distance, bearing ).clone() ); |
| 125 | + } |
| 126 | + f.setGeometry( QgsGeometry( std::move( result ) ) ); |
| 127 | + } |
| 128 | + else |
| 129 | + { |
| 130 | + const QgsPoint *p = static_cast< const QgsPoint * >( g.constGet() ); |
| 131 | + QgsPoint result = p->project( distance, bearing ); |
| 132 | + f.setGeometry( QgsGeometry( result.clone() ) ); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return QgsFeatureList() << f; |
| 137 | +} |
| 138 | + |
| 139 | +///@endcond |
| 140 | + |
0 commit comments