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

underdark
  • 84,148
  • 21
  • 231
  • 413
  • 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 08 '18 at 11:28
  • 1
    @Joseph: Thanks! I have submitted a bug report on redmine so hopefully this gets fixed soon. – absolutelydevastated Mar 09 '18 at 00: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 05: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
  • @xlDias: Nope, no error messages. However, that's because my command line interface is flooded with warnings as one of my fields is too large to be written (which isn't very important). I'm trying to figure out how to suppress such warnings and I'll see if any error messages pop up after that. Thanks for the help! – absolutelydevastated Mar 16 '18 at 01:23
  • I'm voting to close this question as off-topic because it should be a bug report rather than a question on StackExchange. – underdark Feb 15 '19 at 19:03

0 Answers0