Commit 2df96d00 by Isaac Wink

Merge branch 'master' of mit.cs.uchicago.edu:ajfriedman/election_district_drawing

parents b495d044 f34b9869
Showing with 11 additions and 38 deletions
......@@ -2,6 +2,7 @@ import json
import sys
import networkx as nx
import matplotlib.pyplot as plt
# the Basemap code was sourced from StackOverflow: stackoverflow.com/questions/19915266/drawing-a-graph-with-networkx-on-a-basemap
from mpl_toolkits.basemap import Basemap as Basemap
sys.path.insert(0, "../reading_shapes")
from util import recover_json, color_csv
......@@ -9,6 +10,7 @@ from util import recover_json, color_csv
color_dict = color_csv('',"Colors.csv")
# make sure input states with double word names are handled correctly
double_word = {"West_Virginia": "West Virginia",
"South_Carolina": "South Carolina",
"North_Dakota": "North_Dakota",
......@@ -26,9 +28,10 @@ if sys.argv[1] in double_word:
sys.argv[1] = double_word[sys.argv[1]]
state_tracts = recover_json("../reading_shapes/state_tracts_full_json_files/"+ sys.argv[1] + "tract_dict.json")
# create Basemap zoomed to the United States
m = Basemap(projection='merc', llcrnrlon=-130, llcrnrlat=25, urcrnrlon=-60, urcrnrlat=50, lat_ts=0, resolution='i', suppress_ticks=True)
# dictionary
# dictionary
num_args = len(sys.argv)
if num_args == 3:
d = recover_json("district_plans/" + sys.argv[1] + ".json")
......@@ -38,6 +41,7 @@ elif num_args == 2:
unused_all = set()
for num in d:
# create networkx map for each district
G = nx.Graph()
pos1 = {}
label = {}
......@@ -52,10 +56,12 @@ for num in d:
if item in d_all_tracts and item not in d_used:
tract_info = state_tracts[item]
tract_lat = tract_info["lat"]
# remove '+' for positive coordinates so it can be interpreted as float
if tract_lat[0] == "+":
tract_lat = tract_lat[1:]
tract_lon = (tract_info["lon"])
# remove '+' for positive coordinates so it can be interpreted as float
if tract_lon[0] == "+":
tract_lon = tract_lon[1:]
......@@ -69,6 +75,7 @@ for num in d:
d_used.add(item)
unused_all.update(d_used)
# add connection lines between points in adjacent tracts
for d_tract in d_all_tracts:
tract_info = state_tracts[d_tract]
connections = tract_info["connections"]
......@@ -76,8 +83,6 @@ for num in d:
if item in d_used:
G.add_edge(d_tract, item)
nx.draw(G, pos1,node_size=50, node_color=color_dict[num])
G = nx.Graph()
......@@ -105,41 +110,9 @@ for item in state_tracts:
nx.draw(G, pos1,node_size=5, node_color='w')
# manipulations of Basemap
m.drawcountries()
m.drawstates()
m.drawcoastlines()
plt.title('New district outlines')
plt.show()
# position in decimal lat/lon
# lats = [39.96, 42.82, 43.82]
# lons = [-121.29, -73.95, -77.95]
# convert lat and lon to map projection
# mx, my = m(lons, lats)
# Networkx part
# put map projection coordinates in pos dictionary
# G = nx.Graph()
# G.add_node('b')
# pos2 = {}
# pos2['b'] = (mx[1], my[1])
# # draw
# H = nx.Graph()
# H.add_node('a')
# H.add_node('c')
# pos1 = {}
# pos1['c'] = (mx[2], my[2])
# nx.draw_networkx(G, pos2, node_size=100, node_color='blue')
# nx.draw_networkx(H, pos1, node_size=100, node_color='r')
# draw map
\ No newline at end of file
plt.show()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment