Skip to main content
Solved

How to create geometries with a python caller?


arthy
Contributor
Forum|alt.badge.img+8
  • Contributor

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

 

 

Best answer by ebygomm

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)
View original
Did this help you find an answer to your question?

4 replies

david_r
Celebrity
  • March 14, 2019

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.


takashi
Influencer
  • March 15, 2019

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

 


ebygomm
Influencer
Forum|alt.badge.img+33
  • Influencer
  • Best Answer
  • March 15, 2019

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)

Forum|alt.badge.img
  • March 15, 2019

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

 


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