Tôi đang cố chạy qgis:clipthuật toán từ bảng điều khiển, nhưng gặp lỗi khi sử dụng lớp trong bộ nhớ làm tham số lớp phủ. Đây có phải là dự kiến, hoặc tôi đang làm gì đó sai?
Mã số:
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
Trong đoạn mã trên self.output_layervà layerlà các đối tượng lớp vectơ (QssVectorLayer - những đối tượng thích hợp, được tải từ shapefiles trên đĩa), self.output_layer_pathlà một đối tượng chuỗi python có đường dẫn.
Đây là lỗi tôi nhận được:
"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'
Nếu tôi thay đổi cuộc gọi xử lý của mình thành như sau, nó sẽ chạy mà không gặp lỗi:
processing.runalg("qgis:clip", layer, self.output_layer, output) # Runs fine
Ngoài ra, nếu có bất kỳ trợ giúp nào, đây là thuật toán thất bại vì nó được đăng nhập vào process_qgis.log:
processing.runalg("qgis:clip","C:/path/to/shapefile.shp|layerid=0|subset=CONTINENT =
'Europe'","Polygon?crs=epsg:4326","C:/path/to/output")
import tempfilevàtempfile.gettempdir). Đây là cách xử lý qgis hoạt động bằng mọi cách