Skip to content
Permalink
Browse files

Merge pull request #4790 from nyalldawson/proc_layer_param

Some minor processing fixes and improvements
  • Loading branch information
nyalldawson committed Jun 28, 2017
2 parents 891481d + d797a9b commit a39d6d95304cc19e1eb3e14f5e6ca7f49074ab27
@@ -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}
-------------

@@ -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
@@ -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}
@@ -59,6 +59,7 @@

from qgis.core import (QgsVectorLayer,
QgsRasterLayer,
QgsMapLayer,
QgsProject,
QgsApplication,
QgsProcessingContext,
@@ -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

@@ -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', {})

@@ -18,6 +18,7 @@
#include "qgsninecellfilter.h"
#include "qgslogger.h"
#include "cpl_string.h"
#include "qgsfeedback.h"
#include <QProgressDialog>
#include <QFile>

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

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

@@ -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 )
@@ -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() );
@@ -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
@@ -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; }
@@ -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 );
@@ -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 );
@@ -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 )
@@ -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>() )
{
@@ -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
@@ -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 ) );

0 comments on commit a39d6d9

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