Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 0a556a8
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/ExecuteSQL.py
Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions python/plugins/processing/algs/qgis/ExportGeometryInfo.py
Expand Up @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions python/plugins/processing/algs/qgis/RasterCalculator.py
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/analysis/processing/qgsbookmarkalgorithms.cpp
Expand Up @@ -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() );

Expand Down

0 comments on commit 0a556a8

Please sign in to comment.