Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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 (How to get 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 ]
share|improve this question

2 Answers 2

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.

share|improve this answer

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.
share|improve this answer
    
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. –  Larry yesterday
    
What do you mean by "user will select the feature from map" -inside arcmap? user will select inside arcmap? –  SIslam yesterday
    
If this is the case, have a look at gis.stackexchange.com/questions/148687/… –  SIslam yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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