[Qgis-user] Script python e console

Alister Hood alister.hood at synergine.com
Mon May 30 00:07:46 PDT 2011


> Date: Sun, 29 May 2011 22:55:47 +0200
> From: Vincenzo Antedoro <antedoro at email.it>
> Subject: Re: [Qgis-user] Script python e console
> To: giovanni.manghi at gmail.com
> Cc: Qgis-user at lists.osgeo.org
> Message-ID: <2C24E667-0682-4311-9E23-5A8C8F71A135 at email.it>
> Content-Type: text/plain; charset="us-ascii"
> 
> 
> Il giorno 29/mag/2011, alle ore 22.40, Giovanni Manghi ha scritto:
> 
> >
> > Hi,
> >
> >> I just downloaded a python script for QGIS used to draw the
varonoi's
> polygons
> >> I wonder how you can use it in QGIS?
> >
> >
> > the tool already available in QGIS does not work for you?
> >
> > vector -> geometry tools -> voronoi polygons
> 
> No, on vector->geometry tools I do not have the voice "voronoi
polygons"
> I'm on OSX 10.6.7, Q Gis 1.6.0 fTools 0.5.10.
> Why?
> 
> At any rate, I'm interested also on the previous question:
> it's possible to run a python script for Qgis on console and if the
answer is
> not,
> how can I run the script?
> 
> Cheers
> 
> Vincenzo

Hi there,
It is possible - you just need to use execfile("path_to/file")

Does anyone know if there is a place where this sort of information
could be put 
to help people, rather than just telling them to go away and learn all
out Python?
As a complete Python newbie I was looking at this and at editing text
files from 
the Python console, and I started taking notes:


How to say Hi
-------------

# From the python console:
>>> print "hello dude!"
hello dude!

# Defining a function from the python console:
>>> def hello():
... 	print "Hello dude!"
... 	return
... 

# Python uses indentation instead of closing statements, so it won't
work without indentation:
>>> def hello():
... print "Hello Dude!"
  File "<input>", line 2
    print "Hello Dude!"
        ^
IndentationError: expected an indented block

# Callling the function:
>>> hello()
Hello dude!

# Is there any help on this function?  No.
>>> help(hello)
Help on function hello:

hello()

# Put the `print "hello dude!"` command in a new script:
>>> helloPath="C:/test.py"
>>> helloScript=open(helloPath,"w")
>>> helloScript.write('print "hello dude!"')
>>> helloScript.close()

# Call the script
>>> execfile("C:/test.py")
hello dude!

# Append another command
>>> helloScript=open(helloPath,"a")
>>> helloScript.write('\nprint "And Dudettes!"')
>>> helloScript.close()

# Read the file contents to see if they are correct
>>> helloScript=open(helloPath,"r")
>>> helloScript.read()
'print "hello dude!"\nprint "And Dudettes!"'

# Call the script again
>>> execfile(helloPath)
hello dude!
And Dudettes!

# How about a QT dialogue?
# See http://www.commandprompt.com/community/pyqt/x1067
# And http://www.commandprompt.com/community/pyqt/x1149

# Importing Scripts to Python Console, 1.0.2 Kore, WindowsXP
# http://forum.qgis.org/viewtopic.php?f=6&t=6076

-----------------
# Despite the Python console appearing to import these when it starts,
they aren't actually available from the console unless you import them
yourself:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
import traceback
import code

# Oops - bad note-taking.  What was I trying to say here?
_init_commands = ["from qgis.core import *", "import qgis.utils"]



More information about the Qgis-user mailing list