AnsweredAssumed Answered

Update values or copy values from one shape file (polyline) to another shapefile (polygon).

Question asked by agr_wisc on Dec 5, 2017

I want to update field values from one shape file (field "GRID_CODE" in "lyr_riv") to another shape file ("lyr_SUBB").
The code i am using is below. But i am getting runtime error.Spatial join is not a viable option.Because it will duplicate some values.

#first creating feature class
arcpy.MakeFeatureLayer_management(r"E:/brief_model/predefined_FIXEDCODE/SUBB.shp", "lyr_SUBB") #subbasin

arcpy.MakeFeatureLayer_management(r"E:/brief_model/predefined_FIXEDCODE/riv.shp", "lyr_riv") #river

# Add an "SIDE_CODE" field in subbasin layer
arcpy.AddField_management("lyr_SUBB", "new_CODE", "SHORT", "", "", "20")

# Create a search cursor for the states
rows = arcpy.SearchCursor("lyr_riv")
for row in rows:
arcpy.SelectLayerByAttribute_management("lyr_riv", "NEW_SELECTION", "\"GRID_CODE\" = " + str(row.getValue("FID")))
arcpy.SelectLayerByLocation_management("lyr_SUBB", "INTERSECT", "lyr_riv", "", "NEW_SELECTION")
arcpy.CalculateField_management("lyr_SUBB", "SITE_CODE", "'{0}'".format(str(row.getValue("lyr_riv"))), "PYTHON_10.4.1", "")
print "Finished processing " + str(row.getValue("lyr_riv"))

I got an error at point select by location. Instead of grid code values based on " lyr_riv", now I have all zeros in "my new code field". I guess that is my main problem. Because same grid number is at different location in
my both shape file. I want same grid_code to be at same location in both files.
I guess I can use update cursor too, because both shape files has "GRID_CODE" id but values are not same spatially. Creating a new field column (in "lyr_SUBB" with updated or copied grid values (from "lyr_riv") will be helpful 

Your feedback on my code will be helpful

Outcomes