I have a table in MSSQL that I am trying to use to create a spatial layer from using the QgsVectorLayer function to load the table as a layer, and then using Fiona to create a shapefile but I am having problems in working out whether I am going about this in the correct way!
My code is below - I am able to connect to my MSSQL table so have it loaded as a QgsVectorLayer, but I am struggling with instructing Fiona / Shapely to convert the table to a spatial layer by using the XREF and YREF fields from my table as the geometry.
Looking at my code, am I along the right lines here? It does not produce any file so I obviously need to adapt this!
import sysfrom qgis.core import *from PyQt4.QtCore import QVariant from osgeo import ogrimport osfrom shapely import wkb, geometryimport fionaimport ogrtools# Initialize QGIS ApplicationQgsApplication.setPrefixPath("C:\\Program Files\\QGIS Pisa\\apps\\qgis", True)app = QgsApplication([], True)QgsApplication.initQgis()print 'QGIS Application initialized'# Add the path to Processing frameworksys.path.append('C:\\Program Files\\QGIS Pisa\\apps\\qgis\\python\\plugins')# Import and initialize Processing frameworkfrom processing.core.Processing import ProcessingProcessing.initialize()import processingprint 'QGIS processing framework initialized'driverName = "ESRI Shapefile"driver = ogr.GetDriverByName(driverName)GazProp_shp = "X:\\llpg_test.shp"GGP_LLPG_shp = "X:\\GGP_LLPG.shp"GGP_LLPG_test = "X:\\ggp_llpg_test.shp"uri = QgsDataSourceURI()# set host name, port, database name, username and passworduri.setConnection("myserver", "", "GGPNLPGRDC", "GGPAccessRDC", "GGPAccessRDC")# set database schema, table name, geometry column and optionally# subset (WHERE clause)uri.setDataSource("dbo", "ADDRESSTABLE","")vlayer = QgsVectorLayer(uri.uri(), "ADDRESSTABLE", "mssql")if not vlayer.isValid(): print "Layer failed to load!"else: print "llpg_tan layer loaded successfully"with fiona.open(vlayer, 'r') as input: input.schema['geometry'] = "Point" with fiona.open(GGP_LLPG_test, 'w', driverName, input.schema.copy(), 'EPSG:27700') as output: for elem in input: #geometry points=Point(input['XREF'], input['YREF']) output.write({'properties': elem['properties'],'geometry': mapping(points)}) print 'Job done!'
أكثر...
My code is below - I am able to connect to my MSSQL table so have it loaded as a QgsVectorLayer, but I am struggling with instructing Fiona / Shapely to convert the table to a spatial layer by using the XREF and YREF fields from my table as the geometry.
Looking at my code, am I along the right lines here? It does not produce any file so I obviously need to adapt this!
import sysfrom qgis.core import *from PyQt4.QtCore import QVariant from osgeo import ogrimport osfrom shapely import wkb, geometryimport fionaimport ogrtools# Initialize QGIS ApplicationQgsApplication.setPrefixPath("C:\\Program Files\\QGIS Pisa\\apps\\qgis", True)app = QgsApplication([], True)QgsApplication.initQgis()print 'QGIS Application initialized'# Add the path to Processing frameworksys.path.append('C:\\Program Files\\QGIS Pisa\\apps\\qgis\\python\\plugins')# Import and initialize Processing frameworkfrom processing.core.Processing import ProcessingProcessing.initialize()import processingprint 'QGIS processing framework initialized'driverName = "ESRI Shapefile"driver = ogr.GetDriverByName(driverName)GazProp_shp = "X:\\llpg_test.shp"GGP_LLPG_shp = "X:\\GGP_LLPG.shp"GGP_LLPG_test = "X:\\ggp_llpg_test.shp"uri = QgsDataSourceURI()# set host name, port, database name, username and passworduri.setConnection("myserver", "", "GGPNLPGRDC", "GGPAccessRDC", "GGPAccessRDC")# set database schema, table name, geometry column and optionally# subset (WHERE clause)uri.setDataSource("dbo", "ADDRESSTABLE","")vlayer = QgsVectorLayer(uri.uri(), "ADDRESSTABLE", "mssql")if not vlayer.isValid(): print "Layer failed to load!"else: print "llpg_tan layer loaded successfully"with fiona.open(vlayer, 'r') as input: input.schema['geometry'] = "Point" with fiona.open(GGP_LLPG_test, 'w', driverName, input.schema.copy(), 'EPSG:27700') as output: for elem in input: #geometry points=Point(input['XREF'], input['YREF']) output.write({'properties': elem['properties'],'geometry': mapping(points)}) print 'Job done!'
أكثر...