Exemplo n.º 1
0
    def __init__(self):
        self.get = Get()
        self.area = {
            'state': 'state',
            'county': 'county',
            'zip': 'zip+code+tabulation+area'
        }

        self.states = {}
        self.counties = {}
        self.zips = {}
        self.data = {
            'state': self.states,
            'county': self.counties,
            'zip': self.zips
        }
        self.codes = {'state': None, 'county': None}
Exemplo n.º 2
0
    def __init__(self):
        self.get = Get()
        self.area = {
            'state' : 'state',
            'county' : 'county', 
            'zip' : 'zip+code+tabulation+area'
            }

        self.states = {}
        self.counties = {}
        self.zips = {}
        self.data = {
            'state' : self.states,
            'county' : self.counties,
            'zip' : self.zips
            }
        self.codes = {
            'state' : None,
            'county' : None
            }
Exemplo n.º 3
0
class Population(object):
    def __init__(self):
        self.get = Get()
        self.geo = None
        self.output_file = None

    def raw_print(self, data, header, print_header=False, replace={}):
        f = open(self.output_file, 'w')
        for i, d in enumerate(data):
            if i == 0:
                data_key = header[0]
                column_names = []
                for h in header:
                    if h == 'state':
                        column_names.append('state')
                        column_names.append('state_cd')
                    elif h == 'county':
                        column_names.append('county')
                        column_names.append('county_cd')
                    else:
                        column_names.append(h)
                if print_header:
                    o = '|'.join(column_names) + '\n'
                    f.write(o)
            elif i > 0:
                row_data = OrderedDict()
                for j, e in enumerate(d):
                    if header[j] == 'county':
                        cd = str(int(e))
                        row_data[header[j]] = cd
                    else:
                        row_data[header[j]] = e
                row = self._pretty_row(column_names, row_data, replace)
                o = '|'.join(row) + '\n'
                f.write(o)
        f.close()

    def query(self, repo, filters):
        url = self.get.api_url + '/' + str(
            repo['year']) + '/' + self.get.surveys[repo['source']]

        payload = {'get': repo['data'], 'key': self.get.key}

        if len(filters.keys()) == 1 and 'state' in filters:
            payload['for'] = self.geo.area['state'] + ':' + self.geo.data[
                'state'][filters['state']]
        else:
            for f in filters.keys():
                if f == "state":
                    if filters[f] == '*':
                        loc = '*'
                    else:
                        loc = self.geo.data[f][filters[f]]
                    payload['in'] = self.geo.area['state'] + ':' + loc
                else:
                    if filters[f] == '*':
                        loc = '*'
                    else:
                        loc = self.geo.data[f][filters[f]]
                    payload['for'] = self.geo.area[f] + ':' + loc

        full = url + '?' + self.get._create_params(payload)
        try:
            res = requests.get(full)
            if res.status_code == 200:
                return json.loads(res.text)
            else:
                print full
                print res.text
        except ConnectionError as e:
            print e.explanation
Exemplo n.º 4
0
 def __init__(self):
     self.get = Get()
     self.geo = None
     self.output_file = None
