I am trying to add and calculate a field for earthquakes selected within blast zones using the guidelines in this answer Is it possible to programmatically add calculated fields?. Can anyone tell me what I am doing wrong? Here is my code:
import processing
processing.runalg("qgis:selectbylocation",
"/Users/pgcseismolab/Desktop/qgis/moment_all.shp",
"/Users/pgcseismolab/Desktop/qgis/all_buffers.shp", ['within'], 0,0)
from PyQt4.QtCore import QVariant
from qgis.core import QgsField, QgsExpression, QgsFeature
moment_all = iface.activeLayer()
moment_all.startEditing()
blast = QgsField(‘myattr’, QVariant.Int )
moment_all.addAttribute(blast)
idx = moment_all.fieldNameIndex(‘myattr')
moment_all.commitChanges()
processing.runalg("qgis:fieldcalculator",
"/Users/pgcseismolab/Desktop/qgis/moment_all.shp", "blast", 0, 10, 3,
False, ""blast" = ‘b’", None)
I used
execfile(r'/Users/pgcseismolab/Desktop/qgis/blast.py')
and my output error was:
Python Console
Use iface to access QGIS API interface or Type help(iface) for more info
execfile(r'/Users/pgcseismolab/Desktop/qgis/blast.py')
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/Users/pgcseismolab/Desktop/qgis/blast.py", line 19
blast = QgsField( âmyattrâ, QVariant.Int )
SyntaxError: invalid syntax
1 Answer 1
There is a typo/misuse of quotation marks ‘
and "
for strings in (notice the syntax highlighting):
blast = QgsField(‘myattr’, QVariant.Int )
#and
idx = moment_all.fieldNameIndex(‘myattr')
Use 'myattr'
or "myattr"
and not ‘myattr‘
(actually this gives you âmyattrâ
in error)
Also, in the last line Use "
and not "
, and change the processing.runalg
expression argument. You have to use tripple quotes, because of nested quotes (notice the syntax highlighting):
processing.runalg("qgis:fieldcalculator", "/Users/pgcseismolab/Desktop/qgis/moment_all.shp", "blast", 0, 10, 3, False, ""blast" = ‘b’", None)
Expression argument:
#wrong:
""blast"='b'"
#wrong:
'"blast"='b''
#correct:
'''"blast"='b\''''
You can check this with printing in prompt:
>>> print(""blast"='b'")
File "<stdin>", line 1
print(""blast"='b'")
^
SyntaxError: invalid syntax
>>> print('"blast"='b'')
"blast"=
>>> print('''"blast"='b\'''')
"blast"='b'
The last one is the correct string expression you want. The code than should look like this:
processing.runalg("qgis:fieldcalculator", "/Users/pgcseismolab/Desktop/qgis/moment_all.shp", "blast", 0, 10, 3, False, '''"blast" = 'b\'''', None)
-
This resolved the first issue, however I am still having issues with the second. The syntax should be correct with the third option noted but I'm still receiving the following: processing.runalg("qgis:fieldcalculator", "/Users/pgcseismolab/Desktop/qgis/moment_all.shp", "blast", 0, 10, 3, False, '''"blast"='b\'''', None) File "<input>", line 1 processing.runalg(âqgis:fieldcalculatorâ, â/Users/pgcseismolab/Desktop/qgis/moment_all.shpâ, âblastâ, 0, 10, 3, False, '''"blast"='b\'''', None) ^ SyntaxError: invalid syntaxjrowley– jrowley2017年07月18日 23:15:09 +00:00Commented Jul 18, 2017 at 23:15
Explore related questions
See similar questions with these tags.
‘
inblast = QgsField(‘myattr’, QVariant.Int )
andidx = moment_all.fieldNameIndex(‘myattr')
. Use'myattr'
or"myattr"
and not‘myattr‘
'"blast"='b''
instead""blast"='b'"
, if this help you I'll add this as an answer