[Qgis-developer] Re: PyQGIS working script / demo
Noli Sicad
nsicad at gmail.com
Tue Jul 13 22:36:11 EDT 2010
I started developing a simple PyGIS Application example using the cookbook.
Probably we can make this ball rolling and have a working example.
Please edit, add features, more feature othe sample scripts. Please
don't ask why this intial script is wrong or not appropriate just edit
and resend to the list. You can add ui resources as well if you like.
However, if you have a better demo that this, please share to us.
I am attaching the file in this email.
Thanks. Noli
~~~~~~~~~~~~~~~~~~
# PyQGIS Application Example -
#To Do.
# 1. Create as sample Mainform. - Done
# 2. Load Shapefile ??
# 3. Create Map Canvas ??
# 4. Render the Shapefile in the Map Canvas.??
# 5. Print the map into pdf
# http://osgeo-org.1803224.n2.nabble.com/PyQgis-crashes-when-using-QgsComposition-for-printing-td5073720.html
# 6.
# 7.
import sys
import os
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
import resources
# supply path to where is your qgis installed
#QgsApplication.setPrefixPath("/path/to/qgis/installation", True)
QgsApplication.setPrefixPath("C://program files//qgis", True)
class MyWnd(QMainWindow):
def __init__(self, layer):
QMainWindow.__init__(self)
self.canvas = QgsMapCanvas()
self.canvas.setCanvasColor(Qt.white)
self.canvas.setExtent(layer.extent())
self.canvas.setLayerSet( [ QgsMapCanvasLayer(layer) ] )
self.setCentralWidget(self.canvas)
actionZoomIn = QAction(QString("Zoom in"), self)
actionZoomOut = QAction(QString("Zoom out"), self)
actionPan = QAction(QString("Pan"), self)
actionZoomIn.setCheckable(True)
actionZoomOut.setCheckable(True)
actionPan.setCheckable(True)
self.connect(actionZoomIn, SIGNAL("triggered()"), self.zoomIn)
self.connect(actionZoomOut, SIGNAL("triggered()"), self.zoomOut)
self.connect(actionPan, SIGNAL("triggered()"), self.pan)
self.toolbar = self.addToolBar("Canvas actions")
self.toolbar.addAction(actionZoomIn)
self.toolbar.addAction(actionZoomOut)
self.toolbar.addAction(actionPan)
# create the map tools
self.toolPan = QgsMapToolPan(self.canvas)
self.toolPan.setAction(actionPan)
self.toolZoomIn = QgsMapToolZoom(self.canvas, False) # false = in
self.toolZoomIn.setAction(actionZoomIn)
self.toolZoomOut = QgsMapToolZoom(self.canvas, True) # true = out
self.toolZoomOut.setAction(actionZoomOut)
self.pan()
def zoomIn(self):
self.canvas.setMapTool(self.toolZoomIn)
def zoomOut(self):
self.canvas.setMapTool(self.toolZoomOut)
def pan(self):
self.canvas.setMapTool(self.toolPan)
# load vetor - shapefile How do we do this?
vlayer = QgsVectorLayer("/path/to/shapefile/file.shp",
"layer_name_you_like", "ogr")
# load providers
QgsApplication.initQgis()
#QgsApplication.exitQgis()
def main():
app = QApplication(sys.argv)
w = MyWnd()
w.show()
sys.exit(app.exec_())
if __name__ == "__main__":
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pyqqtmain_ex1.py
Type: application/octet-stream
Size: 2574 bytes
Desc: not available
Url : http://lists.osgeo.org/pipermail/qgis-developer/attachments/20100714/5750f31a/pyqqtmain_ex1.obj
More information about the Qgis-developer
mailing list