Workshop with python qgis
-
Upload
qust04 -
Category
Engineering
-
view
815 -
download
18
Transcript of Workshop with python qgis
WORKSHOP ON THE PYTHON PROGRAMMING
IN QGIS
Presented by:
Jyoti Dhakal (07)
Archana K.C (09)
Megha Shrestha (27)
Sushmita Timilsina (32)
Python programming and Qgis
• Python is a very powerful, simple and understandable language
• QGIS is an open source for presenting, interpreting and analyzing the geographic information
Program schedule
• Setup
• Loading layers in QGIS
• Assessing active layers
• Iterating over vector layers
• Modifying layers
• Communicating with User
• Using map canvas to visualize map
Setup
• QGIS is an open source • For this workshop we are using QGIS 2.0.1 Dufor
• For python programming in QGIS • Plugins -> Python console
•Before we begin
• from qgis.core import *
• import qgis.utils
Import the capabilities and utilities to be used through Python in Python console
Loading vector layer through python programming
• layer = QgsVectorLayer(data_source, layer_name, provider_name)
where:
QgsVectorLayer is the inbuilt class inside PyQgis
data_source is the location of the vector layer
layer_name is the name displayed in layers
provider_name is the name of the data provider
Contd…
• Layer from PostGIS database can be loaded using QgsDataSourceURI class
uri = QgsDataSourceURI()
uri.setConnection("localhost", "5432", "dbname", "johny", "xxx")
uri.setDataSource("public", "roads", "the_geom", "cityid = 2643")
vlayer = QgsVectorLayer(uri.uri(), "layer_name_you_like", "postgres")
• CSV or other delimited text files can be loaded as follows:
uri = "/some/path/file.csv?delimiter=%s&xField=%s&yField=%s" % (";", "x","y")
vlayer = QgsVectorLayer(uri, "layer_name_you_like", "delimitedtext")
Contd…
• GPX files
• SpatiaLite database
• WFS connection
Adding layer to the qgis map layer
• QgsMapLayerRegistry is needed which is already defined in qgis.core module
QgsMapLayerRegistry.instance().addMapLayer(layer)
Adding Raster Layer
• L=qgis.utils.iface.addRasterLayer('D:\sushmita\Capture.gif')
Other information about the layer added
• L.width()
• L.height()
• L.extent()
• L.metadata()
Assessing the layers
• There are number of ways to assess the layers
• First of you need to import qgis.utils.iface
• Method 1 : Run the following command:
>>> aLayer = qgis.utils.iface.activeLayer()
>>> aLayer
• Method 2 : >>> canvas = qgis.utils.iface.mapCanvas()
>>> cLayer = canvas.currentLayer()
>>> cLayer.name()
• Method 3 : >>> allLayers = canvas.layers()
>>> for i in allLayers: print i.name()...
It will return only the layers those are visible.
• Method 3 : • It’s also useful sometimes to access layers in the order they are stacked in the table of
contents
>>> canvas.layer(0)
<qgis.core.QgsVectorLayer object at 0x99eaeec>
>>> canvas.layer(0).name()
Geometry type
• QGIS return 0,1,2 for Point, Line and Polygon features respectively
Geometry construction
• from coordinates gPnt = QgsGeometry.fromPoint(QgsPoint(1,1))
gLine = QgsGeometry.fromPolyline( [ QgsPoint(1,1), QgsPoint(2,2) ] )
gPolygon = QgsGeometry.fromPolygon( [ [ QgsPoint(1,1), QgsPoint(2,2), \
QgsPoint(2,1) ] ] )
Accessor functions are required to extract the information >>>gPnt.asPoint()
(1,1)
>>> gLine.asPolyline()
[(1,1), (2,2)]
>>> gPolygon.asPolygon()
[[(1,1), (2,2), (2,1), (1,1)]]
Iterating over vector layers
• features = layer.getFeatures()
• for f in features:
• geom = f.geometry()
• print "Feature ID %d: " % f.id()
• print "Area:", geom.area()
• print "Perimeter:", geom.length()
•
Modifying vector layers
• Start Editing
layer.startEditing()
• Functionality that supports editing of layer data
caps = layer.dataProvider().capabilities()
• Layer.commitChanges()
• Layer.rollback()
• # create layer
• vl = QgsVectorLayer("Point", "temporary_points", "memory")
• pr = vl.dataProvider()
• # add fields
• pr.addAttributes( [ QgsField("name", QVariant.String),
• QgsField("age", QVariant.Int),
• QgsField("size", QVariant.Double) ] )
• # add a feature
• fet = QgsFeature()
• fet.setGeometry( QgsGeometry.fromPoint(QgsPoint(10,10)) )
• fet.setAttributes(["Johny", 2, 0.3])
• pr.addFeatures([fet])
Communicating with the user
• Making a graphical user interface for easy communicating with the user
• QgsMessageBar
• QPushButton
• progressMessageBar
• pushMessage
Map visualizing in canvas
• Map canvas is implemented as QgsMapCanvas class in qgis.gui module