In [4]:
cd chihealthaccess
In [5]:
# Import required libraries
import numpy as np
import pandas as pd
import pysal as ps
import random as rdm
from pysal.contrib.viz import mapping as maps
%matplotlib inline
from pylab import *
In [6]:
# Open census tract shapefile and database.
shp_link = 'data/CensusTractsTIGER2010.shp'
dbf = ps.open('data/CensusTractsTIGER2010.dbf')
In [7]:
# Shapefile and data preparation.
cols = np.array([dbf.by_col(col) for col in dbf.header]).T
df = pd.DataFrame(cols)
df.columns = dbf.header
df.columns = df.columns.map(lambda x: x.lower())
df.commarea = df.commarea.astype('int')
df['order'] = df.index
In [8]:
init_calls = pd.read_csv('data/CDPHtest.csv', dtype=object)
In [9]:
for c in init_calls.columns[1:]:
init_calls[c] = init_calls[c].astype('float')
In [11]:
# Format data and merge on shapefile IDs
ordered_tracts = pd.DataFrame(df.loc[:,['tractce10', 'commarea', 'order']])
calls = pd.merge(init_calls, ordered_tracts, how='right', left_on='tract',
right_on='tractce10', sort=False).fillna(0).sort(['order'])
calls = calls.drop(['order', 'commarea'],1)
class bd:
data = calls
shp_link = shp_link
id = 'tractce10'
level = 'tract'
In [15]:
# Assign the attribute data.
d = bd()
In [16]:
shpOut = 'CDPHTest.shp'
In [19]:
schema = { 'geometry': 'Polygon', 'properties': {
'tract': 'str:24', }}
data = d
with collection(shpOut, "w", "ESRI Shapefile", schema) as output:
for index, row in data.iterrows():
output.write({
'properties': {
'tract': 'str:24', },
'geometry': mapping(polygon)
})
In [ ]: