[Qgis-developer] New API: get attribute values from a PostGIS layer

Stéphane Henriod s at henriod.info
Wed Jul 17 12:13:05 PDT 2013


Dear all

I am trying to get into the new 2.0 API and am still facing issues for very
basic stuff: getting the values of the attributes out of a PostGIS layer.

Basically, it works (with the almost same code) in the Qgis python console,
but I don't manage to get it to work in a stand-alone script.

What I have so far is a PostGIS layer, that I load with:

*uri = QgsDataSourceURI()
> uri.setConnection('localhost', '5432', 'db_name', 'user_name', 'password')
> uri.setDataSource('my_schema', 'my_table', 'the_geom')
>
> my_layer = QgsVectorLayer(uri.uri(),'myhospitals','postgres')
> *


A quick test shows me that the layer has been successfully loaded:

*if not my_layer.isValid():
>     print "Layer failed to load!"
> else:
>     print "Layer loaded successfully"
> *


I would assume that

*for elem in my_layer.getFeatures():*
> *    print elem.attributes()*
>

should return the attributes in a usable format but, instead, I get:
*
*
>
> *[<PyQt4.QtCore.QVariant object at 0xad37aca4>, <PyQt4.QtCore.QVariant
> object at 0xad37acdc>, None, <PyQt4.QtCore.QVariant object at 0xad37ad14>]
> *
> *[<PyQt4.QtCore.QVariant object at 0xad37aca4>, <PyQt4.QtCore.QVariant
> object at 0xad37acdc>, <PyQt4.QtCore.QVariant object at 0xad37ad14>,
> <PyQt4.QtCore.QVariant object at 0xad37ad4c>]*
> *[<PyQt4.QtCore.QVariant object at 0xad37aca4>, <PyQt4.QtCore.QVariant
> object at 0xad37acdc>, <PyQt4.QtCore.QVariant object at 0xad37ad14>,
> <PyQt4.QtCore.QVariant object at 0xad37ad4c>]*
> *[<PyQt4.QtCore.QVariant object at 0xad37aca4>, <PyQt4.QtCore.QVariant
> object at 0xad37acdc>, <PyQt4.QtCore.QVariant object at 0xad37ad14>,
> <PyQt4.QtCore.QVariant object at 0xad37ad4c>]*
>

Does anyone have an idea where the problem could lie?

For those who speak French, the question is posted here as well:
http://www.forumsig.org/showthread.php/37178-Nouvelle-API-r%C3%A9cup%C3%A9rer-les-valeurs-attributaires

I am using Qgis 1.9.0 with Python 2.7 on Ubuntu 13.04

Thanks in advance for ideas and comments...

Cheers

Stéphane
--
"Le mot progrès n'aura aucun sens tant qu'il y aura des enfants malheureux"
-- Albert Einstein

"A journey does not need reasons. Before long, it proves to be reason
enough in itself. One thinks that one is going to make a journey, yet soon
it is the journey that makes or unmakes you." -- Nicolas Bouvier

Photos de voyages, photos de montagne: http://www.henriod.info
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20130717/88e2ea47/attachment.html>


More information about the Qgis-developer mailing list