4

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.

How do I assign the color codes of the raster layer to the vector layer as an attribute?

0

5 Answers 5

4

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.

1
  • 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. Commented Aug 11, 2018 at 16:30
2

On the polygon layer, with Field calculator you can get for each polygon the color code of the raster pixel at the centroid for each feature. Use this expression:

with_variable (
    'color',
    array_foreach (
        array (1,2,3),
        raster_value ('name_of_your_raster_layer', @element, centroid ($geometry))
    ),
    color_rgb (@color[0],@color[1], @color[2])
)

In the screenshot below, the output is as follows:

  • yellow polygon (Austria/Hungary) -> 255,230,128
  • purple polygon (France) -> 198,175,233
  • etc.

For demonstration purpose, the expression is used here to actually color the polygons with data defined override for the color definition: Polygons (with dark blue outline) are filled with the color sampled automatically from the raster in the background (far clarity, raster layer's transparency is reduced):

enter image description here

See here for an advanced use case to create a color legend: https://gis.stackexchange.com/a/385506/88814

3
  • I have used the expression proposed by @Babel solution with a positive result. It is possible to modify this expression so that it works with a polygon layer and extracts the average colour value of all the pixels that are within each polygon? Commented Feb 2 at 8:49
  • 1
    Not with this method. You should post this as a separate question. But see also gis.stackexchange.com/a/419334/88814
    – Babel
    Commented Feb 2 at 10:24
  • I have added a new post question with the following title: Colorize a polygon layer with the average within raster colour pixel Commented Feb 2 at 10:37
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.

0

@ZsuzsannaVitai if this is still an issue for you, then a possible reason would be that your input image has a simple 8-bit format. If you register an image and save it as a TIF or JPEG file with the default "raw data" option without any further specification then it will save as 8-bit format. However, tools like Zonal Statistics require a full 3-band 24-bit image (one 8-bit band each for Red, Green and Blue). The good news is that you can convert your registered image using qgis by applying the menu option Raster => Conversion => PCT to RGB. Then you can use this new image to follow the procedure that is explained in the answer by @geozelot.

0

Somewhat tedious method but works: I assume you have the same integer number values representing the class for the vector and the raster - eg. class "1" means "forest" both in the raster and the vector and you want them to have the same colour.

-First save your existing raster palette symbology to a file (Properties/Symbology --> "Style" button in bottom left corner --> "Save style")

-Open this new file you created in a text editor, such as Notepad++

-You will see the colour palette listed under the tag

"<colorPalette>", with a line such as
 <paletteEntry value="1" alpha="255" label="1" color="#a86828"/>'

-Now open the properties of your vector file, and under "Symbology" select "Categorized" at the top of the panel. Click "classify" at the bottom. This will give you a list of all classes in the vector, with some random colour attached to each.

-Now right-click on the coloured square representing the symbol for the first class, and select modify colour. On the lower right of this panel you will find a text field for "html code" - a string of seven units, starting with a #.

-In the text editor, find the paletteEntry for the appropriate class, and copy the colour code (eg.

color="#a86828"

) from the end of the line.

-paste this string into the "html code" field of the vector symbol colour editor.

-Click OK - you will see in the properties panel that the colour for this category has changed.

-Repeat this for all categories of the vector, copying in the html code of the colour from the text of the raster palette.

-When you are done, save the resulting vector colour palette ("Style" button in the lower left --> "Save style")

Now you will have exactly the same colour form the matching classes in the raster and the vector.

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.