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
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.
2 Answers 2
There are several things regarding your code:
the
uri
for a CSV file must include thefile://
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 theQgsDataProvider
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:
- Notepad
- 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:
References:
-
1You right, my CSV has not the right format! However, I can still use it without replace anything by specifying this "delimiter=;&decimalPoint=,".Meije3984– Meije39842022年04月07日 20:46:55 +00:00Commented Apr 7, 2022 at 20:46
-
1My main mistake was to not use the prefix "file://" before the path to the CSV fileMeije3984– Meije39842022年04月07日 20:49:41 +00:00Commented Apr 7, 2022 at 20:49
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.
-
2It work perfectly ! Thank you for the trick ;)Meije3984– Meije39842022年04月07日 20:39:57 +00:00Commented Apr 7, 2022 at 20:39
Explore related questions
See similar questions with these tags.
562834,932
to562834.932
and so on