I have a georeferenced raster file showing parcels of a town with different colors (yellow for the category single-household residential, green for agricultural areas, etc.). I also have the shapes of the parcels as a separate vector layer. I would like to assign the color codes of the raster layer to the vector layer as an attribute.
Colour is no attribute in QGis. Attributes are values, e.g. postal codes, type of usage, size of an area.
You can nevertheless base your symbology on the attributes. E.g. when your vector layer has the type of usage as an attribute (have fun, lots of manual work unless you vectorize only specific color values of the raster and merge these later) you can choose
classified in the style-tab and enter your desired colour values.
You could build those colors from scratch using Zonal Statistics.
Assuming you are talking about a RGB-Raster,
Processing Toolbox | Raster Analysis | Zonal Statistics
Vector Layer containing zones
- tick mean only in
Statistics to calculate
- for each band in the raster:
- choose band (Band 1 = R, Band 2 = G, Band 3 = B)
- enter column name prefix, i.e. either
- close window
For each band this will add a column with the mean pixel value over the respective polygon to the selected zone layer. Column names are
<column_name_prefix>mean, i.e. e.g.
_Rmean for this example. You've got to figure out which bands you actually have to use, but these are the defaults.
<layer_context> | Properties... | Symbology
Fill | Simple fill
Simple fill | Fill color | Data defined override | Edit...
color_rgb(to_int("_Rmean"), to_int("_Gmean"), to_int("_Bmean"))
- enjoy all your pretty colors
Disclaimer: I write answers like these in the most dummy way to guarantee reproducibility. I don´t like screenshots.