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

Nathan Woodrow madmanwoo at gmail.com
Wed Jul 17 15:14:37 PDT 2013


You will need to get the nightly builds I'm not sure what time they are
done for Ubuntu, but If you would like you can add this code (before any
PyQt4 imports) into your code in the meantime:

import sip
try:
    apis = ["QDate", "QDateTime", "QString", "QTextStream", "QTime",
"QUrl", "QVariant"]
    for api in apis:
        sip.setapi(api, 2)
except ValueError:
    # API has already been set so we can't set it again.
    pass

That will set the API version so you don't need to update just yet.

- Nathan



On Thu, Jul 18, 2013 at 8:10 AM, Stéphane Henriod <stephanebek at gmail.com>wrote:

> Thanks a lot for your answer, I will try tomorrow.
>
> How can I check if my installed version is already taking your change into
> account?
>
> Cheers
>
> Stéphane
>
> Le 18 juil. 2013 à 00:05, Nathan Woodrow <madmanwoo at gmail.com> a écrit :
>
>  Hey Stephane,
>
> This is because your standalone script doesn't use the new SIP API.  I
> pushed a change last night so that now importing qgis will set the API to
> version to.  All you need to do is make sure you import it first before any
> of the PyQt4 modules are.
>
> Before:
> from PyQt4.QtCore import *
> from qgis.core import *
>
> After
> from qgis.core import *
> from PyQt4.QtCore import *
>
>
> If you import PyQt4 first it will set the API to version 1 which you can't
> change after.
>
> - Nathan
>
>
> On Thu, Jul 18, 2013 at 5:13 AM, Stéphane Henriod <s at henriod.info> 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
>>
>> _______________________________________________
>> 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/4080b70b/attachment.html>


More information about the Qgis-developer mailing list