I am trying to write a script that will select points from a feature class that are within a polygon and update the fields within the original point feature class. The problem I'm running into is that I have to use the make feature layer tool in order to have features that can be iterated through with an update cursor. The script I wrote works but the end result is a new feature class. I really need the changes applied to the original data. This post, Select by attribute in feature layer changes the original shapefile, seems related to my issue by some degree but as though the o.p. is having the opposite of my problem within the modelbuilder environment.
Is there a way to make changes to the original data on disk when working with feature layers?
import arcpy, os, datetime, dateutil
from arcpy import mapping, env
from dateutil import parser
refFolder = arcpy.GetParameterAsText(0)
date = arcpy.GetParameterAsText(1)
reviewer = arcpy.GetParameterAsText(2)
formDate = parser.parse(date).strftime('%Y%m%d')
grid = r'C:\Users\ah2703\AppData\Roaming\ESRI\Desktop10.2\ArcCatalog\SDE 29910 on srv80_SDE101.sde\Team_60.SDE.CFW_TopoQC\Team_60.SDE.CFW_1965_TOPO_GRID_INDEX'
qcPoints = r'C:\Users\ah2703\AppData\Roaming\ESRI\Desktop10.2\ArcCatalog\SDE 29910 on srv80_SDE101.sde\Team_60.SDE.CFW_TopoQC\Team_60.SDE.CFW_1965_TOPO_QC_PTS' #This is the feature class I want to update
fc = arcpy.CreateFeatureclass_management("N:/29000s/29910/GIS/John_Working/Scratch.gdb/", "QC_" + str(formDate),"POINT", qcPoints)
arcpy.overwriteOutputs = True
arcpy.env.overwriteOutput = True
arcpy.MakeFeatureLayer_management(qcPoints, "qcPoints_lyr") #This is the feature layer made from the actual data.
for fileName in os.listdir(refFolder): #Location I reference to get unique names for each file in a folder.
name1 = str(fileName)[5:-8]
name2 = str(fileName)[9:-4]
fileNameFull = str(name1) + "-" + str(name2)
altName = str(name1) + "_" + str(name2)
arcpy.MakeFeatureLayer_management(grid, "singleGrid_lyr", ' "INDEX_NO" = ' + "'%s'" %fileNameFull) #This is where I make a feature layer out of a single polygon in the fc.
arcpy.SelectLayerByLocation_management ("qcPoints_lyr", "have_their_center_in", "singleGrid_lyr") #selecting points that fall within the polygon layer.
rows = arcpy.UpdateCursor("qcPoints_lyr") #Here is teh part where I use an update cursor to modify the data within the new feature layer.
for row in rows:
arcpy.AddMessage(str(fileNameFull))
if row.isNull("TILE_INDEX_NO"):
row.setValue("TILE_INDEX_NO", str(altName))
rows.updateRow(row)
arcpy.AddMessage("Tile Index Number Updated!")
else:
arcpy.AddMessage("Tile Index Number already populated.")
if row.isNull("REVIEWER"):
row.setValue("REVIEWER", str(reviewer))
rows.updateRow(row)
arcpy.AddMessage("Reviewer Name Updated!")
else:
arcpy.AddMessage("Reviewer Name already populated.")
if row.isNull("REVIEW_DATE"):
row.setValue("REVIEW_DATE", date)
rows.updateRow(row)
arcpy.AddMessage("Review Date Updated!")
else:
arcpy.AddMessage("Review Date already populated.")
del rows
arcpy.Append_management(["qcPoints_lyr"], fc, "TEST","","") # consolidating the new data.
update: New code that is working.
import arcpy, os, datetime, dateutil
from arcpy import mapping, env
from dateutil import parser
refFolder = arcpy.GetParameterAsText(0)
date = arcpy.GetParameterAsText(1)
reviewer = arcpy.GetParameterAsText(2)
formDate = parser.parse(date).strftime('%Y%m%d')
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
arcpy.overwriteOutputs = True
arcpy.env.overwriteOutput = True
qcPoints = arcpy.mapping.ListLayers(df,"QC Point")[0]
grid = arcpy.mapping.ListLayers(df,"CFW_1965_TOPO_GRID_INDEX")[0]
for fileName in os.listdir(refFolder):
name1 = str(fileName)[5:-8]
name2 = str(fileName)[9:-4]
fileNameFull = str(name1) + "-" + str(name2)
altName = str(name1) + "_" + str(name2)
arcpy.SelectLayerByAttribute_management(grid, "NEW_SELECTION", ' "INDEX_NO" = ' + "'%s'" %fileNameFull)
arcpy.CopyFeatures_management(grid, "N:/29000s/29910/GIS/John_Working/Scratch.gdb/" + "Temp_Grid_" + str(altName))
arcpy.MakeFeatureLayer_management("N:/29000s/29910/GIS/John_Working/Scratch.gdb/" + "Temp_Grid_" + str(altName), "singleGrid_lyr")
arcpy.SelectLayerByLocation_management(qcPoints, "have_their_center_in", "singleGrid_lyr")
rows = arcpy.UpdateCursor(qcPoints)
for row in rows:
arcpy.AddMessage(str(fileNameFull))
if row.isNull("TILE_INDEX_NO"):
row.setValue("TILE_INDEX_NO", str(altName))
rows.updateRow(row)
arcpy.AddMessage("Tile Index Number Updated!")
else:
arcpy.AddMessage("Tile Index Number already populated.")
if row.isNull("REVIEWER"):
row.setValue("REVIEWER", str(reviewer))
rows.updateRow(row)
arcpy.AddMessage("Reviewer Name Updated!")
else:
arcpy.AddMessage("Reviewer Name already populated.")
if row.isNull("REVIEW_DATE"):
row.setValue("REVIEW_DATE", date)
rows.updateRow(row)
arcpy.AddMessage("Review Date Updated!")
else:
arcpy.AddMessage("Review Date already populated.")
del rows