Skip to main content
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

mygis
Supporter
Forum|alt.badge.img+13
  • Supporter
  • June 5, 2017

Hi @jelango1996

Why don't you use the LengthCalculator transformer?

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

 

 


jdh
Contributor
Forum|alt.badge.img+28
  • Contributor
  • June 5, 2017

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


Forum|alt.badge.img+2
  • June 5, 2017
I wouldn't be trying to solve everything with Python in FME

 

 


takashi
Influencer
  • June 7, 2017

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.


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