I'm having a go at using pyqgis from within the QGIS python console to load a layer from SQL Server and display it. I feel like I'm part way there but I'm having a real issue getting the layer to display.
I've loaded the layer, it appears in the layers dock but nothing appears in the map window (even after selecting zoom to layer extent and checking styles). What's also weird is that if I open attributes there is data in there and I can save the layer as a shapefile, reopen it and it appears fine. Is there something I'm missing? Here's my code:
from PyQt4.QtCore import QFileInfo,QSettings
from qgis.core import QgsRasterLayer, QgsCoordinateReferenceSystem
from qgis.gui import *
from PyQt4.QtCore import *
s = QSettings()
oldValidation = str(s.value( "/Projections/defaultBehaviour", "useGlobal" ))
s.setValue( "/Projections/defaultBehaviour", "useGlobal" )
uri = "MSSQL:server=My_Server;database=My_Database;tables=dbo.My_View;trusted_connection=yes"
vlayer = QgsVectorLayer(uri, "my_table", "ogr")
vlayer.setCrs( QgsCoordinateReferenceSystem(4326, QgsCoordinateReferenceSystem.EpsgCrsId) )
s.setValue( "/Projections/defaultBehaviour", oldValidation )
QgsMapLayerRegistry.instance().addMapLayer(vlayer)
No comments:
Post a Comment