Solved

create new features within a python caller

  • 21 February 2017
  • 7 replies
  • 257 views

Badge

Every exemple of python caller I see would only modify attributes or add new attributes to each input feature. Is it possible to output new features or more features than were received by duplicating some inputs ?

icon

Best answer by jeroenstiers 21 February 2017, 14:31

View original

7 replies

Badge +7

Hi @vchalmel

Yes, that is possible indeed. The code below allows you to create a new feature.

You can output this feature by using the second line of the sample.

newFeature = fmeobjects.FMEFeature()

self.pyoutput(newFeature)

This code only works when using the object-implementation (so not the function-implementation) of a PythonCaller. In the function-implementation, the feature entering the PythonCaller is outputted automatically.

Userlevel 5

Sure, as long as you use the class template it's pretty easy. Here's an example that outputs 10 features for every incoming feature, including a new attribute "item_no" (which you'll have to manually expose in the PythonCaller)

import fmeobjects
class Multiply(object):
    def input(self,feature):
        for n in range(10):
            new_feature = feature.clone()
            new_feature.setAttribute('item_no', n)
            self.pyoutput(new_feature)

You can also use the PythonCreator to this effect, although the usage is slightly different (there's a nice example in the documentation).

Badge +16

Why not use the Cloner?

Badge

Hi @vchalmel

Yes, that is possible indeed. The code below allows you to create a new feature.

You can output this feature by using the second line of the sample.

newFeature = fmeobjects.FMEFeature()

self.pyoutput(newFeature)

This code only works when using the object-implementation (so not the function-implementation) of a PythonCaller. In the function-implementation, the feature entering the PythonCaller is outputted automatically.

 

OK, so pythonCreator and pythonCaller share every python function of the python FME library.

 

Badge

Sure, as long as you use the class template it's pretty easy. Here's an example that outputs 10 features for every incoming feature, including a new attribute "item_no" (which you'll have to manually expose in the PythonCaller)

import fmeobjects
class Multiply(object):
    def input(self,feature):
        for n in range(10):
            new_feature = feature.clone()
            new_feature.setAttribute('item_no', n)
            self.pyoutput(new_feature)

You can also use the PythonCreator to this effect, although the usage is slightly different (there's a nice example in the documentation).

That's precisely because pythonCreator receives no input that i was puzzled about pythonCaller ability to use fmeobjects.FMEFeature()

 

Userlevel 5
That's precisely because pythonCreator receives no input that i was puzzled about pythonCaller ability to use fmeobjects.FMEFeature()

 

Exactly, that is the difference

 

  • the PythonCaller gets called once for every incoming feature and outputs n features
  • the PythonCreater is called once when the workspace starts and outputs n features
Userlevel 5

 

OK, so pythonCreator and pythonCaller share every python function of the python FME library.

 

Yes, the fmeobjects Python API is shared by all the FME components that use Python.

 

The documentation is extremely handy: http://docs.safe.com/fme/html/FME_Objects_Python_API/index.html

Reply