6

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.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jul 19, 2019 at 12:43
9
  • 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") Commented Jul 19, 2019 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. Commented Jul 19, 2019 at 12:55
  • @Joseph Thank you. This creates a shapefile but it's a lines layer with no features opposed to a points layer. Commented Jul 19, 2019 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']) Commented Jul 19, 2019 at 13:16
  • @Joseph That looks better although the attribute table is empty, even without headers. Could it be something to do with this? gis.stackexchange.com/questions/302948/… I would not know how to change the encoding when exporting the joined layer as there is no option for this in the join attributes by field value algorithm Commented Jul 19, 2019 at 13:22

2 Answers 2

2

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'])
answered Jul 22, 2019 at 15:29
1

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'])
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
answered Jul 19, 2019 at 13:34
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.