0

I have a current script that calculates how many wells are in each county. Now, how do I update (fill in) the COUNTIES shapefile attribute fields of 'Well_Count' and 'Density'

import arcpy
arcpy.env.workspace = "C:\\Data"

# For each county calculate the total numbers of wells that fall into that county, and update the county table with the total count.

wellsLayer = "C:\\Data\\Wells.shp"
countiesLayer = "C:\\Data\\COUNTIES.shp"

arcpy.SpatialJoin_analysis(countiesLayer, wellsLayer, "CountyWellsJoin.shp")

with arcpy.da.SearchCursor("CountyWellsJoin.shp", ["COUNTY", "Join_Count"]) as cursor:
    for row in cursor:
        COUNTY = row[0]
        Join_Count = row[1]
        print "There are {0} wells in {1} County".format(Join_Count, COUNTY)
0
2

In order to move the join_count value back to the original shapefile, you would need to have some primary key field that you could use to identify the wells. If COUNTY field is unique, this would serve the purpose.

This could be done using a Python dictionary data structure.

wells_per_county = {row[0]:row[1] for row in arcpy.da.SearchCursor("CountyWellsJoin.shp", ["COUNTY", "Join_Count"])}

Now you could use arcpy.da.UpdateCursor:

with arcpy.da.UpdateCursor("CountyWellsJoin.shp", ["COUNTY", "Well_Count"]) as upd_cur:
  for row in upd_cur:
    row[1] = wells_per_county[row[0]]
    upd_cur.updateRow(row)
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy