-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtotal_splits.py
55 lines (45 loc) · 1.51 KB
/
total_splits.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 2 15:02:40 2020
sums the total # of county splits in a partition
which is the length of county_splits
@author: dpg
"""
from gerrychain import updaters
def total_splits(partition):
#county_field = 'COUNTYFP10'
#county_field = "COUNTY"
fieldlist = partition.graph.nodes[0].keys() #get LIST OF FIELDS
if 'COUNTYFP10' in fieldlist:
county_field = 'COUNTYFP10'
elif 'COUNTYFP20' in fieldlist:
county_field = 'COUNTYFP20'
elif 'CTYNAME' in fieldlist:
county_field = 'CTYNAME'
elif 'COUNTYFIPS' in fieldlist:
county_field = 'COUNTYFIPS'
elif 'COUNTYFP' in fieldlist:
county_field = 'COUNTYFP'
elif 'cnty_nm' in fieldlist:
county_field = 'cnty_nm'
elif 'county_nam' in fieldlist:
county_field = 'county_nam'
elif 'FIPS2' in fieldlist:
county_field = 'FIPS2'
elif 'COUNTY' in fieldlist:
county_field = 'COUNTY'
elif 'County' in fieldlist:
county_field = 'County'
elif 'CNTY_NAME' in fieldlist:
county_field = 'CNTY_NAME'
else:
print("no county ID info in shapefile\n")
return 10
gg = updaters.county_splits(partition, county_field)
gg_res = gg(partition)
splitcount=0
for x in gg_res:
splitcount+= len(gg_res[x].contains) -1 #subtract 1 b/c there's 1 county listed if there are no splits
return splitcount
#print(total_splits(initial_partition))