I have written a plugin using the Plugin_Builder tool. It takes a CSV, rearranges it and adds a column of WKT coordinates in the style LINESTRING(x1 y1,x2 y2). It then writes it as a new .csv. This works fine. I can then manually import it as 'delimited text' and it loads as a normal visible, interrogatable layer. What I am trying to do is get the script to load the CSV into QGIS directly after creating the file. After writing the CSV to my chosen location (out_filename), I have added the following:
##adding the file to QGIS
uri='out_filename?&wktField=%s'%("wkt")
#uri ='file:///C://Users//Desktop//test2.csv?delimiter=%s&crs=epsg:2154&wktField=%s' % (",", "WKT")
layer = self.iface.addVectorLayer(uri, "my_layer", "delimitedtext")
#QgsVectorFileWriter.writeAsVectorFormat(layer,out_filename + '.shp', 'CP1250', None, 'ESRI Shapefile')
#QgsProject.instance().addMapLayer(layer)
The # lines are me trying experiments, none of which work. I wasn't sure if it was just not finding the file, hence the explicit name. A layer is loaded but with no contents. The QGIS error message is:
2022-06-04T14:09:44 WARNING Errors in file C://Users//Desktop//test2.csv
2022-06-04T14:09:44 WARNING File cannot be opened or delimiter parameters are not valid
The layer that is created has a red warning icon next to the name. If I click it and redirect it to the file location, it opens as expected.
The headers and first two rows look like:
Name,ID,Seq,Heading,Start time,End time,SOL Fix,EOL Fix,Wind,Waves,Format,Type,N/A,N/A,N/A,N/A,N/A,N/A,N/A,Nav,USBL,Comments,Status,Length,Project,Client,Area,Contractor,Vessel,Datum,EPSG,WKT
EQ22307-01001-04,1,,22.355,,,,,,,Prime,Analogue,-,-,-,-,-,-,-,-,-, ,Virgin,8.644,,,,,,ED50 - UTM 29N/-9E,,"LINESTRING(525182.750 6756568.136,528470.372 6764562.356)"
EQ22307-01005-08,2,,22.355,,,,,,,Prime,Analogue,-,-,-,-,-,-,-,-,-, ,Virgin,8.644,,,,,,ED50 - UTM 29N/-9E,,"LINESTRING(525205.871 6756558.628,528493.493 6764552.848)"
'
around your delimiter in the url?