1

I've got a bunch of CSV files to add as text delimited layers to my GIS project. They all have identical formats in the UK EPSG:27700 CRS with a column marked "eastings"(column 0) and another marked "northings" (column 1).

The csv looks a bit like this:

Eastings,Northings,GridRef,Climate,AT,ATA,ATDefault,CT,DAMS,DAMSA,DAMSAdj,MD,SMR,SNR,SiteClass,SMRA,SNRA,SMRN,SNRN,SMRAN,SNRAN,SMRD,SNRD,SNRB,SNRF,SNRG,SMRAspect,Brash,Drainage,Fertiliser,BrashApplied,DrainageApplied,FertiliserApplied,DamsModified,Filter,species,commonname,latinname,modelVersion,modelConfidence,threat,yc,suit,tsuit,limitingFactor,atR,ctR,damsR,mdR,smrR,snrR
363537,473249,SD635732,Medium-High 2050 (A1b/3q0) AWC method,2351,0,2351,7,11,0,11,166,Moderately dry,Poor,Warm - Sheltered - Moist,Moderately dry,Poor,7,2,7,2,0,0,0,0,0,0,No brash present,No drainage installed,No fertiliser,0,0,0,0,All species,AH,Ash,Fraxinus excelsior,3.0,A,Chalara,0,0.0,0.0,SMR,0.99,1.0,1.0,0.9,0.0,0.0
363537,472249,SD635722,Medium-High 2050 (A1b/3q0) AWC method,2366,0,2366,7,9,0,9,168,Moderately dry,Poor,Warm - Sheltered - Moist,Moderately dry,Poor,7,2,7,2,0,0,0,0,0,0,No brash present,No drainage installed,No fertiliser,0,0,0,0,All species,AH,Ash,Fraxinus excelsior,3.0,A,Chalara,0,0.0,0.0,SMR,0.98,1.0,1.0,0.89,0.0,0.0
363537,471249,SD635712,Medium-High 2050 (A1b/3q0) AWC method,2316,0,2316,7,11,0,11,163,Very moist,Medium,Warm - Sheltered - Moist,Very moist,Medium,3,3,3,3,0,0,0,0,0,0,No brash present,No drainage installed,No fertiliser,0,0,0,0,All species,AH,Ash,Fraxinus excelsior,3.0,A,Chalara,8,0.65,0.65,SNR,1.0,1.0,1.0,0.93,0.76,0.65
363537,470249,SD635702,Medium-High 2050 (A1b/3q0) AWC method,2291,0,2291,7,12,0,12,160,Very moist,Medium,Warm - Sheltered - Moist,Very moist,Medium,3,3,3,3,0,0,0,0,0,0,No brash present,No drainage installed,No fertiliser,0,0,0,0,All species,AH,Ash,Fraxinus excelsior,3.0,A,Chalara,8,0.65,0.65,SNR,1.0,1.0,0.99,0.95,0.76,0.65
363537,469249,SD635692,Medium-High 2050 (A1b/3q0) AWC method,2326,0,2326,7,12,0,12,164,Very moist,Medium,Warm - Sheltered - Moist,Very moist,Medium,3,3,3,3,0,0,0,0,0,0,No brash present,No drainage installed,No fertiliser,0,0,0,0,All species,AH,Ash,Fraxinus excelsior,3.0,A,Chalara,8,0.65,0.65,SNR,1.0,1.0,1.0,0.92,0.76,0.65

I have tried importing them using the code listed here, using the editor box of the Python window:

import os.path, glob
from qgis.core import QgsProject, QgsVectorLayer
layers=[]

for file in glob.glob('C:/pathname/*.csv'): 
      uri = "file:///" + file + "?delimiter=%s&xField=%s&yField=%s&useHeader=no&crs=epsg:27700" % (",","Eastings","Northings") #(Delimeter, XField, YField) #1st column in file is column 1
      vlayer = QgsVectorLayer(uri, os.path.basename(file), "delimitedtext")
      vlayer.setFieldAlias(0,'X') #these order of columns go: 0, 1, 2 etc
      vlayer.setFieldAlias(1,'Y')
      layers.append(vlayer)

QgsProject.instance().addMapLayers(layers)

but... Literally nothing happens when I run the code.

If I try adding the code to a new script in the toolbox, I get an error Seems there is no valid script in the file.

Can anybody tell me what's going on?

0
2

Your .csv files have headers. In uri line change useHeader=no to useHeader=yes. Corrected code (only change as mentioned above):

import os.path, glob
from qgis.core import QgsProject, QgsVectorLayer
layers=[]

for file in glob.glob('C:/pathname/*.csv'): 
      uri = "file:///" + file + "?delimiter=%s&xField=%s&yField=%s&useHeader=yes&crs=epsg:27700" % (",","Eastings","Northings") #(Delimeter, XField, YField) #1st column in file is column 1
      vlayer = QgsVectorLayer(uri, os.path.basename(file), "delimitedtext")
      vlayer.setFieldAlias(0,'X') #these order of columns go: 0, 1, 2 etc
      vlayer.setFieldAlias(1,'Y')
      layers.append(vlayer)

QgsProject.instance().addMapLayers(layers)
3
  • Thanks @Szym the code runs, but I'm still not getting any of the layers appearing on my project though?
    – Will
    May 1 '20 at 12:30
  • I tested it on QGIS 3.10. What is your version? What I did: 1) I took your csv fragment and saved as 2 .csv files. Both of them were added to my map, even though my CRS is WGS84
    – Szym
    May 1 '20 at 12:32
  • OK. That could be sorcery... I've just closed QGis and reopened it, and now it's working. No idea. Thanks for the support!
    – Will
    May 1 '20 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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