0

I have 2 datasets in my workspace and I want to use "PythonCreator" transformer to create a new feature base on those datasets in my workspace but I don't know how to use workspace datasets in "PythonCreator" transformer. Could you help me?

2

I think there are some questions that need to be answered before we can give a full answer, but I'll try.

You have two readers. Let's say each reader reads 10 features. You connect both sets of data to a PythonCaller:

enter image description here

The PythonCaller transformer has two operating modes: as a function or a class. If you use it as a function, then it runs a Python script once per input feature. So each of your 20 features runs the script separately. I gather this is not what you want. You want to process your data together in some way as a group. To do this you would use the PythonCaller as a Class.

When used as a class you create a list to record your features. You create the list in a method called __init__ and use a method called input to handle each of your 20 features.

For example:

import fmeobjects

class FeatureProcessor(object):
  def __init__(self):
    self.featureList = []

  def input(self,feature):
    self.featureList.append(feature)

With that you now have a list of all your features stored together and can carry out processing on them. What processing you do depends on what you are trying to get out of the process. For example, if the features were meant to construct donut polygons with holes in, then you could use FMEFeature.makeDonuts(featureList)

You finish up with the close method and the pyoutput function to output the data back to FME, where it can be written out:

  def close(self):
   for feature in self.featureList:
   self.pyoutput(feature)

The full Python API reference can be found online (at http://docs.safe.com/fme/html/fmepython/index.html) and will show you all of the different classes and methods you can use.

The PythonCreator transformer is slightly different. It creates features directly from a script (with no input, hence no input port). You wouldn't use it here because you already have your features being read from two readers; you aren't trying to create them from scratch. I guess you could just use a PythonCreator instead of the readers, and use Python to read the data, although that's probably not the easiest way.

But! The big question is what you want to do to your data. Is there even a reason you need to use Python? For example, I mentioned building donuts. There is already a transformer that builds donuts (DonutBuilder) so if you carry that out in Python you are just recreating functionality that already exists. There are definitely cases where you can and should use Python with FME, but often what you want to do can be achieved way quicker with an existing transformer.

If you can ask that as a question (i.e. I want to use FME to do X to my data), someone might be able to provide a better overall solution.

  • Hi Mark, Thank you for your answer. It's very helpful for me and I could fix my problem.Hi Mark, Thank you for your answer. It's very helpful for me and I could fix my problem. Actually I have an excel mapping table that called “cable_BOM” and it has all cable conditions and cable codes to generate a list and in the other side I have a "cable" table that has some information about each cable in the my system. I want for each row of “cable” table find all rows in “cable_BOM” and expose the exact code for my list – nafis.g Aug 30 '18 at 0:04
  • (The connection between this two tables is 1 to n, it means for every row in “cable“ there is one or more than one rows in “cable_BOM” table.) and each row in "cable_BOM" is different from others and has some conditions as well so I couldn't use "FeatureMerger" and I think the best way can be programming in python. – nafis.g Aug 30 '18 at 0:04
  • 1
    I'd suggest the InlineQuerier transformer. It will load your two datasets into a temporary (run-time) database on which queries can be performed. It's perfect for more complex queries where the FeatureMerger (or FeatureJoiner) aren't quite good enough. – Mark Ireland Aug 31 '18 at 18:42
2

I think the PythonCaller is what you're looking for. Unlike the PythonCreator it has an input port so you can use data you've read within your scripts.

  • I think Python Caller has an input port. Already I have 2 readers in my workspace and I want to use the data of both to create a new feature in my python code, my problem is how to access to those datasets via python code in "PaythonCreater" transformer? – nafis.g Aug 27 '18 at 22:56
  • You can connect both data sources with the PythonCaller input port and then sort the features within your script using for example the fme_feature_type attribute – fgiron Aug 28 '18 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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