FME Unique Values – Create Attributes from Unique Values of Another Feature’s Class Category Attribute with FME

fields-attributesfmeunique value

I have a table (say landuse polygons for example) structured as follows:

ID | landuse_category
---------------------
1  | woodland
2  | industry
3  | farmland
4  | woodland
5  | lake
6  | farmland
7  | industry
8  | lake

Now I would like to create attributes from the distinct values (farmland,industry,lake,woodland) of landuse_category to a table districts for example like

ID | farmland | industry | lake | woodland

I succeeded in obtaining the distinct values with a DuplicateFilter with key attribute landuse_category on the Unique port and then create a list of these unique values with a ListBuilder like

enter image description here

Since input data may vary, this is a generic task and thus the attribute values themselves should not be part of the process.

How to create attributes on another table using this list?

Best Answer

The simplest way to create the attribute is to use an AttributeManager and use the value of the landuse_category attribute as the name of the new attribute:

enter image description here

When you write that (and I'm using plain CSV format here) the results look like this:

enter image description here

I'm guessing this is what you're looking for. There are two things to consider though...

1) If you want a 0 (or null) value for the other fields, then you could expose those attributes (AttributeExposer) and map them to null (NullAttributeMapper).

2) I'm assuming the output schema is fixed (ie you know the fields are only going to be farmland, industry, lake, woodland). If you are not sure what the fields will be, then you would have to use a Dynamic writer. That's a bit more tricky (I was going to leave it out, but I figure it's worth documenting)...

2a) In the AttributeManager, also create attributes name (as a copy of landuse_category) and fme_data_type (set to your data type, I used fme_char(1))

enter image description here

2b) Add an Aggregator transformer. Set the Generate List option. Call the list attribute and add name and fme_data_type:

enter image description here

btw, you could use this instead of your method to get a schema definition. If you check the output of this you will see a schema defined as a list.

2c) We now need to get the schema definition onto all the features. Do this with a FeatureMerger transformer. Set it up like this with the requestor/supplier set to a constant value (like 1 in my case):

enter image description here

2d) Set the writer to Dynamic mode, using Schema from Schema Feature as the schema source:

enter image description here

Now you have a truly dynamic solution. If a landuse type changed (from farmland to agriculture perhaps) or a new landuse type was added (say urban) or one was deleted, then the workspace will operate automatically without needing to be edited.

Related Question