4

While it's simple to color a point shapefile by an attribute field, I can't figure out how to match the colors with those from a raster.

I have a raster file that I'm displaying with a continuous colormap, with values clipped between -5 and 5. Using 5 colors results in colors -5, 2.5 0, 2.5 and 5. I'm forced to use the singleband_pseudocolor symbology so that I can clip the values.

The values in my point shapefile are all negative, between -0.5 and -2.3. I want to match the colors to the 5 colors in the colormap, but the colormaps will only reflect the values in the point shapefile.

I've tried saving/applying the colormap of the raster file, and manually adding extra points to match my limits; the former does nothing and the latter causes the endpoints to mismatch so there is one less color than there should be. I've also tried to rasterize the points but this isn't working at all.

I'm doing this in QGIS and can attach screen shots if necessary.

raster styling

point styling

  • Can you add a screenshot of the style panel for the raster, and one of the style panel for the point shapefile? I have a couple workarounds in mind but it would help to see what settings and color ramps you're using. – csk Jul 23 at 15:26
1

The ramp_color(ramp_name,value) allows you to set symbol color based on a color ramp. The value parameter controls the position on the ramp to select the color from (as a real number between 0 and 1).

enter image description here

Your point attribute has numbers between -0.5 and -2.3, but you want to use them as though the range was between -5 and 5. To convert the point attribute into the value parameter, we take the following steps:

  • add 5 to convert range from (-5,5) to (0,10)
  • divide by 10 to convert range (0,10) to (0,1)

Converted into Expression Builder syntax, that's ("point_attribute"+5)/10, which we'll use in the next step.

Set point symbol color using data-defined override with this expression:

ramp_color('ramp_name',("point_attribute"+5)/10)

Where the expression says 'ramp_name', substitute the name of the color ramp that you used for your singleband pseudocolor raster style. It looks like you're using the color ramp 'RdBu', but you can check the color ramp names in the style panel:

enter image description here

If you used a custom color ramp, the easiest solution is to save that color ramp with a name, and use the name in the expression. It's also possible to create a custom ramp directly in the expression builder. See the expression builder help for the ramp_color expression for more details.

  • sorry you beat me before i could add the screenshots! will take a stab at your solution now – bbuzz31 Jul 23 at 16:09
  • That's fine. I didn't end up needing the screenshot because I found a better solution that doesn't depend on your settings. The workaround I was originally thinking of was going to involve making a custom color ramp by fiddling with the color stops, which would be a lot of work, not a perfect solution, and not transferable if you decide to put the raster in a different color ramp. – csk Jul 23 at 16:11
  • excellent this works. it's more what I was expecting based on how I did it in python. I am quite new to qgis though could you tell me where to do ("point_attribute"+5)/10... i just created a new field in the points attribute table – bbuzz31 Jul 23 at 16:46
  • You don't actually need to do ("point_attribute"+5)/10 as a separate step, since it's incorporated into the ramp_color expression. I was just explaining the process that went into creating the final expression. – csk Jul 23 at 17:01
6

You can use a data-defined override for the 'Fill color' property of the point layer to achieve this.

First you need to export your singleband raster as 3-band RGB image. Once you apply the colormap, right-click -> Export -> Save As.. In the export raster layer dialog, choose Output mode as 'Rendered Image'. Once exported and loaded, you can use the following expression

Assuming your RGB raster is called 'raster', use the expression as below:

concat(raster_value('raster', 1, $geometry), ',', raster_value('raster', 2, $geometry), ',', raster_value('raster', 3, $geometry))

This samples the value in the R, G and B bands of the raster and creates a string such as 119,20,89 which is used as r,g,b values for the fill color.

data defined override

marker color from raster

  • 1
    This would work if the raster values are the components of RGB colors, but their raster has a single band with values between -5 and 5. The colors come from the "singleband pseudocolor" symbology. – csk Jul 23 at 14:28
  • thanks but as @csk says this doesn't work for singleband pseudocolor symbology... – bbuzz31 Jul 23 at 15:06
  • 1
    Good catch. @csk I have updated the answer. You can save the layer as a 3-band raster after applying the "singleband pseudocolor" symbology as a 'rendered image' and use the expression. – spatialthoughts Jul 23 at 15:43
  • thanks for the update; your solution does indeed work as intended. however, I discovered I have no data values in the raster at the point locations, so they are coming up black (no data color) with this approach – bbuzz31 Jul 23 at 16:10

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.