Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. import arcpy
  2.  
  3. #Set these as the name of the layer in ArcMap's TOC
  4. counties = "County_layer"
  5. hospitals = "Hospital_layer"
  6. #Set this as the name of the field to find max value for
  7. field = "Doctors"
  8.  
  9. object_id = []
  10. hospital_fl = arcpy.MakeFeatureLayer_management(hospitals, "hospital_fl")
  11.  
  12. with arcpy.da.SearchCursor(counties, ("SHAPE@")) as rows:
  13. for row in rows:
  14. polygon_geom = row[0]
  15. arcpy.SelectLayerByLocation_management(hospital_fl, "COMPLETELY_WITHIN", polygon_geom)
  16. doctors = {row[0]: row[1] for row in arcpy.da.SearchCursor(hospital_fl, ("OID@", field))}
  17. max_doctors = max(doctors.values())
  18. for k, v in doctors.items():
  19. if v == max_doctors:
  20. object_id.append(k)
  21.  
  22. arcpy.SelectLayerByAttribute_management(hospital_fl, "NEW_SELECTION", "OBJECTID IN {0}".format(tuple(object_id)))
  23. arcpy.SpatialJoin_analysis(counties, hospital_fl, "in_memory\Counties_output", "JOIN_ONE_TO_MANY", "KEEP_ALL", "#", "CONTAINS")
  24. arcpy.Delete_management(hospital_fl)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement