Skip to content

Commit

Permalink
Add prepareSource method
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso authored and nyalldawson committed Sep 14, 2018
1 parent c7ac4fe commit 0380c06
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
Expand Up @@ -984,6 +984,13 @@ checks based on the geometry type of the layer.

:return: true if the algorithm supports in-place editing

.. versionadded:: 3.4
%End

void prepareSource( const QVariantMap &parameters, QgsProcessingContext &context );
%Docstring
Read the source from ``parameters`` and ``context`` and set it

.. versionadded:: 3.4
%End

Expand Down
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmtransform.cpp
Expand Up @@ -79,6 +79,7 @@ QgsTransformAlgorithm *QgsTransformAlgorithm::createInstance() const

bool QgsTransformAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
{
prepareSource( parameters, context );
mDestCrs = parameterAsCrs( parameters, QStringLiteral( "TARGET_CRS" ), context );
mTransformContext = context.project() ? context.project()->transformContext() : QgsCoordinateTransformContext();
return true;
Expand Down
16 changes: 12 additions & 4 deletions src/core/processing/qgsprocessingalgorithm.cpp
Expand Up @@ -768,6 +768,7 @@ bool QgsProcessingAlgorithm::supportInPlaceEdit( const QgsVectorLayer *layer ) c
return false;
}


bool QgsProcessingAlgorithm::createAutoOutputForParameter( QgsProcessingParameterDefinition *parameter )
{
if ( !parameter->isDestination() )
Expand Down Expand Up @@ -852,10 +853,7 @@ QgsCoordinateReferenceSystem QgsProcessingFeatureBasedAlgorithm::sourceCrs() con

QVariantMap QgsProcessingFeatureBasedAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
mSource.reset( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
if ( !mSource )
throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );

prepareSource( parameters, context );
QString dest;
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest,
outputFields( mSource->fields() ),
Expand Down Expand Up @@ -933,3 +931,13 @@ bool QgsProcessingFeatureBasedAlgorithm::supportInPlaceEdit( const QgsVectorLaye
return true;
}

void QgsProcessingFeatureBasedAlgorithm::prepareSource( const QVariantMap &parameters, QgsProcessingContext &context )
{
if ( ! mSource )
{
mSource.reset( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
if ( !mSource )
throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
}
}

8 changes: 8 additions & 0 deletions src/core/processing/qgsprocessingalgorithm.h
Expand Up @@ -826,6 +826,7 @@ class CORE_EXPORT QgsProcessingAlgorithm

bool createAutoOutputForParameter( QgsProcessingParameterDefinition *parameter );


friend class QgsProcessingProvider;
friend class TestQgsProcessing;
friend class QgsProcessingModelAlgorithm;
Expand Down Expand Up @@ -990,6 +991,13 @@ class CORE_EXPORT QgsProcessingFeatureBasedAlgorithm : public QgsProcessingAlgor
*/
virtual bool supportInPlaceEdit( const QgsVectorLayer *layer ) const override;

/**
* Read the source from \a parameters and \a context and set it
*
* \since QGIS 3.4
*/
void prepareSource( const QVariantMap &parameters, QgsProcessingContext &context );

private:

std::unique_ptr< QgsProcessingFeatureSource > mSource;
Expand Down
21 changes: 21 additions & 0 deletions tests/src/python/test_qgsprocessinginplace.py
Expand Up @@ -88,6 +88,7 @@ def _alg_tester(self, alg_name, input_layer, parameters):
context.setProject(QgsProject.instance())
feedback = QgsProcessingFeedback()

input_layer.rollBack()
with self.assertRaises(QgsProcessingException) as cm:
execute_in_place_run(
alg, input_layer, parameters, context=context, feedback=feedback, raise_exceptions=True)
Expand Down Expand Up @@ -199,6 +200,26 @@ def test_arrayfeatures(self):
# Check selected
self.assertEqual(len(self.vl.selectedFeatureIds()), 3)

def test_reprojectlayer(self):
"""Check that this runs correctly"""

old_count = self.vl.featureCount()

old_features, new_features = self._alg_tester(
'native:reprojectlayer',
self.vl,
{
'TARGET_CRS': 'EPSG:3857',
}
)

g = [f.geometry() for f in new_features][0]
self.assertAlmostEqual(g.get().x(), 1001875.4, 1)
self.assertAlmostEqual(g.get().y(), 5621521.5, 1)

# Check selected
self.assertEqual(self.vl.selectedFeatureIds(), [1])

def _make_compatible_tester(self, feature_wkt, layer_wkb_name, attrs=[1], old_feature=None):
fields = QgsFields()
wkb_type = getattr(QgsWkbTypes, layer_wkb_name)
Expand Down

0 comments on commit 0380c06

Please sign in to comment.