Question

How to add a measure on point of a line?

  • 15 March 2019
  • 3 replies
  • 4 views

Badge +7

Hello,

I have multiple segments line for which I would like to add a measure on the starting/ending of each segment. In other words, I would like to create a "fake" LRS  where the measure will be computed from some attributes of the feature.

I thinking of using python with this logic 

def addMeasure(feature        
    
    coord = feature.getAllCoordinates()
    n=len(coord)
    mfr=feature.getAttribute('MEASURE_TO')
    mto=feature.getAttribute('MEASURE_FROM')         
    if mfr!=None and str(mfr)!='':
        setMeasure(coord[0], mfr)        
        setMeasure(coord[n-1], mfr
    feature.addCoordinates(coord)

where setMeasure should be a python version of the measureSetter Transformer.

How should I proceed?


3 replies

Userlevel 4

Try something like the following:

from fmeobjects import *
def set_measure(feature):
    mfr = str(feature.getAttribute('MEASURE_FROM')) or ''
    mto = str(feature.getAttribute('MEASURE_TO')) or ''
    if mfr.isdigit() and mto.isdigit():
        measures = [float(mfr), float(mto)]
        if feature.getGeometryType() == FME_GEOM_LINE:
            line = feature.getGeometry()
            line.setMeasure(measures)
            feature.setGeometry(line) 

Note that setMeasure() requires that the given list contains the exact same number of floats as the number of vertices on the line geometry. Otherwise you can use setMeasureAt() which allows you to specfiy the vertex index for each measure, e.g.

            line.setMeasureAt(0, measures[0])
            line.setMeasureAt(line.numPoints()-1, measures[1])

More info in the fmeobjects API docs:

http://docs.safe.com/fme/html/FME_Objects_Python_API/index.html

 

Badge +7

Thanks @david_r

The function setMeasureAt does what I want but only for line.

What similar function could I be used to do the same thing for arc?

I wasn't able to see any function like setMeasure for Arc in the fmeobjects API docs.

What I really want is to add a measure to points of curves that could be line or arc.

Userlevel 2
Badge +17

The FMEArc class has setMeasureValues method. You can use the method to set measure to two points (start and end) or three points (start, mid and end) of an arc. This is an example that will set measure to two points of an arc.

import fmeobjects
def addMeasureToArc(feature):
    try:
        arc = feature.getGeometry()
        mfr = float(feature.getAttribute('MEASURE_FROM'))
        mto = float(feature.getAttribute('MEASURE_TO'))
        arc.setMeasureValues(False, (mfr, mto))
        feature.setGeometry(arc)
    except Exception as ex:
        logger = fmeobjects.FMELogFile()
        logger.logMessageString(str(ex), fmeobjects.FME_ERROR)
        raise ex # Terminate the translation.

Hope this helps.

Reply