PyQGIS – Adding Points with Coordinates Delimited by Commas from CSV File

csvimportpoint-creationpyqgisqgis-python-console

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.

Best Answer

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:

Related Question