3

Initial Question Here Followed with Developments Below

I'm aware of the question posted here but my 120 csv files representing UK postcode centroids do not have x and y headers and dive straight into eastings and northings. Please see the code below which assumes coordinates are defined by columns 'x' and 'y'.

import glob, os

# Define path to  directory of your csv files
path_to_csv = "C:/Users/You/Desktop/csv folder/"  

# Set current directory to path of csv files
os.chdir(path_to_csv)  
# Find each .csv file and load them as vector layers
for fname in glob.glob("*.csv"):  
    uri = "file:///" + path_to_csv + fname + "?    delimiter=%s&crs=epsg:4326&xField=%s&yField=%s" % (",", "x", "y")
    name = fname.replace('.csv', '')
    lyr = QgsVectorLayer(uri, name, 'delimitedtext')
    QgsMapLayerRegistry.instance().addMapLayer(lyr)

My data:

I'm only interested in the 1st, 3rd and 4th columns. I've also attached a screenshot of what the data looks like in excel below the example code. How do I define columns 3 and 4 in python to replace 'x' and 'y' referred in this question?

AL1 1AG 10  515487  206498
AL1 1AJ 10  515491  206410
AL1 1AR 10  516270  205897
AL1 1AS 10  515005  206908
AL1 1AT 10  516131  206148
AL1 1AU 10  516141  206088
AL1 1BH 10  514836  206657
AL1 1BU 10  514643  206972
AL1 1BX 10  514619  206893
AL1 1BY 10  514582  206986
AL1 1BZ 10  514667  207047
AL1 1DG 10  514450  206754

enter image description here

Development 1.0

With a response to my initial question, I plugged the code in the editor but received the following error:

exec(open('C:/Users/JAMESB~1/AppData/Local/Temp/tmp3xtgxhmj.py'.encode('utf-8')).read())
Traceback (most recent call last):
File "C:\PROGRA~1\QGIS3~1.2\apps\Python36\lib\code.py", line 91, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
File "<string>", line 4
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

My Script:

import glob, os

# Define path to  directory of your csv files
path_to_csv = "C:\Users\>mynamehere<\Desktop\csv folder"  

# Set current directory to path of csv files
os.chdir(path_to_csv)  
# Find each .csv file and load them as vector layers 
for fname in glob.glob("*.csv"):  
   uri = "file:///" + path_to_csv + fname + "?useHeader=no&delimiter=%s&crs=epsg:27700&xField=3&yField=4" % (",")
   name = fname.replace('.csv', '')
   lyr = QgsVectorLayer(uri, name, 'delimitedtext')
   QgsMapLayerRegistry.instance().addMapLayer(lyr)

I have placed all the csv files into a folder called "csv folder" on my desktop. Can you help me solve my issue? Sorry for my ignorance in python, this is a first for me!

Development 1.1

I corrected my file path with forward slashes and used the code below but still to no avail:

Script

import glob, os

# Define path to  directory of your csv files
path_to_csv = "C:/Users/myname/Desktop/csv folder/"  

# Set current directory to path of csv files
os.chdir(path_to_csv)  
# Find each .csv file and load them as vector layers for fname in 
for fname in glob.glob("*.csv"):  
    uri = "file:///" + path_to_csv + fname + "?useHeader=no&delimiter=%s&crs=epsg:27700&xField=3&yField=4" % (",")
    name = fname.replace('.csv', '')
    lyr = QgsVectorLayer(uri, name, 'delimitedtext')
    QgsMapLayerRegistry.instance().addMapLayer(lyr)

Error

exec(open('C:/Users/JAMESB~1/AppData/Local/Temp/tmp4ejnw3eo.py'.encode('utf-8')).read())
Traceback (most recent call last):
    File "C:\PROGRA~1\QGIS3~1.2\apps\Python36\lib\code.py", line 91, in runcode
        exec(code, self.locals)
    File "<input>", line 1, in <module>
    File "<string>", line 13, in <module>
NameError: name 'QgsMapLayerRegistry' is not defined

2 Answers 2

2

Since your data doesn't have headers, you should exclude them from the loading. The doc under "Delimited text file data provider" then specifies:

xField=column yField=column

Defines the name of the columns holding the x and y coordinates for XY point geometries. If the useHeader is no (ie there are no column names), then this is the column number (with the first column as 1).

So the URI should be

uri = "file:///" + path_to_csv + fname + "?useHeader=no&delimiter=%s&crs=epsg:27700&xField=3&yField=4" % (",")

As pointed out by @Mkennedy, the projection is not 4326 as in the original post, so you will want to set it to the proper value.

3
  • Shouldn't it be &crs=epsg:27700 ?
    – mkennedy
    Commented Aug 23, 2018 at 15:59
  • @mkennedy good catch!
    – JGH
    Commented Aug 23, 2018 at 17:08
  • Thanks @JGH - although my python knowledge is not deep enough to solve this. I'll edit my question in a few minutes time.
    – James B
    Commented Aug 30, 2018 at 15:57
1

There are a handful of syntax errors in the updated code snippet,

  1. the path_to_csv is not escaped property, use forward slashes
  2. the path_to_csv should end in a forward slash
  3. need the for loop in the glob statement

try:

import glob, os

# Define path to  directory of your csv files
path_to_csv = "C:/Users/mynamehere/Desktop/csv folder/"  

# Set current directory to path of csv files
os.chdir(path_to_csv)  
# Find each .csv file and load them as vector layers for fname in
for fname in glob.glob("*.csv"):  
   uri = "file:///" + path_to_csv + fname + "?useHeader=no&delimiter=%s&crs=epsg:27700&xField=3&yField=4" % (",")
   name = fname.replace('.csv', '')
   lyr = QgsVectorLayer(uri, name, 'delimitedtext')
   QgsProject.instance().addMapLayer(lyr)
3
  • Thanks @artwork21 - I'm afraid this has not fixed the problem: please see the edited question
    – James B
    Commented Aug 30, 2018 at 15:58
  • Ah, that method was depricated in v3x, use QgsProject instead. I've updated my answer.
    – artwork21
    Commented Aug 30, 2018 at 18:37
  • You absolute legend. Thank you so much. Not sure if i can work out what the difference is in the last line but this has sparked some interest to learn python language. Thanks again
    – James B
    Commented Aug 31, 2018 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.