Skip to main content

Hello,

How can I replace a line with its corresponding points with a python caller?

I tried the following code but it is not working.

import fme
import fmeobjects

class pointCreate(object):
    def __init__(self):
        pass
        
    def input(self,feature):

        if feature.hasGeometry():
            coord = feature.getAllCoordinates()
            cpt = len(coord)
            #coordSys = feature.getCoordSys()            
            for cpt in range(0,len(coord)):
                feature.setAttribute('X_e', coord[cpt][0])
                feature.setAttribute('Y_e', coord[cpt][1])
                feature.setGeometry(fmeobjects.FMEPoint([float(coord[cpt][0]), float(coord[cpt][1])]))
                #feature.setCoordSys(coordSys)
                self.pyoutput(feature)         
        
    def close(self):
        pass 

 

Any ideas,

Thanks

 

 

Why use a PythonCaller when you can use a Chopper set to 1?

If you also need the X/Y as attributes you can follow up with a CoordinateExtractor.


The constructor of fmeobject.FMEPoint class requires two or three individual float values representing coordinates. See the API reference if you want to leverage Python FME Objects API.

http://docs.safe.com/fme/html/fmepython/api/fmeobjects/geometry/_points/fmeobjects.FMEPoint.html#fmeobjects.FMEPoint

I think this code could work for you.

                feature.setGeometry(fmeobjects.FMEPoint(coord[cpt][0], coord[cpt][1]))

 


Easier to step through the coordinates like this, but as @david_r said, the chopper followed by a coordinate extractor will do the same thing and would be my preference.

            coords = feature.getAllCoordinates()
            for coord in coords:
                feature.setAttribute('X_e',coord[0])
                feature.setAttribute('Y_e',coord[1])
                feature.setGeometry(fmeobjects.FMEPoint(coord[0], coord[1]))
                self.pyoutput(feature)

Personally I prefer importGeometryFromOGCWKT for creating anything in FME python so therefore create a string as Well Known Text format and pass that in.

feature.importGeometryFromOGCWKT("POINT({0} {1})".format(*coord)) 

This is really good for creating lines and polygons

https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry