如何修复错误“_core.QgsProcessingException:执行算法时出错。”

How to fix the error "_core.QgsProcessingException: There were errors executing the algorithm."

我试图在循环中获取两层之间的重叠特征。这在大多数情况下工作正常,但在某些情况下会引发异常。异常回溯甚至没有给出异常的任何提示。

代码:

                    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()

下面是堆栈跟踪:

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.

此错误是由于图层 layer1 和 layer2 中的要素几何无效。我验证了特征的所有几何形状并了解了这一点。该算法现在工作正常。