I am having trouble loading csv files into layers using QgsVectorLayer()
.
I know this looks like a repeat, but I have carefully read
Create a script to add delimited text layer in QGIS
and
http://hub.qgis.org/issues/7491
but still cannot get it work.
An example of my data file, test.csv:
Index,y,x
0,37.7646618,-122.4960858
1,37.777494,-122.416311
2,37.717707,-122.3997056
3,37.732284,-122.498118
4,37.749998,-122.392333
My code:
import os
import urllib
import pathlib
from qgis.core import QgsVectorLayer
local_path = 'C:\dev\FCMS_conflation\data\TMC_nodes.csv'
node_layer = 'test'
abspath = os.path.abspath(local_path)
params = {'delimiter': ',',
'decimalPoint': '.',
'xField': longitude,
'yField': latitude}
uri = "%s?%s" %(pathlib.Path(abspath).as_uri(), urllib.unquote(urllib.urlencode(params)))
layer = QgsVectorLayer(uri, node_layer, "delimitedtext")
The formatted URI looks like:
[] uri
>>> 'file:///C:/dev/FCMS_conflation/data/TMC_nodes.csv?decimalPoint=.&delimiter=,&xField=longitude&yField=latitude'
I have tried many variations of the parameters, including the crs
and type
. But no matter what I try, I cannot create a valid layer.
[] layer.isValid()
>>> False
I have verified that I can add the file using the QGIS gui. I even tried using the uri provided by layer.metadata
from within the Python plugin.
I am using QGIS 2.10.1 on Windows 7 64-bit machine.