AnsweredAssumed Answered

CopyFeatures_management from a Python script returns empty layer

Question asked by nmichelg on Dec 7, 2015
Latest reply on Dec 7, 2015 by nmichelg

I am still very new at Python scripting within ArcGIS and I have a script that clips a shapefile to a new AOI, then writes a field to it and updates it with an Update Cursor. I have been successful in using the script up until the CopyFeatures_management part. The script returns a code 0 and has added the new field and has created the correct named shapefile but the file is empty.

Any help would be welcome.

 

import arcpy

# set local variables
arcpy.env.workspace = "C:/Final Project/GIS"
arcpy.env.overwriteOutput = True

fc = "settlement_dd.shp"
clip_fc = "AOI.shp"
out_fc = "Settlement.shp"
xy_t = ""
fields = ["Name", "Type", "Model"]

# set attribute table and new fc variables
toModel = "YES"
notModel = "No"
modelFC = "Model"

try:
    # clip point features
    arcpy.Clip_analysis(fc, clip_fc, out_fc, xy_t)    
    # add a field called "Model" to the fc
    arcpy.AddField_management(out_fc, "Model", "Text")
except:
    arcpy.AddError("Could not Complete Clip and AddField")

try:
    # create updateCursor and go through rows making the changes to the "Name" field first
    with arcpy.da.UpdateCursor(out_fc,(fields)) as cursor:
        for row in cursor:           
            #  update "Name" field with Boma, Building, or TBD
            if row[1] == "Huts 1-2" or row[1] == "Huts 3-5" or row[1] == "Livestock Enclosure":
                row[0] = "Boma"
               
            elif row[1] == "Buildings 1-2" or row[1] == "Buildings 3-5":
                row [0] = "Buildings"
                
            elif row[1] == "Settlement":
                row[0] = "TBD"

            # update "Model" field with Yes or NO
            if row[0] == "Boma" or row[0] == "TBD":
                row[2] = "notModel"

            elif row[0] == "Buildings":
                row[2] = "toModel"
            cursor.updateRow(row)
except:
    arcpy.AddError("Could not update rows")

try:
    # make feature layer from points
    pointsQuery = ' "Model" = ' + "'" + toModel + " ' "   
    arcpy.MakeFeatureLayer_management(out_fc, "settlement_lyr", pointsQuery)
    arcpy.CopyFeatures_management("settlement_lyr", pointsQuery, modelFC + ".shp")
except:
    arcpy.AddError("Copy Features Unsuccessful")
finally:
    arcpy.Delete_management("settlement_lyr")

 

The correct (albeit empty) layer is indicated in the attached .jpg

Attachments

Outcomes