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
‘inblast = QgsField(‘myattr’, QVariant.Int )andidx = moment_all.fieldNameIndex(‘myattr'). Use'myattr'or"myattr"and not‘myattr‘– Oto Kaláb Jul 18 '17 at 19:38processing.runalg(“qgis:fieldcalculator”, “/Users/pgcseismolab/Desktop/qgis/moment_all.shp”, “blast”, 0, 10, 3, False, ““blast” = ‘b’”, None)
– jrowley Jul 18 '17 at 20:27'"blast"='b''instead""blast"='b'", if this help you I'll add this as an answer – Oto Kaláb Jul 18 '17 at 21:01