def get_county_tract_data(fips_county_code: str) -> List[Dict[str, Any]]:
    '''
    Return Census data for all the tracts in a county, transformed
    so as to be ready for insertion into our database.
    '''

    c = census.Census(CENSUS_API_KEY, year=2018)

    rows: List[Dict[str, Any]] = []

    census_rows = c.acs5st.get(
        (
            'NAME',
            HOUSEHOLDS_MEDIAN_INCOME_DOLLARS,
        ),
        {
            'for': 'tract:*',
            'in': f'state:{states.NY.fips} county:{fips_county_code}',
        },
    )

    for row in census_rows:
        hmid = int(row[HOUSEHOLDS_MEDIAN_INCOME_DOLLARS])
        rows.append({
            'borocode':
            COUNTY_TO_BORO_CODE[row['county']],
            'tract2010':
            row['tract'],
            # https://github.com/datamade/census/issues/72
            'households_median_income_dollars':
            None if hmid <= 0 else hmid,
        })

    return rows
示例#2
0
    def __init__(self, key, state_abbr):
        self.api = census.Census(key, year=2016)
        self.fips = getattr(states, state_abbr).fips
        self.state_abbr = state_abbr

        self.race_variables = RACE_VARIABLES[state_abbr]
        # NAME = geography/location
        # GEO_ID = combination of country, state, county
        self.variables = ['NAME', 'GEO_ID'] + list(self.race_variables.keys())
示例#3
0
def housingunitdata():
    year_int = int(datetime.now().year) - 2
    print(year_int)
    ##
    # BUILD DIRECTORIES ON Z TO HOLD CSV FILES
    ##

    # Create subdirectories if they don't exist
    '''for geo in api_pull:
        directory = base_dir + '\\' + geo
        if not os.path.exists(directory):
            os.makedirs(directory)
    print('\bDone')'''
    tables = ['B25001',  # total housing units
              'B25003',  # housing tenure
              'B25034'  # year structure built--histogram?
               ]
    c = census.Census(api_key, year = 2016)
    fields = ("GEO_ID", "NAME",
              'B25001_001E', 'B25001_001M',  # total housing units
              'B25003_002E', 'B25003_002M',  # owner occupied housing units
              'B25003_003E', 'B25003_003M',  # renter occupied housing units
              'B25034_001E', 'B25034_001M',  # total housing units
              'B25034_002E', 'B25034_002M',  # built 2010 or later
              'B25034_003E', 'B25034_003M',  # built 2000-2009
              'B25034_004E', 'B25034_004M',  # built 1990-1999
              'B25034_005E', 'B25034_005M',  # built 1980-1989
              'B25034_006E', 'B25034_006M',  # built 1970-1979
              'B25034_007E', 'B25034_007M',  # built 1960-1969
              'B25034_008E', 'B25034_008M',  # built 1950-1959
              'B25034_009E', 'B25034_009M',  # built 1940-1949
              'B25034_010E', 'B25034_010M')  # built 1939 or earlier

    fips = {'Lucas':'39095','Wood':'39173','Monroe':'26115'}
    df = pandas.DataFrame()
    for county in fips:
        if county == 'Monroe':
            temp_df = pandas.DataFrame(c.acs5.state_county_subdivision(fields,fips[county][:2],fips[county][2:],'*'))
            temp_df = temp_df.loc[temp_df['county subdivision'].isin(['49700', # Luna Pier
                                 '06740', # Bedford
                                   '26320', # Erie
                                   '86740' # Whiteford
                                    ])]
            df = df.append(temp_df)
        else:
            temp_df = pandas.DataFrame(c.acs5.state_county_subdivision(fields,fips[county][:2],fips[county][2:],'*'))
            df = df.append(temp_df)

    # scrape this web page to get api metadata, i.e., 'label' :https://api.census.gov/data/2016/acs/acs5/variables.json
    res = requests.get('https://api.census.gov/data/2016/acs/acs5/variables.json')
    metadata = pandas.DataFrame(res.json()['variables']).transpose()
    # print(metadata[['label','concept']])

    print(df)
    print("Program Complete in " + str(datetime.now()-start_time))
    return df
示例#4
0
def fetch_places():
	c = census.Census("d0aed3790c96f4f43bc7e37bbddae0e55c56ee01")
	places = c.sf1.state_place(("NAME", "P0010001"), "*", "*")

	out_file = open("source-data/sf1-place-pop.csv", "w+")
	writer = csv.writer(out_file)
	writer.writerow(["Place", "State", "Population"])

	for place in places:
		name = place["NAME"].encode("utf-8")
		state = us.states.lookup(place["state"])
		pop = place["P0010001"]
		writer.writerow([name, state, pop])
	out_file.close()
