ArcPy – Fix Error 000732: Invalid Features in Select by Location and Copy Features

arcmaparcpyerror-000732result-objectselect-by-location

I've been reading through some of the other answers but I haven't found a solution to my problem yet. I'm working with ArcMap in Python 2.7 in a standalone script.

I'm trying to select some features based on a two shapefiles in a given .mxd, and export the selection to a new featureclass or shapefile.

Currently my code lists the layers in my .mxd, creates the selection, and attempts to use the CopyFeatures_management() function – but I get the exception

ERROR 000732: Input Features: Dataset contaminated_land does not exist or is not supported
Failed to execute (MakeFeatureLayer).

Here is my code so far:

import arcpy, os, glob, arcpy.mapping as map

arcpy.env.workspace = "M:\GIS\Oscar\Land_Charges"

os.chdir(arcpy.env.workspace)

mxd_list = glob.glob('*.mxd')

mxd = mxd_list[0]
my_mxd = map.MapDocument(mxd)


for mxd in mxd_list: # Looping through all the .mxd's in a folder
    print mxd
    my_mxd = map.MapDocument(mxd)

frame_list = map.ListDataFrames(my_mxd)
df = frame_list[0]

activeframe = my_mxd.activeDataFrame

layers_list = map.ListLayers(my_mxd,'*',df)

test_poly = layers_list[0]
#test_poly = "M:\GIS\Oscar\Land_Charges\land_charges.gdb\\test_poly"
contaminated_land = layers_list[3]

select = arcpy.SelectLayerByLocation_management(contaminated_land,
                                                'WITHIN',
                                                test_poly,
                                                None,
                                                'NEW_SELECTION'
                                                )

arcpy.MakeFeatureLayer_management(select, 'contam_select')

arcpy.Delete_management('contaminated_land_select.shp')

arcpy.CopyFeatures_management(select, 'contaminated_land_select.shp')

What I am confused about is the contaminated_land shapefile works okay in the SelectLayerByLocation_management() function, but then tells me that it is invalid for MakeFeatureLayer_management() or CopyFeatures_management()?

Any ideas?

Best Answer

Geoprocessing tools return result objects. So what you are calling select is a result object, you would query that for the actual output which would be a count. If count is non-zero then you would act on it.

All tools honor selections so no need to make a new feature layer, simply just feed the layer with the selection into the next tool. That would be contaminated_land.

Related Question