1

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?

1 Answer 1

1

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.

2
  • Thanks for your answer - I was really confused about what the geoprocessing tools returned and couldn't find the documentation. The shapefile returned seems to have no attributes, though when I change the select by location method to 'INTERSECT', the new selection shapefile returns the entire contaminated_land shapefile instead. Feb 17 at 11:42
  • I found the solution to my select problem so I'll put it here for reference: I reset the spatial index (because my inputs were shapefiles rather than feature classes), and then I found that one of my polygons was a multipart polygon. After using the 'explode multipart features polygon' the intersection seemed to work okay. gis.stackexchange.com/questions/216287/… Feb 17 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.