Skip to main content
Solved

PythonCaller list to feature

  • September 4, 2023
  • 2 replies
  • 103 views

nielsgerrits
VIP
Forum|alt.badge.img+60

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.

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

kailinatsafe
Safer
Forum|alt.badge.img+23
  • Safer
  • 720 replies
  • 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+60
  • Author
  • 2938 replies
  • September 8, 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.

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