Update attributes of selected

1674
20
09-10-2020 11:06 AM
2Quiker
Occasional Contributor II

Trying to update some well data from another layer. I am trying to do this in Arcmap (10.6) with a tool/script (3.6.10 Python).

I need to transfer attributes from WellParcels to WellsPoints. I would like to do this with out starting a Edit session in python if possible as this tends to slow down the process...? I would manual start an edit session in ArcMap then run the tool/script when I have points selected, I currently have the following but error out on line 44. I am not sure this is the best/fastest way of doing it, so if someone has a different idea please share how I would do this with my data.

import arcpy,os

arcpy.env.overwriteOutput = True

arcpy.env.workspace = r'Database Connections\Connection to blah.sde'
ptSelection = "WellPoints"  
pointLayer = arcpy.env.workspace + os.sep + "WellPoints"  
parcel = 'WellParcels' 
sjpoints = "In_memory\sjpoints"


try: 
    ptCount = int(arcpy.GetCount_management(ptSelection).getOutput(0))

    dsc = arcpy.Describe(ptSelection)     
    selection_set = dsc.FIDSet             
    if len(selection_set) == 0:           
        print "There are no features selected"        
                     
    elif ptCount >= 1:
        arcpy.SelectLayerByLocation_management(parcel,"INTERSECT",ptSelection)
        arcpy.MakeFeatureLayer_management(parcel,"parcel_lyr")        
        
        arcpy.SpatialJoin_analysis(ptSelection, "parcel_lyr" , sjpoints)
        #sourceFieldsList = 'WellID', 'dept', 'type', ' Geothermal' 'GeoWaterUs', 'Temp', 'TempDate'
        #valueDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(sjpoints, sourceFieldsList)}

        fldList = ['WellID', 'dept', 'type', ' Geothermal' 'GeoWaterUs', 'Temp', 'TempDate']          
        fldDict ={}
        print (fldDict)

        targetFields = ['W_ID', 'dept', 'type', ' Geothermal' 'GeoWaterUs', 'Temp', 'TempDate']         
                      
        with arcpy.da.UpdateCursor(ptSelection, targetFields) as cursor:
            for row in cursor
            row[0] = ['W_ID']
            row[1] = ['dep']
            row[2] = ['type']
            row[3] = ['Geothermal']
            row[4] = ['GeoWaterUs']
            row[5] = ['Temp']
            row[6] = ['TempDate']
            #Attributes from Wellparcels     
            curosr.append(fldDict['Account', 'dept', 'type', ' Geothermal' 'GeoWaterUs', 'Temp', 'TempDate'])     
except Exception, e:     
    # If an error occurred, print line number and error message     
    import traceback, sys     
    tb = sys.exc_info()[2]     
    print("Line %i" % tb.tb_lineno)  
    arcpy.AddMessage("Line %i" % tb.tb_lineno)  
    print(e.message)  
    arcpy.AddMessage(e.message)  ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
20 Replies
JoshuaBixby
MVP Esteemed Contributor

On line #44, cursor is spelled wrong.  Also, the ArcPy DA update cursor doesn't have an append method.

2Quiker
Occasional Contributor II

Got it, thanks. So with arcpy.da.UpdateCursor can on be updated with arcpy.da.Editor. I am trying to do this inside Arcmap with an editor session started but it seems as if i use arcpy.da.UpdateCurosr it won't let me.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Yes, update cursors can be used within edit sessions.  What is the code you are running, and what exactly is the error and traceback?

0 Kudos
DanPatterson
MVP Esteemed Contributor

You can't be using python 3.6.10 because

print "There are no features selected" 

would have errored out.

Also, line 35 needs a colon   

for row in cursor

and 36-43 need to be indented.

And, why not remove the try- except stuff, dedent your code accordingly so you get unaltered error messages.  

You get trackback error lines numbers and error messages without them AND no errors get hidden or obfuscated.


... sort of retired...
2Quiker
Occasional Contributor II

Your right I am on 2.7.14.

I figured that I was on 3.6.10 because that the version of the shell I am using.

2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:19:30) [MSC v.1500 32 bit (Intel)]
1.5.2
1.9.3
0.17.0

0 Kudos
2Quiker
Occasional Contributor II

I was able to find something that works but it currently only works for one feature at a time, I need it to work with one or 10,20,50. How can I get this to work with with multiple selected features?

import arcpy,os

arcpy.env.overwriteOutput = True

