python bindings : postgis QgsVectorLayer

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

python bindings : postgis QgsVectorLayer

kimaidou
Hi All, hi Martin

I am trying to open a postgis vector layer from a python plugin, in
order to add some features on it.
I read the "loading vector layer" of Martin's wiki page
http://wiki.qgis.org/qgiswiki/PythonBindings

I tried this :
*************
          uri = QgsDataSourceURI()
          # set host name, port, database name, username and password
          uri.setConnection("localhost", "5432", "aware", "postgres",
"tikpv")
          # set database schema, table name, geometry column and
optionaly subset (WHERE clause)
          uri.setDataSource("public", "import_parcelle", "wkb_geometry","")
          pglayer = QgsVectorLayer(uri.text(), "import_parcelle",
"postgres")
*************

When I load the plugin, I get this error :

Traceback (most recent call last):
 File "C:/Program Files/Quantum
GIS_092_pre/./python/plugins\cematrace\cematrace.py", line 471, in
traitement
  pglayer = QgsVectorLayer(uri.text(), "import_parcelle", "postgres")
AttributeError: text

I am using this version:

http://whatnick.dyndns.org:8080/tisham/qgis_setup0.9.2rc1_16_02_2008.exe

It seems the text() attribute does not exist... Perhaps it is because my windows version was not compiled with postgres support...
Is there a way to fix this ?

Thanks in advance

Michaël DOUCHIN

_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.qgis.org/cgi-bin/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: python bindings : postgis QgsVectorLayer

Martin Dobias
On Tue, Mar 4, 2008 at 6:11 PM, Michaël Douchin
<[hidden email]> wrote:
>  When I load the plugin, I get this error :
>
>  Traceback (most recent call last):
>   File "C:/Program Files/Quantum
>  GIS_092_pre/./python/plugins\cematrace\cematrace.py", line 471, in
>  traitement
>   pglayer = QgsVectorLayer(uri.text(), "import_parcelle", "postgres")
>  AttributeError: text

Hi,

I'm sorry, the example code was wrong. The method is named uri() and
not text(). I've also corrected the example on wiki.

Bye
Martin
_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.qgis.org/cgi-bin/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: python bindings : postgis QgsVectorLayer

mando
Hi, I would to signal an other possible error (or another my mistake : ) ) into the QGIS 0.9.1 Tutorial: I've tried to realize a standalone app as described at chapter 12. Creating Applications.
I've re-wrote the code, but when I launch the mainapp file I recive an attribute error:

AttributeError: addLayer


This is my addLayer method:

def addLayer(self):
# Add an OGR layer to the map  
file = QFileDialog.getOpenFileName(self, "Open Shapefile", ".", "Shapefiles(*.shp)") 
fileInfo = QFileInfo(file) 
#Add the layer 
layer = QgsVectorLayer(file, fileInfo.fileName(), "ogr")

if not layer.isValid():
return 
# Change the color of the layer to gray 
symbols = layer.renderer().symbols() 
symbol = symbols[0] 
symbol.setFillColor(QColor.fromRgb(192,192,192)) 

# Add layer to the registry 
QgsMapLayerRegistry.instance().addMapLayer(layer); 
# Set extent to the extent of our layer 
self.canvas.setExtent(layer.extent()) 
# Set up the map canvas layer set 
cl = QgsMapCanvasLayer(layer) 
layers = [cl] 
self.canvas.setLayerSet(layers)


And these are the lines that return the error:

self.actionAddLayer = QAction(QIcon(":/images/mActionAddLayer.png"), "Add Layer", self.frame)
self.connect(self.actionAddLayer, SIGNAL("activated()"), self.addLayer)

I work on Mac Os X 10.5, pyQt4, MacPython 2.5.

I would to translate these page of the tutorial in italian but the example don't run correctly (or it's my mistake :) )

Thanks a lot!!!

Luca

_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.qgis.org/cgi-bin/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: python bindings : postgis QgsVectorLayer

Martin Dobias
Hi,

please attach your full code so I can try it myself...

Thanks
Martin

On Tue, Mar 4, 2008 at 7:31 PM, Luca Mandolesi <[hidden email]> wrote:

