Skip to content

Commit

Permalink
Merge pull request #4790 from nyalldawson/proc_layer_param
Browse files Browse the repository at this point in the history
Some minor processing fixes and improvements
  • Loading branch information
nyalldawson authored Jun 28, 2017
2 parents 891481d + d797a9b commit a39d6d9
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 34 deletions.
6 changes: 6 additions & 0 deletions doc/api_break.dox
Original file line number Diff line number Diff line change
Expand Up @@ -1663,6 +1663,12 @@ QgsNewVectorLayerDialog {#qgis_api_break_3_0_QgsNewVectorLayerDialog}
- selectedCrsId() was removed. Use crs() instead.


QgsNineCellFilter {#qgis_api_break_3_0_QgsNineCellFilter}
-----------------

- The QProgressBar argument for processRaster was changed to a QgsFeedback object.


QgsOSMElement {#qgis_api_break_3_0_QgsOSMElement}
-------------

Expand Down
4 changes: 2 additions & 2 deletions python/analysis/raster/qgsninecellfilter.sip
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ Constructor that takes input file, output file and output format (GDAL string)
%End
virtual ~QgsNineCellFilter();

int processRaster( QProgressDialog *p );
int processRaster( QgsFeedback *feedback = 0 );
%Docstring
Starts the calculation, reads from mInputFile and stores the result in mOutputFile
\param p progress dialog that receives update and that is checked for abort. 0 if no progress bar is needed.
\param feedback feedback object that receives update and that is checked for cancelation.
:return: 0 in case of success*
:rtype: int
%End
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/Aspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ def processAlgorithm(self, parameters, context, feedback):

aspect = QgsAspectFilter(inputFile, outputFile, outputFormat)
aspect.setZFactor(zFactor)
aspect.processRaster(None)
aspect.processRaster(feedback)

return {self.OUTPUT_LAYER: outputFile}
12 changes: 10 additions & 2 deletions python/plugins/processing/tests/AlgorithmsTestBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@

from qgis.core import (QgsVectorLayer,
QgsRasterLayer,
QgsMapLayer,
QgsProject,
QgsApplication,
QgsProcessingContext,
Expand Down Expand Up @@ -241,7 +242,10 @@ def check_results(self, results, context, params, expected):
if expected_result['type'] in ('vector', 'table'):
if 'compare' in expected_result and not expected_result['compare']:
# skipping the comparison, so just make sure output is valid
result_lyr = QgsProcessingUtils.mapLayerFromString(results[id], context)
if isinstance(results[id], QgsMapLayer):
result_lyr = results[id]
else:
result_lyr = QgsProcessingUtils.mapLayerFromString(results[id], context)
self.assertTrue(result_lyr.isValid())
continue

Expand All @@ -254,7 +258,11 @@ def check_results(self, results, context, params, expected):
except KeyError as e:
raise KeyError('Expected result {} does not exist in {}'.format(str(e), list(results.keys())))

result_lyr = QgsProcessingUtils.mapLayerFromString(results[id], context)
if isinstance(results[id], QgsMapLayer):
assert False
result_lyr = results[id]
else:
result_lyr = QgsProcessingUtils.mapLayerFromString(results[id], context)

compare = expected_result.get('compare', {})

Expand Down
23 changes: 7 additions & 16 deletions src/analysis/raster/qgsninecellfilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "qgsninecellfilter.h"
#include "qgslogger.h"
#include "cpl_string.h"
#include "qgsfeedback.h"
#include <QProgressDialog>
#include <QFile>

Expand All @@ -43,7 +44,7 @@ QgsNineCellFilter::QgsNineCellFilter()
{
}

int QgsNineCellFilter::processRaster( QProgressDialog *p )
int QgsNineCellFilter::processRaster( QgsFeedback *feedback )
{
GDALAllRegister();

Expand Down Expand Up @@ -103,22 +104,17 @@ int QgsNineCellFilter::processRaster( QProgressDialog *p )

float *resultLine = ( float * ) CPLMalloc( sizeof( float ) * xSize );

if ( p )
{
p->setMaximum( ySize );
}

//values outside the layer extent (if the 3x3 window is on the border) are sent to the processing method as (input) nodata values
for ( int i = 0; i < ySize; ++i )
{
if ( p )
if ( feedback && feedback->isCanceled() )
{
p->setValue( i );
break;
}

if ( p && p->wasCanceled() )
if ( feedback )
{
break;
feedback->setProgress( 100.0 * static_cast< double >( i ) / ySize );
}

if ( i == 0 )
Expand Down Expand Up @@ -182,19 +178,14 @@ int QgsNineCellFilter::processRaster( QProgressDialog *p )
}
}

if ( p )
{
p->setValue( ySize );
}

CPLFree( resultLine );
CPLFree( scanLine1 );
CPLFree( scanLine2 );
CPLFree( scanLine3 );

GDALClose( inputDataset );

if ( p && p->wasCanceled() )
if ( feedback && feedback->isCanceled() )
{
//delete the dataset without closing (because it is faster)
GDALDeleteDataset( outputDriver, mOutputFile.toUtf8().constData() );
Expand Down
6 changes: 3 additions & 3 deletions src/analysis/raster/qgsninecellfilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "gdal.h"
#include "qgis_analysis.h"

class QProgressDialog;
class QgsFeedback;

/** \ingroup analysis
* Base class for raster analysis methods that work with a 3x3 cell filter and calculate the value of each cell based on
Expand All @@ -37,9 +37,9 @@ class ANALYSIS_EXPORT QgsNineCellFilter
virtual ~QgsNineCellFilter() = default;

/** Starts the calculation, reads from mInputFile and stores the result in mOutputFile
\param p progress dialog that receives update and that is checked for abort. 0 if no progress bar is needed.
\param feedback feedback object that receives update and that is checked for cancelation.
\returns 0 in case of success*/
int processRaster( QProgressDialog *p );
int processRaster( QgsFeedback *feedback = nullptr );

double cellSizeX() const { return mCellSizeX; }
void setCellSizeX( double size ) { mCellSizeX = size; }
Expand Down
27 changes: 19 additions & 8 deletions src/core/processing/qgsnativealgorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -918,12 +918,24 @@ QVariantMap QgsExtractByAttributeAlgorithm::processAlgorithm( const QVariantMap

if ( fieldType != QVariant::String && ( op == BeginsWith || op == Contains || op == DoesNotContain ) )
{
#if 0
op = ''.join( ['"%s", ' % o for o in self.STRING_OPERATORS] )
raise GeoAlgorithmExecutionException(
self.tr( 'Operators {0} can be used only with string fields.' ).format( op ) )
#endif
return QVariantMap();
QString method;
switch ( op )
{
case BeginsWith:
method = QObject::tr( "begins with" );
break;
case Contains:
method = QObject::tr( "contains" );
break;
case DoesNotContain:
method = QObject::tr( "does not contain" );
break;

default:
break;
}

throw QgsProcessingException( QObject::tr( "Operator '%1' can be used only with string fields." ).arg( method ) );
}

QString fieldRef = QgsExpression::quotedColumnRef( fieldName );
Expand Down Expand Up @@ -969,8 +981,7 @@ QVariantMap QgsExtractByAttributeAlgorithm::processAlgorithm( const QVariantMap
QgsExpression expression( expr );
if ( expression.hasParserError() )
{
// raise GeoAlgorithmExecutionException(expression.parserErrorString())
return QVariantMap();
throw QgsProcessingException( expression.parserErrorString() );
}

QgsExpressionContext expressionContext = createExpressionContext( parameters, context );
Expand Down
11 changes: 9 additions & 2 deletions src/core/processing/qgsprocessingalgrunnertask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,15 @@ bool QgsProcessingAlgRunnerTask::run()
{
connect( mFeedback.get(), &QgsFeedback::progressChanged, this, &QgsProcessingAlgRunnerTask::setProgress );
bool ok = false;
mResults = mAlgorithm->run( mParameters, mContext, mFeedback.get(), &ok );
return ok && !mFeedback->isCanceled();
try
{
mResults = mAlgorithm->run( mParameters, mContext, mFeedback.get(), &ok );
}
catch ( QgsProcessingException & )
{
return false;
}
return !mFeedback->isCanceled();
}

void QgsProcessingAlgRunnerTask::finished( bool result )
Expand Down
10 changes: 10 additions & 0 deletions src/core/processing/qgsprocessingparameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,11 @@ QgsProcessingFeatureSource *QgsProcessingParameters::parameterAsSource( const Qg
val = fromVar.source;
}

if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( val ) ) )
{
return new QgsProcessingFeatureSource( layer, context );
}

QString layerRef;
if ( val.canConvert<QgsProperty>() )
{
Expand All @@ -285,6 +290,11 @@ QgsProcessingFeatureSource *QgsProcessingParameters::parameterAsSource( const Qg
else if ( !val.isValid() || val.toString().isEmpty() )
{
// fall back to default
if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( definition->defaultValue() ) ) )
{
return new QgsProcessingFeatureSource( layer, context );
}

layerRef = definition->defaultValue().toString();
}
else
Expand Down
9 changes: 9 additions & 0 deletions tests/src/core/testqgsprocessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3924,6 +3924,15 @@ void TestQgsProcessing::processingFeatureSource()
QVERIFY( source->getFeatures().nextFeature( f2 ) );
QCOMPARE( f2.geometry(), f.geometry() );

// direct map layer
params.insert( QStringLiteral( "layer" ), QVariant::fromValue( layer ) );
source.reset( QgsProcessingParameters::parameterAsSource( def.get(), params, context ) );
// can't directly match it to layer, so instead just get the feature and test that it matches what we expect
QVERIFY( source.get() );
QVERIFY( source->getFeatures().nextFeature( f2 ) );
QCOMPARE( f2.geometry(), f.geometry() );


// next using property based definition
params.insert( QStringLiteral( "layer" ), QgsProcessingFeatureSourceDefinition( QgsProperty::fromExpression( QStringLiteral( "trim('%1' + ' ')" ).arg( layer->id() ) ), false ) );
source.reset( QgsProcessingParameters::parameterAsSource( def.get(), params, context ) );
Expand Down

0 comments on commit a39d6d9

Please sign in to comment.