Solved

select different feature type each time workspace runs


Badge +8
I have an FME workspace I run monthly. It compares old data with current data. The old data is usually last month's data with the date stamp as part of the name. Because the feature type changes each time, I want to be prompted to select it. I can't simply use feature types to read because new feature types are being added to the file geodatabase each month (i.e. - it's not a static list). I need FME to read all the feature types in the file geodatabase at runtime and then prompt me to select one. Is there a way to do so? (Win7 64-bit, FME 2014, Build 14230, 32-bit).

 

 

Thanks,

 

 

Aaron

 

Albuquerque, NM
icon

Best answer by takashi 3 July 2014, 11:49

View original

12 replies

Userlevel 4
Hi,

 

 

there is no very simple mechanism for dynamic prompting of parameter values in FME, but you can easily create a list of all the available feature type names using the "Schema (Any Format)" reader on your dataset. You can then send this info into a WorkspaceRunner as the source of "Feature types to read" of a dynamic reader.

 

 

David
Userlevel 2
Badge +17
Hi,

 

 

as David mentioned, dynamic prompting is not so easy, but it's not impossible.

 

One possible way is to create an FME Standard Parameters Dialog with Python script.

 

If you read schemas with a Schema (Any Format) reader, for example, a PythonCaller with this script shows a dialog box for selecting a feature type name, and outputs a feature having the selected name as an attribute named "feature_type_name".

 

-----

 

# Python script example

 

import fmeobjects, os, tempfile

 

import winsound # Windows only

 

 

class FeatureTypeSelector(object):

 

    def __init__(self):

 

        self.names = []

 

       

 

    def input(self, feature):

 

        # Collect feature type names from the input feature

 

        # read by a SCHEMA reader.

 

        self.names.append(feature.getAttribute('fme_feature_type_name'))

 

       

 

    def close(self):

 

        # If the list is empty, do nothing.

 

        if len(self.names) < 1:

 

            return

 

           

 

        # Sort the list so that the name having last date will be the 1st element.

 

        self.names.sort(reverse=True)

 

       

 

        # Create FME GUI directives.

 

        gui = 'GUI TITLE Select Feature Type\\n'

 

        gui += 'DEFAULT_VALUE FEATURE_TYPE %s\\n' % self.names[0]

 

        gui += 'GUI CHOICE FEATURE_TYPE %s Feature Type' % '%'.join(self.names)

 

 

        guiPath = None

 

        try:

 

            # Create a temporary file and save the FME GUI directives.

 

            fd, guiPath = tempfile.mkstemp(dir = '.')

 

            os.write(fd, gui)

 

            os.close(fd)

 

         

 

            # *** Windows only ***

 

            # Play a Windows sound before showing the dialog box.

 

            winsound.PlaySound('SystemAsterisk', winsound.SND_ASYNC)

 

         

 

            # Create and show a parameter settings dialog box.

 

            # If the user exits the dialog with [OK], output a feature

 

            # having the selected name as attribute called "feature_type_name".

 

            dlg = fmeobjects.FMEDialog()

 

            if dlg.parameterPrompt(guiPath):

 

                f = open(guiPath)

 

                rows = [r.strip() for r in f.readlines()]

 

                f.close()

 

                feature = fmeobjects.FMEFeature()

 

                feature.setAttribute('feature_type_name', rows[1])

 

                self.pyoutput(feature)

 

        except:

 

            logger = fmeobjects.FMELogFile()

 

            logger.logMessageString('Parameter getting failure.', fmeobjects.FME_ERROR)

 

        finally:

 

            # Remove the temporary file.

 

            if guiPath and os.path.exists(guiPath):

 

                os.remove(guiPath)

 

-----

 

You can then send the feature to a WorkspaceRunner to run the main workspace.

 

Don't forget to expose the new attribute name (feature_type_name) in the PythonCaller parameter settings.

 

 

See these documentations to learn more about Python API and GUI directives.

 

http://docs.safe.com/fme/html/FME_Objects_Python_API/index.html

 

http://docs.safe.com/fme/html/FME_GuiType/index.html

 

 

Takashi
Userlevel 4
Thanks Takashi, that was a very neat trick!

 

 

One tip, change the keyword "CHOICE" to "LISTBOX" on line 25 and you get this very nice way of picking multiple feature types:

 

 

 

 

The attribute "feature_type_name" will now contain a space-delimited list of all the feature types, ready for the "Feature types to read" in the WorkspaceRunner :-)

 

 

David
Userlevel 2
Badge +17
Thanks for the tip, David.

 

Although I've never try, possibly the FMEDialog trick is also applicable in a scripted parameter definition of the main workspace with using FMEUniversalReader to read schemas.
Badge +8
Takashi and David,

 

 

Thanks for the information! I was able to use a Schema (Any Format) Reader, Takashi's code in a PythonCaller, and a FeatureReader (pushing the features out the Other port) to do what I want. Doing so allowed me to keep everything in one workspace too.

 

 

I tried the WorkspaceRunner but couldn't figure out how to set up "Feature Types To Read" as a parameter. I should do so in the workspace being called by the WorkspaceRunner, correct? I assume the "Feature Types To Read" parameter should then appear as one of the parameters in the WorkspaceRunner. Is that right? If so, could one of you explain the steps involved?

 

 

Thanks again,

 

 

Aaron
Userlevel 2
Badge +17
If the main workspace has a GDB reader, there is shown a parameter called "Fanout Types to Read" in Navigator.

 

 

 

Right-click > Create User Parameter, save the workspace.

 

Then, the parameter will appear in the WorkspaceRunner which calls the main workspace.
Badge +8
That did the trick, Takashi. I had navigated to "Feature Types To Read" in the main workspace before but was confused when the User Parameter dialog wanted me to select a feature type from the dataset. (I wanted to select nothing, leaving everything unchecked.) I didn't realize FME would simply bypass the "hard coded" selection in the main workspace and use the feature type from the WorkspaceRunner parameter. Anyway, I have successfully tested it. Thanks!

 

 

Aaron
Badge +8

It looks like I spoke too soon. Setting a user parameter for Feature Types to Read works but only if I choose one of the feature types already "hard-coded" in the feature types list of the main workspace. Let me explain. The ESRI file geodatabase I am using currently has 3 feature types in it but that number will grow by one every month. Eventually I will want to choose one of the new feature types (that doesn't currently exist). I can dynamically find all of the feature types in the workspace with the PythonCaller script at runtime. But the script called by the WorkspaceRunner has all the feature types "hard coded" in the feature types list. I can't figure out how to get the reader in that main workspace to see all the feature types at runtime. I've scoured FME Help and the FME User Community and perfomed several Google searches to no avail. There has got to be a way to do this. What am I missing?

Userlevel 2
Badge +17
I was not able to see any problem. Can these images help you?

 

 

 

 

Badge +8
My WorkspaceRunner parameters look similar, assuming PromptFeatureType is a folder containing your ESRI file geodatabase (see image below).

 

 

I think the issue is my main workspace, the one being called by the WorkspaceRunner. The main workspace doesn't dynamically update its feature types; they are static. Is there a way for the main workspace to get all the feature types in the file geodatabase at runtime? I haven't been able to get that part to work.

 

 

 

Userlevel 2
Badge +17
You've checked "Merge Feature Type" of the GDB reader feature type? This option allows you to read features of any feature type via a single reader feature type.

 

 

Badge +8
That was the problem, Takashi. I forgot to use Merge Feature Type. I checked it and everything is now working as expected. Thanks so much!

 

 

Aaron

Reply