Skip to content

Commit f5f0a29

Browse files
committedJun 14, 2017
[processing] Port transform alg to c++
1 parent 501081f commit f5f0a29

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed
 

‎src/core/processing/qgsnativealgorithms.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
6262
addAlgorithm( new QgsBufferAlgorithm() );
6363
addAlgorithm( new QgsDissolveAlgorithm() );
6464
addAlgorithm( new QgsClipAlgorithm() );
65+
addAlgorithm( new QgsTransformAlgorithm() );
6566
}
6667

6768

@@ -534,5 +535,64 @@ QVariantMap QgsClipAlgorithm::processAlgorithm( const QVariantMap &parameters, Q
534535
}
535536

536537

538+
QgsTransformAlgorithm::QgsTransformAlgorithm()
539+
{
540+
addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
541+
addParameter( new QgsProcessingParameterCrs( QStringLiteral( "TARGET_CRS" ), QObject::tr( "Target CRS" ), QStringLiteral( "EPSG:4326" ) ) );
542+
addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Reprojected" ) ) );
543+
addOutput( new QgsProcessingOutputVectorLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Reprojected" ) ) );
544+
}
545+
546+
QString QgsTransformAlgorithm::shortHelpString() const
547+
{
548+
return QObject::tr( "This algorithm reprojects a vector layer. It creates a new layer with the same features "
549+
"as the input one, but with geometries reprojected to a new CRS.\n\n"
550+
"Attributes are not modified by this algorithm." );
551+
}
552+
553+
QVariantMap QgsTransformAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const
554+
{
555+
std::unique_ptr< QgsFeatureSource > source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
556+
if ( !source )
557+
return QVariantMap();
558+
559+
QgsCoordinateReferenceSystem targetCrs = parameterAsCrs( parameters, QStringLiteral( "TARGET_CRS" ), context );
560+
561+
QString dest;
562+
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, source->fields(), source->wkbType(), targetCrs, dest ) );
563+
if ( !sink )
564+
return QVariantMap();
565+
566+
long count = source->featureCount();
567+
if ( count <= 0 )
568+
return QVariantMap();
569+
570+
QgsFeature f;
571+
QgsFeatureRequest req;
572+
// perform reprojection in the iterators...
573+
req.setDestinationCrs( targetCrs );
574+
575+
QgsFeatureIterator it = source->getFeatures( req );
576+
577+
double step = 100.0 / count;
578+
int current = 0;
579+
while ( it.nextFeature( f ) )
580+
{
581+
if ( feedback->isCanceled() )
582+
{
583+
break;
584+
}
585+
586+
sink->addFeature( f );
587+
feedback->setProgress( current * step );
588+
current++;
589+
}
590+
591+
QVariantMap outputs;
592+
outputs.insert( QStringLiteral( "OUTPUT" ), dest );
593+
return outputs;
594+
}
595+
596+
537597
///@endcond
538598

‎src/core/processing/qgsnativealgorithms.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,29 @@ class QgsCentroidAlgorithm : public QgsProcessingAlgorithm
6666

6767
};
6868

69+
/**
70+
* Native transform algorithm.
71+
*/
72+
class QgsTransformAlgorithm : public QgsProcessingAlgorithm
73+
{
74+
75+
public:
76+
77+
QgsTransformAlgorithm();
78+
79+
QString name() const override { return QStringLiteral( "reprojectlayer" ); }
80+
QString displayName() const override { return QObject::tr( "Reproject layer" ); }
81+
virtual QStringList tags() const override { return QObject::tr( "transform,reproject,crs,srs,warp" ).split( ',' ); }
82+
QString group() const override { return QObject::tr( "Vector general tools" ); }
83+
QString shortHelpString() const override;
84+
85+
protected:
86+
87+
virtual QVariantMap processAlgorithm( const QVariantMap &parameters,
88+
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const override;
89+
90+
};
91+
6992
/**
7093
* Native buffer algorithm.
7194
*/

0 commit comments

Comments
 (0)
Please sign in to comment.