Creating Many Shapefiles/Features using Python/Model Builder from one Shapefile.

4890
24
04-10-2012 12:35 PM
MarkFoster1
New Contributor
We have a .shp with thousands of tracts each with specific number (i.e. tract 1, 2, 3....etc). Each tract has multiple contract numbers. (i.e. tract 1 = contract A, B, C...etc.) The contracts are in one field based on the tract number. Many of the contracts share tracts, but we want to see what each contract holds.

IS there a way to take the contracts and create them as individual shapefiles from the Tract shapefile. For example, we want to take contract A and all of the tracts associated with it to a shapefile.

We are new to python and any help would be helpful.

We are using Arc 10 and have version 2.6 of python

Thanks
Tags (2)
0 Kudos
24 Replies
MathewCoyle
Frequent Contributor
Some sample data/screen shots would help with next steps, without knowing details, my best guess would be to do something like this.

Get a list of contracts, export to shapefile with a where clause of each contract possibility and export to shapefile.

Code example, untested.
fc = "C:/GIS/test.shp"
contracts = ["A", "B", "C"]
for cont in contracts:
    where = "contractfield like '%"+i+"%'"
    arcpy.FeatureClassToFeatureClass_conversion(templayer, exportdir, "somename"+cont+".shp", where)
0 Kudos
SenthilnathanThyagarajan
New Contributor
Any solutions for this yet. I have a similar question though. I have a shapefile with a field year in it. I want to create multiple shapefiles based on the years. For instance a shapefile for all building before 1990, then a shapefile for building before 1995. Any suggestions here ?

Thank you.
0 Kudos
T__WayneWhitley
Frequent Contributor
Either solution above, slightly adapted, would work for you.  If you want to avoid modifying Python code, then you can 'preprocess' what you need manually before feeding into the ready-made tool Dan was talking about above.

For example, you said you had a single shapefile with a 'year' field which you want to use year ranges with to produce the 'split layers by attributes', so create an extra field in which to code your ranges and select by attributes and code each case you want segregated...once completed, you can run the tool execution to wham bam create the 'split layers', voila!  (assuming you are already familiar with 'Select by Attributes' and the 'Field Calculator')

So to be clear, this would be split into 2 by the new field, 'newYrRange':

'oldYr', 'newYrRange'
1985, 'before 1990'
1980, 'before 1990'
1776, 'before 1990'
1991, '1990 to 1995'
1994, '1990 to 1995'
0 Kudos
SenthilnathanThyagarajan
New Contributor
Thank you for the response.I would like to use python so that I can loop the years and then accordingly create shapefiles such as 1990.shp ,1995.shp.
0 Kudos
T__WayneWhitley
Frequent Contributor
You can still modify Mathew's code.  You'd need to set up a SQL query for the year range and substitute that into say a Make Feature Layer, then feed that result into the feature export (think he use Feature Class To Feature Class but I think you can also use Copy Features).  You may need to test the queries 1st to make sure they work properly.  If it doesn't make the list of query parameters too long, you could conceivably place the entire query strings in there, then call each in turn in the loop.

EDIT:  Actually, I see why Mathew suggested the use of Feature Class To Feature Class - you can enter the SQL query (where_clause) there and make your export in a single line of code - here's the syntax:

FeatureClassToFeatureClass_conversion (in_features, out_path, out_name, {where_clause}, {field_mapping}, {config_keyword})

Feature Class To Feature Class (Conversion)
Resource Center » Professional Library » Geoprocessing » Geoprocessing tool reference » Conversion toolbox » To Geodatabase toolset
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001200000020000000
0 Kudos
SenthilnathanThyagarajan
New Contributor
I tried something like this , but this throws and exception error.
import arcpy
fc = "D:\\2010.shp"
Year = ["1991", "1992", "1997"]
for yr in Year:
    where = "contractfield like '%"+1+"%'"
    arcpy.FeatureClassToFeatureClass_conversion(fc, "D:\\test output", "somename"+yr+ as integer".shp", where)
0 Kudos
T__WayneWhitley
Frequent Contributor
Bear in mind, that was pseudocode Mathew provided, just a general 'untested' guide... it is good though.  Let's take this a step at a time.  First, for readability of your code, enclose it with tags, at the end, and
 at the beginning.

Forget about the loop and inline substitution for a moment - the 1st priority is to get a single execution of Feature Class To Feature Class going with 1 properly formed query...so do something like this:

import arcpy

# lines preceded with the '#' symbol are comment lines (not code)
# your workspace where all input/output will go for now
arcpy.env.workspace = r'D:\\'

# your input shapefile
inputShp = 'yr2010.shp'  # What is your input shapefile?

# your output shapefile
outputShp = 'yr1991.shp'

# where clause -- substitute 'somefield' with your field (keeping the quotes)
# also, is your yr value numeric?
where = "'somefield' = 1991"

# Form Feature Class to Feature Class command based on above params
# FeatureClassToFeatureClass_conversion (in_features, out_path, out_name, {where_clause}, {field_mapping}, {config_keyword})
arcpy.FeatureClassToFeatureClass_conversion(inputShp, arcpy.workspace, outputShp, where) 
0 Kudos
SenthilnathanThyagarajan
New Contributor

import arcpy
## below is the shapefile which I want to split it into
fc = "D:\\2010.shp" #

#the sql string which I want all the features to be selected for
where = '"Year"<= "1990"'
arcpy.FeatureClassToFeatureClass_conversion(fc, "D:\\test output", "1990.shp", where)

0 Kudos