PyQGIS Shapefile Creation – How to Load CSV with X&Y and Save as Shapefile

csvpyqgisshapefile

I am attempting to transform a CSV file with joined fields of Eastings and Northings into a shapefile with PyQGIS (QGIS V3).

Here is where I've got to:

1: Loaded the postcode shapefile which contains postcode and X Y fields

2: Loaded the XLS file holding address/contact details which I will be converting to a shapefile

3: Run algorithm join attributes table which saves output as CSV

My next step(s):

4: Load text delimited layer (CSV) with X & Y and save as shapefile with CRS EPSG:27700

5: Load the resultant shapefile

My code for steps 1-3 is below.

# 1: loads the postcodes SHP file from V drive
from qgis.core import QgsVectorLayer, QgsProject
layer = QgsVectorLayer('V:/GIS - Files/3. Data/OS Data/UK Postcodes/UK Postcodes (2017).shp', 'Postcodes', "ogr")
QgsProject.instance().addMapLayer(layer)

# 2: loads the XLS spreadsheet from the server
from qgis.core import QgsVectorLayer, QgsProject
layer =     QgsVectorLayer('//xxxxsql/xxxxx/xxxxx/xxxxx/WeeklyContacts-2019-07-15.xls', 'Contacts', "ogr")
QgsProject.instance().addMapLayer(layer)

# 3. performs join field attribute algorithm, joining X and Y of 'Postcodes.shp' to 'Contacts.xls' and saves to 'Contacts.csv'
params = { 'DISCARD_NONMATCHING' : False, 'FIELD' : 'Postcode', 'FIELDS_TO_COPY' : ['Easting','Northing'], 'FIELD_2' : 'postcode', 'INPUT' : '//xxxxsql/xxxx/WeeklyAuditExtracts/xxxx/WeeklyContacts-2019-07-15.xls', 'INPUT_2' : 'V:/GIS - Files/3. Data/OS Data/UK Postcodes/UK Postcodes (2017).shp', 'METHOD' : 1, 'OUTPUT' : 'C:/Users/xxxx/OneDrivexxxx/Desktop/Temporary/WeeklyContacts-2019-07-15.csv', 'PREFIX' : '' }
processing.run("native:joinattributestable", params)

The steps of code where I speculate things to go wrong:

# 4. converts the joined CSV to SHP
uri = "C:/Users/xxxx/OneDrivexxxx/Desktop/Temporary/WeeklyContacts-2019-07-15.csv?delimiter=csv&xField=Easting&yField=Northing"
layer_csv = QgsVectorLayer(uri, "WeeklyContacts-2019-07-15", "delimitedtext")
QgsVectorFileWriter.writeAsVectorFormat(layer_csv, 'C:/Users/xxxx/OneDrivexxxx/Desktop/Temporary/WeeklyContacts-2019-07-15.shp', "UTF-8", layer.crs(), "ESRI Shapefile", layerOptions=['SHPT=POINT'])

# 5. loads the SHP file derived from the joined CSV
from qgis.core import QgsVectorLayer, QgsProject
layer = QgsVectorLayer('C:/Users/xxxx/OneDrivexxxx/Desktop/Temporary/WeeklyContacts-2019-07-15.shp', 'WeeklyContacts-2019-07-15', "ogr")
QgsProject.instance().addMapLayer(layer)

The Python console reads this successfully but generates an empty points shapefile without headers.

Best Answer

I had not been using the correct number of forward slashes to define the csv file path in step 4.

Correct version below with credit to Joseph and lrssvt

Adding a csv layer in PyQGIS

# 4. converts the joined CSV to SHP
uri='file:///C:/Users/xxxx/OneDrivexxxx/Desktop/Temporary/WeeklyContacts-2019-07-15.csv?delimiter=,&yField=Northing&xField=Easting'
layer = QgsVectorLayer(uri, 'WeeklyContacts-2019-07-15', 'delimitedtext')
QgsVectorFileWriter.writeAsVectorFormat(layer, 'C:/Users/xxxx/OneDrivexxxx/Desktop/Temporary/WeeklyContacts-2019-07-15.shp', "UTF-8", layer.crs(), "ESRI Shapefile", layerOptions=['SHPT=POINT'])
Related Question