I am building a script to populate a field in one feature class based on a field in another feature class. First I have a leases layer which is all small polygons. Then I have an index grid layer which is large polygons. I want to know which leases exist within which index grid polygon. The index grid layer has an "Id" field, and I want the Id value from the index grid that each lease exists within to be written to a new column in the leases layer.
For example, index grid polygon with an 'Id' of '7' contains leases '14' '6' and '12'. The final result should be a column where lease 14, 6, and 12 all have a value of '7' in the new column.
Here is what I have so far:
import arcpy, sysgrid = arcpy.GetParameterAsText(0)leases = arcpy.GetParameterAsText(1)workspace = arcpy.GetParameterAsText(2)new_field = arcpy.GetParameterAsText(3)if arcpy.ListFields(leases, new_field): sys.exit(arcpy.AddError("Error:Field Already exists"))arcpy.AddField_management(leases, new_field, 'SHORT', '', '', '')grid_lyr = arcpy.MakeFeatureLayer_management(grid, 'grid_lyr')leases_lyr = arcpy.MakeFeatureLayer_management(leases, 'leases_lyr')fields = ['file',new_field]with arcpy.da.UpdateCursor(leases_lyr,fields) as cur: for row in cur: leases = row[0] where = '"file" = \'{}\''.format(leases) arcpy.SelectLayerByAttribute_management(leases_lyr, 'NEW_SELECTION', where) arcpy.SelectLayerByLocation_management(grid_lyr, 'CONTAINS', leases_lyr) map_no = #I think this is all that is missing row[1] = map_no cur.updateRow(row)The value 'map_no' represents the index grid Id which the lease belongs to.
As I have a comment in my code, I think there is only one line that is missing...in the original script which this is derived from, this line uses the arcpy.GetCount_management, but obviously it needs to be something different. It just needs to be some kind of get value function, but I simply cannot figure it out.
I derived this work so far from a script that counts the number of points in in a polygon, and then writes that value to a new column in the polygon layer. I mention this because maybe there is a better solution.
Using ArcMap 10.1.
أكثر...
For example, index grid polygon with an 'Id' of '7' contains leases '14' '6' and '12'. The final result should be a column where lease 14, 6, and 12 all have a value of '7' in the new column.
Here is what I have so far:
import arcpy, sysgrid = arcpy.GetParameterAsText(0)leases = arcpy.GetParameterAsText(1)workspace = arcpy.GetParameterAsText(2)new_field = arcpy.GetParameterAsText(3)if arcpy.ListFields(leases, new_field): sys.exit(arcpy.AddError("Error:Field Already exists"))arcpy.AddField_management(leases, new_field, 'SHORT', '', '', '')grid_lyr = arcpy.MakeFeatureLayer_management(grid, 'grid_lyr')leases_lyr = arcpy.MakeFeatureLayer_management(leases, 'leases_lyr')fields = ['file',new_field]with arcpy.da.UpdateCursor(leases_lyr,fields) as cur: for row in cur: leases = row[0] where = '"file" = \'{}\''.format(leases) arcpy.SelectLayerByAttribute_management(leases_lyr, 'NEW_SELECTION', where) arcpy.SelectLayerByLocation_management(grid_lyr, 'CONTAINS', leases_lyr) map_no = #I think this is all that is missing row[1] = map_no cur.updateRow(row)The value 'map_no' represents the index grid Id which the lease belongs to.
As I have a comment in my code, I think there is only one line that is missing...in the original script which this is derived from, this line uses the arcpy.GetCount_management, but obviously it needs to be something different. It just needs to be some kind of get value function, but I simply cannot figure it out.
I derived this work so far from a script that counts the number of points in in a polygon, and then writes that value to a new column in the polygon layer. I mention this because maybe there is a better solution.
Using ArcMap 10.1.
أكثر...