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

Vincent Mora vincent.mora at oslandia.com
Wed Jul 17 23:43:50 PDT 2013


On 17/07/2013 23:28, Vincent Mora wrote:
> Hi Stephan,
>
> We modified the postgis-workshop tutorial for qgis plugin development
> to use the new python api of qgis 1.9, there are examples of feature
> access in there:
>
> https://github.com/Oslandia/postgis-workshop
>
Wrong link, sorry, the actual tutorial is here now
https://github.com/Oslandia/QGIS-workshop
> Cheers,
>
> Vincent.
>
> On 17/07/2013 21:13, Stéphane Henriod wrote:
>> 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
>> <http://www.henriod.info/>  
>>
>>
>> _______________________________________________
>> Qgis-developer mailing list
>> Qgis-developer at lists.osgeo.org
>> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>
>
>
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20130718/f9b1ecc5/attachment-0001.html>


More information about the Qgis-developer mailing list