示例#5
0
def load_tract_records(keys):
    """




        Need to reload Montana!!!!!!!!

        for state in [us.states.MT, ]:

    """

    c = census.Census(settings.CENSUS_KEY)

    # for state in [us.states.MT]:
    for state in us.STATES:

        print state.name

        counties = Tract.objects.filter(state=state.fips).values_list(
            'county', flat=True)

        for county in sorted(set(counties)):

            print "\t%s" % county

            for chunk in _chunks(keys, 5):

                fields = ",".join(chunk)

                res = c.acs.state_county_tract(fields, state.fips, county,
                                               census.ALL)
                for record in res:

                    tract = record.pop('tract')
                    del record['state']
                    del record['county']

                    for k in record:
                        if record[k] == 'null':
                            record[k] = None

                    qs = Tract.objects.filter(state=state.fips,
                                              county=county,
                                              tract=tract)
                    qs.update(**record)

            time.sleep(1)

        time.sleep(5)
示例#6
0
def load_tracts():

    c = census.Census(settings.CENSUS_KEY)

    with open('tracts.csv', 'w') as outfile:

        writer = csv.writer(outfile)
        writer.writerow(('state', 'county', 'tract', 'name'))

        for state in us.STATES:

            print state.name

            for cr in c.sf1.state_county('NAME', state.fips, census.ALL):
                for tr in c.sf1.state_county_tract('NAME', state.fips,
                                                   cr['county'], census.ALL):
                    record = (state.fips, cr['county'], tr['tract'],
                              tr['NAME'])
                    writer.writerow(record)
示例#7
0
 def __init__(self, key):
     self.c = census.Census(key)
     self.pums_relationship_file_url = "http://www2.census.gov/geo/docs/" \
                                       "maps-data/data/rel/2010_"\
                                       "Census_Tract_to_2010_PUMA.txt"
     self.pums_relationship_df = None
     self.base_url = "http://paris.urbansim.org/data/pums/"
     self.pums_population_base_url = \
         self.base_url + "puma_p_%s_%s.csv"
     self.pums_household_base_url = \
         self.base_url + "puma_h_%s_%s.csv"
     self.pums_population_state_base_url = \
         self.base_url + "puma_p_%s.csv"
     self.pums_household_state_base_url = \
         self.base_url + "puma_h_%s.csv"
     self.fips_url = "http://www2.census.gov/geo/docs/reference/codes/files/" \
                     "national_county.txt"
     self.fips_df = None
     self.pums_cache = {}
示例#8
0
 def __init__(self, key):
     self.c = census.Census(key)
     self.base_url = "https://s3-us-west-2.amazonaws.com/synthpop-data/"
     self.pums_relationship_file_url = self.base_url + "tract10_to_puma.csv"
     self.pums_relationship_df = None
     self.pums10_population_base_url = \
         self.base_url + "puma10_p_%s_%s.csv"
     self.pums10_household_base_url = \
         self.base_url + "puma10_h_%s_%s.csv"
     self.pums00_population_base_url = \
         self.base_url + "puma00_p_%s_%s.csv"
     self.pums00_household_base_url = \
         self.base_url + "puma00_h_%s_%s.csv"
     self.pums_population_state_base_url = \
         self.base_url + "puma_p_%s.csv"
     self.pums_household_state_base_url = \
         self.base_url + "puma_h_%s.csv"
     self.fips_url = self.base_url + "national_county.txt"
     self.fips_df = None
     self.pums_cache = {}
import json
import os
import transitfeed
import census
import requests

from config import *

# This script builds a JSON file containing a transit systems
# routes coupled with census data. Uses GTFS feeds and the census API.

census_api = census.Census(CENSUS_API_KEY, year=2000)


# Helper to call an FCC API to grab the correct census tract for a given lat/lon.
def get_fips(latitude, longitude, year="2010"):
    r = requests.get(
        "http://data.fcc.gov/api/block/%s/find?format=json&latitude=%f&longitude=%f&showall=true"
        % (year, latitude, longitude))
    return r.json()


fips_dict = {}


def build_fips_dict():
    for agency in FIPS_JSON_FILE:
        agency_filename = FIPS_JSON_FILE[agency]

        agency_file = open(agency_filename)
        agency_json = json.load(agency_file)