>
> Hi, I would to signal an other possible error (or another my mistake : ) )
> into the QGIS 0.9.1 Tutorial: I've tried to realize a standalone app as
> described at chapter 12. Creating Applications.
> I've re-wrote the code, but when I launch the mainapp file I recive an
> attribute error:
>
> AttributeError: addLayer
>
>
>
>
> This is my addLayer method:
>
>
>  def addLayer(self):
>  # Add an OGR layer to the map
>  file = QFileDialog.getOpenFileName(self, "Open Shapefile", ".",
> "Shapefiles(*.shp)")
>  fileInfo = QFileInfo(file)
>  #Add the layer
>  layer = QgsVectorLayer(file, fileInfo.fileName(), "ogr")
>
>  if not layer.isValid():
>  return
>
>  # Change the color of the layer to gray
>  symbols = layer.renderer().symbols()
>  symbol = symbols[0]
>  symbol.setFillColor(QColor.fromRgb(192,192,192))
>
>  # Add layer to the registry
>  QgsMapLayerRegistry.instance().addMapLayer(layer);
>
>  # Set extent to the extent of our layer
>  self.canvas.setExtent(layer.extent())
>
>  # Set up the map canvas layer set
>  cl = QgsMapCanvasLayer(layer)
>  layers = [cl]
>  self.canvas.setLayerSet(layers)
>
>
>
> And these are the lines that return the error:
>
>
>  self.actionAddLayer = QAction(QIcon(":/images/mActionAddLayer.png"), "Add
> Layer", self.frame)
>  self.connect(self.actionAddLayer, SIGNAL("activated()"), self.addLayer)
>
> I work on Mac Os X 10.5, pyQt4, MacPython 2.5.
>
> I would to translate these page of the tutorial in italian but the example
> don't run correctly (or it's my mistake :) )
>
>
> Thanks a lot!!!
> Luca
_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.qgis.org/cgi-bin/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: python bindings : postgis QgsVectorLayer

mando
This is my code!

Good luck!

Bye!

Luca

_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.qgis.org/cgi-bin/mailman/listinfo/qgis-developer

pyqtapp.zip (26K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: python bindings : postgis QgsVectorLayer

Martin Dobias
Hi,

there are more problems in your code:
- first and most importantly, take care of the indentation. That's why
addLayer() is not found since it's indented more than it should so
it's created in context of another function. The same problem happens
with zoomIn, zoomOut, pan and zoomFull
- line 48 the 3rd parameter for connect() must be self.zoomIn
- lines 60-62 parameters for addAction() must be actions, not functions

After these changes it runs at least :)

Have fun,
Martin

On Tue, Mar 4, 2008 at 8:14 PM, Luca Mandolesi <[hidden email]> wrote:
> This is my code!
>
> Good luck!
>
> Bye!
>
> Luca
_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.qgis.org/cgi-bin/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: python bindings : postgis QgsVectorLayer

mando


On Tue, Mar 4, 2008 at 8:29 PM, Martin Dobias <[hidden email]> wrote:
Hi,

there are more problems in your code:
- first and most importantly, take care of the indentation. That's why
addLayer() is not found since it's indented more than it should so
it's created in context of another function. The same problem happens
with zoomIn, zoomOut, pan and zoomFull
- line 48 the 3rd parameter for connect() must be self.zoomIn
- lines 60-62 parameters for addAction() must be actions, not functions

After these changes it runs at least :)

Have fun,

Oh, thanks a lot!!!

Just two questions: 

1.
When I launch the app (that now run correctly) through TextMate I receive these messages:

QObject::moveToThread: Current thread (0xdd4a290) is not the object's thread (0x8c7ba0).
Cannot move to target thread (0x8c7ba0)

Can It makes somes problem?


2. Why is not present into the tutorial:
sys.path.insert(1, '/Applications/qgis0.9.0.app/Contents/MacOs/share/qgis/python/')

that allow Python on Mac OS X to find qgis.core and qgis.gui module?

Is there better method?

thanks

Luca




_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.qgis.org/cgi-bin/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: python bindings : postgis QgsVectorLayer

Martin Dobias
On Tue, Mar 4, 2008 at 8:52 PM, Luca Mandolesi <[hidden email]> wrote:

> Just two questions:
>
>
> 1.
> When I launch the app (that now run correctly) through TextMate I receive
> these messages:
>
> QObject::moveToThread: Current thread (0xdd4a290) is not the object's thread
> (0x8c7ba0).
>
> Cannot move to target thread (0x8c7ba0)
>
> Can It makes somes problem?

Never seen that... something Mac specific probably?


> 2. Why is not present into the tutorial:
> sys.path.insert(1,
> '/Applications/qgis0.9.0.app/Contents/MacOs/share/qgis/python/')
>
> that allow Python on Mac OS X to find qgis.core and qgis.gui module?
>
> Is there better method?

You can also do
export PYTHONPATH=.....
before running the application, it has the same function, but hard to
say whether it's a better method.
We'd like to solve these issues with setting paths by creating a
bootstrap script which will do that automatically and could also
search in some default paths.

Martin
_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.qgis.org/cgi-bin/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: python bindings : postgis QgsVectorLayer

kimaidou
In reply to this post by Martin Dobias
Ok Martin,
Thanks a lot, I tried and it worked well

Martin Dobias a écrit :
On Tue, Mar 4, 2008 at 6:11 PM, Michaël Douchin
[hidden email] wrote:
  
 When I load the plugin, I get this error :

 Traceback (most recent call last):
  File "C:/Program Files/Quantum
 GIS_092_pre/./python/plugins\cematrace\cematrace.py", line 471, in
 traitement
  pglayer = QgsVectorLayer(uri.text(), "import_parcelle", "postgres")
 AttributeError: text
    

Hi,

I'm sorry, the example code was wrong. The method is named uri() and
not text(). I've also corrected the example on wiki.

Bye
Martin


  

_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.qgis.org/cgi-bin/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: python bindings : postgis QgsVectorLayer

kimaidou
Hi list,

