I'm doing a spatial join, but the styling doesn't work. What needs to be fixed?
##Style=name
##drivetimepoints=vector
##gridwgs84layer=vector
##DriveTimeGrid=output vector
outputs_QGISJOINATTRIBUTESBYLOCATION_1=processing.runalg('qgis:joinattributesbylocation', gridwgs84layer,drivetimepoints,['intersects'],0.0,0,'sum,mean,min,max,median',0,DriveTimeGrid)
layer = processing.getObject(outputs_QGISJOINATTRIBUTESBYLOCATION_1['OUTPUT'])
layer.loadNamedStyle("C:/QGIS Style File/Drive Time Grids.qml")
layer.triggerRepaint()
1 Answer 1
Once your script has finished, your output is loaded onto canvas. What you are doing is applying the style before the output is loaded into QGIS. So the style is not applied correctly. There is an option in the interface when you run your script:
You can apply the style in a couple of ways:
Load the layer in your script and uncheck the option from the interface:
##Style=name ##drivetimepoints=vector ##gridwgs84layer=vector ##DriveTimeGrid=output vector from qgis.core import QgsMapLayerRegistry outputs_QGISJOINATTRIBUTESBYLOCATION_1=processing.runalg('qgis:joinattributesbylocation', gridwgs84layer,drivetimepoints,['intersects'],0.0,0,'sum,mean,min,max,median',0,DriveTimeGrid) layer = processing.getObject(outputs_QGISJOINATTRIBUTESBYLOCATION_1['OUTPUT']) QgsMapLayerRegistry.instance().addMapLayer(layer) layer.loadNamedStyle("C:/QGIS Style File/Drive Time Grids.qml") layer.triggerRepaint()
Add the
Set style for vector layer
algorithm and again uncheck the option from the interface:##Style=name ##drivetimepoints=vector ##gridwgs84layer=vector ##DriveTimeGrid=output vector outputs_QGISJOINATTRIBUTESBYLOCATION_1=processing.runalg('qgis:joinattributesbylocation', gridwgs84layer,drivetimepoints,['intersects'],0.0,0,'sum,mean,min,max,median',0,DriveTimeGrid) layer = processing.getObject(outputs_QGISJOINATTRIBUTESBYLOCATION_1['OUTPUT']) outputs_QGISSETSTYLEFORVECTORLAYER=processing.runalg("qgis:setstyleforvectorlayer", layer, "C:/QGIS Style File/Drive Time Grids.qml")