0

I'm trying to make a wfs layer transparent, but it gives me a mistake, but I can't solve it until now.

This is my code:

uri ='http://gstore.unm.edu/apps/rgis/datasets/6ca5428a-a78c-4c82-8120-da70dc92f2cc/services/ogc/wfs?SERVICE=wfs&VERSION=1.0.0&REQUEST=GetFeature&TYPENAME=tl_2010_35_state10'

wfslayer = QgsVectorLayer(uri, "MEXICO", "WFS")

QgsProject.instance().addMapLayer(wfslayer)

So far everything is correct

 renderer = wfslayer.rendererV2()

In this function it gives me the mistake

I don't know if it's a problem with the version (QGIS 3.16.2) of the program or with the code.

It no longer lets me put the rest of the code.

symbol = renderer.symbol()
symbol.setAlpha(0.5)
2
  • What is the actual error?
    – Ian Turton
    Commented Jan 14, 2021 at 18:51
  • Hello Ian, I have the error after put: renderer = wfslayer.rendererV2(), then I can no longer continue with the function: symbol = renderer.symbol() and symbol.setAlpha(0.5), to give transparency
    – Oskar
    Commented Jan 15, 2021 at 8:19

1 Answer 1

1

You actually only need to add this line to your code after loading the WFS layer:

wfslayer.setOpacity(0.5) # enter opacity (transparency) value from 0-1

So overall, your code should be:

uri ='http://gstore.unm.edu/apps/rgis/datasets/6ca5428a-a78c-4c82-8120-da70dc92f2cc/services/ogc/wfs?SERVICE=wfs&VERSION=1.0.0&REQUEST=GetFeature&TYPENAME=tl_2010_35_state10'
wfslayer = QgsVectorLayer(uri, "MEXICO", "WFS")
wfslayer.setOpacity(0.5)
QgsProject.instance().addMapLayer(wfslayer)

From the documentation: https://qgis.org/pyqgis/3.0/core/Vector/QgsVectorLayer.html#qgis.core.QgsVectorLayer.setOpacity

I suspect you were trying to implement an approach for the previous version of QGIS (2.X), but with QGIS 3.X onwards there are changes to the API for applying layer symbology.

5
  • Thanks a lot, right, I was applying it like I was working on QGIS2, I will take into account ,as you comment, that the changes for the new current API with QGIS 3. Regards
    – Oskar
    Commented Jan 18, 2021 at 12:30
  • Sure! Could you please mark the answer as accepted (green arrow)? So people will know that this solution has worked.
    – 15Step
    Commented Jan 18, 2021 at 12:41
  • I'm sorry, I can't find how to mark the answer as accepted, as you tell me
    – Oskar
    Commented Jan 18, 2021 at 13:04
  • It's simply the green outlined checkmark to the left of the answer that solved your problem, see here: meta.stackexchange.com/questions/5234/…
    – 15Step
    Commented Jan 18, 2021 at 13:34
  • 1
    Yes! thanks, very easy
    – Oskar
    Commented Jan 18, 2021 at 13:38

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.