How can I copy the values from multiple fields from one feature class to another, based on a spatial join and name query

1377
10
Jump to solution
09-23-2016 07:11 AM
AyomipoAdeyemo
New Contributor

I'm trying to write a standalone script to copy the values from 6 fields in a feature class to another feature class based on a spatial join and name query. I am stuck at updating the values from the join table to my original table.

print "Importing modules..."
import sys, arcpy, traceback


arcpy.env.workspace = r"C:\LVS\PECO\PECO\Python\Python\Script\test_code.gdb\GAS"
arcpy.env.overwriteOutput = True

# Local variables:
GFR = "GFR"
Parcel = "PECO_TaxParcels"
GFR_SpatialJoin = "C:\\Users\\aadeyemo\\Documents\\ArcGIS\\Default.gdb\\GFR_SpatialJoin"

arcpy.MakeFeatureLayer_management("GFR", GFR)
arcpy.MakeFeatureLayer_management("PECO_TaxParcels", Parcel)
# Process: Select Layer By Attribute
arcpy.SelectLayerByAttribute_management(GFR, "NEW_SELECTION", "STREET_NAME IS NULL")
# Process: Spatial Join
arcpy.SpatialJoin_analysis(GFR, Parcel, GFR_SpatialJoin, "JOIN_ONE_TO_ONE", "KEEP_ALL")

path_dict1 = {}

SC1 = arcpy.da.SearchCursor (GFR_SpatialJoin, ["TARGET_FID", "STHSNUM", "STSTNAME", "STSUFFIX", "STCITY", "STSTATE", "STZIP"])
for row in SC1:
   path_dict1[ row[0] ] = row[1]
   print SC1[1]
   print path_dict1
del row

ucursor = arcpy.da.UpdateCursor(GFR, ["OBJECTID", "STREET_NUMBER", "STREET_NAME", "STREET_SUFFIX", "CITY", "STATE", "ZIP_CODE"])
for urow in ucursor:
   if path_dict1.has_key( urow[0]):
   urow[1] = path_dict1[0]‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

This currently returns a value of zero when i try to copy values.

0 Kudos
1 Solution

Accepted Solutions
MitchHolley1
MVP Regular Contributor

Have you tried using a list instead of a dictionary?

path_list =[]

path_split = []

 

with arcpy.da.SearchCursor (GFR_SpatialJoin, ["TARGET_FID", "STHSNUM", "STSTNAME", "STSUFFIX", "STCITY", "STSTATE", "STZIP"]) as cursor:
   for row in cursor:
      path_list.append(row[0] + '|' + row[1] + '|' + row[2] + '|' + row[3] + '|' + row[4] + '|' + row[5] + '|' + row[6])
del row

 

for p in path_list:

   path_split.append(p.split('|'))

with arcpy.da.UpdateCursor(GFR, ["OBJECTID", "STREET_NUMBER", "STREET_NAME", "STREET_SUFFIX", "CITY", "STATE", "ZIP_CODE"]) as cursor:
   for row in cursor:

      for p in path_split:
         if str(row[0]) == s[0]:

               row[1] = s[1]

               row[2] = s[2]

               row[3] = s[3]

               row[4] = s[4]

               row[5] = s[5]

               row[6] = s[6]

         cursor.updateRow(row)

View solution in original post

10 Replies
MitchHolley1
MVP Regular Contributor

Have you tried using a list instead of a dictionary?

path_list =[]

path_split = []

 

with arcpy.da.SearchCursor (GFR_SpatialJoin, ["TARGET_FID", "STHSNUM", "STSTNAME", "STSUFFIX", "STCITY", "STSTATE", "STZIP"]) as cursor:
   for row in cursor:
      path_list.append(row[0] + '|' + row[1] + '|' + row[2] + '|' + row[3] + '|' + row[4] + '|' + row[5] + '|' + row[6])
del row

 

for p in path_list:

   path_split.append(p.split('|'))

with arcpy.da.UpdateCursor(GFR, ["OBJECTID", "STREET_NUMBER", "STREET_NAME", "STREET_SUFFIX", "CITY", "STATE", "ZIP_CODE"]) as cursor:
   for row in cursor:

      for p in path_split:
         if str(row[0]) == s[0]:

               row[1] = s[1]

               row[2] = s[2]

               row[3] = s[3]

               row[4] = s[4]

               row[5] = s[5]

               row[6] = s[6]

         cursor.updateRow(row)

AyomipoAdeyemo
New Contributor

Hi, 

Thanks, i just tried it but it returned error: "s" not defined. Is s =  'path_list'?

0 Kudos
MitchHolley1
MVP Regular Contributor

Oops.. replace 's' with 'p'.

row[1] = p[1]

row[2] = p[2]

and so on...

AyomipoAdeyemo
New Contributor

I just got the chance to try it today. The code works!!! Thanks mitchh300

0 Kudos
RebeccaStrauch__GISP
MVP Esteemed Contributor

Ayomipo Adeyemo   Don't forget to close this thread by marking Mitch Holley's answer as correct.

Also, it would be helpful for others in the future if you change your question/title to something a bit more meaningful. That way others can search and find the answer if they have the same question.  Even though it may look like you can't change it, you should be able to edit it.  If you can't do it, one of us can help.

Suggestion:

How can I copy the values from multiple fields from one feature class to another, based on a spatial join and name query.

AyomipoAdeyemo
New Contributor

I cant seem to figure out hoe to edit the title of this question. @Rebecca Strauch can you help? Thanks

0 Kudos
RebeccaStrauch__GISP
MVP Esteemed Contributor

Done.  If you click the Edit button on the top right, you can then edit the title or question.  It doesn't necessarily look like the title is an option, but click on it and you can do it.  For next time. 

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Hi... make sure you format your code... formatting isn't guaranteed unless you use syntax highlighting

AyomipoAdeyemo
New Contributor

Yes. i took care of that. Thanks

0 Kudos