1

I have a shapefile with contour lines. When I go to classify it to decimeter interval it chooses some rounded off to for example 5,8 while 5,7 is classified by 5,70000001 or simliar. How do I go about getting it round off to the decimeter?

enter image description here

3 Answers 3

3

Rather than directly using your "ELEV" field as classification value you may use some expression to round the value to the desired number of decimal.

In your case you could use Round("ELEV", 1)

1
  • Thanks, that got them displayed correctly on the map!
    – Igzie
    Commented Sep 22, 2023 at 10:13
1

As far as I know, there is no way to define the number of decimals for your legend when you symbolize with the Categorized option.

If you use the Graduated option, you get that ability.

1
  • Yeah the legend is still a mess but for my purpose I got the above to work and just limited the legend to display the symbol, not every level.
    – Igzie
    Commented Sep 22, 2023 at 10:14
0

You can use pyqgis to round the label values:

layer = QgsProject.instance().mapLayersByName("Random points")[0] #Create a layer object from layer name. Adjust this line
value_field = "ELEV" #Name of the field to style by. Adjust this line
renderer = layer.renderer() #Create/extract a renderer object

cats = renderer.categories() #And categories
for cat in cats: #For each category
    val = cat.value() #Extract its value
    if type(val)==float: #If it is a float number
        newlabel = f"{round(val, 1)}" #Round it and convert to a string
        cat.setLabel(newlabel) #Set the category label to it
    
new_renderer = QgsCategorizedSymbolRenderer(value_field, cats) #Create a new renderer
layer.setRenderer(new_renderer) #Apply it to the layer

enter image description here

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.