Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm newbie developing plugins for QGIS using Python, this is the first one and it is very frustrating hehe. Ok, this is the situation, I have a 1 point layer in QGIS. Its attribute table has 4 fields and 1 row:

ID|Estation|Data|Estate

1     1     20  "Green"

What I'm trying to do with my plugin is to get the string data from "Estate" field and check the string data:

if string is "Green", the one point layer symbology will turn to green.

if string is not "Green", the one point layer symbology will turn to red.

The last part of this plugin is to refresh every X seconds to check "Estate" field, and change the symbology of this point layer if "Estate" field has changed.

This is what I have done so far in my python script, I put from the run method:

# run method that performs all the real work
def run(self):

    # create and show the dialog
    dlg = PlayMonitoringDialog() 
    # show the dialog
    dlg.show()
    result = dlg.exec_() 
    # See if OK was pressed
    if result == 1: 
      # do something useful (delete the line containing pass and
      # substitute with your code
      def h():

        self.iface.mapCanvas().refresh
        layer = self.iface.activeLayer()
        Estado = layer.attributeDisplayName(3)
        if Estado == "green":
          props = { 'color' : '0,255,0' }
          sl = QgsSymbolLayerV2Registry.instance().symbolLayerMetadata("SimpleMarker").createSymbolLayer(props)
          s = QgsMarkerSymbolV2([sl])
          layer.setRendererV2(QgsSingleSymbolRendererV2(s))
        else:
          props = { 'color' : '255,0,0' }
          sl = QgsSymbolLayerV2Registry.instance().symbolLayerMetadata("SimpleMarker").createSymbolLayer(props)
          s = QgsMarkerSymbolV2([sl])
          layer.setRendererV2(QgsSingleSymbolRendererV2(s))
        if hasattr(layer, "setCacheImage"): layer.setCacheImage(None)
        layer.triggerRepaint()
      lanza()


  def lanza():
    t=threading.Timer(3.0,h)
    t.start()
  lanza()

It doesn't work :(, so I hope someone can help me. I have seen the small examples in http://www.qgis.org/pyqgis-cookbook, I have checked http://qgis.org/api/index.html and I have looked for examples in the Internet, but I still don't know how to solve the problems in my script. Thank you very much in advance and sorry for my english, I haven't practice for a while.

Lucas

share|improve this question

2 Answers 2

It looks as though your problem is where your functions are defined. You should define all your functions at the beginning of your script, after the import, rather than inside of another function. If you need to use a function inside another, define it first and then call it within the second function. For example, you call lanza() in your first function, but you don't define it until afterwards. Rearranging your functions would take care of the basic Python problems. Any other problems would have to be answered by someone with more QGIS Python experience than I have :).

share|improve this answer
    
Thank you very much Jay!. This is the kind of things that help me a lot. I'm going to define all the functions as you say and try to call them in the run method. –  Lucas Mar 21 '13 at 14:28

Im not a Python programer but should the line:

Estado = layer.attributeDisplayName(3)

read

Estado = layer.attributeDisplayName(4)

as the attribute you are actually checking is the fourth column not the third?

Nigel

share|improve this answer
2  
Nigel-- Python starts counting from 0, so the fourth item would have the count 3. –  Jay Guarneri Mar 21 '13 at 14:26
1  
Thanks Jay, I learn something useful everyday :) –  Nigel Law Mar 21 '13 at 16:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.