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
7,0253 gold badges33 silver badges54 bronze badges
asked Jul 18, 2017 at 17:41
6
  • 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‘ Commented Jul 18, 2017 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) Commented Jul 18, 2017 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 Commented Jul 18, 2017 at 20:33
  • try '"blast"='b'' instead ""blast"='b'", if this help you I'll add this as an answer Commented Jul 18, 2017 at 21:01
  • hmm still invalid syntax with this change. I'll update if anything changes Commented Jul 18, 2017 at 21:07

1 Answer 1

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)
answered Jul 18, 2017 at 21:49
1
  • 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 syntax Commented Jul 18, 2017 at 23:15

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.