Select to view content in your preferred language

Select by polygon - python

2345
4
05-03-2011 09:58 AM
kristinavucic
Emerging Contributor
I need to create a python script where the user can ???select by polygon??? and the selection is then created into a new feature class.  Do I need to use ArcObjects for this? I am currently using ArcGIS 10 and python, but don???t have much experience with ArcObjects. Doing this with a model is easy, but the user would like it to be automated.

Thanks

kv
Tags (2)
0 Kudos
4 Replies
LoganPugh
Frequent Contributor
In ArcGIS 10, you can create a model or script tool with no parameters and put it on a toolbar. Then when you click the button it runs without any user interaction. I imagine this would work for you provided it was set up to run how the user expects it to without their interaction except for selecting the polygon feature and then clicking the button.
0 Kudos
KimOllivier
Honored Contributor
There is a datatype in the tool properties that allows you to sketch a polygon (or point or line) graphic that is passed into the script. You can create a featureclass from it. The data type is a featureset. You need a template defined in a layer file to use it.

You can sketch any type of polygon as in the draw tool, edit it and add attributes to fields defined in the template.
0 Kudos
MikeMacRae
Frequent Contributor
You need to use some geoprocessing tools. Try the "SelectByAttribute" tool to select your polygon from the attribute table. You need to convert the attribute table to a feature layer to in order to make it selectable. Here's a peice from a script of mine that might help. It helps you select points in polygons, but the general idea of what you are looking to do is there:

# Converts unselectable feature class into a selectable feature layer
gp.MakeFeatureLayer(POINT,"point")
gp.MakeFeatureLayer(POLYGON,"polygon")

rows = gp.SearchCursor("polygon")
row = rows.Next()

gp.AddMessage("Calculating Points in Polgons...")


try:

    while row:
       
        #gp.AddMessage(row.OBJECTID)

        # Select each record inside of the polygon feature class    
        SelPoly = gp.SelectLayerByAttribute("polygon", "NEW_SELECTION", "\"OBJECTID\" =" + str(row.OBJECTID))
        #gp.AddMessage(row.OBJECTID)
            
        # Select all the point that are inside of the polygon record
        SelPts = gp.SelectLayerByLocation("point", "WITHIN", SelPoly, 0, "NEW_SELECTION")
        
        # Count the points that are in each polygon
        GetCount = gp.GetCount_management(SelPts)
        
        # Calculate the ASSOC_PTS field with the counted points
        gp.CalculateField_management("polygon", "ASSOC_PTS", GetCount, "VB", "")

        # Move to the next row   
        row = rows.Next()
0 Kudos
kristinavucic
Emerging Contributor
Thank you very much for your replies. I have used Feature Set as my input and it works perfectly.
0 Kudos