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

时间:2019-09-13 10:20:28

标签: python python-3.x qgis

我试图循环获得两层之间的重叠特征。在大多数情况下,这可以正常工作,但在某些情况下,会引发例外情况。异常回溯甚至都没有提供任何有关异常的提示。

代码:

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

下面是堆栈跟踪:

  

回溯(最近通话最近):文件   “ C:/ Users / sn43673 / AppData / Roaming / QGIS / QGIS3 \ profiles \ default / python / plugins \ ad_qgis \ shapefile_validator.py”,   440行,在errorInShapeFile中       'PREDICATE':[5]文件“ C:/PROGRA~1/QGIS3~1.6/apps/qgis /./ python / plugins \ processing \ tools \ general.py”,   105行,运行中       返回Processing.runAlgorithm(algOrName,参数,onFinish,反馈,上下文)文件   “ C:/PROGRA~1/QGIS3~1.6/apps/qgis /./ python / plugins \ processing \ core \ Processing.py”,   第183行,在runAlgorithm中       引发QgsProcessingException(msg)   _core.QgsProcessingException:执行算法时出错。

1 个答案:

答案 0 :(得分:2)

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