Skip to content
Permalink
Browse files

[processing] Port transform alg to c++

  • Loading branch information
nyalldawson committed Jun 14, 2017
1 parent 501081f commit f5f0a299c25a394fbb5962288923a9367631d653
Showing with 83 additions and 0 deletions.
  1. +60 −0 src/core/processing/qgsnativealgorithms.cpp
  2. +23 −0 src/core/processing/qgsnativealgorithms.h
@@ -62,6 +62,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsBufferAlgorithm() );
addAlgorithm( new QgsDissolveAlgorithm() );
addAlgorithm( new QgsClipAlgorithm() );
addAlgorithm( new QgsTransformAlgorithm() );
}


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


QgsTransformAlgorithm::QgsTransformAlgorithm()
{
addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
addParameter( new QgsProcessingParameterCrs( QStringLiteral( "TARGET_CRS" ), QObject::tr( "Target CRS" ), QStringLiteral( "EPSG:4326" ) ) );
addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Reprojected" ) ) );
addOutput( new QgsProcessingOutputVectorLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Reprojected" ) ) );
}

QString QgsTransformAlgorithm::shortHelpString() const
{
return QObject::tr( "This algorithm reprojects a vector layer. It creates a new layer with the same features "
"as the input one, but with geometries reprojected to a new CRS.\n\n"
"Attributes are not modified by this algorithm." );
}

QVariantMap QgsTransformAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const
{
std::unique_ptr< QgsFeatureSource > source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
if ( !source )
return QVariantMap();

QgsCoordinateReferenceSystem targetCrs = parameterAsCrs( parameters, QStringLiteral( "TARGET_CRS" ), context );

QString dest;
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, source->fields(), source->wkbType(), targetCrs, dest ) );
if ( !sink )
return QVariantMap();

long count = source->featureCount();
if ( count <= 0 )
return QVariantMap();

QgsFeature f;
QgsFeatureRequest req;
// perform reprojection in the iterators...
req.setDestinationCrs( targetCrs );

QgsFeatureIterator it = source->getFeatures( req );

double step = 100.0 / count;
int current = 0;
while ( it.nextFeature( f ) )
{
if ( feedback->isCanceled() )
{
break;
}

sink->addFeature( f );
feedback->setProgress( current * step );
current++;
}

QVariantMap outputs;
outputs.insert( QStringLiteral( "OUTPUT" ), dest );
return outputs;
}


///@endcond

@@ -66,6 +66,29 @@ class QgsCentroidAlgorithm : public QgsProcessingAlgorithm

};

/**
* Native transform algorithm.
*/
class QgsTransformAlgorithm : public QgsProcessingAlgorithm
{

public:

QgsTransformAlgorithm();

QString name() const override { return QStringLiteral( "reprojectlayer" ); }
QString displayName() const override { return QObject::tr( "Reproject layer" ); }
virtual QStringList tags() const override { return QObject::tr( "transform,reproject,crs,srs,warp" ).split( ',' ); }
QString group() const override { return QObject::tr( "Vector general tools" ); }
QString shortHelpString() const override;

protected:

virtual QVariantMap processAlgorithm( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const override;

};

/**
* Native buffer algorithm.
*/

0 comments on commit f5f0a29

Please sign in to comment.
You can’t perform that action at this time.