Skip to main content
Question

Calculating the length of a line using python

  • June 5, 2017
  • 4 replies
  • 255 views

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

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

4 replies

mygis
Supporter
Forum|alt.badge.img+14
  • 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+40
  • 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
Celebrity
  • 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.