20
WORKSHOP ON THE PYTHON PROGRAMMING IN QGIS Presented by: Jyoti Dhakal (07) Archana K.C (09) Megha Shrestha (27) Sushmita Timilsina (32)

Workshop with python qgis

  • Upload
    qust04

  • View
    806

  • Download
    18

Embed Size (px)

Citation preview

Page 1: 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)

Page 2: Workshop with python qgis

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

Page 3: Workshop with python qgis

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

Page 4: Workshop with python qgis

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

Page 5: Workshop with python qgis

•Before we begin

• from qgis.core import *

• import qgis.utils

Import the capabilities and utilities to be used through Python in Python console

Page 6: Workshop with python qgis

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

Page 7: Workshop with python qgis

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")

Page 8: Workshop with python qgis

Contd…

• GPX files

• SpatiaLite database

• WFS connection

Page 9: Workshop with python qgis

Adding layer to the qgis map layer

• QgsMapLayerRegistry is needed which is already defined in qgis.core module

QgsMapLayerRegistry.instance().addMapLayer(layer)

Page 10: Workshop with python qgis

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()

Page 11: Workshop with python qgis

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()

Page 12: Workshop with python qgis

• 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()

Page 13: Workshop with python qgis

Geometry type

• QGIS return 0,1,2 for Point, Line and Polygon features respectively

Page 14: Workshop with python qgis

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)]]

Page 15: Workshop with python qgis

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()

Page 16: Workshop with python qgis

Modifying vector layers

• Start Editing

layer.startEditing()

• Functionality that supports editing of layer data

caps = layer.dataProvider().capabilities()

• Layer.commitChanges()

• Layer.rollback()

Page 17: Workshop with python qgis

• # 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])

Page 18: Workshop with python qgis

Communicating with the user

• Making a graphical user interface for easy communicating with the user

• QgsMessageBar

• QPushButton

• progressMessageBar

• pushMessage

Page 19: Workshop with python qgis

Map visualizing in canvas

• Map canvas is implemented as QgsMapCanvas class in qgis.gui module

Page 20: Workshop with python qgis