8

I need to reproduce the style of a raster layer on a polygon layer like this:

enter image description here

where you see in red the hatching inside the red contour of the polygon.

I tried hatched line in the symbology, but I have to define the line length and get the hatched line detached from the contour when angles are too obtuse.

And I need to handle overlapping polygons and display only one hatched line even if many polygons share the same border.

1 Answer 1

14

New method - QGIS 3.36

QGIS 3.36 introduced a new Outline: Filled Line symbology that makes this task a lot easier.

Just configure the following settings under Filled Line:

  • Set Stroke width to x units
  • Set Offset to x / 2 units (half of stroke)
  • Use Join style Miter - ensures sharp corners are filled

enter image description here

It seems to run a lot faster than the old method.


Old method - QGIS <3.36

The best way to achieve that effect is to use a line pattern fill on an "inner buffer" instead of using hatched lines.

Try this expression in Geometry generator - set geometry type to Polygon/Multipolygon, and appropriate units; replace 4 in expression with the desired distance in those units:

difference($geometry,buffer($geometry,-4))

Set the generated polygon style to line pattern fill. Add another symbol below to display a simple line showing the boundaries.

Example:

enter image description here


However, if you plan to use the old method on many polygons, or polygons with lots of vertices, it can be very slow to render.

I would recommend only rendering hatched buffer in the old metohd at lower scales, as required. The most effective way to do this is duplicate the polygon layer, have one version of the layer show only the outlines, and the other version only the hatched buffer - but with layer scale visibility set to 1:500 maximum or as required.

This appears to be more effective than using @map_scale < 500 or similar as a data-defined override for 'Enable symbol layer'.


Additional features (both methods)

Text mask

To also achieve the blanked-out effect behind text as in '3000 ASFC' in your image, use a mask buffer for the label, and set the generated hatch or filled line hatch as the symbol to mask (requires more recent version of QGIS, does not work in 3.28 but does in 3.34+).

enter image description here

Line pattern alignment

If you want the line pattern to match up perfectly at the boundaries, for either method, change the Line Pattern Fill > Alignment setting to Align Pattern to Map Extent.

enter image description here

enter image description here

6
  • Thank you everything works well except the merged feature symbology. When I apply it with the collect() fonction, my symbols disappear. I work with QGIS 3.22 (and can't upgrade for the moment). Btw my polygons must be displayed with a categorized style, so for the moment the sub renderer is categorized, but I tried also with single symbol exactly like your example without success.
    – dmjf08
    Commented Jan 27 at 16:50
  • I think my problem is that geometries from different categories overlap so when I use "merged features" it aggregates them without taking the categories into account... if you have any idea ?
    – dmjf08
    Commented Jan 27 at 17:00
  • 1
    Yes, I believe merged features merges everything and eliminates common boundaries unless you use geom generator and collect(). You could try collect() with your categorising field as a second parameter since it is an aggregate function after all. I can't test such an old version unfortunately, but there were some major changes to style handling after ~3.26 I believe. There might have been some old hacks required back then that have become unnecessary due to later fixes. Try wrapping the whole expression with buffer(... ,0) as well?
    – she_weeds
    Commented Jan 27 at 17:19
  • I found the function collect_geometries() have you ever used it ?
    – dmjf08
    Commented Jan 27 at 17:42
  • 1
    That's irrelevant, it's for arrays. You might need to set up a separate question on the issues you are facing with using merged features renderer with collect() and overlapping polygons and categorised sub renderers, with concrete examples of your data structure, what you've tried, and what happens exactly with each thing you've tried. Though it's likely "upgrade qgis" will be one of the answers. If this answer resolves the bulk of your original question please remember to mark it as accepted to assist others in the future in finding answers.
    – she_weeds
    Commented Jan 28 at 5:21

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.