Skip to main content

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.

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)

 


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


Reply