在QGIS处理/ SEXTANTE中使用内存矢量层


10

我正在尝试qgis:clip从控制台运行算法,但是在使用内存层作为overlay参数时遇到错误。这是意料之中的,还是我做错了什么?

码:

mem_layer = QgsVectorLayer("Polygon?crs=epsg:4326", "temp_layer", "memory")
if not mem_layer.isValid(): raise Exception("Failed to create memory layer")            
mem_layer_provider = mem_layer.dataProvider()

clip_polygon = QgsFeature()
clip_polygon.setGeometry(QgsGeometry.fromRect( 
    QgsRectangle(
        self.output_layer.extent().xMinimum() + 10,
        self.output_layer.extent().yMinimum() + 10,
        self.output_layer.extent().xMaximum() - 10,
        self.output_layer.extent().yMaximum() - 10
    )
))
mem_layer_provider.addFeatures([clip_polygon])
mem_layer.updateExtents()

output = self.output_layer_path + "2"
processing.runalg("qgis:clip", layer, mem_layer, output) # Fails

在上面的代码,self.output_layer并且layer是载体层对象(QgsVectorLayer -正确的,从在磁盘上shape文件加载),self.output_layer_path与路径的Python字符串对象。

这是我得到的错误:

"C:/OSGEO4~1/apps/qgis/./python/plugins\processing\core\GeoAlgorithm.py", line 150, in     
    execute self.processAlgorithm(progress)
File "C:/OSGEO4~1/apps/qgis/./python/plugins\processing\algs\ftools\Clip.py", line 72, 
    in processAlgorithm index = utils.createSpatialIndex(layerB)
File "C:/OSGEO4~1/apps/qgis/./python/plugins\processing\algs\ftools\FToolsUtils.py", 
    line 31, in createSpatialIndex features = QGisLayers.features(layer)
File "C:/OSGEO4~1/apps/qgis/./python/plugins\processing\core\QGisLayers.py", line 211, 
    in features return Features(layer)
File "C:/OSGEO4~1/apps/qgis/./python/plugins\processing\core\QGisLayers.py", line 218, 
    in __init__ self.iter = layer.getFeatures()
AttributeError: 'NoneType' object has no attribute 'getFeatures'

如果我将处理调用更改为以下内容,则它会正常运行:

processing.runalg("qgis:clip", layer, self.output_layer, output) # Runs fine

另外,如果有帮助,这是失败的算法,因为它记录在processing_qgis.log中:

processing.runalg("qgis:clip","C:/path/to/shapefile.shp|layerid=0|subset=CONTINENT = 
    'Europe'","Polygon?crs=epsg:4326","C:/path/to/output")

1
可能由于工具需要解决硬盘驱动器上的物理层而引起了人们的期待。只是一个想法,但为什么不暂时尝试将图层保存到临时文件中(如果需要import tempfile,请输入tempfile.gettempdir)。无论如何,这就是qgis处理的工作方式
Curlew

如果需要的话,我会..只是更轻松,更清洁地在内存中处理此类图层。如果您确定可以预期,请将其发布为答案,我可以接受。
Oystein

Answers:


12

事实证明,只要在使用存储层之前将其添加到目录中,此方法就可以正常工作。似乎dataobjects.getObjectFromUriQGIS源代码中的函数无法处理它。

因此,以下方法非常有效:

QgsMapLayerRegistry.instance().addMapLayer(mem_layer)
processing.runalg("qgis:clip", layer, mem_layer, output)

另请参阅我最近的问题,即如何将存储层用作处理功能的输出(基本上使用processing.runandload代替processing.runalg)。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.