4.2. Lesson: Labels

Des étiquettes peuvent être ajoutées à une carte pour afficher de l’information sur un objet. N’importe quelle couche vecteur peut avoir des étiquettes qui lui sont associées. Le contenu de ces étiquettes repose sur les données attributaires de la couche.

Le but de ce cours: Appliquer des étiquettes utiles, jolies et lisibles à une couche.

4.2.1. basic Follow Along: Utilisation d’étiquettes

First, ensure that the labeling button is visible in the GUI:

  1. Go to the menu item View ‣ Toolbars

  2. Ensure that the Label Toolbar item has a check mark next to it. If it doesn’t, click on the Label Toolbar item to activate it.

  3. Click on the places layer in the Layers panel so that it is highlighted

  4. Click on the labeling toolbar button to open the Labels tab of the Layer Styling panel

  5. Switch from No Labels to labeling Single Labels

    You’ll need to choose which field in the attributes will be used for the labels. In the previous lesson, you decided that the name field was the most suitable one for this purpose.

  6. Select name from the Value list:

    ../../../_images/select_label_with.png
  7. Click Apply

La carte devrait maintenant afficher des étiquettes comme ceci:

../../../_images/first_place_names.png

4.2.2. basic Follow Along: Modification des Options d’Étiquetage

Depending on the styles you chose for your map in earlier lessons, you might find that the labels are not appropriately formatted and either overlap or are too far away from their point markers.

Note

Above, you used the labeling button in the Label Toolbar to open the Layer Styling panel. As with Symbology, the same label options are available via both the Layer Styling panel and the Layer Properties dialog. Here, you’ll use the Layer Properties dialog.

  1. Open the Layer Properties dialog by double-clicking on the places layer

  2. Select the labeling Labels tab

  3. Assurez-vous que Texte est sélectionné dans la liste d’options à gauche, puis mettez à jour les options de formattage de texte pour les faire correspondre à celles montrées ci-dessous:

    ../../../_images/label_formatting_options.png
  4. Click Apply

    That font may be larger and more familiar to users, but its readability is still dependent on what layers are rendered beneath it. To solve this, let’s take a look at the Buffer option.

  5. Select Buffer from the left-hand options list

  6. Sélectionnez la case à cocher à côté de Affiche un tampon, puis choisissez les options qui correspondent aux suivantes:

    ../../../_images/buffer_options.png
  7. Click Apply

    Vous constaterez que cela ajoute une bordure ou un tampon coloré autour des étiquettes des places, les rendant plus faciles à repérer sur la carte:

    ../../../_images/buffer_results.png

    Maintenant, nous pouvons aborder le positionnement des étiquettes par rapport à leurs points de repères.

  8. Select Placement from the left-hand options list

  9. Select Around point and change the value of Distance to 2.0 Millimeters:

    ../../../_images/offset_placement_settings.png
  10. Click Apply

    Vous verrez que les étiquettes ne chevauchent plus désormais les points.

4.2.3. moderate Follow Along: Utiliser des étiquettes à la place de symboles de couche

In many cases, the location of a point doesn’t need to be very specific. For example, most of the points in the places layer refer to entire towns or suburbs, and the specific point associated with such features is not that specific on a large scale. In fact, giving a point that is too specific is often confusing for someone reading a map.

Par exemple: sur une carte du monde, le point indiquant l’Union Européenne peut être quelque part en Pologne, par exemple. Pour quelqu’un qui lit la carte, voir un point marqué Union Européenne en Pologne peut sembler vouloir dire que la capitale de l’Union Européenne est donc en Pologne.

Aussi, pour prévenir ce genre d’incompréhension, il est souvent utile de désactiver les symboles de points et les remplacer complètement par les étiquettes.

Dans QGIS, vous pouvez faire ça en positionnant l’étiquette directement à la place du point auquel elle se réfère.

  1. Open the labeling Labels tab of the Layer Properties dialog for the places layer

  2. Select the Placement option from the options list

  3. Click on the Offset from point button

    Ceci va révéler les options Quadrant que vous pourrez utiliser pour définir la position de l’étiquette par rapport au marqueur de point. Dans le cas-ci, nous souhaitons centrer l’étiquette sur le point donc choisissez le centre du quadrant:

    ../../../_images/quadrant_offset_options.png
  4. Hide the point symbols by editing the layer Symbology as usual, and setting the size of the Marker size to 0.0:

    ../../../_images/hide_point_marker.png
  5. Click Apply and you’ll see this result:

    ../../../_images/hide_point_marker_results.png

If you were to zoom out on the map, you would see that some of the labels disappear at larger scales to avoid overlapping. Sometimes this is what you want when dealing with datasets that have many points, but at other times you will lose useful information this way. There is another possibility for handling cases like this, which we’ll cover in a later exercise in this lesson. For now, zoom out and click on the showUnplacedLabel button in the toolbar and see what happens.

4.2.4. moderate Try Yourself Personnaliser les étiquettes

  • Reset the label and symbol settings to have a point marker and a label offset of 2.0 Millimeters.

