1

First off, i'm trying to learn some python to automate scripts for basic tasks i carry out in QGIS (2.18).

Below I create a virtual layer (polyline) using the following script:

from qgis.core import QgsVectorLayer, QgsMapLayerRegistry
from qgis.utils import iface
from qgis.core import *
from qgis.utils import *
from qgis.gui import * 

vlayer = QgsVectorLayer("?query=SELECT cm_code, cm_ndcode1 as 'nd_1', cm_ndcode2 as 'nd_2', COUNT() as 'Num CM', geometry FROM t_cheminement GROUP BY   cm_ndcode1, cm_ndcode2 HAVING   COUNT() > 1" , "CM_DUBLONS", "virtual" )

props = { 'width' : '3', 'color' : '255,0,0' } sl=QgsSymbolLayerV2Registry.instance().symbolLayerMetadata("SimpleLine").createSymbolLayer(props) s = QgsLineSymbolV2([sl])

vlayer.setRendererV2(QgsSingleSymbolRendererV2( s ))
QgsMapLayerRegistry.instance().addMapLayer(vlayer)

The above code works fine - but next I want to create a fixed distance buffer around the polylines.

I started off with this:

qgis_fixeddistancebuffer(input = CM_DUBLONS, output = 'c:/SIG_DEV_MODEL/buffer_test.shp', distance = 1000)

But it returns the error message below:

Traceback (most recent call last): File "< input >", line 1, in NameError: name 'qgis_fixeddistancebuffer' is not defined

I've also tried

processing.runalg('qgis:fixeddistancebuffer', CM_DUBLONS, 500, 6, dissolve, "C:/SIG/SIG_DEV_MODEL/CM_buf2.shp")

but likewise I get a similar error message "'processing' is not defined"

I'm probably missing something simple...

Otherwise I've since looked up the following:

Pyqgis Fixed Buffer Creation in Meters with Haversine

and

How to create dissolved buffer layer with pyqgis?

But it's not quite what I'm looking for, and I'm not sure how to adapt the script.

1

Not sure of my answer but ... did u try something like : import processing ?

  • If you're not sure or can not provide further information, why your solution should work, rather post a comment than an answer ;-) – Erik Jan 17 at 16:31
  • Hi thanks for the input, honestly not sure if it worked or not, but I've got a new error message in anycase! 'Processing.runalg('qgis:fixeddistancebuffer', 'CM_DUBLONS', 500, 6, dissolve = false, "C:/SIG/SIG_DEV_MODEL/CM_buf2.shp") File "<input>", line 1 SyntaxError: non-keyword arg after keyword arg' – lhowarth Jan 18 at 7:05
  • i think there must be an issue with your dissolve argument. i tried the following : import processing then processing.runalg('qgis:fixeddistancebuffer', 'cities', 500, 20, 'true', "D:/test.shp") which ran perfectly. Try O/1 or 'true'/'false' that should work ... – snaileater Jan 18 at 10:40
  • So ? Are you still stuck on your problem ? – snaileater Jan 20 at 14:42
  • Hi, thanks for the reply - the bug was with the dissolve arguement - Everything seems to work fine now! Cheers! – lhowarth Jan 21 at 8:51
2

So thanks to snaileater I've managed to sort the script out - the final script is as below:

from qgis.core import *
from qgis.utils import *
from qgis.gui import *
import processing

vlayer = QgsVectorLayer( "?query=SELECT cm_ndcode1 as 'nd_1', cm_ndcode2 as 'nd_2', COUNT() as 'Num CM', geometry FROM t_cheminement GROUP BY cm_ndcode1, cm_ndcode2 HAVING COUNT() > 1" , "CM_DUBLONS", "virtual" )
props = { 'width' : '3', 'color' : '255,0,0' }
sl = QgsSymbolLayerV2Registry.instance().symbolLayerMetadata("SimpleLine").createSymbolLayer(props)
s = QgsLineSymbolV2([sl])
vlayer.setRendererV2( QgsSingleSymbolRendererV2( s ) )
QgsMapLayerRegistry.instance().addMapLayer(vlayer)

processing.runalg('qgis:fixeddistancebuffer', 'CM_DUBLONS', 100, 6, 'true', "C:/XXX/XXXX/XXX/XXXXX/CM_PARA.shp")
QgsMapLayerRegistry.instance().removeMapLayer(vlayer)
layer = QgsVectorLayer("C:/XXX/XXXX/XXX/XXXXX/CM_PARA.shp", "CM_PARA", "ogr")
QgsMapLayerRegistry.instance().addMapLayer(layer)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.