[Qgis-developer] add rasterlayer to qgsmapcanvas

Felix Schmidt felix.schmidt at uni-weimar.de
Sun Jun 8 03:49:29 PDT 2014


Hello everyone, did anybody assorted my code and had an idea, where the 
problem can be located. I am so confused about the behavior of this 
plugin. The code you can also find on Github 
(https://github.com/MenschMarcus/HiBo-plugin)

  hibo.py:

# Import the PyQt and QGIS libraries
from PyQt4 import QtCore,  QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
# Initialize Qt resources from file resources.py
import resources
import sys
import os
# Import the code for the dialog
from Ui_hibo import Ui_hibo

class hibo:

     def __init__(self, iface):
         # Save reference to the QGIS interface
         self.iface = iface
         self.gui = Ui_hibo ()
     self.gui.setupUi()

     def connects(self):
         #self.dlg.ui.load_button.clicked.connect(self.loadImage)
         pass

     def initGui(self):
         # Create action that will start plugin configuration
         self.action = QAction(QIcon("icon.png"), "HiBo", 
self.iface.mainWindow())
         # connect the action to the run method
         QObject.connect(self.action, SIGNAL("activated()"), self.run)

         # Add toolbar button and menu item
         self.iface.addToolBarIcon(self.action)
         self.iface.addPluginToMenu("&HiBo", self.action)

     def unload(self):
         # Remove the plugin menu item and icon
         self.iface.removePluginMenu("&HiBo",self.action)
         self.iface.removeToolBarIcon(self.action)

     # run method that performs all the real work
     def run(self):
         # show the dialog
         self.gui.show()
     #print os.getcwd()
         result = self.gui.exec_()
         # See if OK was pressed
         if result == 1:
             print "test1"

ui_hibo.py:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import os

from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *


try:
     _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
     def _fromUtf8(s):
         return s

try:
     _encoding = QtGui.QApplication.UnicodeUTF8
     def _translate(context, text, disambig):
         return QtGui.QApplication.translate(context, text, disambig, 
_encoding)
except AttributeError:
     def _translate(context, text, disambig):
         return QtGui.QApplication.translate(context, text, disambig)

class Ui_hibo(QtGui.QDialog):

     def __init__(self):
         QtGui.QDialog.__init__(self)
     self.setupUi()

     #self.connect(self.loadRaster, QtCore.SIGNAL('triggered()'), 
self.loadRasterImage)
     self.connect(self.canvasRaster, QtCore.SIGNAL('renderStarting()'), 
self.startRendering)

         self.setWindowTitle(self.tr("HiBo"))

     def setupUi(self):
     """setup for toolbar"""
     self.toolbarVector     = QtGui.QToolBar('vector', self)
     self.toolbarRaster     = QtGui.QToolBar('raster', self)

     self.zoominVector     = QtGui.QAction(QtGui.QIcon("zoomin.png"), 
'zoominVector', self)
     self.zoomoutVector     = QtGui.QAction(QtGui.QIcon("zoomout.png"), 
'zoomoutVector', self)
     self.moveVector     = QtGui.QAction(QtGui.QIcon("move.png"), 
'moveVector', self)
     self.loadVector     = QtGui.QAction(QtGui.QIcon("load.png"), 
'loadVector', self)
     self.zoominRaster     = QtGui.QAction(QtGui.QIcon("zoomin.png"), 
'zoominRaster', self)
     self.zoomoutRaster     = QtGui.QAction(QtGui.QIcon("zoomout.png"), 
'zoomoutRaster', self)
     self.moveRaster     = QtGui.QAction(QtGui.QIcon("move.png"), 
'moveRaster', self)
     self.loadRaster     = QtGui.QAction(QtGui.QIcon("load.png"), 
'loadRaster', self)
     self.selectRaster    = QtGui.QAction(QtGui.QIcon("select.png"), 
'selectRaster', self)

     self.toolbarVector.addAction(self.loadVector)
     self.toolbarVector.addAction(self.zoominVector)
     self.toolbarVector.addAction(self.zoomoutVector)
     self.toolbarVector.addAction(self.moveVector)

     self.toolbarRaster.addAction(self.loadRaster)
     self.toolbarRaster.addAction(self.zoominRaster)
     self.toolbarRaster.addAction(self.zoomoutRaster)
     self.toolbarRaster.addAction(self.moveRaster)
     self.toolbarRaster.addAction(self.selectRaster)

     """setup for canvas"""
     self.canvasVector    = QgsMapCanvas()
     self.canvasVector.setCanvasColor(QtGui.QColor(255,255,255,255))
     self.canvasVector.enableAntiAliasing(True)
     self.canvasRaster    = QgsMapCanvas()
     self.canvasRaster.setCanvasColor(QtGui.QColor(255,255,255,255))
     self.canvasRaster.enableAntiAliasing(True)

     #fileName = QFileDialog.getOpenFileName(None, "historical map", 
