Skip to main content
Question

Python Caller - How to set feature as a global


Forum|alt.badge.img
In my python caller script I can not access to feature outside of the function checkViewer(). I would like to be able to access it in the Index() class.

 

 

How can I make the feature as a global variable?

 

 

import fmeobjects

 

import json

 

import matplotlib.pyplot as plt

 

from matplotlib.widgets import Button

 

from descartes import PolygonPatch

 

 

      

 

def checkViewer(feature):

 

    BLUE = '#6699cc'

 

    geometry = feature.getAttribute('_geometry')

 

    geoJson = json.loads(geometry)

 

    fig = plt.figure()

 

    ax = fig.gca()

 

    ax.add_patch(PolygonPatch(geoJson, fc=BLUE, ec=BLUE, alpha=0.5, zorder=2 ))

 

    ax.axis('scaled')

 

   

 

    callback = Index()

 

    axprev = plt.axes([0.7, 0.05, 0.1, 0.075])

 

    axnext = plt.axes([0.81, 0.05, 0.1, 0.075])

 

    bnext = Button(axnext, 'Garder')

 

    bnext.on_clicked(callback.garder)

 

    bprev = Button(axprev, 'Rejeter')

 

    bprev.on_clicked(callback.rejeter)

 

   

 

    plt.show()

 

 

 

class Index():

 

    def __init__(self):

 

        self.feature = feature

 

       

 

    def gader(self, event):

 

        plt.close()

 

        feature.setAttribute('check', "yes") #this is not working because feature is not global

 

 

    def rejeter(self, event):

 

        plt.close()

 

        feature.setAttribute('check', "no") #this is not working

2 replies

takashi
Influencer
  • June 30, 2014
Hi,

 

 

I'm not familiar with maplotlib, but the feature object probably can be passed to the callback object with the following way:

 

 

def checkViewer(feature):

 

    ....

 

    # create an index object

 

    callback = index(feature)

 

    ....

 

 

# index class definition

 

class index:

 

    def __init__(self, feature):

 

        self.feature = feature

 

    def gader(self, event):

 

        self.feature.setAttribute('check', 'yes')

 

    ....

 

 

Takashi

david_r
Celebrity
  • June 30, 2014
Hi,

 

 

Takashi is right, the FMEFeature object is a native Python object and you can pass it along as such. If you need to access a particular feature later on (outside the PythonCaller), you will probably have to make a clone of the feature and manage it yourself, see FMEFeature.clone() in the docs.

 

 

David

 

 

 


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings