[英]Import csv into QGIS using Python
我正在尝试使用python脚本将文件导入QGIS。 我在接受CRS时遇到了问题。 代码到目前为止
来自PyQt4.QtGui import * from PyQt4.QtCore import * from qgis.core import * from qgis.utils import iface
InFlnm = 'Input.CSV'
InDrPth = 'G:/测试'
InFlPth = “文件:///” + InDrPth + InFlnm
uri = InFlPth +“?delimiter =%s&xField =%s&yField =%s”%(“,”,“x”,“y”)
bh = QgsVectorLayer(uri,InFlnm,“delimitedtext”)
QgsMapLayerRegistry.instance()。addMapLayer(BH)
现在以上所有工作都正常,QGIC在执行脚本的最后一行显示图层之前提示我输入CRS - 只要我注释掉第6步
但是,如果尝试从步骤6中设置CRS删除###,则会在显示这些点的最后一行上收到语法错误报告(步骤7)。 请注意这里的技巧是什么 - 我对Python很陌生,但我知道我对其他一些编程语言的了解
我在http://www.purplelinux.co.nz/找到了问题最后部分的答案。 我似乎需要抑制提示CRS的表单。 所以我的脚本现在看起来像
#--- Load a csv file and set CRS
#---1 Reference library
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from qgis.core import *
from qgis.utils import iface
#--- 2 Turn of the CRS dialog box
s = QSettings()
oldValidation = s.value( "/Projections/defaultBehaviour")
s.setValue( "/Projections/defaultBehaviour", "useGlobal" )
#--- 3 Set file name here
InFlnm='Test.csv'
#--- 4 Set pathname here
InDrPth='C:/_Work/PyQGIS/Test/'
#--- 5 Build file name an path for uri
InFlPth="file:///"+InDrPth+InFlnm
#--- 6 Set import Sting here note only need to set x and y other come for free
uri = InFlPth+"?delimiter=%s&xField=%s&yField=%s" % (",","x","y")
#--- 7 Load point layer
bh = QgsVectorLayer(uri, InFlnm, "delimitedtext")
#--- 8 Confirm something is loaded and valid
bh.isValid()
#--- 9 Set CRS
bh.setCrs(QgsCoordinateReferenceSystem(32365, QgsCoordinateReferenceSystem.EpsgCrsId))
#--- 10 Display the layer into QGIS (but it asks for CRS before displaying_
QgsMapLayerRegistry.instance().addMapLayer(bh)
#--- 11 turn CRS dialog box back on again
s.setValue( "/Projections/defaultBehaviour", oldValidation )
现在显示导入的点,但我收到一条错误,指出CRS无法识别,因此怀疑上面的步骤9无法正常工作。 如果我可以解决这个问题,我会再次发布,否则我可能不得不对CRS默认值感到满意。
感谢导入示例,对于geocoder python脚本非常有用,我想将csv输出到qgis中。 要解决您的问题,请在您的uri行中添加crs:
uri = InFlPth+"?crs=epsg:32365&delimiter=%s&xField=%s&yField=%s" % (",","x","y")
在你的-6行代码的末尾有一个括号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.