I have a GeoJSON layer consisting of some LineStrings. Each feature has a color property:
{
"type": "featureCollection",
"features": [
{
"type": "Feature",
"properties": { "name": "1", "color": "#ffcc90" },
"geometry": {
"type": "LineString",
"coordinates": [
...
]
}
},
...
]
}
I'd like to set each feature color according to the color value by the color attribute in properties.
I'm able to color the whole layer with a color of my choice, but don't know how to color individual features.
This is what I have done so far:
from qgis.core import QgsProject
# get list of layer names
layerNames = []
root = QgsProject.instance().layerTreeRoot()
for ch in root.children():
layerNames.append(ch.name())
# populate layers to a dictionary
layers = {}
for lName in layerNames:
mapLayer = QgsProject.instance().mapLayersByName(lName)
if len(mapLayer) == 1:
layers[lName] = mapLayer[0]
# loop through layers
for layerName in layers:
layer = layers[layerName]
for feature in layer.getFeatures():
print(feature['color'])
# here I want to set the color