Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. import arcpy
  2. import sys
  3.  
  4.  
  5. VISTABALLOTAREAS = r'Database Connections\SGID10.sde\SGID10.POLITICAL.VistaBallotAreas'
  6. RESIDENCES = r'C:\Temp\Temp.gdb\Residences'
  7. PRECINCTS = r'C:\Temp\vista_prod.odc\GV_VISTA.PRECINCTS'
  8. RESIDENCES_SOURCE = r'C:\Temp\vista_prod.odc\GV_VISTA.RESIDENCES'
  9. COUNTIES = r'Database Connections\SGID10.sde\SGID10.BOUNDARIES.Counties'
  10. errors = []
  11. counties = {}
  12.  
  13.  
  14. def process_county(id):
  15. print('Processing County: {}'.format(counties[id]))
  16.  
  17. precincts_layer = arcpy.MakeFeatureLayer_management(VISTABALLOTAREAS, 'precincts_layer', 'CountyID = {}'.format(id))
  18. residences_layer = arcpy.MakeFeatureLayer_management(RESIDENCES, 'residences_layer', 'COUNTY_ID = {}'.format(id))
  19.  
  20. arcpy.AddJoin_management(residences_layer, 'PRECINCT_ID', PRECINCTS, 'PRECINCT_ID')
  21. precinct_ids = [row[0] for row in arcpy.da.SearchCursor(precincts_layer, ['VistaID'])]
  22. i = 0
  23. for precinct_id in precinct_ids:
  24. i += 1
  25. print('{} | {} out of {} | {} County'.format(precinct_id, i, len(precinct_ids), counties[id]))
  26. arcpy.SelectLayerByAttribute_management(precincts_layer, 'NEW_SELECTION', 'VistaID = \'{}\''.format(precinct_id))
  27. arcpy.SelectLayerByLocation_management(residences_layer, 'INTERSECT', precincts_layer, selection_type='NEW_SELECTION')
  28. with arcpy.da.SearchCursor(residences_layer, ['Residences.RESIDENCE_ID', 'PRECINCT'], 'PRECINCT <> \'{}\''.format(precinct_id)) as cursor:
  29. for row in cursor:
  30. msg = '{}: error with res id: {}, should be {} but it\'s {}.'.format(counties[id], row[0], precinct_id, row[1])
  31. errors.append(msg)
  32. print(msg)
  33.  
  34. arcpy.Delete_management(precincts_layer)
  35. arcpy.Delete_management(residences_layer)
  36.  
  37.  
  38. try:
  39. county_id = sys.argv[1]
  40. except IndexError:
  41. county_id = raw_input('county id (leave blank to run all): ')
  42.  
  43. try:
  44. refresh_residences = sys.argv[2] == 'Y'
  45. except IndexError:
  46. refresh_residences = raw_input('refresh residence data from vista (Y/N)? ') == 'Y'
  47.  
  48. if refresh_residences:
  49. print('pulling new residence data from vista database...')
  50. if arcpy.Exists(RESIDENCES):
  51. arcpy.Delete_management(RESIDENCES)
  52. print('making query table...')
  53. xy_table = arcpy.MakeQueryTable_management([RESIDENCES_SOURCE], 'xy_table', 'USE_KEY_FIELDS', 'GV_VISTA.RESIDENCES.RESIDENCE_ID')
  54. print('making xy event layer...')
  55. xy_layer = arcpy.MakeXYEventLayer_management(xy_table, 'X', 'Y', 'xy_layer', arcpy.SpatialReference(26912))
  56. print('copying features ...')
  57. arcpy.CopyFeatures_management(xy_layer, RESIDENCES)
  58.  
  59. for row in arcpy.da.SearchCursor(COUNTIES, ['COUNTYNBR', 'NAME']):
  60. counties[int(row[0])] = row[1]
  61.  
  62. if len(county_id) > 0:
  63. process_county(int(county_id))
  64. else:
  65. for id in counties.keys():
  66. process_county(id)
  67.  
  68. for error in errors:
  69. print(error)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement