Skip to main content
Solved

How to filter a list for two (or more) specific element values?

  • September 23, 2022
  • 2 replies
  • 130 views

larue
Contributor
Forum|alt.badge.img+8
  • Contributor

I am using NeighborFinder to find adjacent horizontal (or vertical) lines. I create a list and the transformer automatically supplies the angle value for each item. I want to remove any list item which does not have an angle value of 90 or 270.

The ListElementFilter has been very helpful, but it seems to only filter one value at a time. I also tried AttributeManager to conditionally change the list{}_angle value to 90 when it finds 270, but no luck there.

Would this be a Python situation?

Any help is appreciated.

 

 

Best answer by david_r

Lists are notoriously tricky once you go beyond what the standard (or FME Hub) transformers can do. Personally I tend to reach for Python, although I'm certain it could be done with regular transformers as well.

For example:

# Iterates through list{}._angle and replaces any occurrance of 270 with 90 
def modify_angles(feature):
    angles = [float(x) for x in feature.getAttribute('list{}._angle'or []]
    for idx, item in enumerate(angles):
        if item == 270.0:
            angles[idx] = 90.0
    if angles:
        feature.setAttribute('list{}._angle', angles)

 

View original
Did this help you find an answer to your question?

2 replies

david_r
Celebrity
  • Best Answer
  • September 24, 2022

Lists are notoriously tricky once you go beyond what the standard (or FME Hub) transformers can do. Personally I tend to reach for Python, although I'm certain it could be done with regular transformers as well.

For example:

# Iterates through list{}._angle and replaces any occurrance of 270 with 90 
def modify_angles(feature):
    angles = [float(x) for x in feature.getAttribute('list{}._angle'or []]
    for idx, item in enumerate(angles):
        if item == 270.0:
            angles[idx] = 90.0
    if angles:
        feature.setAttribute('list{}._angle', angles)

 


larue
Contributor
Forum|alt.badge.img+8
  • Author
  • Contributor
  • September 24, 2022
david_r wrote:

Lists are notoriously tricky once you go beyond what the standard (or FME Hub) transformers can do. Personally I tend to reach for Python, although I'm certain it could be done with regular transformers as well.

For example:

# Iterates through list{}._angle and replaces any occurrance of 270 with 90 
def modify_angles(feature):
    angles = [float(x) for x in feature.getAttribute('list{}._angle'or []]
    for idx, item in enumerate(angles):
        if item == 270.0:
            angles[idx] = 90.0
    if angles:
        feature.setAttribute('list{}._angle', angles)

 

Thanks @david_r​ , I’ll give it a try.  

I may be able to do it with transformers, but how many will it take?  When I run into these types of challenges, I start with transformers. Before too long I’my 30 deep and down a rabbit hole.  

I know list handling is on the radar, but a little python makes more sense for these situations. 


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