def test_compare(self): from geoid.acs import Tract t1 = Tract.parse('14000US06001442800') t2 = Tract.parse('14000US06037205110') print(t1 < t2)
def row_generator(resource, doc, env, *args, **kwargs): """ An example row generator function. Reference this function in a Metatab file as the value of a Datafile: Datafile: python:pylib#row_generator The function must yield rows, with the first being headers, and subsequenct rows being data. :param resource: The Datafile term being processed :param doc: The Metatab document that contains the term being processed :param args: Positional arguments passed to the generator :param kwargs: Keyword arguments passed to the generator :return: The env argument is a dict with these environmental keys: * CACHE_DIR * RESOURCE_NAME * RESOLVED_URL * WORKING_DIR * METATAB_DOC * METATAB_WORKING_DIR * METATAB_PACKAGE It also contains key/valu pairs for all of the properties of the resource. """ yield 'int float prop ratio str acs_tract census_tract tiger_tract date time datetime'.split() for i in range(10): tract = Tract(6, 72, randint(1, 10)) yield [ i, float(i) / pi, random(), float(randint(1, 1000)) / float(randint(1, 1000)), 'string-' + str(i), tract, tract.convert(CensusGeoid), tract.convert(TigerGeoid), date(randint(2000, 2020), randint(1, 12), randint(1, 27)), time(randint(0, 23), randint(0, 59), randint(0, 59)), datetime(randint(2000, 2020), randint(1, 12), randint(1, 27), randint(0, 23), randint(0, 59), randint(0, 59)) ]
def tract_geoid(row): """COnvert doted notation into a geoid """ try: a, b = row['tract'].split('.') except ValueError: a = row['tract'] b = '0' return Tract(6, 73, int(a) * 100 + int(b.ljust(2, '0')))
def parse_tract(v, row, row_n, i_s, i_d, header_s, header_d, scratch, errors, accumulator): """ Parse the tract number, within Los Angeles County """ from geoid.acs import Tract # 6 == CA, 037 == Los Angeles County return Tract(6, 37, v)
def lookup(zip, n): try: # The map will return a Census geoid, which has 11 charasters, but it is often missing # the leading 0, so we have to put it back. Then it much be converted to an # ACS Tract census_tract_str = str(f_map[int(zip)](int(n)%100 / 100.0)).zfill(11) return str(AcsTract.parse(census_tract_str)) except KeyError: return None
def row_generator(resource, doc, env, *args, **kwargs): """ An example row generator function. Reference this function in a Metatab file as the value of a Datafile: Datafile: python:pylib#row_generator The function must yield rows, with the first being headers, and subsequenct rows being data. :param resource: The Datafile term being processed :param doc: The Metatab document that contains the term being processed :param args: Positional arguments passed to the generator :param kwargs: Keyword arguments passed to the generator :return: The env argument is a dict with these environmental keys: * CACHE_DIR * RESOURCE_NAME * RESOLVED_URL * WORKING_DIR * METATAB_DOC * METATAB_WORKING_DIR * METATAB_PACKAGE It also contains key/value pairs for all of the properties of the resource. """ from operator import itemgetter from geoid.acs import Tract st = resource.schema_term header = [] columns = [] for c in st.children: header.append(c.name) if c.get('col_pos'): columns.append(int(c.col_pos) - 1) yield header ig = itemgetter(*columns) for ref in doc.references(): for row in ref: yield (Tract(row[2], row[3], row[4]), ) + ig(row)
def extract_county(resource, doc, env, *args, **kwargs): from geoid.acs import Tract r = doc.reference('census_planning_database') state_id = int(resource.state) county_id = int(resource.county) for i, row in enumerate(r.iterdict): state = row['State'] county = row['County'] tract = row['Tract'] del row['GIDTR'] del row['State'] del row['County'] del row['State_name'] del row['County_name'] del row['Tract'] del row['Num_BGs_in_Tract'] if i == 0: yield ['geoid'] + list(row.keys()) if int(state) == state_id and int(county) == county_id: for col in ['Med_House_Value_ACS_12_16', 'Med_House_Value_ACSMOE_12_16', 'Aggr_House_Value_ACS_12_16', 'Aggr_House_Value_ACSMOE_12_16', 'avg_Agg_HH_INC_ACS_12_16', 'avg_Agg_HH_INC_ACSMOE_12_16', 'Med_HHD_Inc_ACS_12_16', 'Med_HHD_Inc_ACSMOE_12_16', 'Aggregate_HH_INC_ACS_12_16', 'Aggregate_HH_INC_ACSMOE_12_16', 'avg_Agg_House_Value_ACS_12_16','avg_Agg_House_Value_ACSMOE_12_16']: row[col] = clean_currency(row[col]) tract = Tract(state, county, tract ) yield [str(tract)] + list(row.values())
for row_n, was_geocoded, row in chunked_geocode(doc): if not row.get('tract_geoid'): row['tract_geoid'] = zip_to_tract(fac_zip[int(row['unique_id'])], int(row['unique_id'])) row['side_of_street'] = None row['tiger_id'] = None if row['tract_geoid']: if len(row['tract_geoid']) != 18: # It's probably still a Census Tract, so convert it to an Acs tract. row['tract_geoid'] = str(CensusTract.parse(row['tract_geoid'].zfill(11)).convert(AcsTract)) assert(len(row['tract_geoid'])) == 18, row['tract_geoid'] t = AcsTract.parse(row['tract_geoid']) #print(str(t), file=sys.stderr) row['state_fips'] = t.state row['county_fips'] = t.county row['tract_fips'] = t.tract if row.get('state_fips'): row['state_fips'] = str(row['state_fips']).zfill(2) if row.get('county_fips'): row['county_fips'] = str(row.get('county_fips')).zfill(3) try:
def mk_tract_geoid(v): if v is None or v is np.nan: return None else: return str(Tract(6, 73, int(v)))