Select to view content in your preferred language

List Layers in Data Frame Extent in Python Add-in

04-29-2020 02:00 PM
Occasional Contributor

This has been a project that I want to test out but, due to my lack of the Python language, it's been very difficult for me to construct the layout of the code.  It has many steps but the first one is hard for me to understand what commands I should be using.

I want to make a python add-in that looks at polygons that are in the data frame extent and populates a drop down menu with them (using one of its field columns to populate the list).

This might be the first of many questions but any help would be great!


0 Kudos
2 Replies
MVP Alum

When creating a new script tool or add-in, I will start experimenting in the Python window with the various arcpy functions that look applicable.  From your description, you are wanting something to identify polygon layers, select one of those layers, make an initial selection of polygons from the current map extent, and get a list of attributes to allow the user to make a further choices.  I hope this is a reasonable summary of your project.

With that in mind, here's a test script that I've been experimenting with in the Python window using desktop 10.5.  You may need to check the arcpy functions if you're using Pro or other version.

mxd = arcpy.mapping.MapDocument("CURRENT")
polyLayers = [] # empty list

# ListLayers (map_document_or_layer, {wildcard}, {data_frame})
lyrs = arcpy.mapping.ListLayers(mxd)
for lyr in lyrs:
    # Describe (object)
    desc = arcpy.Describe(lyr)
    if desc.ShapeType == 'Polygon':

selected_layer = polyLayers[0] # for demo, select first layer
print selected_layer

fields = [] # empty list for field selection
# ListFields (dataset, {wild_card}, {field_type})
flds = arcpy.ListFields(selected_layer, field_type='String')
for f in flds:
    # print

print fields
selected_field = fields[0] # select first field tor testing
print selected_field

# get extent and select polygons in selected layer
# ListDataFrames (map_document, {wildcard})
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
ext = df.extent
array = arcpy.Array([arcpy.Point(ext.XMin, ext.YMin),
                     arcpy.Point(ext.XMin, ext.YMax),
                     arcpy.Point(ext.XMax, ext.YMax),
                     arcpy.Point(ext.XMax, ext.YMin)
polygon = arcpy.Polygon(array) # use this polygon for making selection

# SelectLayerByLocation_management (in_layer, {overlap_type}, {select_features}, {search_distance}, {selection_type}, {invert_spatial_relationship})
arcpy.SelectLayerByLocation_management(selected_layer, "INTERSECT", polygon, "", "NEW_SELECTION", "NOT_INVERT")

selected = [] # empty list for selected polygons' attributes

# SearchCursor (in_table, field_names, {where_clause}, {spatial_reference}, {explode_to_points}, {sql_clause})
with arcpy.da.SearchCursor(selected_layer,selected_field) as cursor:
    for row in cursor:

print selected # user would choose from this list

# SelectLayerByAttribute(in_layer_or_view, {selection_type}, {where_clause}, {invert_where_clause})
arcpy.SelectLayerByAttribute_management(selected_layer, "CLEAR_SELECTION") # clear selected items

Once the code is tested and does what is desired, then you can work it into a script tool or add-in.  Hope this helps.

0 Kudos
New Contributor II

good idea, there's a simple method to get polygon from extent after v10.3:


0 Kudos