Skip to main content
Solved

Is this PythonCaller documentation incorrect?

  • Jan 27, 2021 11:56 AM UTC
  • 2 replies
  • 73 views

btl
Supporter
Forum|alt.badge.img+12

I tried running the Class Interface example on this page:

https://docs.safe.com/fme/html/FME_Desktop_Documentation/FME_Transformers/Transformers/pythoncaller.htm

But the code as written, (after correcting the indentation) gives me the same area for each polygon.

To get the expected result I have to remove the 3rd & 4th from last lines (and re-correct the indents) i.e.:

import fmeobjects
 
class FeatureProcessor(object):
 
    def __init__(self):
 
        self.featureList = []
 
        self.totalArea = 0.0
 
     
 
    def input(self,feature):
 
        self.featureList.append(feature)
 
        self.totalArea += feature.getGeometry().getArea()
 
        feature.setAttribute("total_area", self.totalArea)
 
        self.pyoutput(feature)

Was this written for an earlier version of Python, or is it an error?  I'm not good enough at Python to know why the original script didn't work.

Best answer by ebygomm

The indentation is incorrect in the example, and the changes you've made have removed the def close statement. It should be as follows

import fmeobjects
 
class FeatureProcessor(object):
    def __init__(self):
        self.featureList = []
        self.totalArea = 0.0
 
    def input(self,feature):
        self.featureList.append(feature)
        self.totalArea += feature.getGeometry().getArea()
 
    def close(self):
        for feature in self.featureList:
            feature.setAttribute("total_area", self.totalArea)
            self.pyoutput(feature)

 

View original
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.

2 replies

ebygomm
Evangelist
Forum|alt.badge.img+54
  • Evangelist
  • Best Answer
  • Jan 27, 2021 12:05 PM UTC

The indentation is incorrect in the example, and the changes you've made have removed the def close statement. It should be as follows

import fmeobjects
 
class FeatureProcessor(object):
    def __init__(self):
        self.featureList = []
        self.totalArea = 0.0
 
    def input(self,feature):
        self.featureList.append(feature)
        self.totalArea += feature.getGeometry().getArea()
 
    def close(self):
        for feature in self.featureList:
            feature.setAttribute("total_area", self.totalArea)
            self.pyoutput(feature)

 


btl
Supporter
Forum|alt.badge.img+12
  • Author
  • Supporter
  • Jan 27, 2021 12:23 PM UTC

I see. My amendment actually gives the cumulative area, not the area of each polygon.