虽然在这个问题上有一些帖子,但其中任何一个都没有答案。这就是为什么我再问一次。如何使用PyQGIS加载栅格图层?
一后我发现了https://gis.stackexchange.com/questions/68032/raster-layer-invalid
我读了下面的链接信息:https://hub.qgis.org/wiki/17/Arcgis_rest。
我使用了命令:gdal_translate“http://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer?f=json&pretty=true”s.xml - of WMS。它成功生成了文件。但是,当我尝试打开文件并假定提供程序是wms时,代码报告图层无效。
我使用的代码是:
file = QFileDialog.getOpenFileName(self,
"Open WMS", ".", "WMS (*.xml)")
fileInfo = QFileInfo(file)
# Add the layer
layer = QgsRasterLayer(file, fileInfo.fileName(),"wms")
if not layer.isValid():
print "Failed to load."
return
我只是选择从对话框中的文件。
我也尝试了其他命令:qgis.utils.iface.addRasterLayer(“http://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer?f=json&pretty=true”,“raster” )通过使用以下代码:
layer = QgsRasterLayer("http://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer?f=json&pretty=true", "layer")
if not layer.isValid():
print "Failed to load."
return
它也报告“加载失败”。原始命令可以在QGIS python命令行中成功运行。另外,如果我尝试在python控制台中输入代码,那么layer.isValid()将返回true。它在独立脚本中不起作用。