3

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.

  • Try adding the following line to the end which can be used to export to a shapefile: QgsVectorFileWriter.writeAsVectorFormat(layer_csv, path/to/output.shp, "UTF-8", layer.crs(), "ESRI Shapefile") – Joseph Jul 19 at 12:52
  • Try to change Users\xxxx into Users/xxxx and &yField=Northnig into &yField=Northing. And .format(";", "x", "y") part is unnecessary, so you can remove. – Kadir Şahbaz Jul 19 at 12:55
  • @KadirŞahbaz Thank you I've made the corrections – James B Jul 19 at 13:04
  • @Joseph Thank you. This creates a shapefile but it's a lines layer with no features opposed to a points layer. – James B Jul 19 at 13:05
  • @JamesB - In that case, try QgsVectorFileWriter.writeAsVectorFormat(layer_csv, path/to/output.shp, "UTF-8", layer.crs(), "ESRI Shapefile", layerOptions=['SHPT=POINT']) – Joseph Jul 19 at 13:16
0

Try the following:

# 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/xxxxx/xxxxx/xxxxx/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/xxxxx/OneDrive - xxxxx/Desktop/Temporary/WeeklyContacts-2019-07-15.csv', 'PREFIX' : '' }
result = processing.run("native:joinattributestable", params)
# 4
QgsProject.instance().addMapLayer(result['OUTPUT'])
# Set the relevant output path and crs
QgsVectorFileWriter.writeAsVectorFormat(result['OUTPUT'], 'path/to/output.shp', "UTF-8", 'some_crs', "ESRI Shapefile", layerOptions=['SHPT=POINT'])
0

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'])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.