0

I have a map containing Polylines. There are some blank field names. I want to fill them with the name of connected features. How to use python to find the connected features and update the attribute table? My strategy is to read each row of the attribute table and find the field name of corresponding connected feature. If there are multiple connected features, just pick one of them.

I'm fairly new to ArcGIS.

The "connected" here means INTERSECT (A, C, D, E, F, G, H, I, J)

enter image description here

The following code is what I have so far.

import arcpy
my_shp = "Z:\Desktop\example\Export_Output.shp"
rows = arcpy.UpdateCursor(my_shp)
arcpy.MakeFeatureLayer_management(my_shp, "new_lyr")
arcpy.AddField_management(my_shp, "NEWFIELD", "TEXT", "", "", "25", "", "NON_NULLABLE", "NON_REQUIRED", "")
for row in rows:
    targetRow = row.targetfield
    query = """ "NAME" = '%s'""" % targetRow
    arcpy.SelectLayerByAttribute_management("new_lyr", "NEW_SELECTION", query)
    my_selection = arcpy.SelectLayerByLocation_management("new_lyr", "INTERSECT", my_shp, "", "NEW_SELECTION")
    ##################
    # unfinished here#
    ##################
    print(targetRow)
    print(THE RETURN NAME)
    #update the NEWFIELD with the return name
    row.NEWFIELD = return_name  #the return name
    rows.updateRow(row)
del row
del rows

Attribute table

enter image description here


Expected result

For example, The field NAME of FID = 11 is blank and if featcure of FID = 7 is connected to it, then fill the blank NAME with "S32"

enter image description here


Data

  • 2
    Please don't just delete and re-ask the same question. You are welcome to edit to refine and clarify as required, but deleting loses the history of questions/comments others have already made – Midavalo Sep 17 '16 at 2:14
  • ... and must be putting you perilously close to automated question limiting/banning kicking in. Deleting questions is something that should be done only as a last resort. Lots of deletions early in the life of an account suggests to the algorithms that a user is not working within the focussed Q&A format of the site. – PolyGeo Sep 17 '16 at 2:19
  • To prevent your question being too broad I think it needs to be focussed from How to use python to find the connected features and update the attribute table? to How to use python to find the connected features?. You clearly already know how to update the attribute table, so anything in your question about that is distracting to potential answerers and future visitors to the site who might benefit from this Q&A. – PolyGeo Sep 17 '16 at 2:26
  • The issue of re-posting deleted questions came up today on Meta SE: meta.stackexchange.com/questions/284628/… The answer seems relevant to your question, and its deleted predecessors: meta.stackexchange.com/a/284641/215590 – PolyGeo Sep 17 '16 at 3:06
  • I suggest creating basic model and export it to script, if this IS about scripting. Create layer using definition query name=''. Create another name<>''. Spatial join and transfer name to noname layer. Rerun model until feature count in first layer=0 – FelixIP Sep 17 '16 at 3:17
3

This answer uses ArcPy Data Access (arcpy.da) cursors which were introduced at ArcGIS 10.1 for Desktop, and so can only be used in its entirety at that version or later. It was formulated prior to the asker indicating that they were using ArcGIS Desktop 10.0.

  • project your file, I suggest WGS_1984_UTM_Zone_50N
  • keep in mind that your no name line has name = SPACE, i.e. ' '

Input:

enter image description here

Script:

import arcpy
from arcpy import env
env.overwriteOutput = True
sj=r"in_memory\sj"
infc=r"c:\felix_data\projected.shp"
noName,withName="noname","withname"
empty="''"
while True:
    arcpy.MakeFeatureLayer_management(infc, noName,'"NAME"=%s' %empty)
    arcpy.MakeFeatureLayer_management(infc, withName,'"NAME"<>%s' %empty)
    arcpy.SpatialJoin_analysis(noName,withName,sj,"JOIN_ONE_TO_ONE","KEEP_COMMON")
    result=arcpy.GetCount_management(sj)
    nF=int(result.getOutput(0))
    if nF==0:break
    aDict={}
    with arcpy.da.SearchCursor(sj,("REC","NAME_1")) as cursor:
        for rec,name in cursor:aDict[rec]=name
    updates=0
    with arcpy.da.UpdateCursor(infc,("REC","NAME")) as cursor:
        for rec,name in cursor:
            if rec in aDict:
                cursor.updateRow((rec,aDict[rec]))
                updates+=1
    arcpy.Delete_management("in_memory")
    arcpy.Delete_management(noName)
    arcpy.Delete_management(withName)
    arcpy.AddMessage("Updated %s" %updates)

OUTPUT:

enter image description here

  • 1
    +1 although the OP has just commented that he's using ArcGIS 10.0 so arcpy.da won't be available – Midavalo Sep 17 '16 at 6:20
  • I get an error, " for rec,name in cursor:aDict[rec]=name RuntimeError: A column was specified that does not exist." Shall I add two fields "REC" and "NAME_1" before run the code? – XJ.C Sep 17 '16 at 7:46
  • I used your data and it works, I have no clue how you change it, thus no advice – FelixIP Sep 17 '16 at 9:09
  • @Midavalo error message, see his her comment above tells different story, not missing da module – FelixIP Sep 17 '16 at 21:42
  • It is missing da module before update ArcGis @FelixIP – XJ.C Sep 18 '16 at 6:03
2

You could consider using (the very powerful) Attribute Assistant Add-In.

It has an Intersecting Feature method that copies a value from an intersecting feature in the specified layer.

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.