2

In QGIS 3.4 I can run algorithms under the General setting "Invalid features filtering" -> "Ignore features with invalid geometries" However, when I try Processing through Python functions like the qgis:fieldcalculator it just breaks. Is there a way to make my QGIS python interpreter recognize that I do not care for invalid geometries?

Here is an example of a working code with valid geometries which breaks when invalid :

import os
import sys
import gdal
gdal.UseExceptions()

from qgis.core import QgsApplication
from qgis.analysis import QgsNativeAlgorithms

QgsApplication.setPrefixPath(os.path.join("C:", os.sep, "OSGeo4W64", "apps", "qgis"), True)
qgs = QgsApplication([], False)
qgs.initQgis()

sys.path.append(os.path.join("C:", os.sep, "OSGeo4W64", "apps", "qgis", "python", "plugins"))
import processing
from processing.core.Processing import Processing

Processing.initialize()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())


def add_area(input_vector):
    out = "pathtooutput.shp"
    params = {
                'INPUT': input_vector,
                'FIELD_NAME': 'area',
                'FIELD_TYPE': 0,
                'FIELD_LENGTH': 10,
                'FIELD_PRECISION': 3,
                'NEW_FIELD': True,
                'FORMULA': ' $area ',
                'OUTPUT': out
                }
    res = processing.run("qgis:fieldcalculator", params)
    print(res)
    return out

inp = "path_to_testvector_with_invalid_or_valid_geometries_for_test.shp"

add_area(inp)
  • 3
    Can you post an example of the code you've tried? – ndawson Jan 2 at 17:14
  • It is not necessary, I try the same at QGIS with the "Invalid features filtering" -> "Ignore features with invalid geometries" and it works. I try "Invalid features filtering" -> "Stop algorithm execution when a geometry is invalid" and it finds invalid geometries, thus it stops.I know I have invalid geometries, so Python breaks QGIS Processing when it finds them, while when removed through side ways, it outputs properly. – ODstuck Jan 2 at 21:08
  • The question is not a matter of code, but if in this python framework of QGIS there is an option that could be set somewhere – ODstuck Jan 2 at 21:10
  • 1
    There is, but we'd need to see your current code to post the solution. – ndawson Jan 2 at 23:10
  • There you go, hope it helps a solution and anyone trying python with qgis – ODstuck Jan 3 at 15:13
4

Try changing the part:

params = {
            'INPUT': input_vector,
            'FIELD_NAME': 'area',
            'FIELD_TYPE': 0,
            'FIELD_LENGTH': 10,
            'FIELD_PRECISION': 3,
            'NEW_FIELD': True,
            'FORMULA': ' $area ',
            'OUTPUT': out
            }
res = processing.run("qgis:fieldcalculator", params)

to

params = {
            'INPUT': input_vector,
            'FIELD_NAME': 'area',
            'FIELD_TYPE': 0,
            'FIELD_LENGTH': 10,
            'FIELD_PRECISION': 3,
            'NEW_FIELD': True,
            'FORMULA': ' $area ',
            'OUTPUT': out
            }
context = dataobjects.createContext()
context.setInvalidGeometryCheck(QgsFeatureRequest.GeometryNoCheck)
res = processing.run("qgis:fieldcalculator", params, context=context)

You'll also need to add an import:

from processing.tools import dataobjects
  • 1
    It also needs the addition of "from qgis.core import QgsFeatureRequest", for the completeness of the answer – ODstuck Feb 1 at 18:28
  • Apart from that, it works like a charm. Even adds the area. Attention to "context" param in processing call. Thanks a lot @ndawson – ODstuck Feb 1 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.