|
| 1 | +/*************************************************************************** |
| 2 | + qgsalgorithmlinesubstring.cpp |
| 3 | + --------------------- |
| 4 | + begin : August 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 "qgsalgorithmlinesubstring.h" |
| 19 | +#include "qgsgeometrycollection.h" |
| 20 | +#include "qgscurve.h" |
| 21 | + |
| 22 | +///@cond PRIVATE |
| 23 | + |
| 24 | +QString QgsLineSubstringAlgorithm::name() const |
| 25 | +{ |
| 26 | + return QStringLiteral( "linesubstring" ); |
| 27 | +} |
| 28 | + |
| 29 | +QString QgsLineSubstringAlgorithm::displayName() const |
| 30 | +{ |
| 31 | + return QObject::tr( "Line substring" ); |
| 32 | +} |
| 33 | + |
| 34 | +QStringList QgsLineSubstringAlgorithm::tags() const |
| 35 | +{ |
| 36 | + return QObject::tr( "linestring,curve,split,portion,reference,referencing,distance,interpolate" ).split( ',' ); |
| 37 | +} |
| 38 | + |
| 39 | +QString QgsLineSubstringAlgorithm::group() const |
| 40 | +{ |
| 41 | + return QObject::tr( "Vector geometry" ); |
| 42 | +} |
| 43 | + |
| 44 | +QString QgsLineSubstringAlgorithm::groupId() const |
| 45 | +{ |
| 46 | + return QStringLiteral( "vectorgeometry" ); |
| 47 | +} |
| 48 | + |
| 49 | +QString QgsLineSubstringAlgorithm::outputName() const |
| 50 | +{ |
| 51 | + return QObject::tr( "Substring" ); |
| 52 | +} |
| 53 | + |
| 54 | +QString QgsLineSubstringAlgorithm::shortHelpString() const |
| 55 | +{ |
| 56 | + return QObject::tr( "This algorithm returns the portion of a line (or curve) which falls " |
| 57 | + "between the specified start and end distances (measured from the " |
| 58 | + "beginning of the line).\n\n" |
| 59 | + "Z and M values are linearly interpolated from existing values." ); |
| 60 | +} |
| 61 | + |
| 62 | +QString QgsLineSubstringAlgorithm::shortDescription() const |
| 63 | +{ |
| 64 | + return QObject::tr( "Returns the substring of lines which fall between start and end distances." ); |
| 65 | +} |
| 66 | + |
| 67 | +QList<int> QgsLineSubstringAlgorithm::inputLayerTypes() const |
| 68 | +{ |
| 69 | + return QList<int>() << QgsProcessing::TypeVectorLine; |
| 70 | +} |
| 71 | + |
| 72 | +QgsProcessing::SourceType QgsLineSubstringAlgorithm::outputLayerType() const |
| 73 | +{ |
| 74 | + return QgsProcessing::TypeVectorLine; |
| 75 | +} |
| 76 | + |
| 77 | +QgsLineSubstringAlgorithm *QgsLineSubstringAlgorithm::createInstance() const |
| 78 | +{ |
| 79 | + return new QgsLineSubstringAlgorithm(); |
| 80 | +} |
| 81 | + |
| 82 | +void QgsLineSubstringAlgorithm::initParameters( const QVariantMap & ) |
| 83 | +{ |
| 84 | + std::unique_ptr< QgsProcessingParameterDistance> startDistance = qgis::make_unique< QgsProcessingParameterDistance >( QStringLiteral( "START_DISTANCE" ), |
| 85 | + QObject::tr( "Start distance" ), 0.0, QStringLiteral( "INPUT" ), false, 0 ); |
| 86 | + startDistance->setIsDynamic( true ); |
| 87 | + startDistance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Start Distance" ), QObject::tr( "Start distance" ), QgsPropertyDefinition::DoublePositive ) ); |
| 88 | + startDistance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) ); |
| 89 | + addParameter( startDistance.release() ); |
| 90 | + |
| 91 | + std::unique_ptr< QgsProcessingParameterDistance> endDistance = qgis::make_unique< QgsProcessingParameterDistance >( QStringLiteral( "END_DISTANCE" ), |
| 92 | + QObject::tr( "End distance" ), 1.0, QStringLiteral( "INPUT" ), false, 0 ); |
| 93 | + endDistance->setIsDynamic( true ); |
| 94 | + endDistance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "End Distance" ), QObject::tr( "End distance" ), QgsPropertyDefinition::DoublePositive ) ); |
| 95 | + endDistance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) ); |
| 96 | + addParameter( endDistance.release() ); |
| 97 | +} |
| 98 | + |
| 99 | +QgsProcessingFeatureSource::Flag QgsLineSubstringAlgorithm::sourceFlags() const |
| 100 | +{ |
| 101 | + // skip geometry checks - this algorithm doesn't care about invalid geometries |
| 102 | + return QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks; |
| 103 | +} |
| 104 | + |
| 105 | +bool QgsLineSubstringAlgorithm::prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * ) |
| 106 | +{ |
| 107 | + mStartDistance = parameterAsDouble( parameters, QStringLiteral( "START_DISTANCE" ), context ); |
| 108 | + mDynamicStartDistance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "START_DISTANCE" ) ); |
| 109 | + if ( mDynamicStartDistance ) |
| 110 | + mStartDistanceProperty = parameters.value( QStringLiteral( "START_DISTANCE" ) ).value< QgsProperty >(); |
| 111 | + |
| 112 | + mEndDistance = parameterAsDouble( parameters, QStringLiteral( "END_DISTANCE" ), context ); |
| 113 | + mDynamicEndDistance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "END_DISTANCE" ) ); |
| 114 | + if ( mDynamicEndDistance ) |
| 115 | + mEndDistanceProperty = parameters.value( QStringLiteral( "END_DISTANCE" ) ).value< QgsProperty >(); |
| 116 | + |
| 117 | + return true; |
| 118 | +} |
| 119 | + |
| 120 | +QgsFeatureList QgsLineSubstringAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * ) |
| 121 | +{ |
| 122 | + QgsFeature f = feature; |
| 123 | + if ( f.hasGeometry() ) |
| 124 | + { |
| 125 | + const QgsGeometry geometry = f.geometry(); |
| 126 | + double startDistance = mStartDistance; |
| 127 | + if ( mDynamicStartDistance ) |
| 128 | + startDistance = mStartDistanceProperty.valueAsDouble( context.expressionContext(), startDistance ); |
| 129 | + |
| 130 | + double endDistance = mEndDistance; |
| 131 | + if ( mDynamicEndDistance ) |
| 132 | + endDistance = mEndDistanceProperty.valueAsDouble( context.expressionContext(), endDistance ); |
| 133 | + |
| 134 | + const QgsCurve *curve = nullptr; |
| 135 | + if ( !geometry.isMultipart() ) |
| 136 | + curve = qgsgeometry_cast< const QgsCurve * >( geometry.constGet() ); |
| 137 | + else |
| 138 | + { |
| 139 | + if ( const QgsGeometryCollection *collection = qgsgeometry_cast< const QgsGeometryCollection * >( geometry.constGet() ) ) |
| 140 | + { |
| 141 | + if ( collection->numGeometries() > 0 ) |
| 142 | + { |
| 143 | + curve = qgsgeometry_cast< const QgsCurve * >( collection->geometryN( 0 ) ); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + if ( curve ) |
| 148 | + { |
| 149 | + std::unique_ptr< QgsCurve > substring( curve->curveSubstring( startDistance, endDistance ) ); |
| 150 | + QgsGeometry result( std::move( substring ) ); |
| 151 | + f.setGeometry( result ); |
| 152 | + } |
| 153 | + else |
| 154 | + { |
| 155 | + f.clearGeometry(); |
| 156 | + } |
| 157 | + } |
| 158 | + return QgsFeatureList() << f; |
| 159 | +} |
| 160 | + |
| 161 | +///@endcond |
| 162 | + |
| 163 | + |
0 commit comments