CopyFeatures_management from a Python script returns empty layer

3271
3
Jump to solution
12-07-2015 04:44 PM
natashaGregory-Michelman
New Contributor III

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

Reply
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Esteemed Contributor

first step... did you check that the extents overlap...not visually but by examining the extents in the properties of each of the shapefiles

(ps, removed extraneous lines from code to make it easier to read)

PPS

you should really dump those long convoluted if this if that if the other thing, and check whether something is IN something else...

for example

>>> a = "some stuff"

>>> options = ["nope","yup","some stuff"]

>>> if a in options: print("found {}".format(a))

found some stuff

And you need to ensure you have a case where something isn't found which I am not sure you have in those nested loope.

View solution in original post

3 Replies
DanPatterson_Retired
MVP Esteemed Contributor

first step... did you check that the extents overlap...not visually but by examining the extents in the properties of each of the shapefiles

(ps, removed extraneous lines from code to make it easier to read)

PPS

you should really dump those long convoluted if this if that if the other thing, and check whether something is IN something else...

for example

>>> a = "some stuff"

>>> options = ["nope","yup","some stuff"]

>>> if a in options: print("found {}".format(a))

found some stuff

And you need to ensure you have a case where something isn't found which I am not sure you have in those nested loope.

View solution in original post

natashaGregory-Michelman
New Contributor III

Dan you are correct that I was not looking properly into whether the original shapefile was correctly noted in the attribute table but I think the main thing was that I was incorrectly using quotation's around the variables that were to update the table within the updateCursor.

I manually corrected the original file and was able to execute the script with your direction. I'm hoping to be able to do this with python in the future.

  Thankyou so much. I really mean it.

Reply
0 Kudos
natashaGregory-Michelman
New Contributor III

I'm working on the convolution of the if elif statements.

   I see now what you're saying

Reply
0 Kudos