9

My code in the standalone program

import sys

sys.path.append("C:/OSGeo4W64/apps/qgis-ltr/python/plugins")

from qgis.core import *

from PyQt4.QtCore import *
from PyQt4.QtGui import *

import processing
from processing.core.Processing import Processing

QgsApplication.setPrefixPath("C:/OSGeo4W64/apps/qgis-ltr", True)
qgs = QgsApplication([], True)
qgs.initQgis()

Processing.initialize()
Processing.updateAlgsList()

# Here on
shp1 = QgsVectorLayer("C:/Users/Documents/shape.shp", "shp1", "ogr")

processing.runalg('qgis:fieldcalculator', shp1, "new_field", 0, 1, 2, True, "$Area", "C:/Users/Documents/shape2.shp")

qgs.exitQgis()

The exact same code after the label # Here on above works when using the Python console within QGIS. Whereas for the standalone program I get the field names but no values. I'm using version 2.18.17. Is this a bug with the software or am I doing something wrong?

Bug report submitted at: https://issues.qgis.org/issues/18395

7
  • 1
    Seems like a bug, I can also replicate this. The same thing occurs when also using qgis:advancedpythonfieldcalculator. You should consider submitting a bug report :) – Joseph Mar 8 '18 at 11:28
  • 1
    @Joseph: Thanks! I have submitted a bug report on redmine so hopefully this gets fixed soon. – absolutelydevastated Mar 9 '18 at 0:50
  • I think this answer can be useful – xlDias Mar 13 '18 at 20:37
  • @xlDias: Not really. That may work but is unnecessarily lengthy (requiring one to manually iterate through every single polygon) whereas using the field calculator needs only a one-liner. My issue is also that my code works within the Python console in QGIS Desktop but does not work in a command line environment. – absolutelydevastated Mar 15 '18 at 5:23
  • Does the processing return or print any error message? some algorithms allows the use of a progress object, then the processing can send any problem that it haves through this object. I wrote about it in here, maybe you discover something – xlDias Mar 15 '18 at 13:18