2

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.

underdark
84.9k22 gold badges237 silver badges418 bronze badges
asked Jan 17, 2019 at 14:52

2 Answers 2

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)
answered Jan 21, 2019 at 8:55
0
1

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

answered Jan 17, 2019 at 15:52
5
  • If you're not sure or can not provide further information, why your solution should work, rather post a comment than an answer ;-) Commented Jan 17, 2019 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' Commented Jan 18, 2019 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 ... Commented Jan 18, 2019 at 10:40
  • So ? Are you still stuck on your problem ? Commented Jan 20, 2019 at 14:42
  • Hi, thanks for the reply - the bug was with the dissolve arguement - Everything seems to work fine now! Cheers! Commented Jan 21, 2019 at 8:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.