2

I'm following this PyQgis workshop: http://www.qgisworkshop.org/html/workshop/python_in_qgis_tutorial2.html#accessing-data-attributes

I have a point layer shapefile. I want to be able to retrieve and store x, y and name of each point( they already exist in attribute table and have indexes of 10, 11 and 1), hopefully in a dictionary format ((x,y) as key, name as value).

My problem starts here:

while provider.nextFeature(feat):
map = feat.attributeMap() 

I can't figure out how to use this 'map' dictionary. iterating through it I either get

"PyQt4.QtCore.QVariant object at 0x23C9C3B0" or "built-in method toString of QVariant object at 0x10D38D50".

any help is appreciated!

asked Apr 2, 2013 at 0:36

1 Answer 1

2

Use the toString() method on a QVariant object to get it's string value. There are a number of other methods (e.g. toInt(), toFloat(), toDouble() to name a few) for getting the value.

Here is an example:

>>> variant_string = QVariant('this is a string')
>>> variant_int = QVariant( 100)
>>> variant_string
<PyQt4.QtCore.QVariant object at 0x100f5abb0>
>>> variant_string.toString()
PyQt4.QtCore.QString(u'this is a string')
>>> str(variant_string.toString())
'this is a string'
>>> variant_int.toInt()
(100, True)

Notice that toInt() returns a tuple, the first element being the value and the second being a boolean indicating if the conversion to integer was successful.

answered Apr 2, 2013 at 22:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.