Exemplo n.º 5
0
class Geography(object):
    def __init__(self):
        self.get = Get()
        self.area = {
            'state': 'state',
            'county': 'county',
            'zip': 'zip+code+tabulation+area'
        }

        self.states = {}
        self.counties = {}
        self.zips = {}
        self.data = {
            'state': self.states,
            'county': self.counties,
            'zip': self.zips
        }
        self.codes = {'state': None, 'county': None}

    def download_states(self, output_file):
        if not os.path.exists(output_file):
            year = 2010
            url = self.get.api_url + '/' + str(
                year) + '/' + self.get.surveys['census']
            payload = {'for': 'state:*', 'get': 'NAME', 'key': self.get.key}
            full = url + '?' + self.get._create_params(payload)
            try:
                res = requests.get(full)
                if res.status_code == 200:
                    f = open(output_file, "w")
                    f.write(res.text)
                    f.close()
            except ConnectionError as e:
                print e.explanation

        self._load_state_codes(output_file)

    def download_counties(self, output_file):
        year = 2010
        url = self.get.api_url + '/' + str(
            year) + '/' + self.get.surveys['census']
        for s in self.states.keys():
            state_file = output_file + '-' + s
            if not os.path.exists(state_file):
                payload = {
                    'for': 'county:*',
                    'in': 'state:%s' % self.states[s],
                    'get': 'NAME',
                    'key': self.get.key
                }
                full = url + '?' + self.get._create_params(payload)
                try:
                    res = requests.get(full)
                    if res.status_code == 200:
                        f = open(state_file, "w")
                        f.write(res.text.encode('utf8'))
                        f.close()
                except ConnectionError as e:
                    print e.explanation
        self._load_county_codes(output_file)

    def download_zip(self, output_file):
        year = 2010
        url = self.get.api_url + '/' + str(
            year) + '/' + self.get.surveys['census']

        for s in self.states.keys():
            state_file = output_file + '-' + s
            if not os.path.exists(state_file):
                payload = {
                    'for': self.area['zip'] + ':*',
                    'in': 'state:%s' % self.states[s],
                    'get': 'NAME',
                    'key': self.get.key
                }
                full = url + '?' + self.get._create_params(payload)
                try:
                    res = requests.get(full)
                    if res.status_code == 200:
                        f = open(state_file, "w")
                        f.write(res.text.encode('utf8'))
                        f.close()
                except ConnectionError as e:
                    print e.explanation
        self._load_zip_codes(output_file)

    def _load_state_codes(self, input_file):
        f = open(input_file, "r")
        out = f.read()
        data = json.loads(out)
        for i, d in enumerate(data):
            if i > 0:
                self.states[d[0]] = d[1]

        # Also create a reverse index
        self.state_codes = {v: k for k, v in self.states.items()}
        self.codes['state'] = self.state_codes

    def _load_county_codes(self, input_file):
        self.county_codes = {}
        for s in self.states.keys():
            state_file = input_file + '-' + s
            f = open(state_file, "r")
            out = f.read()
            data = json.loads(out)

            self.counties[s] = []
            self.county_codes[s] = {}
            for i, d in enumerate(data):
                if i > 0:
                    cd = str(int(d[2]))
                    self.counties[s].append({d[0]: cd})
                    self.county_codes[s][cd] = d[0]

        self.codes['county'] = self.county_codes

    def _load_zip_codes(self, input_file):
        for s in self.states.keys():
            state_file = input_file + '-' + s
            f = open(state_file, "r")
            out = f.read()
            data = json.loads(out)

            self.zips[s] = []
            for i, d in enumerate(data):
                if i > 0:
                    self.zips[s].append(d[2])
