Friday, 3 July 2015

labeling - Interrupting contour lines beneath elevation labels (instead of using label buffers) using QGIS?


Is there a way to interrupt a contour line beneath an elevation label using QGIS?


enter image description here



Answer



Yes, doable. Normally I'd suggest a partially transparent buffer, but I see why you want to do this cartographically.


This could be slow, and you need to manually decide where you want the labels to go - but cartographically speaking, that's not a bad thing!



Here's a screenshot...


As you can see, no buffers. The raster underneath is unaffected. I've included thinner intermediate contour lines, and styled them so they're only shown when ELEV % 50 <>0


example of interrupted contour lines


I've done this in QGIS 2.12 ... your mileage may vary with earlier versions.


I assume here you have an "ELEV" field on each contour line.


Segmentize the contour lines




  1. Use processing and the GRASS algorithm v.split.length to split your contours into segments of equal length. You need to choose a length which will be close to the size of your label in map units, assuming you're using meters. Here I used 200m.


    Be careful with this as it will make your file much, much larger (note the feature counts in the screenshot).



    To get around this, you might want to generate only those contours lines you wish to style (e.g. every 50 or 100 meters) to avoid processing all the intermediate contour lines.




  2. To this layer, add a 1 digit integer field called showLabel. Default to 0 or NULL.




  3. Change the label to only show on a segment where this field is set to 1. Use this for the label text expression...


    if ( "showlabel" is not null, "ELEV", "")

    I think if(expression,true-value,false-value) is fairly new; if using an older version, you can use CASE-ELSE





  4. Change the line style so the fixed length segments are all drawn, except those segments where the label is displayed. So use Rule-Based rendering with two rules


    Rule 1: "showLabel" is null
    Black, 0% transparent

    Rule 2: "showLabel" is not null
    Any colour, 100% transparent

    Now, all contours will show by default, but no labels.



    Manually edit segments where you want to show labels


    Go into edit mode and manually select the segments where you want the contour values to display, and set the value of showLabel to 1 for the selected features. You can use Ctrl + select (on Ubuntu/Win, Cmd + Ctrl + Click / on Mac?) to multi-select segments to speed things up.


    This should now 'clip' the contours where you want the labels to show, and the labels will show in the gaps.




In this case, my label settings were:


CRS: EPSG 27700 (Local UTM for UK, in meters)
Text size: 50 map units
Placement: Parallel, On Line


Hope that helps!


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...