4

I have a raster layer, as seen on the attached image, that works as a background of my working area. The red line, that marks the limit between the two countries, is a .shp file that, naturally, goes beyond the area of interest, the aforementioned raster.

What I am trying to achieve is a way of displaying the red line only when it overlays the raster, not outside it.

Using Clip or Intersect inside the Vector - Geoprocessing Tools menu don't work here, as they only work Vector on Vector; I tried creating a Scratch layer with a polygon of the same shape as the raster. In this way, I can clip the border line, but it creates, naturally, a new line around the raster. enter image description here

Is there a way to mask the red line as desired? Alternatively, is there a way to "cut" the polygon in two parts, by making two cuts (something similar as what the Scissors tool does in Illustrator) and just deleting the unneeded line?

Edit: This is the outcome using the method in this reply, which is the same outcome as using Clip or Intersect: https://i.sstatic.net/R11zD.jpg

The reason behind that, methinks, is that the border line marked in red is a closed polygon. See here: enter image description here

With this update, any further solution comes to mind?

0

3 Answers 3

5

We can use the Scratch Layer polygon which has the same shape as the raster inside geometry generator for the line as follows:
My Scratch Layer here is called Extent. This layer can remain unrendered/invisible.

  intersection( $geometry, geometry( get_feature_by_id('Extent',1)))

Geometry Generator

Example:
Example Picture

3
  • By using the Geometry Generator in this way, I get the same result as with the Clip tool. See here: imgur.com/BUngFuA
    – Trikelians
    Commented Apr 18, 2023 at 15:11
  • 1
    Apply a (very) small, negative buffer around the extent layer so that the rectancular boundary line is hidden. Disadvantage is that the border does not exactly reach until the border of the raster
    – Babel
    Commented Apr 18, 2023 at 15:36
  • 1
    As I commented on the answer below, I managed to make it work just by changing the Geometry Type from Polygon to Line on the .shp layer. I accepted the second answer instead of this one just because it doesn't require the extra step of creating the Scratch Layer, but just for the record, this works like a charm, too.
    – Trikelians
    Commented Apr 18, 2023 at 16:16
3

The best way (fastest, no extra data or expressions, done purely in the graphical rendering step) would be to group the two layers, render them as group and let the raster mask the line. Here is an example:

enter image description here

Add both layers to a group and set the group to "Render Layers as Group": enter image description here

Have the line layer on top of the raster layer and set its rendering mode to "Paint Inside Below":

enter image description here

3
  • Great idea, didn't know about that, +1. However, OP has not a line, but a polygon layer and with this, it will not work without extra expressions.
    – Babel
    Commented Apr 18, 2023 at 19:39
  • Are you sure? It should work the same. Commented Apr 18, 2023 at 19:50
  • Seems you're right - only thing to be aware of is that the Fill style has to be changed to No Brush and the rendering mode is deactivated if the layer's visibility is turned off and on again. Not sure if this might be a bug.
    – Babel
    Commented Apr 18, 2023 at 20:19
2

Based on the answer by @Cushen, but with two improvements:

  1. Avoid the problem of the outline along the raster's borders. It seems your vector layer is a polygon, not line. So you need the intersection of the boundary of the polygon with the raster, not of the polygon itself. use boundary() function for that.
  2. Needs not auxiliary layer. You can get the extent of the raster layer automatically with the function layer_property().

Like this, you can do it all at once with geometry generator, using this expression (replace raster_layer with the name of your raster layer and be sure to set Geometry generator's Geomtry type to LineString):

intersection( 
    boundary($geometry),  
    layer_property ('raster_layer', 'extent')
)

enter image description here

4
  • 1
    This is a really elegant solution indeed. However, in my case, it tries to close the polygon anyway, resulting on this shape: imgur.com/5dAF46z
    – Trikelians
    Commented Apr 18, 2023 at 15:57
  • Can you provide sample data for testing?
    – Babel
    Commented Apr 18, 2023 at 15:58
  • 1
    Edit: I managed to make it work. I just had to change the Geometry Type from Polygon to Line on the .shp layer.
    – Trikelians
    Commented Apr 18, 2023 at 16:06
  • Nice, had a look but didn't know about " layer_property ('raster_layer', 'extent') "
    – Cushen
    Commented Apr 18, 2023 at 23:50

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.