Unique Feature Layers for processing a series of select by locations in Python?

1092
6
05-05-2011 07:47 AM
JamesSnider
New Contributor
I'm attempting to perform what I thought would be a simple series of select by locations using arcpy -- e.g. create feature layers for each input feature and select feature and then complete the select by location.

boundary_file_list = arcpy.ListFeatureClasses()
for boundary_file in boundary_file_list:
              
                arcpy.MakeFeatureLayer_management(boundary_file, "bnd_lyr")
                arcpy.MakeFeatureLayer_management(copy_ft, "in_lyr") 
                arcpy.SelectLayerByLocation_management ("in_lyr", "INTERSECT", "bnd_lyr")
                arcpy.CopyFeatures_management("in_lyr", output_ft)


But I get an error message on the second round of the processing, as the boundary feature layer already exists:

ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000725: Output Layer: Dataset bnd_lyr already exists.
Failed to execute (MakeFeatureLayer).

Does that mean I have create a unique boundary layer name for each iteration of the series? Eg. "bnd_lyr" + str(x) , while x >= 0 and x< 10 ?

Is there some other way to remove or delete the feature layer?

Many thanks!
Tags (2)
0 Kudos
6 Replies
JamesSnider
New Contributor
Simple answer, isn't it?

arcpy.Delete_management...
0 Kudos
MathewCoyle
Frequent Contributor
You could use DeleteFeatures_management ("bnd_lyr") at the end of your loop.
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001700000036000000.htm
That should just remove your feature layer each time.
0 Kudos
JamesSnider
New Contributor
Thanks, Mathew. That would also work. I was expecting a simple solution, but couldn't find the tool. Thanks!

James
0 Kudos
JakeSkinner
Esri Esteemed Contributor
You can also add 'arcpy.env.overwriteOutput = True' at the start of your code.  This will overwrite the feature layer.
0 Kudos
ChrisSnyder
Regular Contributor III
How about this:

arcpy.env.overwriteOutput = True
arcpy.MakeFeatureLayer_management(copy_ft, "in_lyr")
boundary_file_list = arcpy.ListFeatureClasses()
for boundary_file in boundary_file_list:
   arcpy.SelectLayerByLocation_management ("in_lyr", "INTERSECT", boundary_file)
   arcpy.CopyFeatures_management("in_lyr", output_ft)
0 Kudos
JamesSnider
New Contributor
The overwrite option is a good idea as well. Although, when implementing that approach, I find I need to be carefule to use "arcpy.Exists(output_feature) != True:" to ensure that I don't go overwriting everything...

That could be the simplest solution, if you aren't considered about lossing outputs that have already been created.
0 Kudos