简体   繁体   English

使用Python将csv导入QGIS

[英]Import csv into QGIS using Python

I am attempting to import a file into QGIS using a python script. 我正在尝试使用python脚本将文件导入QGIS。 I'm having a problem getting it to accept the CRS. 我在接受CRS时遇到了问题。 Code so far 代码到目前为止

from PyQt4.QtGui import * from PyQt4.QtCore import * from qgis.core import * from qgis.utils import iface 来自PyQt4.QtGui import * from PyQt4.QtCore import * from qgis.core import * from qgis.utils import iface

----1 Set file name here ---- 1在此处设置文件名

InFlnm='Input.CSV' InFlnm = 'Input.CSV'

---2 Set pathname here --- 2在此处设置路径名

InDrPth='G:/test' InDrPth = 'G:/测试'

---3 Build the file name and path for uri --- 3为uri构建文件名和路径

InFlPth="file:///"+InDrPth+InFlnm InFlPth = “文件:///” + InDrPth + InFlnm

---4 Set import Sting here note only need to set x and y others come for free! --- 4设置导入Sting这里注意只需要设置x和y其他人免费!

uri = InFlPth+"?delimiter=%s&xField=%s&yField=%s" % (",","x","y") uri = InFlPth +“?delimiter =%s&xField =%s&yField =%s”%(“,”,“x”,“y”)

---5 Load the points into a layer --- 5将点加载到图层中

bh = QgsVectorLayer(uri, InFlnm, "delimitedtext") bh = QgsVectorLayer(uri,InFlnm,“delimitedtext”)

---6 Set the CRS (Not sure if this is working seems to?) --- 6设置CRS(不确定这是否正常工作?)

bh.setCrs(QgsCoordinateReferenceSystem(32365, QgsCoordinateReferenceSystem.EpsgCrsId) bh.setCrs(QgsCoordinateReferenceSystem(32365,QgsCoordinateReferenceSystem.EpsgCrsId)

---7 Display the layer in QGIS (Here I get a syntax error?) --- 7在QGIS中显示图层(这里我得到语法错误?)

QgsMapLayerRegistry.instance().addMapLayer(bh) QgsMapLayerRegistry.instance()。addMapLayer(BH)

Now all the above works OK and QGIC prompts me for a CRS before executing the last line of the script to display the layer - as long as I comment-out step 6 现在以上所有工作都正常,QGIC在执行脚本的最后一行显示图层之前提示我输入CRS - 只要我注释掉第6步

However, if a attempt to set the CRS removing ### from step 6 I get a syntax error reporting on the last line that displays the points (Step 7). 但是,如果尝试从步骤6中设置CRS删除###,则会在显示这些点的最后一行上收到语法错误报告(步骤7)。 Note sure what the trick is here - I'm pretty new to Python but know my way around some other programming lagnuages 请注意这里的技巧是什么 - 我对Python很陌生,但我知道我对其他一些编程语言的了解

I found the answer to the final part of the problem at http://www.purplelinux.co.nz/ . 我在http://www.purplelinux.co.nz/找到了问题最后部分的答案。 I seems you need to suppress the form that prompts for the CRS. 我似乎需要抑制提示CRS的表单。 So the my script now looks like 所以我的脚本现在看起来像

#--- 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 )

The imported points now display but I'm getting an error stating that the CRS is not recognised so suspect step 9 above is not working. 现在显示导入的点,但我收到一条错误,指出CRS无法识别,因此怀疑上面的步骤9无法正常工作。 I will post again if I can crack this problem otherwise I may have to be happy with the CRS default perhaps. 如果我可以解决这个问题,我会再次发布,否则我可能不得不对CRS默认值感到满意。

Thanks for the import example, really helpful for a geocoder python script I want to output a csv from into qgis. 感谢导入示例,对于geocoder python脚本非常有用,我想将csv输出到qgis中。 To solve your problem, add the crs in your uri line: 要解决您的问题,请在您的uri行中添加crs:

uri = InFlPth+"?crs=epsg:32365&delimiter=%s&xField=%s&yField=%s" % (",","x","y")    

在你的-6行代码的末尾有一个括号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM