TransWikia.com

Changing the symbology of a .geojson file in QGIS, through the Python console

Geographic Information Systems Asked by LiamHems on December 28, 2020

I have tried to look through the PyQGIS developer cookbook and change the symbology of a point file to a black circle but unfortunately every time I add it, the symbology does not change. It should just be a simple process but it does not seem to work. The first line of code I have written so far adds the .geojson file to the project and the second one aims to change the symbology:

vlayer = iface.addVectorLayer("path_to_.geojsonfile", "Earthquake Event: ", "ogr")
if not vlayer:
    print("Layer failed to load!")

symbol = QgsMarkerSymbol.createSimple({'name': 'circle', 'color': 'black'})
layer.renderer().setSymbol(symbol)
# show the change
layer.triggerRepaint()

One Answer

You are using layer.renderer() and layer.triggerRepaint() which are incorrect. It should be vlayer.renderer() and vlayer.triggerRepaint().

Notice the v before layer.renderer() and layer.triggerRepaint().

You need to choose whether to remove v from vlayer variable when adding the vector layer, or add v to the variable when calling .renderer() and .triggerRepaint() methods.

Here is a working code

vlayer = iface.addVectorLayer("path_to_.geojsonfile", "Earthquake Event: ", "ogr")
if not vlayer:
    print("Layer failed to load!")

symbol = QgsMarkerSymbol.createSimple({'name': 'circle', 'color': 'black'})
vlayer.renderer().setSymbol(symbol)
# show the change
vlayer.triggerRepaint()

Here is the output:

enter image description here

Correct answer by ahmadhanb on December 28, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP