3

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
  • 1
    It looks like you are trying to select once, all points that fall within "ANY" polygon. Is that correct? If not you may need to put your code inside of a loop (searchcursor) that goes through each polygon. That may be the issue. – jbchurchill Oct 14 '14 at 13:26
  • Actually, there are two "selections". The make feature layer command has a where clause in it that selects only one polygon. The code above works except for the fact that I can't figure out how to apply a search cursor on anything but a feature layer. I want to be able to access and edit the original data on disk. I have a couple ideas that I will try today. Also, anyone know the difference bewtween arcpy.da.UpdateCursor and arcpy.UpdateCursor? – GeoJohn Oct 14 '14 at 13:53
  • 1
    The da.UpdateCursor is supposedly faster and more efficient. I was going to ask if you tried setting rows = arcpy.UpdateCursor("qcPoints") instead of setting rows to "qcPoints_lyr". The MakeFeatureLayer allows you to do the selection. It shouldn't affect what you set the updateCursor to. I'm not sure if that would work or not but I would try it (on a backup of course). – jbchurchill Oct 14 '14 at 14:08
  • @jbchurchill, Thanks! What you suggested has played into the solution for sure. Instead of making feature layers at all from on disk data, I've just referenced the existing layers from within the map document. I should have been doing this in the first place probably. Anyways, it seems to be working perfectly now. I'll paste the new code above if anyone is interested. Thanks for you're help. – GeoJohn Oct 14 '14 at 14:26
3

Try using ...

set rows = arcpy.UpdateCursor("qcPoints")

instead of setting rows to qcPoints_lyr. You should be able to do away with the Append that way. I thought that might work and it sounds like you confirmed it. MakeFeatureLayer is needed for doing selections but you don't need to set your updateCursor to the output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.