[Qgis-user] Problem refreshing map canvas with pyqgis
Sylvie Perdriel
sperdriel at cairn-dev.fr
Thu Mar 20 06:23:54 PDT 2014
Hello Every Body,
I’d like to develop a standalone application for air pollution modeling
based on pyqgis. I installed QGIs version 2.2 on windows 7 64bits from
osgeo installer and my development platform is Eclispe/pydev.
As a first step, I’m trying to load a shapefile and plot it on a
qgsmapcanvas (see attached python code) : the shapefile is properly loaded
but I can see it plotted only after minimizing the window and restoring it.
I have tried to add repaint(), refresh() calls on the qgsmapcanvas or the
window without any success. I must surely miss something simple and would
appreciate some help if possible.
Thank you in advanced and best regards,
Sylvie
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20140320/e183b1c8/attachment.html>
-------------- next part --------------
import PyQt4.uic
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from qgis.core import QgsVectorLayer, QgsMapLayerRegistry, QgsApplication,QgsProviderRegistry
from qgis.gui import *
import os
basepath="E:/TECHNIQUE/DEVCAIRN/PYTHON/UI/tests"
#
class Window(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
#PyQt4.uic.loadUi(os.path.join(basepath,'canvas.ui'), self)
#self.canvas.setCanvasColor(Qt.yellow)
#self.canvas.enableAntiAliasing(True)
#self.setCentralWidget(self.canvas)
self.setupGui()
actionZoomIn = QAction(QString("Zoom in"), self)
actionZoomOut = QAction(QString("Zoom out"), self)
actionPan = QAction(QString("Pan"), self)
actionZoomIn.setCheckable(True)
actionZoomOut.setCheckable(True)
actionPan.setCheckable(True)
self.connect(actionZoomIn, SIGNAL("triggered()"), self.zoomIn)
self.connect(actionZoomOut, SIGNAL("triggered()"), self.zoomOut)
self.connect(actionPan, SIGNAL("triggered()"), self.pan)
self.toolbar = self.addToolBar("Canvas actions")
self.toolbar.addAction(actionZoomIn)
self.toolbar.addAction(actionZoomOut)
self.toolbar.addAction(actionPan)
# create the map tools
self.toolPan = QgsMapToolPan(self.canvas)
self.toolPan.setAction(actionPan)
self.toolZoomIn = QgsMapToolZoom(self.canvas, False) # false = in
self.toolZoomIn.setAction(actionZoomIn)
self.toolZoomOut = QgsMapToolZoom(self.canvas, True) # true = out
self.toolZoomOut.setAction(actionZoomOut)
self.pan()
def setupGui(self):
frame = QFrame(self)
self.setCentralWidget(frame)
self.grid_layout = QGridLayout(frame)
self.canvas = QgsMapCanvas()
self.canvas.setCanvasColor(QColor(255, 255, 255))
self.canvas.enableAntiAliasing(True)
self.grid_layout.addWidget(self.canvas)
# add tool bar
self.toolbar = self.addToolBar("Map Tools")
def loadLayer(self, path, name):
if os.path.exists(path):
if len(name)==0:
(name, ext) = os.path.basename(path).split('.')
layer = QgsVectorLayer(path, name, 'ogr')
if not layer.isValid():
raise IOError, "Failed to open the layer"
#self.canvas.setExtent(layer.extent())
QgsMapLayerRegistry.instance().addMapLayer(layer)
#layers = self.canvas.mapRenderer().layerSet()
#layers.append(layer.id())
print " layer id :",layer.id()
#print " nb layers :",len(layers)
#self.canvas.mapRenderer().setLayerSet(layers)
canvas_layer = QgsMapCanvasLayer(layer)
self.canvas.setLayerSet([canvas_layer])
self.canvas.zoomToFullExtent()
#refreshMap()
#self.canvas.emit(SIGNAL("mapCanvasRefreshed()"))
#emit self.canvas.mapCanvasRefreshed()
else:
print "file ",path," doesnot exists!"
def zoomIn(self):
self.canvas.setMapTool(self.toolZoomIn)
def zoomOut(self):
self.canvas.setMapTool(self.toolZoomOut)
def pan(self):
self.canvas.setMapTool(self.toolPan)
if __name__ == "__main__":
# supply path to where is your qgis installed
pathapp=u"F:/OSGEO4w64/apps/qgis"
QgsApplication.setPrefixPath(pathapp, True)
QgsApplication.initQgis()
# creation de l'application
app = QgsApplication([], True)
#print QgsApplication.showSettings()
#r = QgsProviderRegistry.instance()
#print len(r.providerList())
#for i in r.providerList():
# print i
#ok=QLibrary(pathapp+u'/plugins/ogrprovider.dll').load()
#print ok
window = Window()
window.loadLayer("E:/ETUDES_CAIRN/BV_MSSA/emissions_MSSA_rev1_sp.kml","myname")
window.repaint()
window.show()
app.exec_()
# delete main window
window=None
QgsApplication.exitQgis()
More information about the Qgis-user
mailing list