Skip to main content
Solved

How to create geometries with a python caller?

  • March 14, 2019
  • 4 replies
  • 294 views

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

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

david_r
Celebrity
  • 8394 replies
  • 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
Celebrity
  • 7843 replies
  • 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+44
  • Influencer
  • 3429 replies
  • 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
  • 48 replies
  • 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