1

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.

  • Did you ever find a solution? I am stuck with the exact some problem. – JohnGIS Apr 12 at 13:05
  • Unfortunately I didn't. – Zsuzsanna Vitai Apr 15 at 13:21
1

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.

1

You could build those colors from scratch using Zonal Statistics.

Assuming you are talking about a RGB-Raster,

  • open Processing Toolbox | Raster Analysis | Zonal Statistics
    • choose Raster Layer and 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 _R, _G or _B
      • hit Run

    • 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.


  • open <layer_context> | Properties... | Symbology
    • apply Single symbol and Fill | Simple fill
    • click Simple fill | Fill color | Data defined override | Edit...
      • enter color_rgb(to_int("_Rmean"), to_int("_Gmean"), to_int("_Bmean"))
      • hit OK
    • hit OK

  • enjoy all your pretty colors



Disclaimer: I write answers like these in the most dummy way to guarantee reproducibility. I don´t like screenshots.

  • Thank you for the answer. I have known this method but the problem in the current case is that zonal statistics can't be used because the rasters don't have any values. The raster layer was georeferenced from a pdf file. – Zsuzsanna Vitai Aug 11 '18 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.