6

I can't figure out how to add point from a CSV file with the Python Console.

This is my CSV file:

UTM32_E;UTM32_N
562834,932;8762874,054
560758,523;8762624,904
562483,074;8762760,832
562131,643;8762216,202

This the code I use (I found it here : Importing CSV file to create point layer):

uri = "/Users/pugliesipc/Desktop/new.csv?delimiter=;&crs=epsg:32632&xField=UTM32_E&yField=UTM32_N"
vlayer = QgsVectorLayer(uri,'Points','delimitedtext')

QgsProject.instance().addMapLayer(vlayer)

That what I got

enter image description here

I know that I could rather use the graphic option of QGIS but since I have to do this with a lot of files I really would like to be able to automatise the process.

I am using QGIS 3.16.15-Hannover with Python 3.8.7 on MacOS.

Taras
  • 32,823
  • 4
  • 66
  • 137
Meije3984
  • 155
  • 7
  • I think the problem is your csv file has comma decimal points, and not points. Try editing the file and change 562834,932 to 562834.932 and so on – BERA Apr 07 '22 at 16:18
  • Hi ! Thanks for your help. I had already tried to convert my csv file to a regular one. I mean with dot for decimal separator and comma for values seperator. However the same error happen... – Meije3984 Apr 07 '22 at 16:33

2 Answers2

6

There are several things regarding your code:

  • the uri for a CSV file must include the file:// prefix, as it is mentioned in the QGIS Documentation:

    The provider string is structured as a URL, so the path must be prefixed with file://.

  • there is a method called isValid() from the QgsDataProvider class:

    Returns true if this is a valid layer. It is up to individual providers to determine what constitutes a valid layer.

As was already mentioned by @BERA in his comment the reason why you do not see anything on your map canvas is because your coordinates are with , (562834,932) instead of . (562834.932).

And there are several solutions available:

A solution without changing the input CSV file

This solution based on adding the decimalPoint=',' info the uri.

#Set up inputs
absolute_path_to_csv_file = '/C:/Users/taras/Downloads/POINTS_.csv'
encoding = 'UTF-8'
delimiter = ';'
decimal = ','
crs = 'epsg:32632'
x = 'UTM32_E'
y = 'UTM32_N'

uri = f"file://{absolute_path_to_csv_file}?encoding={encoding}&delimiter={delimiter}&decimalPoint={decimal}&crs={crs}&xField={x}&yField={y}"

#Make a vector layer layer = QgsVectorLayer(uri, "Points", "delimitedtext")

#Check if layer is valid if not layer.isValid(): print ("Layer not loaded")

#Add CSV data QgsProject.instance().addMapLayer(layer)

A solution with modifying the input CSV file

There are several solutions available for replacing all commas with dots in your input CSV file:

  1. Notepad
  2. some Python (This solution maybe better covered on the StackOverflow)
absolute_path_to_files = 'C:/Users/taras/Downloads/'

input = open(absolute_path_to_files + 'POINTS.csv', "r") text = ''.join([i for i in input]).replace(',','.') output = open(absolute_path_to_files + 'POINTS_.csv', "w") output.writelines(text) output.close()

And then use the following code (either refer to an updated CSV file or a newly created)

#Set up inputs
absolute_path_to_csv_file = '/C:/Users/taras/Downloads/POINTS_.csv'
encoding = 'UTF-8'
delimiter = ';'
crs = 'epsg:32632'
x = 'UTM32_E'
y = 'UTM32_N'

uri = f"file://{absolute_path_to_csv_file}?encoding={encoding}&delimiter={delimiter}&crs={crs}&xField={x}&yField={y}"

#Make a vector layer layer = QgsVectorLayer(uri, "Points", "delimitedtext")

#Check if layer is valid if not layer.isValid(): print ("Layer not loaded")

#Add CSV data QgsProject.instance().addMapLayer(layer)

to get the output like this:

result


References:

Taras
  • 32,823
  • 4
  • 66
  • 137
  • 1
    You right, my CSV has not the right format! However, I can still use it without replace anything by specifying this "delimiter=;&decimalPoint=,". – Meije3984 Apr 07 '22 at 20:46
  • 1
    My main mistake was to not use the prefix "file://" before the path to the CSV file – Meije3984 Apr 07 '22 at 20:49
5

I replaced the commas with dots in the csv file then cheated like this: Add the Delimited text layer manually, fetch its source and pass it to QgsVectorLayer:

lyr = QgsProject.instance().mapLayersByName('manuallyAdded')[0]
uri = lyr.source()
print(uri)
layer = QgsVectorLayer(uri, 'thelayer', 'delimitedtext')
QgsProject.instance().addMapLayer(layer)

Compare your uri with the printed one to see what wasnt correct.

enter image description here

BERA
  • 72,339
  • 13
  • 72
  • 161