qgis – Importing CSV File to Create Point Layer in QGIS with Python

csvpyqgispythonqgis

enter image description hereI have a CSV file with following fields:

  • Nodename
  • Nodeid
  • Latitude
  • Longtitude
  • Elevation

I am using Python console in QGIS to automate this. How should I write so that point with latitude, longtitude and elevation are maked as point layer?
I have tried with below code, but I am getting error.

uri = "/home/priti/Desktop/MTP work/nodeinput.csv?\
type=csv&xField=Longtitude\
&yField=Lattitude\
&spatialIndex=no&subsetIndex=no&watchFile=no"

vlayer = QgsVectorLayer(uri, 'Nodes', "delimitedtext")

Best Answer

Try:

from qgis.core import QgsMapLayerRegistry #Qgis2
#from qgis.core import QgsProject #QGIS3

uri = "file:///C:/Test/points.csv?delimiter=,&crs=epsg:4326&xField=Longitude&yField=Latitude"
vlayer = QgsVectorLayer(uri,'Points','delimitedtext')

QgsMapLayerRegistry.instance().addMapLayer(vlayer) #Qgis2
#QgsProject.instance().addMapLayer(vlayer) #QGIS3

enter image description here