Skip to main content
Solved

Is this PythonCaller documentation incorrect?


btl
Contributor
Forum|alt.badge.img+3
  • Contributor

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

2 replies

ebygomm
Influencer
Forum|alt.badge.img+38
  • Influencer
  • Best Answer
  • January 27, 2021

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
Contributor
Forum|alt.badge.img+3
  • Author
  • Contributor
  • January 27, 2021

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


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