Exemplo n.º 6
0
class Geography(object):
    def __init__(self):
        self.get = Get()
        self.area = {
            'state' : 'state',
            'county' : 'county', 
            'zip' : 'zip+code+tabulation+area'
            }

        self.states = {}
        self.counties = {}
        self.zips = {}
        self.data = {
            'state' : self.states,
            'county' : self.counties,
            'zip' : self.zips
            }
        self.codes = {
            'state' : None,
            'county' : None
            }

    def download_states(self, output_file):
        if not os.path.exists(output_file):
            year = 2010
            url = self.get.api_url + '/' + str(year) + '/' + self.get.surveys['census']
            payload = { 'for' : 'state:*',
                        'get': 'NAME',
                        'key' : self.get.key }
            full = url + '?' + self.get._create_params(payload)
            try:
                res = requests.get(full)
                if res.status_code == 200:
                    f = open(output_file, "w")
                    f.write(res.text)
                    f.close()
            except ConnectionError as e:
                print e.explanation

        self._load_state_codes(output_file)

    def download_counties(self, output_file):
        year = 2010
        url = self.get.api_url + '/' + str(year) + '/' + self.get.surveys['census']
        for s in self.states.keys():
            state_file = output_file + '-' + s
            if not os.path.exists(state_file):
                payload = { 'for' : 'county:*',
                            'in' : 'state:%s' % self.states[s], 
                            'get': 'NAME',
                            'key' : self.get.key }
                full = url + '?' + self.get._create_params(payload)
                try:
                    res = requests.get(full)
                    if res.status_code == 200:
                        f = open(state_file, "w")
                        f.write(res.text.encode('utf8'))
                        f.close()
                except ConnectionError as e:
                    print e.explanation
        self._load_county_codes(output_file)

    def download_zip(self, output_file):
        year = 2010
        url = self.get.api_url + '/' + str(year) + '/' + self.get.surveys['census']

        for s in self.states.keys():
            state_file = output_file + '-' + s
            if not os.path.exists(state_file):
                payload = { 'for' : self.area['zip'] + ':*',
                            'in' : 'state:%s' % self.states[s], 
                            'get': 'NAME',
                            'key' : self.get.key }
                full = url + '?' + self.get._create_params(payload)
                try:
                    res = requests.get(full)
                    if res.status_code == 200:
                        f = open(state_file, "w")
                        f.write(res.text.encode('utf8'))
                        f.close()
                except ConnectionError as e:
                    print e.explanation
        self._load_zip_codes(output_file)

    def _load_state_codes(self, input_file):
        f = open(input_file, "r")
        out = f.read()
        data =  json.loads(out)
        for i, d in enumerate(data):
            if i > 0:
                self.states[d[0]] = d[1]

        # Also create a reverse index
        self.state_codes = {v:k for k, v in self.states.items()}
        self.codes['state'] = self.state_codes

    def _load_county_codes(self, input_file):
        self.county_codes = {}
        for s in self.states.keys():
            state_file = input_file + '-' + s
            f = open(state_file, "r")
            out = f.read()
            data = json.loads(out)

            self.counties[s] = []
            self.county_codes[s] = {}
            for i, d in enumerate(data):
                if i > 0:
                    cd = str(int(d[2]))
                    self.counties[s].append( { d[0] : cd } )
                    self.county_codes[s][cd] = d[0]

        self.codes['county'] = self.county_codes
                    
    def _load_zip_codes(self, input_file):
        for s in self.states.keys():
            state_file = input_file + '-' + s
            f = open(state_file, "r")
            out = f.read()
            data = json.loads(out)

            self.zips[s] = []
            for i, d in enumerate(data):
                if i > 0:
                    self.zips[s].append( d[2] )
Exemplo n.º 7
0
 def __init__(self):
     self.get = Get()
     self.geo = None
     self.output_file = None
Exemplo n.º 8
0
class Population(object):
    def __init__(self):
        self.get = Get()
        self.geo = None
        self.output_file = None

    def raw_print(self, data, header, print_header=False, replace = {}):
        f = open(self.output_file, 'w')
        for i, d in enumerate(data):
            if i == 0:
                data_key = header[0]
                column_names = []
                for h in header:
                    if h == 'state':
                        column_names.append('state')
                        column_names.append('state_cd')
                    elif h == 'county':
                        column_names.append('county')
                        column_names.append('county_cd')
                    else:
                        column_names.append(h)
                if print_header:
                    o = '|'.join(column_names) + '\n'
                    f.write(o)
            elif i > 0:
                row_data = OrderedDict()
                for j, e in enumerate(d):
                    if header[j] == 'county':
                        cd = str(int(e))
                        row_data[header[j]] = cd
                    else:
                        row_data[header[j]] = e
                row = self._pretty_row(column_names, row_data, replace)
                o = '|'.join(row) + '\n'
                f.write(o)
        f.close()

    def query(self, repo, filters):
        url = self.get.api_url + '/' + str(repo['year']) + '/' + self.get.surveys[repo['source']]

        payload = { 'get': repo['data'],
                    'key' : self.get.key }

        if len(filters.keys()) == 1 and 'state' in filters:
                payload['for'] = self.geo.area['state'] + ':' +self.geo.data['state'][filters['state']]
        else:
            for f in filters.keys():
                if f == "state":
                    if filters[f] == '*':
                        loc = '*'
                    else:
                        loc = self.geo.data[f][filters[f]]
                    payload['in'] = self.geo.area['state'] + ':' + loc
                else:
                    if filters[f] == '*':
                        loc = '*'
                    else:
                        loc = self.geo.data[f][filters[f]]
                    payload['for'] = self.geo.area[f] + ':' + loc

        full = url + '?' + self.get._create_params(payload)
        try:
            res = requests.get(full)
            if res.status_code == 200:
                return json.loads(res.text)
            else:
                print full
                print res.text
        except ConnectionError as e:
            print e.explanation