So I'm fairly new to arcpy and have been trying to figure out a relatively simple select by location/extract features to new layer automation in ArcMap. What I am trying to do is select features from about 40 target layers from an SDE database that are within a parish/county layer then extract them to new layers that go by the same layer name they are being outputted from but in a different location.
Below I have a couple of general issues:
1) The first for loop does not work because it will not accept "fc" as input. What I was planning out was to have it loop through each feature class and apply the parish/county layer over it to apply the selection. Of course, this does not work. This may just be due to my misuse of "fc".
2) The second for loop runs but it stops once it gets tot he second iteration/layer of the loop because it can't overwrite the filename that was already created in the first iteration. This for loop is intended to loop through each feature class and extract the same feature class to a different folder using the same layer name it was outputted from.I should also mention that the layer names go by "HM_Production.DBO.Agriculture", HM_Production.DBO.Banking", etc. and when I check the file its trying to overwrite, it has it outputted as "HM_Production.DBO". I assume this has something to do with the dot notation of the layer name?
EDIT: This is what I'm using now. It's a combination of everything so far. It goes through everything fine but is still giving me the output error of "line 2434, in CopyFeatures raise e ExecuteError: ERROR 000210: Cannot create output C:\output\HM_Production_DBO_WATER_Water_Storage_Unit.shp Failed to execute (CopyFeatures). I should also mention that all of the scripts that have been suggested have the same issue. :
import arcpy
from arcpy import env
env.workspace = r'C:\ArcScratch\HM_Production.sde'
stateFeature = r'C:\ArcScratch\State\Parishes_LDOTD_2007.shp'
parish = "Orleans"
nameField = "PARISH"
arcpy.MakeFeatureLayer_management(stateFeature, 'SelectionStateLayer', '"' +
str(nameField) + '" = ' + "'" + str(parish) + "'")
for fc in arcpy.ListFeatureClasses():
if arcpy.Exists('lyr'):
arcpy.Delete_management('lyr')
arcpy.MakeFeatureLayer_management(fc, 'lyr')
arcpy.SelectLayerByLocation_management('lyr', "WITHIN", 'SelectionStateLayer')
outname = fc.replace(".","_") #replace all . with _
outpath = r"C:\output\{0}.shp".format(outname)
if arcpy.Exists(outpath):
arcpy.Delete_management(output)
arcpy.CopyFeatures_management('lyr', outpath)
arcpy.Delete_management('lyr')
if arcpy.Exists()
statements are off, and that's my fault because I posted bad code earlier. Please see my newly edited post! Sorry about that! Whatever you're checking is what should be deleted in the if statement. Again, that's my fault!