2

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

2

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:

Script interface


You can apply the style in a couple of ways:

  1. 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()
    
  2. 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")
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.