我正在尝试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")
如果需要的话,我会..只是更轻松,更清洁地在内存中处理此类图层。如果您确定可以预期,请将其发布为答案,我可以接受。
—
Oystein
import tempfile
,请输入tempfile.gettempdir
)。无论如何,这就是qgis处理的工作方式