Skip to content
Permalink
Browse files

Add prepareSource method

  • Loading branch information
elpaso authored and nyalldawson committed Sep 14, 2018
1 parent c7ac4fe commit 0380c06003b2ecb1922faacad5496bf1ca22346e
@@ -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

@@ -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;
@@ -768,6 +768,7 @@ bool QgsProcessingAlgorithm::supportInPlaceEdit( const QgsVectorLayer *layer ) c
return false;
}


bool QgsProcessingAlgorithm::createAutoOutputForParameter( QgsProcessingParameterDefinition *parameter )
{
if ( !parameter->isDestination() )
@@ -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() ),
@@ -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" ) ) );
}
}

@@ -826,6 +826,7 @@ class CORE_EXPORT QgsProcessingAlgorithm

bool createAutoOutputForParameter( QgsProcessingParameterDefinition *parameter );


friend class QgsProcessingProvider;
friend class TestQgsProcessing;
friend class QgsProcessingModelAlgorithm;
@@ -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;
@@ -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)
@@ -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)

0 comments on commit 0380c06

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