2

I have earthquake data and within the data, there are magnitude, longitude and latitude values. However, these values have around 6/7 decimal places but I would like to only have 2 decimal places for each value, when I export my map. I add the earthquake event through QgsVectorLayer rather than a CSV format when using python and I can access features such as magnitude, longitude and latitude, by using feat['magnitude']), as can be seen below: enter image description here

I then add each feature to the layout exporter, ready to export. I have tried various ways to only get 2 decimal places on each feature but non have prevailed as of yet. The ways I have tried are: to_real(ID), format_number(ID, decimal place), float(ID), round($ID, decimal places) and layer.dataProvider().addAttributes([QgsField("fieldname", QVariant.String, "String", 10, 2)]). I may have done part of the code wrong though, hence why it did not work. Each feature is within a string variable but I can not seem to find a way how to convert to double or real. A small snippet of my map is included below where the values of magnitude, longitude and latitude can be seen, so that I would like each value to say 4.39 (Mag), -72.02 (Long) and 6.40 (Lat). Hope this makes sense. enter image description here

16
  • 1
    Please provide information on the "various ways" you tried, especially code examples.
    – Erik
    Feb 3, 2021 at 12:36
  • Just updated my answer to the ways I have tried
    – LiamHems
    Feb 3, 2021 at 12:41
  • I now am confused whether this is about changing the data, or changing the display of the values, as well as why there is the python tag, since you seem to care about the display in the map composer?
    – Erik
    Feb 3, 2021 at 12:44
  • Sorry for the confusion. Basically, I would like the end result so that the display of the values on the map composer has 2 decimal places for each value. Whether that is changing the data so that when I add each feature to the layout, the data will already have 2 decimal places, then adding the data or if it is adding the data first and then changing the number of decimal places on the display of the map composer, either one works for me. As long as the values have 2 decimal places. I added a python tag as I am using the python console in QGIS to get this result. Hope this helps?
    – LiamHems
    Feb 3, 2021 at 12:49
  • The correct syntax using the field calculator would be round(to_real("field-name"),2) - while "field name" should be exchanged with some syntax for the currently chosen/visible feature.
    – Erik
    Feb 3, 2021 at 13:32

1 Answer 1

2
+50

Edit: definitive answer

mag.setText("Magnitude: " + str(round(float(feat['magnitude']), 2)))

Archived answer for people using Atlas

You can find below a sample to illustrate. This sample is using an existing QgsLayoutItemLabel you must select manually to work instead of one you created like your sample. I'm also in the context of an atlas to loop each feature.

layout_name = 'your_layout_name'
project = QgsProject.instance()
projectLayoutManager = project.layoutManager()
layout = projectLayoutManager.layoutByName(layout_name)


# You should select your label layout item before runnong this line of code
existing_layout_item_label = layout.selectedLayoutItems()[0]

text = "[% attribute(@atlas_feature ,'NAME')%]\nRounded lat : [%round(to_real(attribute(@atlas_feature ,'LATITUDE')),2)%]"
existing_layout_item_label.setText(text)

layout.refresh()
1
  • Thanks for your answer but I am not using an Atlas feature. I can not seem to find a way using my code to use: [%round(to_real(attribute(@atlas_feature ,'LATITUDE')),2)%], as it just prints this statement rather than rounding latitude to 2 decimal places.
    – LiamHems
    Feb 5, 2021 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.