Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python “如何修复错误”_core.QgsProcessingException:执行算法时出错。”;_Python_Python 3.x_Qgis - Fatal编程技术网

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

Python “如何修复错误”_core.QgsProcessingException:执行算法时出错。”;,python,python-3.x,qgis,Python,Python 3.x,Qgis,我试图在一个循环中获得两个层之间的重叠特征。这在大多数情况下都很好,但在某些情况下会引发异常。异常回溯甚至没有给出异常的任何提示 代码: 下面是stacktrace: 回溯(最近一次调用上次):文件 “C:/Users/sn43673/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\ad\u QGIS\shapefile\u validator.py”, 第440行,在errorInShapeFile中 '谓词':[5]文件“

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

代码:

下面是stacktrace:

回溯(最近一次调用上次):文件 “C:/Users/sn43673/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\ad\u QGIS\shapefile\u validator.py”, 第440行,在errorInShapeFile中 '谓词':[5]文件“C:/PROGRA~1/QGIS3~1.6/apps/qgis//python/plugins\processing\tools\general.py”, 第105行,运行中 返回处理.runAlgorithm(algOrName、参数、onFinish、反馈、上下文)文件 “C:/PROGRA~1/QGIS3~1.6/apps/qgis//python/plugins\processing\core\processing.py”, 第183行,运行算法 引发QgsProcessingException(msg) _core.QgsProcessingException:执行算法时出错


此错误是由于图层1和图层2中要素的几何图形无效造成的。我验证了所有特征的几何结构,并了解了这一点。该算法现在运行良好。

您是否能够共享验证几何体中所有特征的代码?更好的是,是否有一种Pythonic方法将QgsProject.instance()的处理设置设置为“忽略无效输入功能”选项,就像直接使用QGIS 3.x一样?我正在搜索QgsProcessing和QgsProject类,但没有发现任何忽略无效输入功能的方法。。
                    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()