arcpy.env.workspace = r'Database Connections\Connection to blah.sde'
ptSelection = "WellPoints"  
pointLayer = arcpy.env.workspace + os.sep + "WellPoints"  
parcel = 'WellParcels' 
sjpoints = "In_memory\sjpoints"



ptCount = int(arcpy.GetCount_management(ptSelection).getOutput(0))

dsc = arcpy.Describe(ptSelection)     
selection_set = dsc.FIDSet             
if len(selection_set) == 0:           
    print "There are no features selected"        
                 
elif ptCount >= 1:
    arcpy.SelectLayerByLocation_management(parcel,"INTERSECT",ptSelection)
    arcpy.MakeFeatureLayer_management(parcel,"parcel_lyr")        
    
    fldList = ['WellID', 'dept', 'type', ' Geothermal' 'GeoWaterUs', 'Temp', 'TempDate']          
    fldDict ={}

    if int(arcpy.GetCount_management("parcel_lyr").getOutput(0))==1:        
        for parrow in arcpy.da.SearchCursor("parcel_lyr",fldList):        
            for w in range(len(fldList)):        
                fldDict[fldList]=parrow        
    del parrow

    targetFields = ['W_ID', 'dept', 'type', ' Geothermal' 'GeoWaterUs', 'Temp', 'TempDate']         
                  
    with arcpy.da.UpdateCursor(ptSelection, targetFields) as cursor:
        for row in cursor
        row = []
        row.append(fldDict['WellID']
        row.append(fldDict['dep']
        row.append(fldDict['type']
        row.append(fldDict['Geothermal']
        row.append(fldDict['GeoWaterUs']
        row.append(fldDict['Temp']
        row.append(fldDict['TempDate']
        rows.updateRow(row)
    del row
0 Kudos
DanPatterson
MVP Esteemed Contributor

You still didn't fix the rest like I suggested.  Lines 34 onward

you need a colon after cursor

The remaining lines need to be indented

you redefine row as an empty list

Did you not get any error messages?

    with arcpy.da.UpdateCursor(ptSelection, targetFields) as cursor:
        for row in cursor
        row = []
        row.append(fldDict['WellID']
        row.append(fldDict['dep']
        row.append(fldDict['type']
        row.append(fldDict['Geothermal']
        row.append(fldDict['GeoWaterUs']
        row.append(fldDict['Temp']
        row.append(fldDict['TempDate']
        rows.updateRow(row)

... sort of retired...
2Quiker
Occasional Contributor II

I thought I had pasted the code with the indent and when I pasted the code and without the empty list but I didn't, sorry about that.

The code does have the the indent and empty list removed.

import arcpy,os

arcpy.env.overwriteOutput = True

arcpy.env.workspace = r'Database Connections\Connection to blah.sde'
ptSelection = "WellPoints"  
pointLayer = arcpy.env.workspace + os.sep + "WellPoints"  
parcel = 'WellParcels' 


ptCount = int(arcpy.GetCount_management(ptSelection).getOutput(0))

dsc = arcpy.Describe(ptSelection)     
selection_set = dsc.FIDSet             
if len(selection_set) == 0:           
    print "There are no features selected"        
                 
elif ptCount >= 1:
    arcpy.SelectLayerByLocation_management(parcel,"INTERSECT",ptSelection)
    arcpy.MakeFeatureLayer_management(parcel,"parcel_lyr")        
    
    fldList = ['WellID', 'dept', 'type', ' Geothermal' 'GeoWaterUs', 'Temp', 'TempDate']          
    fldDict ={}

    if int(arcpy.GetCount_management("parcel_lyr").getOutput(0))==1:        
        for parrow in arcpy.da.SearchCursor("parcel_lyr",fldList):        
            for w in range(len(fldList)):        
                fldDict[fldList]=parrow        
    del parrow

    targetFields = ['W_ID', 'dept', 'type', ' Geothermal' 'GeoWaterUs', 'Temp', 'TempDate']         
                  
    with arcpy.da.UpdateCursor(ptSelection, targetFields) as cursor:
        for row in cursor:
            row.append(fldDict['WellID']
            row.append(fldDict['dep']
            row.append(fldDict['type']
            row.append(fldDict['Geothermal']
            row.append(fldDict['GeoWaterUs']
            row.append(fldDict['Temp']
            row.append(fldDict['TempDate']
            rows.updateRow(row)
    del row‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
DanPatterson
MVP Esteemed Contributor

tiny steps... tiny steps...

you still didn't get the crucial line correct

for row in cursor

should be

for row in cursor:

... sort of retired...