processing_standalone.py
1 |
#!/usr/bin/env python
|
---|---|
2 |
|
3 |
import os |
4 |
import sys |
5 |
|
6 |
qgisprefix = '/usr'
|
7 |
|
8 |
# configure paths for QGIS
|
9 |
os.environ['PATH'] = qgisprefix+'/bin' |
10 |
os.environ['LD_LIBRARY_PATH'] = qgisprefix+'/lib' |
11 |
sys.path.insert(0, qgisprefix+'/share/qgis/python') |
12 |
sys.path.insert(1, qgisprefix+'/share/qgis/python/plugins') |
13 |
|
14 |
# disable QGIS debug messages
|
15 |
os.environ['QGIS_DEBUG'] = '-1' |
16 |
|
17 |
# import QGIS modules
|
18 |
from qgis.core import * |
19 |
from qgis.gui import * |
20 |
|
21 |
# configure QGIS paths
|
22 |
QgsApplication.setPrefixPath(qgisprefix, True)
|
23 |
|
24 |
# initalise QGIS
|
25 |
QgsApplication.initQgis() |
26 |
|
27 |
# create a new application object
|
28 |
# without this importing processing causes the following error:
|
29 |
# QPixmap: Must construct a QApplication before a QPaintDevice
|
30 |
app = QgsApplication([], True)
|
31 |
|
32 |
# import PyQt4
|
33 |
from PyQt4 import QtCore, QtGui |
34 |
|
35 |
# import processing module
|
36 |
import processing |
37 |
|
38 |
# dummy iface
|
39 |
class DummyInterface(object): |
40 |
def __init__(self): |
41 |
self.destCrs = None |
42 |
def __getattr__(self, *args, **kwargs): |
43 |
def dummy(*args, **kwargs): |
44 |
return DummyInterface()
|
45 |
return dummy
|
46 |
def __iter__(self): |
47 |
return self |
48 |
def next(self): |
49 |
raise StopIteration |
50 |
def layers(self): |
51 |
# simulate iface.legendInterface().layers()
|
52 |
return QgsMapLayerRegistry.instance().mapLayers().values()
|
53 |
iface = DummyInterface() |
54 |
|
55 |
# initalise processing plugin with dummy iface object
|
56 |
plugin = processing.classFactory(iface) |
57 |
|
58 |
# create a memory layer and add a point to it
|
59 |
layer = QgsVectorLayer("Point?crs=EPSG:27700", "temporary_points", "memory") |
60 |
feature = QgsFeature() |
61 |
feature.setGeometry(QgsGeometry.fromPoint(QgsPoint(42, 42))) |
62 |
layer.dataProvider().addFeatures([feature]) |
63 |
layer.updateExtents() |
64 |
|
65 |
print 'source:', layer.source() |
66 |
print 'isValid:', layer.isValid() |
67 |
|
68 |
# add the layer to the map layer registry
|
69 |
# required for memory layers which can't be accessed by uri alone
|
70 |
QgsMapLayerRegistry.instance().addMapLayer(layer) |
71 |
|
72 |
# run the 'fixed distance buffer' algorithm, distance=1, segments=100
|
73 |
ret = processing.runalg('qgis:fixeddistancebuffer', layer, 1, 100, False, None) |
74 |
|
75 |
# load the result
|
76 |
output = processing.load(ret['OUTPUT'])
|
77 |
|
78 |
print 'output:', ret['OUTPUT'] |
79 |
print 'isValid:', output.isValid() |
80 |
print 'features:', output.dataProvider().featureCount() |
81 |
|
82 |
# calculate the total area of the features
|
83 |
total_area = 0
|
84 |
for feature in output.getFeatures(): |
85 |
total_area += feature.geometry().area() |
86 |
print 'total area (should approximate pi):', total_area |
87 |
|
88 |
# exit QGIS gracefully
|
89 |
QgsApplication.exitQgis() |