Solved

Search nested list elements

  • 23 September 2015
  • 6 replies
  • 21 views

Hi,

 

 

How is it possible to search nested lists in FME? By nested list, I mean that list that is returned from SpatialRelator and contains pass criterias.
icon

Best answer by jdh 23 September 2015, 16:21

View original

6 replies

Userlevel 5
Hi

 

 

The easiest is probably to use two ListExploders, the first for "_relationships{}" and the next for "pass{}". You can then use e.g. a Tester on "pass" to check the results.

 

 

David
I figured it out, but wanted to avodi ListExploder. Thanks anyway.
Badge +22

If you want to do complex list manipulation without exploding into multiple features, then python is a good choice.

 

 

If you just want to know if _relationships{}.pass{}  contains a specific value than something like the following should work:

def processFeature(feature):

 

    ct = feature.getAttribute('_related_candidates')

 

    value = feature.getAttribute('_criteria')

 

    i = 0

 

    while i <2:

 

        p = feature.getAttribute('_relationships{'+str(i)+'}.pass{}')

 

        i+=1

 

        if value in p:

 

            feature.setAttribute('_found', 'Yes')

 

        else:

 

            feature.setAttribute('_found', 'No')

 

 

where _criteria contains the value your looking for. 

 

 

The trick with lists and python is you can retrieve a list, but not a matrix,  so you can't simply get _relationships{}.pass{}  but you can retrierve _relationships{0}.pass{} or _relationships{}.pass{0}  so with nested lists you need to to wrap the getAttribute in a loop
Userlevel 5
Hi

 

 

I agree, I try to avoid the ListExploder as much as possible too, it has a tendency to make the memory consumption explode.

 

 

Depending on what you're trying to accomplish, it can sometimes be much faster to implement your list iteration in in a PythonCaller, that way you avoid the overhead of creating a new feature from each list element.

 

 

David

 

 
Badge +3
i simply use a tester (or attribute creator) for that.

 

You can acces those lists even without exposing them.

 

 

Or you can also create a comma separated string of the results and use a ïn"clause in your tester.

 

If the number is unknonw use a listelementcounter or so.

 

Or loop etc.etc.

 

 

Ther is no need at all for reverting to python or tcl (or java).

 

 

No need to exlode the list, even when using transformers and no scripting.

 

 

 
Badge +22

Changed my mind, the logic should be:

def processFeature(feature):

 

    ct = feature.getAttribute('_related_candidates')

 

    value = feature.getAttribute('_criteria')

 

    i = 0

 

    found = 'No'

 

    while i <2:

 

        p = feature.getAttribute('_relationships{'+str(i)+'}.pass{}')

 

        i+=1

 

        if value in p:

 

            found = 'Yes'

 

    feature.setAttribute('_found', found)

 

 

the original version would only tell you the yes/no of the last item in the list, also reduced the overhead of setting the attribute at each iteration of the loop.

 

            

 

Reply