Select to view content in your preferred language

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

6328
24
04-10-2012 12:35 PM
MarkFoster1
Deactivated User
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
SenthilnathanThyagarajan
Emerging Contributor
This is the error when I run the code :

AttributeError: 'module' object has no attribute 'workspace'

yes the field Year used in Double


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:\\test output'

# your input shapefile
inputShp = '"D:\\2010.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 = "'Year' = 1990"

# 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
T__WayneWhitley
Honored Contributor
Try the code you posted again - I think your only problem there was your value was quoted - since it is a double val, remove the quotes:
where = '"Year"<= 1990'

...run the below again (I inserted the correction - also, make sure the 'test output' folder exists):

import arcpy
fc = "D:\\2010.shp"
where = '"Year" <= 1990'
arcpy.FeatureClassToFeatureClass_conversion(fc, "D:\\test output", "1990.shp", where)
0 Kudos
SenthilnathanThyagarajan
Emerging Contributor
I ran the code above and it creates the 1990.shp perfectly 🙂

How do i get the loop for all the years so that it creates shapefile for each of the years.
0 Kudos
T__WayneWhitley
Honored Contributor
Great, the rest should be relatively easy...

So do you have just a few queries to run?  If so, we can save the inline sub for later - what I mean is you can put your whole queries in a list:

myList = ['strQuery1', 'strQuery2', 'strQuery3']

Then loop on the list as Mathew did... if the code bails on a loop, then you now know it's likely it failed on a where clause...

Do something like:

myList = ['strQuery1', 'strQuery2', 'strQuery3']

for eachWhere in myList:
     # execute Feature Class To Feature class
     # (substitute 'where' variable with the loop variable, 'eachWhere' [no quotes])



I did forget something critical though - you need something to create dynamically changing output shapefile names so you don't end up overwriting your output.  So what you could do is either in-line var substitution or if it is a standard query like the one you already have successfully executed, you could only make a list of the values, say 1990, 1991, etc., and name the shapefiles accordingly...

EDIT:  Okay, the following is just a little bit clunky, but if you want to come up with your own string filenames and you just can't parse it very readily related to vals in your SQL strings, you can do this:

outNames = ['name1', 'name2', 'name3']
whereClauses = ['where1', 'where2', 'where3']
for i in range(len(outNames)):
     arcpy.FeatureClassToFeatureClass_conversion(fc, "D:\\test output", outNames, whereClauses)
0 Kudos
SenthilnathanThyagarajan
Emerging Contributor
I have to my list of years ready for which I want to create the shapefiles starting from (1991,1992,1996,1998,1999,2000,2003,2004,22005,2006,2008,2010).

So simultaneously I need a shapefile for each of these years. The first one was pretty straightforward but now looping is not that I understand.
0 Kudos
T__WayneWhitley
Honored Contributor
So, make sure I'm understanding, you want individual yearly outputs, correct?  If so you only need one list:

yrs = [1991,1992,1996,1998,1999,2000,2003,2004,2005,2006,2008,2010]

Then you can dynamically create both the SQL qry and filenames...as in:

for yr in yrs:
     where = '"Year" <=' + str(yr) + "'"
     filename = str(yr) + '.shp'



So, modifying your existing code to add a list variable and loop:
import arcpy
fc = "D:\\2010.shp"
yrs = [1991,1992,1996,1998,1999,2000,2003,2004,2005,2006,2008,2010]

for yr in yrs:
     where = '"Year" <=' + str(yr) + "'"
     filename = str(yr) + '.shp'     
     arcpy.FeatureClassToFeatureClass_conversion(fc, "D:\\test output", filename, where)


The only problem I see with this is I thought originally you wanted some yr ranges?
0 Kudos
SenthilnathanThyagarajan
Emerging Contributor
Is this how it should be

import arcpy
fc = "D:\\2010.shp"
yrs = [1991,1992,1996,1998,1999,2000,2003,2004,2005,2006,2008,2010]
for yr in yrs:
     where = '"Year" <=' + yr
     filename = str(yr) + '.shp'
arcpy.FeatureClassToFeatureClass_conversion(fc, "D:\\test output", filename, where)


Now the error is

An invalid SQL statement was used.
An invalid SQL statement was used.
Failed to execute (FeatureClassToFeatureClass).
0 Kudos
T__WayneWhitley
Honored Contributor
Yes, I just corrected that...see the last post.

There is another problem, I forgot about the 'less than or equal to' so you're actually querying for ranges correct?
So that for the 3rd item in the list, 1996, that range would actually be 1992 on, or:

1992 < the range <= 1996

- give me a minute.
0 Kudos
SenthilnathanThyagarajan
Emerging Contributor

import arcpy
fc = "D:\\2010.shp"
yrs = [1991,1992,1996,1998,1999,2000,2003,2004,2005,2006,2008,2010]
for yr in yrs:
      where = '"Year" <=' + str(yr) + "'"
      filename = str(yr) + '.shp'     
      arcpy.FeatureClassToFeatureClass_conversion(fc, "D:\\test output", filename, where)

del yr      



I tried the code and it gives me output for just 1991.shp
0 Kudos
T__WayneWhitley
Honored Contributor
OK, test this:

import arcpy
fc = "D:\\2010.shp"
yrs = [1991,1992,1996,1998,1999,2000,2003,2004,2005,2006,2008,2010]
subtract = ''
for yr in yrs:
     where = '"Year" <= ' + str(yr) + subtract
     subtract = ' AND "Year" > ' + str(yr)
     filename = str(yr) + '.shp'
     arcpy.FeatureClassToFeatureClass_conversion(fc, "D:\\test output", filename, where)


I just read your last message...we are almost there, but I must have screwed up on the quotes.
Standy by please, this should be a minor fix...
0 Kudos