3

I have the following map in .png-format that I would like to replicate, but I cannot obtain the original data.

The map shows the current water supply vulnerability for local municipalities in South Africa. I took a screenshot of the map and georeferenced with a local municipality shapefile. Here is the original .png-map:

Example_1

And here is the Local Municipality shapefile on top of the georeferenced raster:

Example_2

I would now like to extra each municipality value/colour (there are 10) to the shapefile. I tried using the SAGA-GIS Module 'Add raster values to features' tool, but the result classifies all features as 255:

Example_3

Where am I going wrong, or is it not possible to do this? This question is similar but the answer exceeds my capabilities.

5
  • 1
    Why not using a native colour picker in QGIS or another tool similar to it since you do not have that many colours? Have you tried r.colors.out or r.colors? – Taras Apr 11 '19 at 8:31
  • 1
    I hope this question gets a decent answer. This is a common real-world task that can be rather tricky in practice because the individual colours in an image file often turn out to not be very uniform at all. If you only have one map, I suspect it might be better to just accept that you will need to take a couple of hours to do it manually. – Alexander Apr 12 '19 at 8:58
  • @Taras would something like that be able to assign a unique value (out of 10 possible option/colours) to each feature in the shapefille attribute table? – JohnGIS Apr 12 '19 at 12:06
  • @Alexander I hope so too. I already had to do this manually and it took forever. I have two more maps left to make so it would be great if I could figure out a solution to streamline this. – JohnGIS Apr 12 '19 at 12:07
  • okay, clear. I thought it only involves one raster layer. – Taras Apr 12 '19 at 12:14
2

You can use a simple QGIS expression to extract the color value of a raster image and add this as an attribute to your vector layer or use it directy to set the color of your vector features by setting the it's color to data driven override. Use this expression - I use a point layer and get the color of the raster layer named raster_map.

Be aware: raster- and vector layer should be in the same CRS to work!

color_rgb( 
    raster_value( 'raster_map', 1, make_point ($x,$y)),
    raster_value( 'raster_map', 2, make_point ($x,$y)),
    raster_value( 'raster_map', 3, make_point ($x,$y))
)

For polygons (as in your case) you could replace make_point ($x,$y) with centroid ($geometry) to get the color of the raster layer at the center of each polygon.

See screenshot: data driven override for a point layer - getting the color of the raster pixel located at the point's x/y coordinates. Data driven override of set color: QGIS data driven override for vector color: getting color from raster pixel

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.