4

I have worked out how to change the color of a polygon using pyQGIS but cannot change the stroke color or line with based on the questions detailed here and here. My example code below that works to make the polygon transparent fill but not for the blue border and desired linewidth.

# Attach core modules 
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
from qgis.core import *                        # attach main QGIS library
from qgis.utils import *                       # attach main python library
import os                                      # attach operating system library
import processing

Set the working directory - recommended to use unix forward slash for path

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ wd = "C:/TEST" # Set work directory os.chdir(wd) # Change the directory os.getcwd() # Confirm change

Set a variable for the current project instance

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Prj = QgsProject.instance() # Object for current project

Save the project to this file name

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ pnm = "200924_Test.qgs" # Project file name pnm = wd + "/" + pnm # Concat. with path Prj.write(pnm) # Save the project

Create an array [] object with the polygon vertices for MRE model area

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ vrtcs = [] vrtcs.append(QgsPointXY(541600,6504400)) vrtcs.append(QgsPointXY(541600,6505100)) vrtcs.append(QgsPointXY(542160,6505100)) vrtcs.append(QgsPointXY(542160,6504400))

Create a polygon from the vertices

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ply_01 = QgsGeometry.fromPolygonXY([vrtcs])

Create a feature object then append the polygon into the feature

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ftr = QgsFeature() ftr.setGeometry(ply_01) print(ftr.geometry())

Create a layer for the feature, in the desired CRS 28351 GDA94/MGA 51

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ lyr = QgsVectorLayer('Polygon?crs=epsg:28351', '200924_MRE_Area',"memory") Prj.addMapLayers([lyr])

Make the layer editable, add the feature with the polygon and save the layer

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ lyr.startEditing() lyr.addFeature(ftr) lyr.commitChanges()

Save as a shapefile

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Fl_ou = 'Test' Fl_ou = wd + '/' + Fl_ou

options = QgsVectorFileWriter.SaveVectorOptions() options.driverName = "ESRI Shapefile"

Change the fill, stroke colour and line width of the the polygon

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ rndr = lyr.renderer() # object to change rendering rndr.dump() # report current rendering

sym = rndr.symbol() # object to change the symbology of layer syms = sym[0] # use first layer in QGIS tree syms.setColor(QColor("transparent")) # remove the fill colour
syms.symbolLayer(0).setStrokeColor(QColor("blue")) # change the stroke colour (Fails) syms.symbolLayer(0).setWidth(3) # lyr.triggerRepaint() # Refresh canvas

Additionally, the questions (linked) also suggest the following lines of code should be included at the end of this process - I would like to understand why they are required

iface.legendInterface().refreshLayerSymbology(layer)
    iface.mapCanvas().refresh()
ahmadhanb
  • 40,826
  • 5
  • 51
  • 105
user2627043
  • 403
  • 3
  • 12

1 Answers1

5

Change the following two lines from:

syms.symbolLayer(0).setStrokeColor(QColor("blue"))   # change the stroke colour (Fails)
syms.symbolLayer(0).setWidth(3)                      #

to:

syms.setStrokeColor(QColor("blue"))
syms.setStrokeWidth(3)

and it should work.

enter image description here

ahmadhanb
  • 40,826
  • 5
  • 51
  • 105
  • Thanks for the quick reply - when I use syms.setStrokeColor(QColor,("blue")) I get a new error as follows > syms.setStrokeColor(QColor,("blue")) # change the stroke colour FAILS Traceback (most recent call last): File "C:\PROGRA~1\QGIS3~1.14\apps\Python37\lib\code.py", line 90, in runcode exec(code, self.locals) File "", line 1, in TypeError: setStrokeColor(self, strokeColor: Union[QColor, Qt.GlobalColor]): argument 1 has unexpected type 'sip.wrappertype' – user2627043 Sep 24 '20 at 02:55
  • Are you sure you are using the same code you posted? I just copy and paste your code and changed the two lines that I posted, and everything is okay, as you can see in the answer. I think this is related to another line. It shows the error at line 90. Please check that line. – ahmadhanb Sep 24 '20 at 03:04
  • Thanks again for the quick feedback - you are correct it works in the stripped-down version I posted - must be something else I've done in the main code - have a good day! – user2627043 Sep 24 '20 at 03:11