Search code examples
gisshapefilearcpyarcmap

source layer updating along with output layer


Source layer is layer, output layer is output. The script is updating the source layer with the new fields and their tally, along with the output layer. I've tried deleting fields from layer at the end; setting fc as a different output, copying fc to ouput at the end and then deleting the fields from fc/layer after that; and copying the source layer right of the bat (conceptually this makes the most sense to me...maybe I did it wrong)...no dice.

Any ideas that would preserve the source layer as is but get this script to run and tally on the output? Thanks for any input!!

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

#buildings
layer = arcpy.GetParameterAsText(1)

#trees
trees = arcpy.GetParameterAsText(2)

#buffer building to search
buffer = arcpy.GetParameterAsText(3)

#tree field interested in - tree condition, tree location, or tree pit
tf = arcpy.GetParameterAsText(4)

#output file
output = arcpy.GetParameterAsText(5)

#make feature layers to reference
treelayer = arcpy.MakeFeatureLayer_management(trees, trees + ".shp")
fc = arcpy.MakeFeatureLayer_management(layer, output)

pit = ["Sidewalk", "Continuous", "Lawn"]


if tf == "Tree Pit":
for a in pit:
    arcpy.AddField_management(fc, a, "SHORT")

with arcpy.da.SearchCursor(fc, ["OBJECTID"]) as fcrows:
    for a in fcrows:

        arcpy.SelectLayerByAttribute_management(fc, "NEW_SELECTION", "OBJECTID={}".format(a[0]))
        arcpy.SelectLayerByLocation_management(treelayer, "WITHIN_A_DISTANCE", fc, buffer, "NEW_SELECTION")

        tlrows = arcpy.da.SearchCursor(treelayer, "SITE")
        list1 = []
        for tlrow in tlrows:

            list1.append(int(tlrow[0]))     

        fcrows1 = arcpy.da.UpdateCursor(fc, pit)
        for fcrow1 in fcrows1:
            if list1.count(1) > 0:
                fcrow1[0] = list1.count(1)
            else:
                fcrow1[0] = 0
            if list1.count(2) > 0:
                fcrow1[1] = list1.count(2)
            else:
                fcrow1[1] = 0
            if list1.count(3) > 0:
                fcrow1[2] = list1.count(3)
            else:
                fcrow1[2] = 0
            fcrows1.updateRow(fcrow1)

Solution

  • You don't want a variable equal to the function -- just make the feature layer.

    arcpy.MakeFeatureLayer_management(layer, output)
    

    Then, subsequent steps should affect only the output layer and ignore the source layer, e.g.:

    for a in pit:
        arcpy.AddField_management(output, a, "SHORT")
    
    with arcpy.da.SearchCursor(output, ["OBJECTID"]) as fcrows: