Skip to content

Commit 0380c06

Browse files
elpasonyalldawson
authored andcommitted
Add prepareSource method
1 parent c7ac4fe commit 0380c06

File tree

5 files changed

+49
-4
lines changed

5 files changed

+49
-4
lines changed

python/core/auto_generated/processing/qgsprocessingalgorithm.sip.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,13 @@ checks based on the geometry type of the layer.
984984

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

987+
.. versionadded:: 3.4
988+
%End
989+
990+
void prepareSource( const QVariantMap &parameters, QgsProcessingContext &context );
991+
%Docstring
992+
Read the source from ``parameters`` and ``context`` and set it
993+
987994
.. versionadded:: 3.4
988995
%End
989996

src/analysis/processing/qgsalgorithmtransform.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ QgsTransformAlgorithm *QgsTransformAlgorithm::createInstance() const
7979

8080
bool QgsTransformAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
8181
{
82+
prepareSource( parameters, context );
8283
mDestCrs = parameterAsCrs( parameters, QStringLiteral( "TARGET_CRS" ), context );
8384
mTransformContext = context.project() ? context.project()->transformContext() : QgsCoordinateTransformContext();
8485
return true;

src/core/processing/qgsprocessingalgorithm.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,7 @@ bool QgsProcessingAlgorithm::supportInPlaceEdit( const QgsVectorLayer *layer ) c
768768
return false;
769769
}
770770

771+
771772
bool QgsProcessingAlgorithm::createAutoOutputForParameter( QgsProcessingParameterDefinition *parameter )
772773
{
773774
if ( !parameter->isDestination() )
@@ -852,10 +853,7 @@ QgsCoordinateReferenceSystem QgsProcessingFeatureBasedAlgorithm::sourceCrs() con
852853

853854
QVariantMap QgsProcessingFeatureBasedAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
854855
{
855-
mSource.reset( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
856-
if ( !mSource )
857-
throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
858-
856+
prepareSource( parameters, context );
859857
QString dest;
860858
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest,
861859
outputFields( mSource->fields() ),
@@ -933,3 +931,13 @@ bool QgsProcessingFeatureBasedAlgorithm::supportInPlaceEdit( const QgsVectorLaye
933931
return true;
934932
}
935933

934+
void QgsProcessingFeatureBasedAlgorithm::prepareSource( const QVariantMap &parameters, QgsProcessingContext &context )
935+
{
936+
if ( ! mSource )
937+
{
938+
mSource.reset( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
939+
if ( !mSource )
940+
throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
941+
}
942+
}
943+

src/core/processing/qgsprocessingalgorithm.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,7 @@ class CORE_EXPORT QgsProcessingAlgorithm
826826

827827
bool createAutoOutputForParameter( QgsProcessingParameterDefinition *parameter );
828828

829+
829830
friend class QgsProcessingProvider;
830831
friend class TestQgsProcessing;
831832
friend class QgsProcessingModelAlgorithm;
@@ -990,6 +991,13 @@ class CORE_EXPORT QgsProcessingFeatureBasedAlgorithm : public QgsProcessingAlgor
990991
*/
991992
virtual bool supportInPlaceEdit( const QgsVectorLayer *layer ) const override;
992993

994+
/**
995+
* Read the source from \a parameters and \a context and set it
996+
*
997+
* \since QGIS 3.4
998+
*/
999+
void prepareSource( const QVariantMap &parameters, QgsProcessingContext &context );
1000+
9931001
private:
9941002

9951003
std::unique_ptr< QgsProcessingFeatureSource > mSource;

tests/src/python/test_qgsprocessinginplace.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ def _alg_tester(self, alg_name, input_layer, parameters):
8888
context.setProject(QgsProject.instance())
8989
feedback = QgsProcessingFeedback()
9090

91+
input_layer.rollBack()
9192
with self.assertRaises(QgsProcessingException) as cm:
9293
execute_in_place_run(
9394
alg, input_layer, parameters, context=context, feedback=feedback, raise_exceptions=True)
@@ -199,6 +200,26 @@ def test_arrayfeatures(self):
199200
# Check selected
200201
self.assertEqual(len(self.vl.selectedFeatureIds()), 3)
201202

203+
def test_reprojectlayer(self):
204+
"""Check that this runs correctly"""
205+
206+
old_count = self.vl.featureCount()
207+
208+
old_features, new_features = self._alg_tester(
209+
'native:reprojectlayer',
210+
self.vl,
211+
{
212+
'TARGET_CRS': 'EPSG:3857',
213+
}
214+
)
215+
216+
g = [f.geometry() for f in new_features][0]
217+
self.assertAlmostEqual(g.get().x(), 1001875.4, 1)
218+
self.assertAlmostEqual(g.get().y(), 5621521.5, 1)
219+
220+
# Check selected
221+
self.assertEqual(self.vl.selectedFeatureIds(), [1])
222+
202223
def _make_compatible_tester(self, feature_wkt, layer_wkb_name, attrs=[1], old_feature=None):
203224
fields = QgsFields()
204225
wkb_type = getattr(QgsWkbTypes, layer_wkb_name)

0 commit comments

Comments
 (0)