Select to view content in your preferred language

Error 000732 when trying to run MakeFeatureLayer

1020
3
09-27-2012 01:47 PM
RileyCotter
Occasional Contributor
I am fairly new to Python, so bear with me. I am writing a script which loops through a folder containing multiple rasters, and exports polygons from the rasters.  I have gotten the script to reclassify cells using 'Slice', and can output shapefiles, but when I try to MakeFeatureLayer(i need to do this to select certain polygons) from the shapefile it is giving me error 000732 which says the shapefile doesn't exist.

Below is a snippet from the code....I just cant get it to recognize that the shapefile does indeed exist.

import arcpy
from arcpy import env

arcpy.env.workspace = arcpy.GetParameterAsText(0)

arcpy.CheckOutExtension("3D")

try:
    fcs = arcpy.ListRasters()

    for fc in fcs:
        outRaster = "reclass_" + fc

        #Reclassify's raster cells
        arcpy.Slice_3d(fc, outRaster, 2, "EQUAL_INTERVAL")

        outShp = fc.strip(".tif")

        #converts raster to polygon shapefile
        arcpy.RasterToPolygon_conversion(outRaster, outShp, "SIMPLIFY", "VALUE" )  

        outLyr = outShp + "_lyr"

        #make layer from shapefile so I can select by attribute
        arcpy.MakeFeatureLayer_management(outShp, outLyr)
        
        arcpy.SelectLayerByAttribute_management(outLyr, "NEW_SELECTION", ' "GRIDCODE" = 1 ')


Thanks for any help!
Tags (2)
0 Kudos
3 Replies
MathewCoyle
Honored Contributor
Please read.
http://forums.arcgis.com/threads/48475-Please-read-How-to-post-Python-code

Is it your "outShp" variable it is saying does not exist?
0 Kudos
RileyCotter
Occasional Contributor
Sorry about that....Yes it is the outShp variable that the script doesn't recognize
0 Kudos
MathewCoyle
Honored Contributor
Sorry about that....Yes it is the outShp variable that the script doesn't recognize


I would imagine you are looking for an actual shapefile (.shp) to create the feature layer from? Or did you create a feature class in a  geodatabase?

I'd try something like this, probably not really needed depending on your needs, but you should be able to adapt to your purposes if the output type/extension is the issue you are indeed having.

import arcpy

outWorkspace = arcpy.GetParameterAsText(0)

arcpy.env.workspace = outWorkspace

arcpy.CheckOutExtension("3D")

try:
    fcs = arcpy.ListRasters()

    for fc in fcs:
        outRaster = "reclass_" + fc

        #Reclassify's raster cells
        arcpy.Slice_3d(fc, outRaster, 2, "EQUAL_INTERVAL")

        outShp = fc.strip(".tif")

        if outWorkspace.endswith(".gdb"):
            output_type = "FGDB"
            output = os.path.join(outWorkspace, "%s" % outShp)

        elif outWorkspace.endswith(".mdb"):
            output_type = "PGDB"
            output = os.path.join(outWorkspace, "%s" % outShp)

        else:
            output_type = "SHP"
            output = os.path.join(outWorkspace, "%s.shp" % outShp)

        #converts raster to polygon shapefile
        arcpy.RasterToPolygon_conversion(outRaster, output, "SIMPLIFY", "VALUE" )

        outLyr = outShp + "_lyr"

        #make layer from shapefile so I can select by attribute
        arcpy.MakeFeatureLayer_management(outShp, outLyr)

        arcpy.SelectLayerByAttribute_management(outLyr, "NEW_SELECTION", ' "GRIDCODE" = 1 ')
0 Kudos