Skip to main content

I want to calculate the length of a line shape file using python scripting. Pls somebody help me do this

Hi @jelango1996

Why don't you use the LengthCalculator transformer?

https://www.safe.com/transformers/length-calculator/

 

 


While I agree that the LengthCalculator is the simplest option, if you absolutely need to use the python caller, the FMECurve class has a getLength{} method.

 

 

geom = feature.getGeometry()

 

length = geom.getLength(False)

 

 

Use true if you want the 3D length.

Note that this will throw an error if your geometry is not a curve (line).


I wouldn't be trying to solve everything with Python in FME

 

 


You can also use the FMEFeature.performFunction to call the @Length() function. e.g.

    len2 = float(feature.performFunction('@Length(2)')) # 2D Length
    len3 = float(feature.performFunction('@Length(3)')) # 3D Length

With this method, the result will be equal to the result from the LengthCalculator. That is, the length can be computed even if the input geometry is not a Curve (e.g. the perimeter of an Area feature).

I usually use the LengthCalculator transformer or sometimes use the @Length() function within a transformer parameter (not with Python script), unless there is a specific reason for using Python script.


Reply