Question

Calculating the length of a line using python


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


4 replies

Badge +2

Hi @jelango1996

Why don't you use the LengthCalculator transformer?

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

 

 

Badge +22

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).

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

 

 

Userlevel 2
Badge +17

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