I'm trying to extract attribute data from a layer in QGIS (version 2.18.2 for mac) to use it internally on my plugin (I need that due to complex matrix operations with numpy and scipy), but QGIS is always returning rounded values with different methods, e.g.:
Original data when viewing it from open attribute table option:
[3.46000000, 0.48000000, 12.86999999, 13.30000000, ...]
Using getValues()
or getDoubleValues()
functions:
layer = iface.activeLayer()
print layer.getValues('G2')[0] #G2 is the field name
>>> [3.46, 0.48, 12.87, 13.3, ...]
Using getFeatures()
function:
field = [f['G2'] for f in layer.getFeatures()]
print field
>>> [3.46, 0.48, 12.87, 13.3, ...]
And using dataProvider()
function:
dpro = layer.dataProvider()
feat = [f['G2'] for f in dpro.getFeatures()]
print feat
>>> [3.46, 0.48, 12.87, 13.3, ...]
At the first moment I thought it could be a display limitation from console's output (I was testing there before implementing on the plugin), but running from the plugin I had the same result.
Is there a method where I can extract the data with all the decimals?
1 Answer 1
This looks like you have run into a very special rounding case inside of QGIS, and the short answer is that the methods that you are using are getting "all of the decimals."
If you look in your attribute table, there are probably more 9's than shown in the original question. I'm guessing it was displayed as 12.86999999...
and if you widen the column you would probably see 12.869999999999999
which is the full 15 digits of precision possible.
In this particular case it seems to decide that you have so many 9's it's essentially .9 repeating and in that case it makes sense to round to 12.87 (if it really was .9 repeating, then mathematically it would be accurate to say that it is 12.87)
If you edit your attribute table and just take out one of the 9's and do a layer.getValues('G2')[0]
then the return will be 12.86999999999999
.
-
Nice explanation, this really make sense, especially because I tested it on a different layer where most of the data has a long decimal length (not repeated) and it displayed the value correctly... Thanks a lot!Sandro Sousa– Sandro Sousa2017年03月09日 20:59:45 +00:00Commented Mar 9, 2017 at 20:59
Type: double; Type name: Real; Length: 23; Precision: 15; WMS and WFS flagged
3.46000000
but some lines has31.53999999
. Looks like it's trying to guess the data type from a sample and applying it to the rest of the lines. I thought could be a data problem but it should return the field length as it is.