Vérifiez vos résultats

  • Set the map to the scale 1:100000. You can do this by typing it into the Scale box in the Status Bar. Modify your labels to be suitable for viewing at this scale.

Vérifiez vos résultats

4.2.5. moderate Follow Along: Étiquetage de Lignes

Maintenant que vous comprenez le fonctionnement de l’étiquetage, il y a un nouveau problème. Points et polygones sont faciles à étiqueter, mais qu’en est-il des lignes? Si vous les étiquetez de la même manière que les points, vos résultats devraient ressembler à quelque chose comme ceci:

../../../_images/bad_street_labels.png

We will now reformat the roads layer labels so that they are easy to understand.

  1. Hide the places layer so that it doesn’t distract you

  2. Activate labeling Single Labels for the roads layer as you did above for places

  3. Set the font Size to 10 so that you can see more labels

  4. Zoom in on the Swellendam town area

  5. In the Labels tab’s Placement tab, choose the following settings:

    ../../../_images/street_label_settings.png

    You’ll probably find that the text styling has used default values and the labels are consequently very hard to read. Update the Text to use a dark-grey or black Color and the Buffer to use a light-yellow Color.

    Selon l’échelle, la carte devrait ressembler à peu près à ceci:

    ../../../_images/street_label_formatted.png

    Vous verrez que certains noms de voies apparaissent plus d’une fois sans que ce ne soit toujours nécessaire. Pour empêcher cela:

  6. In the Labels tab of the Layer Properties dialog, choose the Rendering option and select Merge connected lines to avoid duplicate labels as shown:

    ../../../_images/merge_lines_option.png
  7. Cliquez sur OK.

    Une autre fonction utile permet d’empêcher l’affichage des entités trop courtes pour être indiquées.

  8. In the same Rendering panel, set the value of Suppress labeling of features smaller than … to 5.00 mm and note the results when you click Apply

    Try out different Placement settings as well. As we’ve seen before, the Horizontal option is not a good idea in this case, so let’s try the Curved option instead.

  9. Select the Curved option in the Placement panel of the Layers tab

Voici le résultat:

../../../_images/final_street_labels.png

As you can see, this hides some labels that were previously visible, because of the difficulty of making some of them follow twisting street lines while still being legible. It makes other labels much more useful since they track the roads rather than float in space between them. You can decide which of these options to use, depending on what you think seems more useful or what looks better.

4.2.6. hard Follow Along: Valeurs définies par des données

  1. Deactivate labeling for the roads layer

  2. Reactivate labeling for the places layer

  3. Open the attribute table for places via the openTable button

    It has one field which is of interest to us now: place which defines the type of urban area for each record. We can use this data to influence the label styles.

  4. Navigate to the Text panel in the places Labels panel

  5. Click the dataDefined button next to the Italic text button beneath Style and select Edit… to open the Expression String Builder:

    ../../../_images/expression_string_builder.png
  6. Under Fields and Values, double click on place and then click All Unique. This will list all unique values of the place field of this layer. Add a = in the text editor and then double click on town.

    Alternatively, you can type: "place" = 'town' directly in the text editor.

  7. Click OK twice:

    ../../../_images/expression_builder_settings.png

Notice that the labels for all places whose place field matches town are displayed in italics.

../../../_images/italic_label_result.png

4.2.7. hard Try Yourself Utiliser les Valeurs définies par des données

Note

Nous allons aller un peu plus loin ici pour vous présenter quelques paramètres avancés d’étiquetage. A ce niveau avancé, nous assumons que vous avez suffisamment de connaissances sur le sujet pour comprendre ce qui va suivre. Si ce n’est pas le cas, quittez cette section et revenez plus tard lorsque vous aurez étudiez les pré-requis.

  1. Open the Attribute Table for places

  2. Enter edit mode by clicking the toggleEditing button

  3. Add a new column with the newAttribute button

  4. Configurez-le comme ceci:

    ../../../_images/font_size_column.png
  5. Use this to set custom font sizes for each different type of place (each key in the place field)

Vérifiez vos résultats

4.2.8. hard Possibilités Avancées de l’étiquetage

We can’t cover every option in this course, but be aware that the Label tab has many other useful functions. You can set scale-based rendering, alter the rendering priority for labels in a layer, and set every label option using layer attributes. You can even set the rotation, XY position, and other properties of a label (if you have attribute fields allocated for the purpose), then edit these properties using the tools adjacent to the main Layer Labeling Options button:

labeling showPinnedLabels pinLabels showHideLabels moveLabel rotateLabel changeLabelProperties

(Ces outils seront actifs tant que les champs requis existent dans la table d’attributs et que vous êtes en mode édition.)

N’hésitez pas à explorer les diverses possibilités offertes par le système d’étiquetage.

4.2.9. In Conclusion

Vous avez appris comment utiliser les attributs d’une couche pour créer des étiquettes dynamiques. Cela peut rendre votre carte plus informative et plus élégante.

4.2.10. What’s Next?

Maintenant que vous savez comment les attributs peuvent apporter une différence visuelle à votre carte, que diriez-vous de les utiliser pour changer la symbologie des objets eux-mêmes ? C’est l’objet de la leçon suivante!