Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have 2 shapefiles (A and B) each with thousands of polygons. I want to select polygons from A which intersect a specific polygon from B, and save the selected polygons from A as a new shapefile.

I can do this using the 'select by location' feature and 'create layer from selected features'.

However, I want to repeat this for every single polygon in B.

Is it possible to automate such a task? I don't know Python, but may be able to have somebody do this for me if that is the best method.

share|improve this question

For something like this, your best bet is to start learning Python and let ModelBuilder generate the heart of your Python code for you. Then just add the components that you need to accomplish your task.

  1. Start by creating a model in ArcGIS like this: enter image description here
  2. Export it to a Python Script with Model-->Export-->to Script-->Python which will give you the basics of your code, then you just need to learn how to wrap your own code around it.
  3. Add the iteration code around the source that ModelBuilder created for you. It should look something like the code block below. See ArcGIS Help on using Cursors for a sample Python code.
  4. Create a script UI in ArcGIS (Toolbox-->Add-->Script) where you ask for user input like Layer A, Layer B, Output Folder, & file basename. My script just appends a 1,2,3 to the end of the basename but you could come up with your own version if you wanted it different.

Learning Python is not as daunting as you think, especially since ModelBuilder will generate the meat of your code for you during the export process. You'll see when you generate your own Python script from the model that I didn't add much to it.

# Usage: CreateFeatures <A> <B> <outputfolder> <output basename>
# ---------------------------------------------------------------------------

# Import system modules
import sys, string, os, arcgisscripting

# Create the Geoprocessor object
gp = arcgisscripting.create()

# Load required toolboxes...
gp.AddToolbox("D:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")

# Script arguments...
A = sys.argv[1]
B = sys.argv[2]
outfolder = sys.argv[3]
basename = sys.argv[4]

# Create a Search Cursor that references the features in layer B
rows = gp.searchcursor(B)
row =

# Create a numbering system for new output files
filenum = 0

while row:
    # Report a message back to the user about progress
    gp.addmessage("Creating selected set #" + str(filenum) + " from " + A)

    #Create a feature layer to cycle through your selection features
    fidvalue = row.GetValue("FID")
    gp.MakeFeatureLayer(B,"cursor_lyr","FID =" + str(fidvalue))

    # Process: Select Layer By Location...
    gp.SelectLayerByLocation_management(A, "INTERSECT", "cursor_lyr", "", "NEW_SELECTION")

    outname = outfolder + "\\" + basename + str(filenum)
    # Process: Copy Features...
    gp.CopyFeatures_management(A, outname, "", "0", "0", "0")

    #iterate the output file number
    filenum = filenum + 1

    #go to the next row
    row =
share|improve this answer

You could set up this geoprocessing task using model builder.

What is Model Builder

share|improve this answer

You could also do a spatial join to get the attributes of "B" onto "A" and then split shapefile "A" based on that attribute.

share|improve this answer

i see two options

  1. at 10.0 use model builder's feature iterator with the select layer by location and the copy features tools.
  2. at any release since 9.0, use a py script to do the same as above but replace the 'feature iterator' with make feature layer with an expression selecting each individual oid. The expression is going to require using a searchcursor through the feature class to build the list of oids.
share|improve this answer

Your Answer


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.