0

I have a huge shapefile with an attribute field called category.

How do I add random individual colors to this category attribute and then export it with the style so that I can open it in other software?

4
  • working on QGIS software
    – Abinash
    Jan 6, 2022 at 11:26
  • Shapefiles don't store styling elements - you need to tell us what you want to open it with
    – Ian Turton
    Jan 6, 2022 at 11:28
  • 1
    Actually, step 0: Shapefiles do not carry a color property. You can, however, add a numeric or text field (provided that doing so doesn't exceed on of the many limitations of dBase format).
    – Vince
    Jan 6, 2022 at 11:32
  • If you need random color most GIS software are able to do that, so no need to add a QGIS steep, if your other software is not a GIS software you need to tell us witch format it will read (or at last what kind of software you plan to use)
    – J.R
    Jan 6, 2022 at 15:09

1 Answer 1

1

There might be a better way to do it, but here is my take on this. It is assuming your category field is called "code". All you have to do is create a dictionary with your random colors based on category field and store values in a new field "Color". Then you can use that to create a categorized renderer for your layer. Now the color is part of your shapefile and could probably be used in a different software. I hope this helps.

# import modules
import random
from qgis.core import *
from qgis.utils import *

# create a hexadecimal random color
def rand_color():
    hexadecimal = "#" + ''.join([random.choice('ABCDEF0123456789') for i in range(6)])
    return hexadecimal

# assign layer
layer = iface.activeLayer()
# create empty dict to store category:color pairs
colors = {}
# iterate through features and generate random color
# based on category to populate your color dictionary 
for feat in layer.getFeatures():
    field = feat["code"]
    colors[field] = rand_color()
#create new field to store color
pr = layer.dataProvider()
pr.addAttributes([QgsField("Color",QVariant.String, "text", 50)])
layer.updateFields()

# update color field
with edit(layer):
    for feat in layer.getFeatures():
            feat["Color"] = colors[feat["code"]]
            layer.updateFeature(feat)

# create the categorised renderer
categorized_renderer = []
# iterate to assign the color based on the category
for feat in layer.getFeatures():
    category = feat["code"]
    color = feat["Color"]
    categorized_renderer.append(
        QgsRendererCategory(category, QgsFillSymbol().createSimple({'color': color}), 'code: {}'.format(category))
    )

## apply symbol to layer renderer
renderer = QgsCategorizedSymbolRenderer('code', set(categorized_renderer))
layer.setRenderer(renderer)
# repaint the layer
layer.triggerRepaint()

enter image description here

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.