|
| 1 | +/*************************************************************************** |
| 2 | + qgsalgorithmextendlines.cpp |
| 3 | + --------------------- |
| 4 | + begin : July 2018 |
| 5 | + copyright : (C) 2018 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 "qgsalgorithmextendlines.h" |
| 19 | + |
| 20 | +///@cond PRIVATE |
| 21 | + |
| 22 | +QString QgsExtendLinesAlgorithm::name() const |
| 23 | +{ |
| 24 | + return QStringLiteral( "extendlines" ); |
| 25 | +} |
| 26 | + |
| 27 | +QString QgsExtendLinesAlgorithm::displayName() const |
| 28 | +{ |
| 29 | + return QObject::tr( "Extend lines" ); |
| 30 | +} |
| 31 | + |
| 32 | +QStringList QgsExtendLinesAlgorithm::tags() const |
| 33 | +{ |
| 34 | + return QObject::tr( "linestring,continue,grow,extrapolate" ).split( ',' ); |
| 35 | +} |
| 36 | + |
| 37 | +QString QgsExtendLinesAlgorithm::group() const |
| 38 | +{ |
| 39 | + return QObject::tr( "Vector geometry" ); |
| 40 | +} |
| 41 | + |
| 42 | +QString QgsExtendLinesAlgorithm::groupId() const |
| 43 | +{ |
| 44 | + return QStringLiteral( "vectorgeometry" ); |
| 45 | +} |
| 46 | + |
| 47 | +QString QgsExtendLinesAlgorithm::outputName() const |
| 48 | +{ |
| 49 | + return QObject::tr( "Extended" ); |
| 50 | +} |
| 51 | + |
| 52 | +QString QgsExtendLinesAlgorithm::shortHelpString() const |
| 53 | +{ |
| 54 | + return QObject::tr( "This algorithm extends line geometries by a specified amount at the start and end " |
| 55 | + "of the line. Lines are extended using the bearing of the first and last segment " |
| 56 | + "in the line." ); |
| 57 | +} |
| 58 | + |
| 59 | +QString QgsExtendLinesAlgorithm::shortDescription() const |
| 60 | +{ |
| 61 | + return QObject::tr( "Extends LineString geometries by extrapolating the start and end segments." ); |
| 62 | +} |
| 63 | + |
| 64 | +QList<int> QgsExtendLinesAlgorithm::inputLayerTypes() const |
| 65 | +{ |
| 66 | + return QList<int>() << QgsProcessing::TypeVectorLine; |
| 67 | +} |
| 68 | + |
| 69 | +QgsProcessing::SourceType QgsExtendLinesAlgorithm::outputLayerType() const |
| 70 | +{ |
| 71 | + return QgsProcessing::TypeVectorLine; |
| 72 | +} |
| 73 | + |
| 74 | +QgsExtendLinesAlgorithm *QgsExtendLinesAlgorithm::createInstance() const |
| 75 | +{ |
| 76 | + return new QgsExtendLinesAlgorithm(); |
| 77 | +} |
| 78 | + |
| 79 | +void QgsExtendLinesAlgorithm::initParameters( const QVariantMap & ) |
| 80 | +{ |
| 81 | + std::unique_ptr< QgsProcessingParameterDistance> startDistance = qgis::make_unique< QgsProcessingParameterDistance >( QStringLiteral( "START_DISTANCE" ), |
| 82 | + QObject::tr( "Start distance" ), 0.0, QStringLiteral( "INPUT" ), false, 0 ); |
| 83 | + startDistance->setIsDynamic( true ); |
| 84 | + startDistance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Start Distance" ), QObject::tr( "Start distance" ), QgsPropertyDefinition::DoublePositive ) ); |
| 85 | + startDistance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) ); |
| 86 | + addParameter( startDistance.release() ); |
| 87 | + |
| 88 | + std::unique_ptr< QgsProcessingParameterDistance> endDistance = qgis::make_unique< QgsProcessingParameterDistance >( QStringLiteral( "END_DISTANCE" ), |
| 89 | + QObject::tr( "End distance" ), 0.0, QStringLiteral( "INPUT" ), false, 0 ); |
| 90 | + endDistance->setIsDynamic( true ); |
| 91 | + endDistance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "End Distance" ), QObject::tr( "End distance" ), QgsPropertyDefinition::DoublePositive ) ); |
| 92 | + endDistance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) ); |
| 93 | + addParameter( endDistance.release() ); |
| 94 | +} |
| 95 | + |
| 96 | +QgsProcessingFeatureSource::Flag QgsExtendLinesAlgorithm::sourceFlags() const |
| 97 | +{ |
| 98 | + // skip geometry checks - this algorithm doesn't care about invalid geometries |
| 99 | + return QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks; |
| 100 | +} |
| 101 | + |
| 102 | +bool QgsExtendLinesAlgorithm::prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * ) |
| 103 | +{ |
| 104 | + mStartDistance = parameterAsDouble( parameters, QStringLiteral( "START_DISTANCE" ), context ); |
| 105 | + mDynamicStartDistance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "START_DISTANCE" ) ); |
| 106 | + if ( mDynamicStartDistance ) |
| 107 | + mStartDistanceProperty = parameters.value( QStringLiteral( "START_DISTANCE" ) ).value< QgsProperty >(); |
| 108 | + |
| 109 | + mEndDistance = parameterAsDouble( parameters, QStringLiteral( "END_DISTANCE" ), context ); |
| 110 | + mDynamicEndDistance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "END_DISTANCE" ) ); |
| 111 | + if ( mDynamicEndDistance ) |
| 112 | + mEndDistanceProperty = parameters.value( QStringLiteral( "END_DISTANCE" ) ).value< QgsProperty >(); |
| 113 | + |
| 114 | + return true; |
| 115 | +} |
| 116 | + |
| 117 | +QgsFeatureList QgsExtendLinesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * ) |
| 118 | +{ |
| 119 | + QgsFeature f = feature; |
| 120 | + if ( f.hasGeometry() ) |
| 121 | + { |
| 122 | + const QgsGeometry geometry = f.geometry(); |
| 123 | + double startDistance = mStartDistance; |
| 124 | + if ( mDynamicStartDistance ) |
| 125 | + startDistance = mStartDistanceProperty.valueAsDouble( context.expressionContext(), startDistance ); |
| 126 | + |
| 127 | + double endDistance = mEndDistance; |
| 128 | + if ( mDynamicEndDistance ) |
| 129 | + endDistance = mEndDistanceProperty.valueAsDouble( context.expressionContext(), endDistance ); |
| 130 | + |
| 131 | + const QgsGeometry outGeometry = geometry.extendLine( startDistance, endDistance ); |
| 132 | + if ( !outGeometry ) |
| 133 | + throw QgsProcessingException( QObject::tr( "Error calculating extended line" ) ); // don't think this can actually happen! |
| 134 | + |
| 135 | + f.setGeometry( outGeometry ); |
| 136 | + } |
| 137 | + return QgsFeatureList() << f; |
| 138 | +} |
| 139 | + |
| 140 | +///@endcond |
| 141 | + |
| 142 | + |
0 commit comments