SHARE
TWEET

Untitled

a guest Jul 19th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # 1: loads the postcodes SHP file from V drive
  2. from qgis.core import QgsVectorLayer, QgsProject
  3. layer = QgsVectorLayer('V:/GIS - Files/3. Data/OS Data/UK Postcodes/UK Postcodes (2017).shp', 'Postcodes', "ogr")
  4. QgsProject.instance().addMapLayer(layer)
  5.  
  6. # 2: loads the XLS spreadsheet from the server
  7. from qgis.core import QgsVectorLayer, QgsProject
  8. layer =     QgsVectorLayer('//xxxxsql/xxxxx/xxxxx/xxxxx/WeeklyContacts-2019-07-15.xls', 'Contacts', "ogr")
  9. QgsProject.instance().addMapLayer(layer)
  10.  
  11. # 3. performs join field attribute algorithm, joining X and Y of 'Postcodes.shp' to 'Contacts.xls' and saves to 'Contacts.csv'
  12. 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' : '' }
  13. processing.run("native:joinattributestable", params)
  14.      
  15. # 4. converts the joined CSV to SHP
  16. uri = "C:/Users/xxxx/OneDrivexxxx/Desktop/Temporary/WeeklyContacts-2019-07-15.csv?delimiter=csv&xField=Easting&yField=Northing"
  17. layer_csv = QgsVectorLayer(uri, "WeeklyContacts-2019-07-15", "delimitedtext")
  18. QgsVectorFileWriter.writeAsVectorFormat(layer_csv, 'C:/Users/xxxx/OneDrivexxxx/Desktop/Temporary/WeeklyContacts-2019-07-15.shp', "UTF-8", layer.crs(), "ESRI Shapefile", layerOptions=['SHPT=POINT'])
  19.  
  20. # 5. loads the SHP file derived from the joined CSV
  21. from qgis.core import QgsVectorLayer, QgsProject
  22. layer = QgsVectorLayer('C:/Users/xxxx/OneDrivexxxx/Desktop/Temporary/WeeklyContacts-2019-07-15.shp', 'WeeklyContacts-2019-07-15', "ogr")
  23. QgsProject.instance().addMapLayer(layer)
  24.      
  25. # 3: performs join field attribute algorithm, joining X and Y of 'Postcodes.shp' to 'Contacts.xls' and saves to 'Contacts.csv'
  26. 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' : '' }
  27. result = processing.run("native:joinattributestable", params)
  28. # 4
  29. QgsProject.instance().addMapLayer(result['OUTPUT'])
  30. # Set the relevant output path and crs
  31. QgsVectorFileWriter.writeAsVectorFormat(result['OUTPUT'], 'path/to/output.shp', "UTF-8", 'some_crs', "ESRI Shapefile", layerOptions=['SHPT=POINT'])
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top