Skip to content
Permalink
Browse files

Gracefully handle no project available in context in algorithms

where a project is only required in some circumstances
  • Loading branch information
nyalldawson committed Jul 29, 2020
1 parent d57a638 commit 0a556a816148a79740ba46adb72cc8cf8cb40782
@@ -133,7 +133,7 @@ def processAlgorithm(self, parameters, context, feedback):
# access (thanks the QgsVirtualLayerProvider) to memory layer that
# belongs to temporary QgsMapLayerStore, not project.
# So, we write them to disk is this is the case.
if not context.project().mapLayer(layer.id()):
if context.project() and not context.project().mapLayer(layer.id()):
basename = "memorylayer." + QgsVectorFileWriter.supportedFormatExtensions()[0]
tmp_path = QgsProcessingUtils.generateTempFilename(basename)
QgsVectorFileWriter.writeAsVectorFormat(
@@ -139,6 +139,8 @@ def processAlgorithm(self, parameters, context, feedback):
self.distance_area.setSourceCrs(source.sourceCrs(), context.transformContext())
self.distance_area.setEllipsoid(context.ellipsoid())
elif method == 1:
if not context.project():
raise QgsProcessingException(self.tr('No project is available in this context'))
coordTransform = QgsCoordinateTransform(source.sourceCrs(), context.project().crs(), context.project())

features = source.getFeatures()
@@ -144,8 +144,9 @@ def _cellsize(layer):
expression = self.mappedNameToLayer(lyr, expression, layersDict, context)

# check for layers available in the project
for lyr in QgsProcessingUtils.compatibleRasterLayers(context.project()):
expression = self.mappedNameToLayer(lyr, expression, layersDict, context)
if context.project():
for lyr in QgsProcessingUtils.compatibleRasterLayers(context.project()):
expression = self.mappedNameToLayer(lyr, expression, layersDict, context)

# create the list of layers to be passed as inputs to RasterCalculaltor
# at this phase expression has been modified to match available layers
@@ -93,7 +93,11 @@ bool QgsBookmarksToLayerAlgorithm::prepareAlgorithm( const QVariantMap &paramete
{
QList< int > sources = parameterAsEnums( parameters, QStringLiteral( "SOURCE" ), context );
if ( sources.contains( 0 ) )
{
if ( !context.project() )
throw QgsProcessingException( QObject::tr( "No project is available for bookmark extraction" ) );
mBookmarks.append( context.project()->bookmarkManager()->bookmarks() );
}
if ( sources.contains( 1 ) )
mBookmarks.append( QgsApplication::bookmarkManager()->bookmarks() );

0 comments on commit 0a556a8

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