-
Notifications
You must be signed in to change notification settings - Fork 2
/
centroid_calculator.py
executable file
·70 lines (54 loc) · 1.96 KB
/
centroid_calculator.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 6 11:16:29 2018
@author: tug30201
"""
import os
# geospatial
import geopandas as gpd
import pysal as ps
import numpy as np
from tqdm import tqdm
# visualization
import matplotlib.pyplot as plt
# data retrieval
import os
from urllib import urlopen
from zipfile import ZipFile
from tqdm import tqdm
url = "https://www2.census.gov/geo/tiger/TIGER2012/VTD/"
name = "tl_2012_FIPS_vtd10.zip"
#this is missing KY and RI
FIPS = {"02":"AK", "01":"AL", "05":"AR", "04":"AZ", "06":"CA",
"08":"CO", "09":"CT", "10":"DE", "12":"FL", "13":"GA",
"15":"HI", "19":"IA", "16":"ID", "17":"IL", "18":"IN",
"20":"KS", "22":"LA", "25":"MA", "24":"MD",
"23":"ME", "26":"MI", "27":"MN", "29":"MO", "28":"MS",
"30":"MT", "37":"NC", "38":"ND", "31":"NE", "33":"NH",
"34":"NJ", "35":"NM", "32":"NV", "36":"NY", "39":"OH",
"40":"OK", "41":"OR", "42":"PA", "45":"SC",
"46":"SD", "47":"TN", "48":"TX", "49":"UT", "51":"VA",
"50":"VT", "53":"WA", "55":"WI", "54":"WV", "56":"WY"}
# data retrieval
def get_and_unzip(url, data_dir=os.getcwd()):
basename = url.split("/")[-1]
if not os.path.exists(os.path.join(data_dir, basename)):
file_data = urlopen(url)
data_to_write = file_data.read()
with open(basename, "wb") as f:
f.write(data_to_write)
zip_obj = ZipFile(basename)
zip_obj.extractall()
del(zip_obj)
def get_centroids(fname, state):
df_counties = gpd.read_file(fname)
county_centroids = df_counties.centroid
c_x = county_centroids.x
c_y = county_centroids.y
with open("%s.txt" % state, "w") as f:
for z in zip(c_x, c_y):
f.write("%.3f %.3f\n" % z)
for k in tqdm(FIPS.keys()):
fname = name.replace("FIPS", k)
get_and_unzip(url+fname, ".")
get_centroids(fname.replace("zip", "shp"), FIPS[k])