@@ -71,36 +71,36 @@ void QgsShortestLineAlgorithm::initAlgorithm( const QVariantMap & )
71
71
{
72
72
addParameter ( new QgsProcessingParameterFeatureSource ( QStringLiteral ( " SOURCE" ), QObject::tr ( " Source layer" ), QList<int >() << QgsProcessing::TypeVectorAnyGeometry ) );
73
73
addParameter ( new QgsProcessingParameterFeatureSource ( QStringLiteral ( " DESTINATION" ), QObject::tr ( " Destination layer" ), QList<int >() << QgsProcessing::TypeVectorAnyGeometry ) );
74
- addParameter ( new QgsProcessingParameterNumber ( QStringLiteral ( " NEIGHBORS" ), QObject::tr ( " Maximum number of neighbors" ), QgsProcessingParameterNumber::Integer, 1 , false , 1 ) );
75
- addParameter ( new QgsProcessingParameterDistance ( QStringLiteral ( " DISTANCE" ), QObject::tr ( " Maximum distance" ), QVariant (), QString (" SOURCE" ), true ) );
76
- addParameter ( new QgsProcessingParameterFeatureSink ( QStringLiteral ( " OUTPUT" ), QObject::tr (" Shortest lines" ) ) );
74
+ addParameter ( new QgsProcessingParameterNumber ( QStringLiteral ( " NEIGHBORS" ), QObject::tr ( " Maximum number of neighbors" ), QgsProcessingParameterNumber::Integer, 1 , false , 1 ) );
75
+ addParameter ( new QgsProcessingParameterDistance ( QStringLiteral ( " DISTANCE" ), QObject::tr ( " Maximum distance" ), QVariant (), QString ( " SOURCE" ), true ) );
76
+ addParameter ( new QgsProcessingParameterFeatureSink ( QStringLiteral ( " OUTPUT" ), QObject::tr ( " Shortest lines" ) ) );
77
77
}
78
78
79
79
bool QgsShortestLineAlgorithm::prepareAlgorithm ( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * )
80
80
{
81
- mSource .reset ( parameterAsSource ( parameters, QStringLiteral ( " SOURCE" ), context) );
81
+ mSource .reset ( parameterAsSource ( parameters, QStringLiteral ( " SOURCE" ), context ) );
82
82
if ( !mSource )
83
83
throw QgsProcessingException ( invalidSourceError ( parameters, QStringLiteral ( " SOURCE" ) ) );
84
84
85
- mDestination .reset ( parameterAsSource ( parameters, QStringLiteral ( " DESTINATION" ), context) );
85
+ mDestination .reset ( parameterAsSource ( parameters, QStringLiteral ( " DESTINATION" ), context ) );
86
86
if ( !mSource )
87
87
throw QgsProcessingException ( invalidSourceError ( parameters, QStringLiteral ( " DESTINATION" ) ) );
88
88
89
89
mKNeighbors = parameterAsInt ( parameters, QStringLiteral ( " NEIGHBORS" ), context );
90
90
91
91
double paramMaxDist = parameterAsDouble ( parameters, QStringLiteral ( " DISTANCE" ), context ); // defaults to zero if not set
92
- if ( paramMaxDist)
92
+ if ( paramMaxDist )
93
93
mMaxDistance = paramMaxDist;
94
94
95
95
return true ;
96
96
}
97
97
98
98
QVariantMap QgsShortestLineAlgorithm::processAlgorithm ( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
99
99
{
100
- if ( mKNeighbors > mDestination ->featureCount ())
100
+ if ( mKNeighbors > mDestination ->featureCount () )
101
101
mKNeighbors = mDestination ->featureCount ();
102
102
103
- QgsFields fields = QgsProcessingUtils::combineFields ( mSource ->fields (), mDestination ->fields ());
103
+ QgsFields fields = QgsProcessingUtils::combineFields ( mSource ->fields (), mDestination ->fields () );
104
104
fields.append ( QgsField ( QStringLiteral ( " distance" ), QVariant::Double ) );
105
105
106
106
QString dest;
@@ -111,46 +111,46 @@ QVariantMap QgsShortestLineAlgorithm::processAlgorithm( const QVariantMap ¶m
111
111
QgsFeatureRequest request = QgsFeatureRequest ();
112
112
request.setDestinationCrs ( mSource ->sourceCrs (), context.transformContext () );
113
113
114
- QgsSpatialIndex idx = QgsSpatialIndex ( mDestination ->getFeatures (request), nullptr , QgsSpatialIndex::FlagStoreFeatureGeometries );
114
+ QgsSpatialIndex idx = QgsSpatialIndex ( mDestination ->getFeatures ( request ), nullptr , QgsSpatialIndex::FlagStoreFeatureGeometries );
115
115
116
116
QgsFeatureIterator sourceIterator = mSource ->getFeatures ();
117
117
double step = mSource ->featureCount () > 0 ? 100.0 / mSource ->featureCount () : 1 ;
118
118
119
119
QgsDistanceArea da = QgsDistanceArea ();
120
- da.setSourceCrs (mSource ->sourceCrs (), context.transformContext ());
121
- da.setEllipsoid (context.ellipsoid ());
120
+ da.setSourceCrs ( mSource ->sourceCrs (), context.transformContext () );
121
+ da.setEllipsoid ( context.ellipsoid () );
122
122
123
123
int i = 0 ;
124
124
QgsFeature sourceFeature;
125
125
while ( sourceIterator.nextFeature ( sourceFeature ) )
126
126
{
127
- if ( feedback->isCanceled ())
127
+ if ( feedback->isCanceled () )
128
128
break ;
129
129
130
130
131
131
QgsGeometry sourceGeom = sourceFeature.geometry ();
132
- QgsFeatureIds nearestIds = idx.nearestNeighbor (sourceGeom, mKNeighbors , mMaxDistance ).toSet ();
132
+ QgsFeatureIds nearestIds = idx.nearestNeighbor ( sourceGeom, mKNeighbors , mMaxDistance ).toSet ();
133
133
134
134
QgsFeatureRequest targetRequest = QgsFeatureRequest ();
135
135
targetRequest.setFilterFids ( nearestIds );
136
136
137
- QgsFeatureIterator destinationIterator = mDestination ->getFeatures (targetRequest);
137
+ QgsFeatureIterator destinationIterator = mDestination ->getFeatures ( targetRequest );
138
138
QgsFeature destinationFeature;
139
- while ( destinationIterator.nextFeature (destinationFeature) )
139
+ while ( destinationIterator.nextFeature ( destinationFeature ) )
140
140
{
141
141
QgsGeometry destinationGeom = destinationFeature.geometry ();
142
142
143
- QgsGeometry shortestLine = sourceGeom.shortestLine (destinationGeom);
144
- double dist = da.measureLength (shortestLine);
143
+ QgsGeometry shortestLine = sourceGeom.shortestLine ( destinationGeom );
144
+ double dist = da.measureLength ( shortestLine );
145
145
146
146
QgsFeature f;
147
147
148
148
QgsAttributes attrs = sourceFeature.attributes ();
149
149
attrs << destinationFeature.attributes () << dist;
150
150
151
- f.setAttributes (attrs);
152
- f.setGeometry (shortestLine);
153
- sink->addFeature (f, QgsFeatureSink::FastInsert);
151
+ f.setAttributes ( attrs );
152
+ f.setGeometry ( shortestLine );
153
+ sink->addFeature ( f, QgsFeatureSink::FastInsert );
154
154
}
155
155
156
156
i++;
0 commit comments