Skip to main content
Question

What is the simplest way of getting the last element in FME list?

  • October 16, 2018
  • 3 replies
  • 953 views

Forum|alt.badge.img

I am using an attribute creator that is given a List of coordinate. From that list i want the last x value( or the second to last in my example). How do I do it?

In this picture I have tried calculating the number of elements in the list with a list element counter and then i simply write: @Value(_indices_normal{@sub(@Value(_nbr_indices),2)}.x). However this return a sad <missing>.

 

I haven't been able to find anything in the tutorial: Tutorial: Working with List Attributes or the links provided in it about this specific problem. The closest to an answer that I've found is this lecture slide show: list-manipulation-in-fme.

3 replies

fmelizard
Safer
Forum|alt.badge.img+18
  • Safer
  • October 16, 2018

Hi @hadhafang Use a ListIndexer and set "List Index To Copy" to "@Value(_element_count) - 1" because lists are numbered from 0. But if you just want the last coordinate, instead set the Mode in the CoordinateExtractor to "Specify Coordinate" with a "Coordinate Index" of -1.


Forum|alt.badge.img
  • Author
  • October 16, 2018
fmelizard wrote:

Hi @hadhafang Use a ListIndexer and set "List Index To Copy" to "@Value(_element_count) - 1" because lists are numbered from 0.  But if you just want the last coordinate, instead set the Mode in the CoordinateExtractor to "Specify Coordinate" with a "Coordinate Index" of -1.

Thank you for a quick answer @DanAtSafe. I had an old coordinate extractor i think. However it doesn't seem to work on my data set but i wrote a pythonCaller that does the same thing.

 

 

class FeatureProcessor(object):
    def __init__(self):
        self.featureList = []
        
    def input(self,feature):
        self.featureList.append(feature)
        xList=feature.getAttribute('_indices_normal{}.x')
        feature.setAttribute('_x2', xList[-1])
        self.pyoutput(feature)       
    def close(self):
        pass

takashi
Influencer
  • October 17, 2018

This question seems to be a continuation from the previous one:
"Error: @Coordinate is out of range -- it must be an integer i such that -n = i  n, where n is the number of vertices in the feature".

If the input geometry was a vector such as Line or Polygon, @DanAtSafe's solution would be simplest. That is, you can extract desired coordinates directly by setting "Specify Coordinate" to the Mode parameter and "-1" to the Coordinate Index parameter in the CoordinateExtractor.

However, if the input geometry was a BRepSolid, the setting above could cause the same error reported in the previous question.

As you tried, it would be a workaround to extract all coordinates as a list attribute and then select one, but you need to be aware these points.

  • Since the @sub function returns a floating point number even if the arguments were integers, the resulting value could not be used as a list index directly. You will have to convert the return value to an integer using the @int function, or use the - operator instead of the @sub function.
  • A math operation embedded within a string expression has to be evaluated by the @Evaluate function. 
For example, these expressions would return x-coordinate of the last vertex.
@Value(_indices_normal{@Evaluate(@int(@sub(@Value(_nbr_indices),1)))}.x)
@Value(_indices_normal{@Evaluate(@Value(_nbr_indices)-1)}.x)

Alternatively, you can calculate the target index at the first row in the AttriuteCreator, and use it in the following rows, as in:

0684Q00000ArKSSQA3.png


The ListIndexer could also be used effectively. 

0684Q00000ArKC8QAN.png

 

 


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