AnsweredAssumed Answered

Is there a way to use a spatial selection to populate fields in another layer via python?

Question asked by RP.CObb on Oct 16, 2019
Latest reply on Oct 22, 2019 by RP.CObb

Hi,

 

I have a script where I spatially select another layer, and the selected records in that layer are then used to populate the fields in the layer that was used for the spatial selection. So specifically I am trying to transfer attributes, but I did some research and the only concern with scripting the Transfer Attributes is the additional field that may get added. I believe I could possibly delete the field to fix that issue but I am unfortunately working in an sde database and I don't want to risk messing with the schemas. If anyone has another possible solution for this I would be very open to it.

 

with arcpy.da.UpdateCursor(fc, ["OID@", "LANDDISTRICT", "LANDLOT"], """LANDDISTRICT IS NULL OR LANDLOT IS NULL""") as selected_fc:
            for row in selected_fc:
               
                #Create temp fc copy
                arcpy.MakeFeatureLayer_management(fc, r"in_memory\fcLayer")

                #Select layer by attribute
                arcpy.SelectLayerByAttribute_management (r"in_memory\fcLayer", "NEW_SELECTION", "OBJECTID = {}".format(row[0]))

                #Select layer by location
                arcpy.SelectLayerByLocation_management(r"in_memory\DLL_layer", "INTERSECT", r"in_memory\fcLayer", '', "NEW_SELECTION")
               

                #If selected district and landlot in row are null
                if row[1] is None and row[2] is None:
                    print '{} Land District is {} and Land Lot is {} '.format(row[0], row[1], row[2])

                    def DL():

                        for DL in arcpy.da.SearchCursor(r"in_memory\DLL_layer", ["LAND_DIST"]):                
                            print DL
                        return DL[0]

                    def LL():

                        for LL in arcpy.da.SearchCursor(r"in_memory\DLL_layer", ["LAND_LOT"]):
                            print LL
                        return LL[0]
                   
                    row[1] = DL()
                    row[2] = LL()
                   
                    selected_fc.updateRow(row)
                    print '{} Land District is {} and Land Lot is {} '.format(row[0], row[1], row[2])

I have tried several attempts with this and I either run into the same error or an error referring to something else.

Traceback (most recent call last):
  File "U:\Models_Tools\Scripts related to Landlot and District\Populate Landlot and District Test 2.py", line 65, in <module>
    row[1] = DL()
  File "U:\Models_Tools\Scripts related to Landlot and District\Populate Landlot and District Test 2.py", line 57, in DL
    return DL[0]
UnboundLocalError: local variable 'DL' referenced before assignment

Outcomes