0

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
Oto Kaláb
  • 6,895
  • 3
  • 29
  • 50
jrowley
  • 257
  • 1
  • 8
  • 1
    There is a typo/misuse of quotation marks in blast = QgsField(‘myattr’, QVariant.Int ) and idx = moment_all.fieldNameIndex(‘myattr'). Use 'myattr' or "myattr" and not ‘myattr‘ – Oto Kaláb Jul 18 '17 at 19:38
  • that seems to have fixed the first section, though the last line is now saying "invalid character in Identifier". Any idea which character this would be?

    processing.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
  • File "/Users/pgcseismolab/Desktop/qgis/blast.py", line 24 processing.runalg(âqgis:fieldcalculatorâ, â/Users/pgcseismolab/Desktop/qgis/moment_all.shpâ, âblastâ, 0, 10, 3, False, ââblastâ = âbââ, None) ^ SyntaxError: invalid syntax – jrowley Jul 18 '17 at 20:33
  • try '"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
  • hmm still invalid syntax with this change. I'll update if anything changes – jrowley Jul 18 '17 at 21:07

1 Answers1

4

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)
Oto Kaláb
  • 6,895
  • 3
  • 29
  • 50
  • 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 "", line 1 processing.runalg(âqgis:fieldcalculatorâ, â/Users/pgcseismolab/Desktop/qgis/moment_all.shpâ, âblastâ, 0, 10, 3, False, '''"blast"='b'''', None) ^ SyntaxError: invalid syntax

    – jrowley Jul 18 '17 at 23:15