4

I have a shapefile that contains 200+ metropolitan cities. In each city, there are some tract polygons. The dbf table looks like:

tract  city    xvar
1      0040    20
2      0040    12
3      0040    45
...    ...     ...
5      5602    22
6      5602    44
...    ...     ...

My goal is to select a city and save it as a separate shapefile the name of which is the same as the city code:

0040.shp

tract  city    xvar
1      0040    20
2      0040    12
3      0040    45
...    ...     ...

5602.shp

tract  city    xvar
5      5602    22
6      5602    44
...    ...     ...

How should I automate this process for all the cities using arcpy?

4 Answers 4

5

You can use Select (Analysis) to perform this operation. This method has the added benefit of preserving your attributes. There are two main steps:

  1. Use a generator to list all of the unique city codes
  2. Loop through unique cities codes and add the code to the Select SQL expression

import arcpy, os

# Define the output workspace
outws = r'C:\temp\out'

# Set the input shapefile
shp = r'C:\temp\test.shp'

# Get a list of unique city codes using a generator
cities = set(row[0] for row in arcpy.da.SearchCursor(shp, "city"))

# Select unique cities and export to new shapefiles
for c in cities:
    out_fc = os.path.join(outws, c) # Define the output name
    where_clause = '"city" = \'%s\'' % c # Select based on city name
    arcpy.Select_analysis(shp, out_fc, where_clause) # Perform the Select
1
  • 1
    Thank you very much, Aaron. Your code works perfectly after adding import os.
    – Bill TP
    Commented Aug 20, 2015 at 12:42
2

You can use model builder to iterate through the features and export each as a separate feature class. In model builder if you right click on the open space you can select iterators and also the model only tool parse path. Open Parse path and select name for the drop down menu. Connect everything as i Have in the graphic below, use variable substitution to pass the name of the feature into the output of the feature class to feature class tool. This should do exactly what you are trying to accomplish.

enter image description here

1
  • By using variable substitution, I mean in the output of the feature class to feature class tool, put %Value (2)% or whatever you want to name the variable from the parse path tool. It will then take the actual value that is contained by that variable.
    – Michael B
    Commented Aug 20, 2015 at 11:45
1

If this is a one time job then, I will suggest you to use split by attributes tool from here.

http://www.umesc.usgs.gov/management/dss/split_by_attribute_tool.html

If you want to build your own tool using arcpy then here is my suggestion.

  1. Find all the unique values of the cities
  2. Select the main layer based on these unique values of cities and export it to a folder.
1

I would dissolve the tracts by city and then use that with the Split tool in the Analysis toolbox to separate out the tracks.

Your Answer

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

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