5

I am trying to get the overlapping features between two layers in a loop. This works fine in most cases but raises an exception in some cases. The exception traceback does not even give any hint what the exception is about.

Code:

feature1_layers_list = []
feature2_layers_list = []

layer1 = QgsVectorLayer('Polygon?crs=epsg:4326', 'Layer1', 'memory')
prov = layer1.dataProvider()
prov.addAttributes([QgsField("flash_feat_id", QVariant.Int)])
prov.addAttributes([QgsField("lane_type", QVariant.String)])
prov.addAttributes([QgsField("center_line_Id", QVariant.String)])
layer1.updateFields()

for feature1_centerline in feature1_all_center_lines:
    feature1_Lane_Polygon, lanetype1, center_line_id1 = self.get_lane_border(feature1_centerline,
                                                                      self.laneCenterline_layer,
                                                                      centerlineFeatures,
                                                                      laneBorder_layer,
                                                                      laneBorderFeatures)

    if feature1_Lane_Polygon != 0 and lanetype1 != 0:
        fet = QgsFeature()
        fields = layer1.fields()
        fet.setFields(fields, True)
        fet.setGeometry(QgsGeometry.fromWkt(feature1_Lane_Polygon.wkt))
        fet['flash_feat_id'] = int(center_line_id1)
        fet['lane_type'] = str(lanetype1)
        fet['center_line_Id'] = str(feature1_centerline)
        feature1_layers_list.append(fet)

prov.addFeatures(feature1_layers_list)
layer1.updateExtents()

layer2 = QgsVectorLayer('Polygon?crs=epsg:4326', 'Layer2', 'memory')
prov2 = layer2.dataProvider()
prov2.addAttributes([QgsField("flash_feat_id", QVariant.Int)])
prov2.addAttributes([QgsField("lane_type", QVariant.String)])
prov2.addAttributes([QgsField("center_line_Id", QVariant.String)])
layer2.updateFields()

# _writer1 = QgsVectorFileWriter.writeAsVectorFormat(layer1,
#                                                    r"C:/QGIS_ShapeFile/output/layer1.shp",
#                                                   "utf-8", driverName="ESRI Shapefile")
# _writer2 = QgsVectorFileWriter.writeAsVectorFormat(layer2,
#                                                   r"C:/QGIS_ShapeFile/output/layer2.shp",
#                                                   "utf-8", driverName="ESRI Shapefile")

for feature2_centerline in feature2_all_center_lines:
    feature2_Lane_Polygon, lanetype2, center_line_id2 = self.get_lane_border(feature2_centerline,
                                                                      self.laneCenterline_layer,
                                                                      centerlineFeatures,
                                                                      laneBorder_layer,
                                                                      laneBorderFeatures)
    if feature2_Lane_Polygon != 0 and lanetype2 != 0:
        feat = QgsFeature()
        fields2 = layer2.fields()
        feat.setFields(fields2, True)
        feat.setGeometry(QgsGeometry.fromWkt(feature2_Lane_Polygon.wkt))
        feat['flash_feat_id'] = int(center_line_id2)
        feat['lane_type'] = str(lanetype2)
        feat['center_line_Id'] = str(feature2_centerline)
        feature2_layers_list.append(feat)

prov2.addFeatures(feature2_layers_list)
layer2.updateExtents()

uuid1 = uuid.uuid4()
uuid2 = uuid.uuid4()
layer3_path = 'C:/QGIS_ShapeFile/output/layers/layer_' + str(uuid1) + '.shp'
layer4_path = 'C:/QGIS_ShapeFile/output/layers/layer_' + str(uuid2) + '.shp'

try:
    algorithmOutput1 = processing.run(
        "qgis:extractbylocation",
        {
            'INPUT': layer1,
            'INTERSECT': layer2,
            'OUTPUT': layer3_path,
            'PREDICATE': [5]
        }
    )
    algorithmOutput2 = processing.run(
        "qgis:extractbylocation",
        {
            'INPUT': layer2,
            'INTERSECT': layer1,
            'OUTPUT': layer4_path,
            'PREDICATE': [5]
        }
    )

    layer3 = QgsVectorLayer(layer3_path, "layer3", "ogr")
    layer4 = QgsVectorLayer(layer4_path, "layer4", "ogr")
    self.get_overlap_pair(layer3, layer4)

except Exception as e:
    traceback.print_exc()

And below is the stacktrace:

Traceback (most recent call last): File "C:/Users/sn43673/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\ad_qgis\shapefile_validator.py", line 440, in errorInShapeFile 'PREDICATE': [5] File "C:/PROGRA~1/QGIS3~1.6/apps/qgis/./python/plugins\processing\tools\general.py", line 105, in run return Processing.runAlgorithm(algOrName, parameters, onFinish, feedback, context) File "C:/PROGRA~1/QGIS3~1.6/apps/qgis/./python/plugins\processing\core\Processing.py", line 183, in runAlgorithm raise QgsProcessingException(msg) _core.QgsProcessingException: There were errors executing the algorithm.

5
  • Why don't you check line 440 in the file shapefile_validator.py for errorInShapeFile 'PREDICATE': [5] File, as the traceback suggests? Looks like the shapefile you are using has an error (maybe the field 'PREDICATE')?
    – root676
    Commented Sep 13, 2019 at 8:53
  • @root676 But the code is running fine in most cases and 'PREDICATE': [5] is used for overlapping. I have checked it with processing.algorithmHelp("qgis:extractbylocation") Commented Sep 13, 2019 at 9:09
  • Does the message log panel give any clues?
    – ndawson
    Commented Sep 13, 2019 at 20:58
  • @ndawson No, the message just printed is "There were errors executing the algorithm." Commented Sep 16, 2019 at 9:02
  • The processing algorithm works fine for some of the layers, but it fails for some layers. The layers are valid and the geometry is also valid. Now there is nothing in my mind I think I am making any mistake of. I really do not have any clue. Commented Sep 16, 2019 at 9:21

1 Answer 1

1

This error was due to the wrong geometry of features in the layers layer1 and layer2. I validated all the geometry of the features and came to know that. The algorithm works fine now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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