Monday 21 October 2019

Using round function in QGIS expression?


I created a grid of rectangular polygons with the Create Grid function, from the Vector Creation group of the Processing Toolbox in QGIS 3.4.2. The rectangles measure half a degree of longitude by half a degree of latitude.


When I want to label the polygons, I write an expression that rounds the values of latitude and longitude attributes to 2 decimal places, since the attributes created in the grid brings many decimal places.


But the round function rounds the values badly.



I know that I can create new attributes that express the coordinates with fewer decimal places and label the entities based on those attributes, but I was struck by the behavior of the function. I expected it to round to the nearest value.


Does anyone know what the problem is that the round function wrongly rounds off the decimal number?


Attached screenshots of Source Fields and the Attribute Table.


source-fields


round-expression



Answer



Actually I cannot replicate the same behavior that you have. I created a grid with 0.5 degree width of latitude and longitude, and I round it to 2 decimal degree and it works fine. Then I added another column with a value of 50.49 and round it to 1 decimal and it worked as expected and I got 50.5. Here is the table with values:


enter image description here


enter image description here


enter image description here



I am not sure what was going wrong in your case. I used the exact version of QGIS 3.4.2.


Update


You are right. If I use negative latitude and longitude, I got strange results. I think this is a bug in QGIS 3.4.2. You should report this as a bug in QGIS 3.4.2 to the QGIS developer team here:


enter image description here


Update 2


The issue of rounding negative values was fixed in the latest stable release of QGIS 3.4.4-3. Now rounding negative numbers produces correct results:


enter image description here


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...