[Qgis-user] Python Console and python scripts geoprocessing in QGIS

Noli Sicad nsicad at gmail.com
Thu Jan 14 05:33:24 PST 2010


>> Did anybody manage to use qgis in python?
>>
>> from PyQt4.QtCore import *
>> from qgis.core import *
>>
>
> Do you mean to run python scripts from command line that would use
> functionality of QGIS libraries? It's possible to do it and many times
> very useful. I've written several such scripts for various uses, e.g.
> map rendering or aligning points with street numbers to network of
> streets. It's also possible to create interactive applications using
> QGIS gui library where you can use map canvas with just few lines of
> code (see [1]).

I was reading your blog before and I forget to visit your site. I'm
sorry. I try to download and run it now. I got error.

>pythonw -u "mainwindow.py"
Traceback (most recent call last):
  File "mainwindow.py", line 9, in <module>
    from qgis.core import *
ImportError: No module named qgis.core

I have these paths in windows xp. I am using Redmond Path and GUSEK
(Scite) as editor.

C:\OSGeo4W\apps\qgis-unstable\lib
C:\OSGeo4W\apps\qgis-unstable\python
C:\OSGeo4W\apps\qgis-unstable\python\qgis
C:\OSGeo4W\apps\qgis-unstable\python\plugins

Where are these qgis.core and qgis.gui located. It seems that this
problem is also mentioned in the comments in your blog.

> As Carson have already written, there are barely any plugins which
> would have good support of interaction from QGIS python console
> (although I admit it would be very useful). It depends how well is the
> plugin code split into 'core' and 'gui' part: having core code bounded
> tightly with the gui would make it hard to use plugin's functionality
> from console. Second problem is that probably not many plugin
> developers are willing to keep some code compatibility among different
> versions to ensure that potential users don't have to change the code
> with new versions.

I think probably QGIS can put a guidelines in python writing plugin
that the core and gui part of the plugins should be separated. About
the compatibility issue, the author should place disclaimer, maybe
something can be done on this. I am not really a develop. I am just
user.

> Anyway, just a small example how the plugins can be accessed (PostGIS
> Manager plugin is used and prints DB server info):
>>>> pm = qgis.utils.plugins['postgis_manager']
>>>> pm.dlg.db.get_info()
> u'PostgreSQL 8.3.8 on x86_64-pc-linux-gnu, compiled by GCC
> gcc-4.4.real (Ubuntu 4.4.1-3ubuntu3) 4.4.1'

> Of course the plugin must be started first. Btw. this works for QGIS
>>= 1.4. For QGIS <= 1.3 the first command would be:
>>>> pm = plugins['postgis_manager']

This good as start. We can probably create some wiki for python script
in GGIS and this is the first example on python scripting in QGIS.

Thanks.

Regards, Noli



More information about the Qgis-user mailing list