Sunday, 8 February 2015

pyqgis - Plugin processAlgorithm. Same code, different results in script vs plugin code


I'm adding functionality to a QGIS Plugin to create a print layout and add stuff to it like a map, legend, etc.


To test this code, I wrote a script. The script works and creates the print layout the way I want. Now I'm trying to add the code into the processAlgorithm() of my plugin's code. I'm getting different results with the same code from the script.


Here is what I have so far:


    def processAlgorithm(self, parameters, context, feedback):
"""This actually does the processing for creating the print layout and exporting as .pdf"""
#needs all the arguments (self, parameters, context, feedback)


log = feedback.setProgressText

input_vector = self.parameterAsVectorLayer(parameters, self.INPUT_VECTOR, context)
input_title = self.parameterAsString(parameters, self.INPUT_TITLE, context)

log(f"Input vector name: {input_vector.name()}")
log(f"Title: {input_title}")


"""This creates a new print layout"""

project = QgsProject.instance()
manager = project.layoutManager()
layout = QgsPrintLayout(project)
layoutName = "PrintLayout"

layout = QgsPrintLayout(project)
layout.initializeDefaults()
layout.setName(layoutName)
manager.addLayout(layout)




"""This adds a map item to the Print Layout"""
map = QgsLayoutItemMap(layout)
map.setRect(20, 20, 20, 20)
#Set Extent
canvas = iface.mapCanvas()
map.setExtent(canvas.extent()) #sets map extent to current map canvas
layout.addLayoutItem(map)
#Move & Resize

map.attemptMove(QgsLayoutPoint(5, 27, QgsUnitTypes.LayoutMillimeters))
map.attemptResize(QgsLayoutSize(239, 178, QgsUnitTypes.LayoutMillimeters))

results = {}
return results

When I run this part of the code in the script, I get a nice map in my print layout. It looks like this: enter image description here


When I run my plugin with the code above, I get this: enter image description here This map is the correct size and in the correct position. But obviously there is no map and something is broken.


Here is the log after I run the plugin: enter image description here


Is the problem in the way I return the "results" from the processAlgorithm? In the QGIS documentation it says this: enter image description here



However, I don't want to return anything. I just want to create the print layout with items on it. There is no output from this part of the plugin.


I just create "results" variable as an empty dictionary and return it because I am supposed to return a dictionary. So obviously I am missing something.



Answer



I think the reason is that your algorithm is being executed in a background thread, which is not allowed for print layouts. You'll need to adapt your algorithm and ensure it returns the FlagNoThreading flag:


     def flags(self):
return super().flags() | QgsProcessingAlgorithm.FlagNoThreading

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...