Skip to main content
Solved

create new features within a python caller

  • Feb 21, 2017 1:25 PM UTC
  • 7 replies
  • 1485 views

Forum|alt.badge.img

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 ?

Best answer by jeroenstiers

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.

View original
This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

7 replies

Forum|alt.badge.img+7
  • Best Answer
  • Feb 21, 2017 1:31 PM UTC

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.


david_r
Celebrity
  • Feb 21, 2017 1:31 PM UTC

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).


itay
Supporter
Forum|alt.badge.img+19
  • Supporter
  • Feb 21, 2017 1:32 PM UTC

Why not use the Cloner?


Forum|alt.badge.img
  • Author
  • Feb 21, 2017 1:44 PM UTC

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.

 


Forum|alt.badge.img
  • Author
  • Feb 21, 2017 1:45 PM UTC

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()

 


david_r
Celebrity
  • Feb 21, 2017 1:48 PM UTC
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

david_r
Celebrity
  • Feb 21, 2017 1:50 PM UTC

 

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