Skip to content

Commit

Permalink
[processing] Port transform alg to c++
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 14, 2017
1 parent 501081f commit f5f0a29
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/core/processing/qgsnativealgorithms.cpp
Expand Up @@ -62,6 +62,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsBufferAlgorithm() );
addAlgorithm( new QgsDissolveAlgorithm() );
addAlgorithm( new QgsClipAlgorithm() );
addAlgorithm( new QgsTransformAlgorithm() );
}


Expand Down Expand Up @@ -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

23 changes: 23 additions & 0 deletions src/core/processing/qgsnativealgorithms.h
Expand Up @@ -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.
*/
Expand Down

0 comments on commit f5f0a29

Please sign in to comment.