带有插件“从电子表格创建点层”的Python代码

时间:2019-07-02 13:13:06

标签: python-3.x spreadsheet spatial qgis

我是python的新手,这是第一个问题。 其次,我正在尝试自动化使用Python从电子表格(xlsx文件)中添加矢量点图层的任务。 可以使用插件“添加电子表格图层”手动完成此任务。 我有一个大约有20个xlsx文件的文件夹,需要将其作为矢量点图层添加到QGIS项目中。 我尝试了以下代码片段,以检查添加电子表格图层的核心任务是否确实有效:

计算机具有Win7操作系统。问题程序是Python,包含在程序QGIS 3.4中。 我要通过python控制的插件称为“添加电子表格层”。

from qgis.core import *
import processing
processing.run("qgis:createpointslayerfromtable",
{'INPUT':r'C:\Users\Desktop\PlayItAll\Test.xlsx',
'XFIELD':'X_Pos',
'YFIELD':'Y_Pos',
'ZFIELD':None,
'MFIELD':None,
'TARGET_CRS':QgsCoordinateReferenceSystem('EPSG:4326'),
'OUTPUT':r'memory'})

它会产生此错误:

File "C:/PROGRA1/QGIS31.4/apps/qgis/./python/plugins\processing\core\Processing.py", line 183, in runAlgorithm
raise QgsProcessingException(msg)

我已经联系了插件的程序员,他给了我这段代码来尝试:

import processing
processing.runAndLoadResults("qgis:createpointslayerfromtable",
{
    'INPUT':r'C:\Users\username\Desktop\Delete\test.xlsx',
    'XFIELD':'Longitude',
    'YFIELD':'Latitude',
    'ZFIELD':None,               
    'MFIELD':None,
    'TARGET_CRS':QgsCoordinateReferenceSystem('EPSG:4326'),
    'OUTPUT':'memory'
})

对他来说,它奏效了,对我而言,它没有奏效。 我在“处理”标签上得到了这个

2019-07-03T13:19:43     CRITICAL    Traceback (most recent call last):
              File "C:/PROGRA~1/QGIS3~1.4/apps/qgis/./python/plugins\processing\algs\qgis\PointsLayerFromTable.py", line 112, in processAlgorithm
              fields, wkb_type, target_crs)
             Exception: unknown


2019-07-03T13:19:43     CRITICAL    Traceback (most recent call last):
              File "C:/PROGRA~1/QGIS3~1.4/apps/qgis/./python/plugins\processing\algs\qgis\PointsLayerFromTable.py", line 112, in processAlgorithm
              fields, wkb_type, target_crs)
             Exception: unknown

2019-07-03T13:19:43     CRITICAL     There were errors executing the algorithm.

“ python警告”标签显示如下:

2019-07-03T13:19:43     WARNING    warning:__console__:1: ResourceWarning:

             unclosed file


             traceback: File "C:/PROGRA~1/QGIS3~1.4/apps/qgis/./python\console\console.py", line 575, in runScriptEditor
              self.tabEditorWidget.currentWidget().newEditor.runScriptCode()
              File "C:/PROGRA~1/QGIS3~1.4/apps/qgis/./python\console\console_editor.py", line 629, in runScriptCode
              .format(filename.replace("\\", "/"), sys.getfilesystemencoding()))
              File "C:/PROGRA~1/QGIS3~1.4/apps/qgis/./python\console\console_sci.py", line 635, in runCommand
              more = self.runsource(src)
              File "C:/PROGRA~1/QGIS3~1.4/apps/qgis/./python\console\console_sci.py", line 665, in runsource
              return super(ShellScintilla, self).runsource(source, filename, symbol)
              File "C:\PROGRA~1\QGIS3~1.4\apps\Python37\lib\code.py", line 74, in runsource
              self.runcode(code)
              File "C:\PROGRA~1\QGIS3~1.4\apps\Python37\lib\code.py", line 90, in runcode
              exec(code, self.locals)
              File "", line 1, in 

0 个答案:

没有答案