Skip to main content
Solved

PythonCaller list to feature


nielsgerrits
VIP
Forum|alt.badge.img+54

To get the number of features from a FeatureClass in a File Geodatabase you can use arcpy's GetCount.

arcpy.management.GetCount(<featureclasspath>)

So I tried and got this working.

import fme
import fmeobjects
import arcpy
 
class FeatureProcessor(object):
 
    def __init__(self):
        pass
 
    def input(self, feature):
        arcpy.env.workspace = feature.getAttribute('path_windows')
 
        feature_classes = arcpy.ListFeatureClasses()
        
        fc_list = []
 
        for fc in feature_classes:
            count = arcpy.GetCount_management(fc)
            fc_list.append((fc, int(count[0])))
            print(fc_list)
 
        self.pyoutput(feature)
    def close(self):
        pass
 
    def process_group(self):
        pass

But no matter what I tried, I could not get the list to the feature. I expected it to be something like:

feature.setAttribute("fc_list",fc_list)

The log keeps reporting the error:

Python Exception <TypeError>: Could not convert attribute value to a supported attribute type.

 

After a while I surrendered and a colleague saved me with the following solution:

import fme
import fmeobjects
import arcpy
 
class FeatureProcessor(object):
 
    def __init__(self):
        pass
 
    def input(self, feature):
        arcpy.env.workspace = feature.getAttribute('path_windows')
 
        feature_classes = arcpy.ListFeatureClasses()
        record_counts = []
        count = 0
 
        for fc in feature_classes:
            featurecount = arcpy.GetCount_management(fc)[0]
            feature.setAttribute("record_counts" + "{" + str(count) + "}.FeatureClass",fc)
            feature.setAttribute("record_counts" + "{" + str(count) + "}.FeatureCount",featurecount)
            count = count + 1
 
        self.pyoutput(feature)
    def close(self):
        pass
 
    def process_group(self):
        pass

It does work, but it feels hacky. So my question is, is this the way it should be done? I'm trying to learn the right thing here. Thanks for looking.

Best answer by kailinatsafe

Hello @nielsgerrits​! I think you're on the right track, if not very close. It looks like you're trying to save a list of tuples to a single attribute, perhaps we should be trying to store them as separate lists/attributes? Take a look at the following script snippet:

count_list = []
for fc in feature_classes:
    count = arcpy.GetCount_management(fc)
    count_list.append(count[0])
feature.setAttribute("record_counts{}.FeatureClass", feature_classes)
feature.setAttribute("record_counts{}.FeatureCount", count_list)

I don't think we have this documented anywhere in the Getting and setting attributes doc, I will see if it can be added! Hope this helps, Kailin.

View original
Did this help you find an answer to your question?

2 replies

kailinatsafe
Safer
Forum|alt.badge.img+21
  • Safer
  • Best Answer
  • September 6, 2023

Hello @nielsgerrits​! I think you're on the right track, if not very close. It looks like you're trying to save a list of tuples to a single attribute, perhaps we should be trying to store them as separate lists/attributes? Take a look at the following script snippet:

count_list = []
for fc in feature_classes:
    count = arcpy.GetCount_management(fc)
    count_list.append(count[0])
feature.setAttribute("record_counts{}.FeatureClass", feature_classes)
feature.setAttribute("record_counts{}.FeatureCount", count_list)

I don't think we have this documented anywhere in the Getting and setting attributes doc, I will see if it can be added! Hope this helps, Kailin.


nielsgerrits
VIP
Forum|alt.badge.img+54
kailinatsafe wrote:

Hello @nielsgerrits​! I think you're on the right track, if not very close. It looks like you're trying to save a list of tuples to a single attribute, perhaps we should be trying to store them as separate lists/attributes? Take a look at the following script snippet:

count_list = []
for fc in feature_classes:
    count = arcpy.GetCount_management(fc)
    count_list.append(count[0])
feature.setAttribute("record_counts{}.FeatureClass", feature_classes)
feature.setAttribute("record_counts{}.FeatureCount", count_list)

I don't think we have this documented anywhere in the Getting and setting attributes doc, I will see if it can be added! Hope this helps, Kailin.

Hej @kailinatsafe, thanks for the response. This really helps, adding it to the documentation would be great :)


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