3

I have a feature class in a file geodatabase. I want to select features from the feature class and export their attribute to an excel sheet. I need a python tool in ArcGIS which does this. My problem is, how can i pass only selected features to arcpy cursor.

In the post (Getting list of selected features in ArcGIS for Desktop using Python code? ) i read that cursor object only returns selected rows for a layer. But this does not seem to work for a feature class in file geodatabase. When i pass a feature class to search cursor it returns all the features in the feature class even when there are selected features in ArcMAP.

If you need more info feel free to ask.

code to pass the feature class to python toolbox

def getParameterInfo(self):
    param1 = arcpy.Parameter(
        displayName = "Input Feature",
        name = "in_layer",
        datatype = "DEFeatureClass",
        parameterType = "Required",
        direction= "Input")
    param1.filter.list = ["Polyline"]

   params = [param1]
   return params

def execute(self, parameters, messages):

    fc = parameters[0].valueAsText

    fields = [f.name for f in arcpy.ListFields(fc)] 
    l = [[row.getValue(field)  for row in arcpy.SearchCursor(fc)] for field in fields ]

2 Answers 2

4

When defining your parameter you need to use a layer, not a feature class, if you want to take advantage of any selections already made upon it.

def getParameterInfo(self):
    """Define parameter definitions"""
    fc = arcpy.Parameter(displayName='Features',
        name='features',
        datatype='Feature Layer',
        parameterType='Required',
        direction='Input')

I grabbed the above code from Set default values for value table in Python Toolbox tool - it looks right, with the key thing to pay attention to being datatype='Feature Layer', but I have not tested it.

1

In fact there are solutions in place.

  1. Use "where_clause" in cursor input parameters to select from the feature class and pass the selected feature to the cursor.For detailed explanation here
  2. Use select by attribute or location as needed and run cursor on that feature class- now cursor will be active on the selected features. Details here.
3
  • i can use all of it where_clause, select by aatribute /location. But for my application the user will select the feature from map and then pass it as input to the tool.
    – Jio
    Sep 5, 2015 at 9:20
  • What do you mean by "user will select the feature from map" -inside arcmap? user will select inside arcmap?
    – Learner
    Sep 5, 2015 at 9:49
  • If this is the case, have a look at gis.stackexchange.com/questions/148687/…
    – Learner
    Sep 5, 2015 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.