Skip to main content
Question

Find <missing> values via python caller that puts out dynamic schema and data for excel writer


killphil84
Participant
Forum|alt.badge.img

Hey - I'm using a python caller to execute this script (shortened version of original script):

 

---------------------------------------------------------------------------------------------------------

 

import fme

import fmeobjects

class FeatureProcessor(object):

  def __init__(self):

    self.Types = []

    self.features = []

     

  def input(self, feature):

    if feature.getAttribute('_is_ftr_type'):

      self.Types.append(feature.getAttribute('Feature_Type'))

    else:

      self.features.append(feature)

     

  def close(self):

    # Create and output a Schema Feature

    schema = fmeobjects.FMEFeature()

    schema.setAttribute('fme_schema_handling', 'schema_only') # indicates it's a schema feature

    schema.setAttribute('fme_feature_type_name', 'MySchema') # schema definition name

    schema.setAttribute('fme_geometry{0}', 'fme_area') # geometry type definition

        

    schema.setAttribute('attribute{0}.name', 'Element-ID')

    schema.setAttribute('attribute{0}.fme_data_type', 'fme_varchar(200)')        

    schema.setAttribute('attribute{1}.name', 'Name')

    schema.setAttribute('attribute{1}.fme_data_type', 'fme_varchar(30)')     

    schema.setAttribute('attribute{2}.name', 'Area')

    schema.setAttribute('attribute{2}.fme_data_type', 'fme_real64')

    for i, type in enumerate(self.Types, 3):

      schema.setAttribute('attribute{%d}.name' % i, type)    

      schema.setAttribute('attribute{%d}.fme_data_type' % i, 'fme_real64')

    self.pyoutput(schema)

     

    # Output Data Features

    for feature in self.features:

      self.pyoutput(feature)

 

---------------------------------------------------------------------------------------------------------

 

MySchema goes in dynamic schema definition in my excel writer and we've got three fixed columns (Element-ID,Name, Area) and the respective data that goes in there. After the third column (actually in the script it is column"3", because column counting starts at 0), name and data is read in dynamically (all this is coming from DGNs and the columns are equivalent to the levels). To this point, everything works fine. Now, I'm facing <missing> - values in the dynamically generated columns and I have to replace these with 0. I don't have a lot of experience with using python, my guess is, that I have to do something like this:

 

if i = '<missing>':

i=0

 

But I really don't know the exact syntax nor where to put this (I guess somewhere after "for i, type in enumerate(self.Types, 3):"). Any help would be greatly appreciated 🙂

6 replies

debbiatsafe
Safer
Forum|alt.badge.img+20

Hello @killphil84​ 

Are you able to clarify what you mean by <missing> values in the dynamically generated columns?

It may help to provide a snippet of sample data and your expected output for the schema feature in these cases.


killphil84
Participant
Forum|alt.badge.img
  • Author
  • Participant
  • September 12, 2022

Hi! Sorry for the late response - I was on vacation. However, the topic is still relevant in the context of a current project. I'm testing for several condictions which leads some features to run through an areacalculator. Therefore, these features have a valid value for "Area" (e.g. "10" for 10 square meter). The features that don't run through the areacalculator do net get a value here, it stays empty (this example is simplified - I would rather solve my problem in the python scrpit than change something else). This leads to empty fields when writing the results to excel. Instead of these empty fields (FME is showing me "<missing>"), I need to have an value of "0" in all of them.


debbiatsafe
Safer
Forum|alt.badge.img+20
  • Safer
  • September 13, 2022
killphil84 wrote:

Hi! Sorry for the late response - I was on vacation. However, the topic is still relevant in the context of a current project. I'm testing for several condictions which leads some features to run through an areacalculator. Therefore, these features have a valid value for "Area" (e.g. "10" for 10 square meter). The features that don't run through the areacalculator do net get a value here, it stays empty (this example is simplified - I would rather solve my problem in the python scrpit than change something else). This leads to empty fields when writing the results to excel. Instead of these empty fields (FME is showing me "<missing>"), I need to have an value of "0" in all of them.

Hi @killphil84​ 

If a value is displayed as <missing> for attributes in the Data Inspector, that means the attributes does not exist on the feature itself and not the value itself is the text <missing>. It is an important distinction. Here is an article with more details on null attributes in FME in case it helps.

I would recommend you explore the isAttributeMissing method. This method tests whether a given attribute maps is missing on a feature so if this method returns True, then you should assign the attribute value to 0.

Note: for the scenario you mentioned, the NullAttributeMapper is already an existing transformer that performs a very similar function.


killphil84
Participant
Forum|alt.badge.img
  • Author
  • Participant
  • September 16, 2022

Hi, thank you for your reply. I'm aware of the ways FME handles null attributes. Thank you for providing links for further information - but I guess my problem is slighty different. As I said, the columns I have to test for null attributes and in case of finding one have to set to 0 are derived from level names in input dgn-files. It is intended to put the worbench on our fme server, so that all my colleagues can use this process. I don't know the level names in their inout dgn-files in advance, so I have to read and write dynamically. The data is processed in the python script in my initial post before writing it out with an excel writer. Therefore, I guess, I have to do the check for null attributes in the python script.


debbiatsafe
Safer
Forum|alt.badge.img+20
  • Safer
  • September 17, 2022
killphil84 wrote:

Hi, thank you for your reply. I'm aware of the ways FME handles null attributes. Thank you for providing links for further information -  but I guess my problem is slighty different. As I said, the columns I have to test for null attributes and in case of finding one have to set to 0 are derived from level names in input dgn-files. It is intended to put the worbench on our fme server, so that all my colleagues can use this process. I don't know the level names in their inout dgn-files in advance, so I have to read and write dynamically. The data is processed in the python script in my initial post before writing it out with an excel writer. Therefore, I guess, I have to do the check for null attributes in the python script.

It sounds like you are wanting to set attribute values to 0 if they are <missing>/<null> when you don't know the schema beforehand. Assuming self.Types contains all possible attributes for all features, you should be able to use self.Types in conjunction with isAttributeMissing method and isAttributeNull method to determine if a given attribute is missing or its value is null on a feature.  You would want to do this when looping through the features array.

# Output Data Features
for feature in self.features:
  for attr in self.Types:
    if feature.isAttributeMissing(attr) or feature.isAttributeNull(attr):
      feature.setAttribute(attr,0)
  self.pyoutput(feature)

 


paalped
Contributor
Forum|alt.badge.img+5
  • Contributor
  • September 17, 2022

 

Ok Im gonna go all in on this assumtion:

 

# Output Data Features

reuired_ft = set(self.Types)

for feature in self.features:

  ft = feature.getAttribute(‘Feature_Type’)

  missing_fts = required_ft - set([ft])

  for ft in missing_fts:

   feature.setAttribute(ft,0)

  self.pyoutput(feature)


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