I come back to make a feedback.
I am writing a plugin which load data from a csv file and then
* write a shapefile
or
* write the data in a postgis table

To choose one manner or the other, I use this code:
          #shapefile
          mylayer = QgsVectorLayer("test.shp"), "test", "ogr")
          myprovider = mylayer.getDataProvider()
Or        
          #postgis table
          uri = QgsDataSourceURI()
          uri.setConnection("localhost", "5432", "aware", "postgres", "tikpv")
          uri.setDataSource("public", "import_all", "wkb_geometry","")
          pglayer = QgsVectorLayer(uri.uri(), "import_parcelle", "postgres")
          myprovider = pglayer.getDataProvider()

So basically, I just change the value of "myprovider" in order to save the data in a shapefile or in a postgis table

Then, I create a list of features (one feature per line of my original csv file) with
        list_feat.append(feat)
And I add all the features in myprovider like this :
        myprovider.addFeatures(list_feat) #list_fet is my list of features

Both methods work well BUT I have a big speed issue with the postgis way.
My csv file has about 3000 lines and 35 columns. So it is a big set of data.
With the shapefile method, the process lasts about 5 seconds
With the postgis method, the process lasts about 10 minutes ! ( the postgres.exe process uses 100% processor )

When I use ogr2ogr to import the shapefile into postgis, it lasts only 3 secondes

Have anyone an idea of what is wrong with my postgis method ?
Thanks in advance
Michael

Michaël Douchin a écrit :
Ok Martin,
Thanks a lot, I tried and it worked well

Martin Dobias a écrit :
On Tue, Mar 4, 2008 at 6:11 PM, Michaël Douchin
[hidden email] wrote:
  
 When I load the plugin, I get this error :

 Traceback (most recent call last):
  File "C:/Program Files/Quantum
 GIS_092_pre/./python/plugins\cematrace\cematrace.py", line 471, in
 traitement
  pglayer = QgsVectorLayer(uri.text(), "import_parcelle", "postgres")
 AttributeError: text
    

Hi,

I'm sorry, the example code was wrong. The method is named uri() and
not text(). I've also corrected the example on wiki.

Bye
Martin


  

_______________________________________________ Qgis-developer mailing list [hidden email] http://lists.qgis.org/cgi-bin/mailman/listinfo/qgis-developer

_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.qgis.org/cgi-bin/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: python bindings : postgis QgsVectorLayer

Martin Dobias
On Wed, Mar 5, 2008 at 11:00 AM, Michaël Douchin
<[hidden email]> wrote:

>
>  Then, I create a list of features (one feature per line of my original csv
> file) with
>          list_feat.append(feat)
>  And I add all the features in myprovider like this :
>          myprovider.addFeatures(list_feat) #list_fet is my list of features
>
>  Both methods work well BUT I have a big speed issue with the postgis way.
>  My csv file has about 3000 lines and 35 columns. So it is a big set of
> data.
>  With the shapefile method, the process lasts about 5 seconds
>  With the postgis method, the process lasts about 10 minutes ! ( the
> postgres.exe process uses 100% processor )

Looking at the code of postgres provider, I see that the features are
being added there one after other in separate queries, that's the
reason of such slow import... someone should fix that :-)

Martin
_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.qgis.org/cgi-bin/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: python bindings : postgis QgsVectorLayer

kimaidou
Humm. Now I understand :-D
Since I can't do anything on my side, I wil wait for the modification.
Thanks

Martin Dobias a écrit :
On Wed, Mar 5, 2008 at 11:00 AM, Michaël Douchin
[hidden email] wrote:
  
 Then, I create a list of features (one feature per line of my original csv
file) with
         list_feat.append(feat)
 And I add all the features in myprovider like this :
         myprovider.addFeatures(list_feat) #list_fet is my list of features

 Both methods work well BUT I have a big speed issue with the postgis way.
 My csv file has about 3000 lines and 35 columns. So it is a big set of
data.
 With the shapefile method, the process lasts about 5 seconds
 With the postgis method, the process lasts about 10 minutes ! ( the
postgres.exe process uses 100% processor )
    

Looking at the code of postgres provider, I see that the features are
being added there one after other in separate queries, that's the
reason of such slow import... someone should fix that :-)

Martin


  

_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.qgis.org/cgi-bin/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: python bindings : postgis QgsVectorLayer

Martin Dobias
On Thu, Mar 6, 2008 at 3:57 PM, Michaël Douchin
<[hidden email]> wrote:
>
>  Humm. Now I understand :-D
>  Since I can't do anything on my side, I wil wait for the modification.
>  Thanks

I've added a ticket for this issue so that we'll not forget it:
https://trac.osgeo.org/qgis/ticket/977

Martin
_______________________________________________
Qgis-developer mailing list
[hidden email]
http://lists.qgis.org/cgi-bin/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: python bindings : postgis QgsVectorLayer

Zuborg
Hi

And what syntax of the "Sql" parameter  of a "uri.setDataSource()" method? When I try even simplest "LIMIT 10" text instead empty string, creation of layer fails (how to read its error message? "lastError()" of layer return nothing). Same result with full request "SELECT * FROM xxxx" etc.