".", "Image Files (*.png *.jpg *.bmp *.tiff)")
     fileName = "/home/felix/programming/HiBo-plugin/hibo/map.tif"
         fileInfo = QFileInfo(fileName)
         baseName = fileInfo.baseName()
         rlayer = QgsRasterLayer(fileName, baseName)
         if not rlayer.isValid():
             print "Layer failed to load!"
         QgsMapLayerRegistry.instance().addMapLayer(rlayer)
     self.canvasRaster.setExtent(rlayer.extent())
     self.canvasRaster.setLayerSet( [ QgsMapCanvasLayer(rlayer) ] )

     """layout"""
     vectorarea     = QtGui.QWidget()
     rasterarea     = QtGui.QWidget()

     layoutVector     = QtGui.QVBoxLayout()
     layoutRaster     = QtGui.QVBoxLayout()

     vectorarea.setLayout(layoutVector)
     rasterarea.setLayout(layoutRaster)

     layoutVector.addWidget(self.toolbarVector)
     layoutVector.addWidget(self.canvasVector)
     layoutRaster.addWidget(self.toolbarRaster)
     layoutRaster.addWidget(self.canvasRaster)

     layoutCentral = QtGui.QHBoxLayout(self)
     layoutCentral.addWidget(vectorarea)
     layoutCentral.addWidget(rasterarea)

     def retranslateUi(self):
         self.setWindowTitle(_translate("hibo", "hibo", None))

     #here it loads only in the legend of qgis main window
     """@QtCore.pyqtSlot()
     def loadRasterImage(self):
         #fileName = QFileDialog.getOpenFileName(None, "historical map", 
".", "Image Files (*.png *.jpg *.bmp *.tiff)")
     fileName = "/home/felix/programming/HiBo-plugin/hibo/map.tif"
         fileInfo = QFileInfo(fileName)
         baseName = fileInfo.baseName()
         rlayer = QgsRasterLayer(fileName, baseName)
         if not rlayer.isValid():
             print "Layer failed to load!"
         QgsMapLayerRegistry.instance().addMapLayer(rlayer)
     self.canvasRaster.setExtent(rlayer.extent())
     self.canvasRaster.setLayerSet( [ QgsMapCanvasLayer(rlayer) ] )
     #self.canvasRaster.refresh()"""

     @QtCore.pyqtSlot()
     def startRendering(self):
     print "start rendering"