Created on Mon Feb 17 06:38:41 2014

@author: raymondyee
"""

import numpy as np
import pandas as pd
from pandas import Series


import census
import us

import settings

c = census.Census(key=settings.CENSUS_KEY)

# generators for the various census geographic entities of interest

def states(variables='NAME'):
    geo={'for':'state:*'}
    states_fips = set([state.fips for state in us.states.STATES])
    # need to filter out non-states
    for r in c.sf1.get(variables, geo=geo):
        if r['state'] in states_fips:
            yield r
            
def counties(variables='NAME'):
    """ask for all the states in one call"""
    
    # tabulate a set of fips codes for the states
示例#11
0
# coding: utf-8

# ### Import libraries

# In[84]:

import census
import pandas as pd
import numpy as np

# ### Set API key

# In[85]:

key = '4c26aa6ebbaef54a55d3903212eabbb506ade381'
c = census.Census(key)

# ### Choose city and census tracts of interest

# In[86]:

city_name = 'Memphis'
# These are the counties
#If reproducing for another city, add elif for that city & desired counties here

if city_name == 'Chicago':
    state = '17'
    FIPS = ['031', '043', '089', '093', '097', '111', '197']

elif city_name == 'Atlanta':
    state = '13'
示例#12
0
# <headingcell level=2>

# Total Population of California

# <markdowncell>

# [2010 Census Summary File 1](http://www.census.gov/prod/cen2010/doc/sf1.pdf)
# 
# P0010001 is found in [2010 SF1 API Variables \[XML\]](http://www.census.gov/developers/data/sf1.xml) = "total population"

# <codecell>

from settings import CENSUS_KEY
import census

c=census.Census(settings.CENSUS_KEY) 
c.sf1.get(('NAME', 'P0010001'), {'for': 'state:%s' % states.CA.fips})

# <codecell>

"population of California: {0}".format(
        int(c.sf1.get(('NAME', 'P0010001'), {'for': 'state:%s' % states.CA.fips})[0]['P0010001']))

# <markdowncell>

# Let's try to get at the counties of California and their populations

# <codecell>

ca_counties = c.sf1.get(('NAME', 'P0010001'), geo={'for': 'county:*', 'in': 'state:%s' % states.CA.fips})
示例#13
0
# Download Census population data by tract
## Upload population data and census boundary files to S3

import numpy as np
import pandas as pd
import geopandas as gpd
import intake
import boto3
import census
from us import states

# Set env
# Can't figure out how to read the API key from env
c = census.Census('2dacc2d1fe8ae85c99e2f934a70576d6f731bb0f', year=2017)
catalog = intake.open_catalog('./catalogs/*.yml')
s3 = boto3.client('s3')

#----------------------------------------------------------------#
# Download 2017 ACS 5-year population data
#----------------------------------------------------------------#
# 2018 is not available for ACS 5-year
# ACS 1-year doesn't have tract-level data
raw = c.acs5.state_county_tract('B01003_001E', states.CA.fips, '037',
                                census.ALL)
df = pd.DataFrame(raw)

# Subset for LA County
df['GEOID'] = df.state + df.county + df.tract
df = df[['GEOID', 'B01003_001E']]
df.rename(columns={'B01003_001E': 'pop'}, inplace=True)
df = df.sort_values('GEOID', ascending=True)
示例#14
0
import census
import csv
import us

c = census.Census("d0aed3790c96f4f43bc7e37bbddae0e55c56ee01")

aggregation_fields = {
    "zip": "zip code tabulation area",
    "county_fips": "county",
    "state_fips": "state",
}

fields = {
    "population": "B01003_001E",
    "edu_divisor": "B15003_001E",
    "edu_high_school": "B15003_017E",
    "edu_ged": "B15003_018E",
    "edu_year_of_college": "B15003_019E",
    "edu_years_of_college": "B15003_020E",
    "edu_assoc": "B15003_021E",
    "edu_bach": "B15003_022E",
    "edu_mast": "B15003_023E",
    "edu_professional": "B15003_024E",
    "edu_doctorate": "B15003_025E",
    "race_total": "B03002_001E",
    "race_white_nonhisp": "B03002_003E",
    "race_black_nonhisp": "B03002_004E",
    "race_native_nonhisp": "B03002_005E",
    "race_asian_nonhisp": "B03002_006E",
    "race_pacific_nonhisp": "B03002_007E",
    "race_other_nonhisp": "B03002_008E",