Last active
February 3, 2020 01:22
-
-
Save bheni/c5cceb745297fbbc0df3119aa2be7c65 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# csv_to_map.py converts csvs containing states and percentanges and converts them to a format that can be imported | |
# into https://mapchart.net/usa.html | |
import sys | |
import json | |
import os.path | |
# constants | |
BRANCHES = [str(branch) for branch in range(2011, 2018)] | |
# empty_map_data creates a dictionary with the appropriate keys to be serialized into the json format that mapchart.net | |
# expects | |
def empty_map_data(): | |
return { | |
"groups": {}, | |
"hidden": [], | |
"background": "#ffffff", | |
"borders": "#000000" | |
} | |
# percentage_to_color transforms a percentage in the range of min_percentage to max_percentage to a color value in | |
# the gradient range #00c840 to #ff0040 | |
def percentage_to_color(per): | |
weight = (per - min_percentage) / percentage_range | |
r = int((1.0 - weight) * 255) | |
g = int(weight * 200) | |
return '#%02x%02x40' % (r, g) | |
# parse command line parameters | |
if len(sys.argv) < 4: | |
print("usage: python csv_to_map.py <dir> <min-percentage> <max-percentage>") | |
sys.exit(1) | |
directory = sys.argv[1] | |
if not os.path.exists(directory): | |
print("%s is not a valid path" % directory) | |
sys.exit(1) | |
min_percentage = float(sys.argv[2]) | |
max_percentage = float(sys.argv[3]) | |
percentage_range = max_percentage-min_percentage | |
# for each branch | |
for branch in BRANCHES: | |
map_data = empty_map_data() | |
map_data["title"] = branch | |
# open the csv file containing the states and percentages. skip the first line containing the headers then transform | |
# the percentage for each state into a color and add the appropriate data to the map_data dictionary | |
with open(os.path.join(directory, branch + ".csv")) as f: | |
first = True | |
box_number = 0 | |
for line in f: | |
if first: | |
first = False | |
continue | |
state, percentage = line.split(',') | |
color = percentage_to_color(float(percentage)) | |
if color in map_data["groups"]: | |
map_data["groups"][color]["paths"].append(state) | |
else: | |
box = "#box%d" % box_number | |
map_data["groups"][color] = { | |
"div": box, | |
"label": "", | |
"paths": [state], | |
} | |
box_number += 1 | |
# create a new file and write the serialized map_data json to it. | |
with open(os.path.join(directory, branch + "map.json"), "w+") as f: | |
f.write(json.dumps(map_data, indent=4)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment