def insertGroups(): """ STATEFP,COUNTYFP,TRACTCE,BLKGRPCE,POPULATION,LATITUDE,LONGITUDE 42,001,030101,1,2580,+40.015805,-077.081172 """ print "loading census block groups" count = 0 with open(GROUP_DATA_FILE) as csvfile: reader = csv.reader(csvfile) for row in reader: try: int(row[0]) except: pass else: # print row count += 1 printCount(count) new_group = { "_id": "%s%s%s%s" % (row[0], row[1], row[2], row[3]), "state_fips": row[0], "county_fips": row[1], "tract": row[2], "group": row[3], "lat": float(row[5]), "lon": float(row[6]), "population": float(row[4]), } db.groups.save(new_group) print count, "groups loaded"
def insertBlocksWithCurrentDistricts(): print "loading census blocks" count = 0 with open(BLOCK_DATA_FILE) as csvfile: reader = csv.reader(csvfile) for row in reader: try: int(row[0]) except: pass else: count += 1 printCount(count) _id = row[0] block_data = { "_id": _id, "district": int(row[1]), "state_fips": _id[0:2], "county_fips": _id[2:5], "tract": _id[5:11], "group": _id[11], "tabulation": _id[11:15], } db.blocks.save(block_data) print count, "blocks inserted"
def loadNewDistrictAssignments(): print "loading new district assignments" count = 0 with open(NEW_DISTRICT_FILE) as csvfile: reader = csv.reader(csvfile) for row in reader: count += 1 printCount(count) db.blocks.update({"_id": row[0]}, {"$set": {"b_district": int(row[1]) + 1}}) # +1 because they're 0 indexed print count, "blocks updated"
def insertCounties(): print "loading counties" count = 0 county_data = json.loads(file(COUNTY_DATA_FILE).read()) for county in county_data: count += 1 printCount(count) county["_id"] = county["id"] db.counties.save(county) print count, "counties loaded"
def insertDistricts(): print "loading districts" count = 0 with open(DISTRICT_DATA_FILE) as csvfile: reader = csv.reader(csvfile) for row in reader: printCount(count) count += 1 db.districts.save({"_id": int(row[0]), "vote_rep": int(row[1]), "vote_dem": int(row[2])}) print count, "districts loaded"
def drawCurrentVotes(flat=True, key='house'): img, pixels = newImage() count = 0 # if not flat: # max_votes = [] # max_delta = [] # for block in db.blocks.find(): # max_votes.append(block['votes'][key]['rep']) # max_votes.append(block['votes'][key]['dem']) # max_delta.append(abs(block['votes'][key]['rep'] - block['votes']['house']['dem'])) # max_votes = max(max_votes) # max_delta = max(max_delta) # print max_votes, max_delta for block in db.blocks.find(): count += 1 printCount(count) x, y = getBlockXY(block) votes = block['votes'][key] if flat: if votes.get('rep',0) > votes.get('dem',0): pixels[x, y] = (255,0,0) else: pixels[x, y] = (0,0,255) else: max_votes = votes.get('rep',0) + votes.get('dem', 0) # r_ratio = votes.get('rep',0) / max_rep # b_ratio = votes.get('dem',0) / max_dem # r = int( r_ratio * 100 + (155 * (1 - b_ratio)) ) # b = int( b_ratio * 100 + (155 * (1 - r_ratio)) ) print max_votes if max_votes: shift_r = 128 * votes.get('rep',0) / max_votes shift_b = 128 * votes.get('dem',0) / max_votes r = int( 128 + shift_r - shift_b ) b = int( 128 + shift_b - shift_r ) else: r = 0 b = 0 # r = int( 255 * votes.get('rep',0) / max_votes ) # b = int( 255 * votes.get('dem',0) / max_votes ) # 0 255 # 255 0 pixels[x, y] = (pixels[x,y][0] + r,0,pixels[x,y][2] + b) writeImage(img, BLOCK_VOTES_IMAGE % (key,))
def drawBlockHousePresidentDelta(): img, pixels = newImage() count = 0 for block in db.blocks.find(): count += 1 printCount(count) x, y = getBlockXY(block) pres_votes = block['votes']['president'] house_votes = block['votes']['house'] pres_rep = pres_votes.get('rep',0) > pres_votes.get('dem',0) house_rep = house_votes.get('rep',0) > house_votes.get('dem',0) pixels[x,y] = (40,40,40) if pres_rep != house_rep: pres_total = 0 house_total = 0 for party, votes in pres_votes.items(): pres_total += votes for party, votes in house_votes.items(): house_total += votes def deltaFor(house_party, pres_party): house_percent = house_votes.get(house_party,0) / house_total pres_percent = pres_votes.get(pres_party,0) / pres_total print pres_total, pres_percent, house_total, house_percent return abs(house_percent - pres_percent) if house_rep: delta = deltaFor('rep', 'dem') color = (255,0,0) else: delta = deltaFor('dem', 'rep') color = (0,0,255) pixels[x,y] = ( pixels[x,y][0] + int(color[0] * delta), pixels[x,y][1], pixels[x,y][2] + int(color[2] * delta), ) writeImage(img, BLOCKS_PRES_HOUSE_DIFF_IMAGE)
else: # print row count += 1 printCount(count) new_group = { "_id": "%s%s%s%s" % (row[0], row[1], row[2], row[3]), "state_fips": row[0], "county_fips": row[1], "tract": row[2], "group": row[3], "lat": float(row[5]), "lon": float(row[6]), "population": float(row[4]), } db.groups.save(new_group) print count, "groups loaded" start = datetime.now() printCount() insertDistricts() insertCounties() insertBlocksWithCurrentDistricts() loadNewDistrictAssignments() insertGroups() printCount() print (datetime.now() - start).total_seconds(), "seconds"