Am 06.06.2014 07:40, schrieb Denis Rouzaud:
> can you maybe show us the whole code?
>
> Cheers,
>
> Denis
>
> On 05.06.2014 20:55, Felix Schmidt wrote:
>> The layer is loading in the legend and shown in the main window.
>> What do you mean, with the symbology. In front of the layer, in the 
>> legend, is a raster icon, if you mean this.
>>
>> I copied the code from the slot to the building of the ui. so the 
>> layer is loading in the canvas by building the gui and not by 
>> clicking in the toolbar.
>>
>> If I start qgis with this state of code, its starts and load load the 
>> layer, when I start the plugin the canvas is still white and when I 
>> clicked on the starting picture of qgis which is still there, the 
>> layer is shown in the canvas of the plugin.
>>
>> It's confusing me.
>>
>> Felix
>>
>> Am 05.06.2014 20:20, schrieb Tim Sutton:
>>> Hi
>>>
>>>
>>>
>>> On Fri, Jun 6, 2014 at 12:15 AM, Felix Schmidt 
>>> <felix.schmidt at uni-weimar.de <mailto:felix.schmidt at uni-weimar.de>> 
>>> wrote:
>>>
>>>     Same mistake with refresh() at the end.
>>>
>>>     Am 05.06.2014 19:11, schrieb Rouzaud Denis:
>>>
>>>         self.canvasRaster.refresh() at the end ?
>>>
>>>         On 05 Jun 2014, at 18:27, Felix Schmidt
>>>         <felix.schmidt at uni-weimar.de
>>>         <mailto:felix.schmidt at uni-weimar.de>> wrote:
>>>
>>>
>>>
>>>
>>>             Hello everyone,
>>>             I try to load on signal, a rasterlayer to qgsmapcanvas.
>>>             I try it like in
>>>             the pycookbook:
>>>
>>>             @QtCore.pyqtSlot()
>>>                  def loadRasterImage(self):
>>>                  print "slot works"
>>>                      fileName = QFileDialog.getOpenFileName(None,
>>>             "historical map",
>>>             ".", "Image Files (*.png *.jpg *.bmp *.tiff)")
>>>                      fileInfo = QFileInfo(fileName)
>>>                      baseName = fileInfo.baseName()
>>>                      rlayer = QgsRasterLayer(fileName, baseName)
>>>                      if not rlayer.isValid():
>>>                          print "Layer failed to load!"
>>>              QgsMapLayerRegistry.instance().addMapLayer(rlayer)
>>>                  print rlayer.extent().yMinimum()
>>>              self.canvasRaster.setExtent(rlayer.extent())
>>>                  self.canvasRaster.setLayerSet( [
>>>             QgsMapCanvasLayer(rlayer) ] )
>>>
>>>             self.canvasRaster is defined in the gui . I search for
>>>             8h but I dont
>>>             find a solutionen, because there are no errors.
>>>
>>>             It load the rasterlayer to the main program of qgis, but
>>>             not in my
>>>             qgsmapcanvas. it is still white.
>>>
>>>
>>> Is the layer loaded, showing in the legend but just not visible? 
>>> Check the symbology for the layer is defined properly if this is the 
>>> case.
>>>
>>> Regards
>>>
>>> Tim
>>>
>>>             please help me.
>>>
>>>             Felix
>>>
>>>
>>>
>>>             _______________________________________________
>>>             Qgis-developer mailing list
>>>             Qgis-developer at lists.osgeo.org
>>>             <mailto:Qgis-developer at lists.osgeo.org>
>>>             http://lists.osgeo.org/mailman/listinfo/qgis-developer
>>>
>>>
>>>     _______________________________________________
>>>     Qgis-developer mailing list
>>>     Qgis-developer at lists.osgeo.org
>>>     <mailto:Qgis-developer at lists.osgeo.org>
>>>     http://lists.osgeo.org/mailman/listinfo/qgis-developer
>>>
>>>
>>>
>>>
>>> -- 
>>> Tim Sutton - QGIS Project Steering Committee Member
>>> ==============================================
>>> Please do not email me off-list with technical
>>> support questions. Using the lists will gain
>>> more exposure for your issues and the knowledge
>>> surrounding your issue will be shared with all.
>>>
>>> Irc: timlinux on #qgis at freenode.net <http://freenode.net>
>>> ==============================================
>>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20140608/e8056cc6/attachment-0001.html>


More information about the Qgis-developer mailing list