@@ -62,6 +62,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
62
62
addAlgorithm ( new QgsBufferAlgorithm () );
63
63
addAlgorithm ( new QgsDissolveAlgorithm () );
64
64
addAlgorithm ( new QgsClipAlgorithm () );
65
+ addAlgorithm ( new QgsTransformAlgorithm () );
65
66
}
66
67
67
68
@@ -534,5 +535,64 @@ QVariantMap QgsClipAlgorithm::processAlgorithm( const QVariantMap ¶meters, Q
534
535
}
535
536
536
537
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 ¶meters, 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
+
537
597
// /@endcond
538
598
0 commit comments