def cache_segregation(): # Segregation index tot_pct_white = get_pop_weighted_characteristic("pct_whitenh",\ tracts=CensusTract.objects.all()) tot_pct_black = get_pop_weighted_characteristic("pct_blacknh",\ tracts=CensusTract.objects.all()) tot_pct_hisp = get_pop_weighted_characteristic("pct_hispanic",\ tracts=CensusTract.objects.all()) tot_pct_asian = get_pop_weighted_characteristic("pct_asiannh",\ tracts=CensusTract.objects.all()) # We'll define it as the percentage of people who would have to # move in order for the geometry to match Cook County as a whole. for geom_type,geom_str in \ zip([CensusTract,Municipality,Ward,CommunityArea],\ ['Census Tract', 'Municipality', 'Ward', 'Community Area']): for geom in geom_type.objects.all(): pct_white = get_pop_weighted_characteristic("pct_whitenh",\ geoms=geom) pct_black = get_pop_weighted_characteristic("pct_blacknh",\ geoms=geom) pct_hisp = get_pop_weighted_characteristic("pct_hispanic",\ geoms=geom) pct_asian = get_pop_weighted_characteristic("pct_asiannh",\ geoms=geom) segregation = (\ abs(tot_pct_white-pct_white)+\ abs(tot_pct_black-pct_black)+\ abs(tot_pct_hisp-pct_hisp)+\ abs(tot_pct_asian-pct_asian))/2.0 cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name = 'segregation',\ indicator_value = segregation) cv.save()
def cache_population(): # Total population. for geom_type,geom_str in \ zip([CensusTract,Municipality,Ward,CommunityArea],\ ['Census Tract', 'Municipality', 'Ward', 'Community Area']): for geom in geom_type.objects.all(): cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name = 'pop',\ indicator_value = get_population(geoms=geom)) cv.save()
def cache_vacancyusps_indicator(): for geom_type,geom_str in \ zip([CensusTract,Municipality,Ward,CommunityArea],\ ['Census Tract', 'Municipality', 'Ward', 'Community Area']): for geom in geom_type.objects.all(): print '.' retval = get_vacancyusps(geoms=geom) for k, v in retval.iteritems(): cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='vacancy_usps',\ indicator_value = v*100.0,\ indicator_date = quarter_to_datetime(k)) cv.save()
def cache_vacancy311_indicator(): lastyear = cst.localize(datetime.datetime.now() - datetime.timedelta(days=365.25)) city_geom = Municipality.objects.get(name='Chicago').geom for geom_type,geom_str in \ zip([CensusTract,Municipality,Ward,CommunityArea],\ ['Census Tract', 'Municipality', 'Ward', 'Community Area']): for geom in geom_type.objects.all(): if (geom_type == CensusTract): if not city_geom.intersects(geom.loc): continue myinds = Vacancy311.objects.filter(loc__within=geom.loc).\ filter(request_date__gte=lastyear) assess = Assessor.objects.filter(loc__contained=geom.loc) cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='vacancy_311',\ indicator_value = float(len(myinds))/len(assess)*100,\ indicator_date = lastyear) cv.save() else: if not city_geom.intersects(geom.geom): continue if geom_type == Municipality and geom.name != 'Chicago': continue myinds = Vacancy311.objects.filter(loc__within=geom.geom).\ filter(request_date__gte=lastyear) assess = Assessor.objects.filter(loc__contained=geom.geom) cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='vacancy_311',\ indicator_value = float(len(myinds))/len(assess)*100,\ indicator_date = lastyear) cv.save()
def cache_demolition_indicator(): lastyear = cst.localize(datetime.datetime.now() - datetime.timedelta(days=365.25)) city_geom = Municipality.objects.get(name='Chicago').geom for geom_type,geom_str in \ zip([CensusTract,Municipality,Ward,CommunityArea],\ ['Census Tract', 'Municipality', 'Ward', 'Community Area']): for geom in geom_type.objects.all(): pop = IndicatorCache.objects.get(indicator_name='pop', \ area_type=geom_str, area_id=geom.id).indicator_value if pop == 0: continue if (geom_type == CensusTract): if not city_geom.intersects(geom.loc): continue myinds = BuildingPermit.objects.filter(loc__within=geom.loc).\ filter(timestamp__gte=lastyear).\ filter(permit_type__exact='PERMIT - WRECKING/DEMOLITION') cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='demolitions_pc',\ indicator_value = float(len(myinds))/pop,\ indicator_date = lastyear) cv.save() else: if not city_geom.intersects(geom.geom): continue if geom_type == Municipality and geom.name != 'Chicago': continue myinds = BuildingPermit.objects.filter(loc__within=geom.geom).\ filter(timestamp__gte=lastyear).\ filter(permit_type__exact='PERMIT - WRECKING/DEMOLITION') cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='demolitions_pc',\ indicator_value = float(len(myinds))/pop,\ indicator_date = lastyear) cv.save()
def cache_vacancyusps_indicator(): for geom_type,geom_str in \ zip([CensusTract,Municipality,Ward,CommunityArea],\ ['Census Tract', 'Municipality', 'Ward', 'Community Area']): for geom in geom_type.objects.all(): print '.' retval = get_vacancyusps(geoms=geom) for k,v in retval.iteritems(): cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='vacancy_usps',\ indicator_value = v*100.0,\ indicator_date = quarter_to_datetime(k)) cv.save()
def cache_income(): for field in [\ 'inc_lt_10', 'inc_10_15', 'inc_15_25', 'inc_25_35',\ 'inc_35_50', 'inc_50_75', 'inc_75_100', 'inc_100_150',\ 'inc_150_200', 'inc_gt_200', 'med_inc']: for geom_type,geom_str in \ zip([CensusTract,Municipality,Ward,CommunityArea],\ ['Census Tract', 'Municipality', 'Ward', 'Community Area']): for geom in geom_type.objects.all(): val = get_pop_weighted_characteristic(field,\ geoms=geom, income=True) cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name = field,\ indicator_value = val) cv.save()
def cache_accessibility_indicators(): for geom_type,geom_str in \ zip([CensusTract,Municipality,Ward,CommunityArea],\ ['Census Tract', 'Municipality', 'Ward', 'Community Area']): for geom in geom_type.objects.all(): geomloc = geom.loc if geom_type==CensusTract else geom.geom blocks = CensusBlock.objects.filter(loc__dwithin=(geomloc, 5280)) retval = 0.0 for block in blocks: for emp in block.censusblockemployment_set.all(): retval += emp.jobs pop = IndicatorCache.objects.get(indicator_name='pop', \ area_type=geom_str, area_id=geom.id).indicator_value if pop==0: continue cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='jobs_within_mile_pc',\ indicator_value = retval/float(pop)) cv.save()
def cache_census(): # Other census indicators. for indicator in ['median_age', 'pct_18plus', 'pct_65plus', 'pct_whitenh',\ 'pct_blacknh', 'pct_asiannh', 'pct_hispanic', 'pct_owner_occupied',\ 'pct_occ_units',\ 'pct_renter_occupied', 'owner_occ_hh_size', 'renter_occ_hh_size']: for geom_type,geom_str in \ zip([CensusTract,Municipality,Ward,CommunityArea],\ ['Census Tract', 'Municipality', 'Ward', 'Community Area']): for geom in geom_type.objects.all(): retval = get_pop_weighted_characteristic(indicator, geoms=geom) cv = IndicatorCache(\ area_type=geom_str,\ area_id = geom.id,\ indicator_name = indicator,\ indicator_value = retval) cv.save()
def cache_accessibility_indicators(): for geom_type,geom_str in \ zip([CensusTract,Municipality,Ward,CommunityArea],\ ['Census Tract', 'Municipality', 'Ward', 'Community Area']): for geom in geom_type.objects.all(): geomloc = geom.loc if geom_type == CensusTract else geom.geom blocks = CensusBlock.objects.filter(loc__dwithin=(geomloc, 5280)) retval = 0.0 for block in blocks: for emp in block.censusblockemployment_set.all(): retval += emp.jobs pop = IndicatorCache.objects.get(indicator_name='pop', \ area_type=geom_str, area_id=geom.id).indicator_value if pop == 0: continue cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='jobs_within_mile_pc',\ indicator_value = retval/float(pop)) cv.save()
def cache_vacancy311_indicator(): lastyear = cst.localize(datetime.datetime.now() - datetime.timedelta(days=365.25)) city_geom=Municipality.objects.get(name='Chicago').geom for geom_type,geom_str in \ zip([CensusTract,Municipality,Ward,CommunityArea],\ ['Census Tract', 'Municipality', 'Ward', 'Community Area']): for geom in geom_type.objects.all(): if (geom_type==CensusTract): if not city_geom.intersects(geom.loc): continue myinds = Vacancy311.objects.filter(loc__within=geom.loc).\ filter(request_date__gte=lastyear) assess = Assessor.objects.filter(loc__contained=geom.loc) cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='vacancy_311',\ indicator_value = float(len(myinds))/len(assess)*100,\ indicator_date = lastyear) cv.save() else: if not city_geom.intersects(geom.geom): continue if geom_type==Municipality and geom.name!='Chicago': continue myinds = Vacancy311.objects.filter(loc__within=geom.geom).\ filter(request_date__gte=lastyear) assess = Assessor.objects.filter(loc__contained=geom.geom) cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='vacancy_311',\ indicator_value = float(len(myinds))/len(assess)*100,\ indicator_date = lastyear) cv.save()
def cache_demolition_indicator(): lastyear = cst.localize(datetime.datetime.now() - datetime.timedelta(days=365.25)) city_geom=Municipality.objects.get(name='Chicago').geom for geom_type,geom_str in \ zip([CensusTract,Municipality,Ward,CommunityArea],\ ['Census Tract', 'Municipality', 'Ward', 'Community Area']): for geom in geom_type.objects.all(): pop = IndicatorCache.objects.get(indicator_name='pop', \ area_type=geom_str, area_id=geom.id).indicator_value if pop==0: continue if (geom_type==CensusTract): if not city_geom.intersects(geom.loc): continue myinds = BuildingPermit.objects.filter(loc__within=geom.loc).\ filter(timestamp__gte=lastyear).\ filter(permit_type__exact='PERMIT - WRECKING/DEMOLITION') cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='demolitions_pc',\ indicator_value = float(len(myinds))/pop,\ indicator_date = lastyear) cv.save() else: if not city_geom.intersects(geom.geom): continue if geom_type==Municipality and geom.name!='Chicago': continue myinds = BuildingPermit.objects.filter(loc__within=geom.geom).\ filter(timestamp__gte=lastyear).\ filter(permit_type__exact='PERMIT - WRECKING/DEMOLITION') cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='demolitions_pc',\ indicator_value = float(len(myinds))/pop,\ indicator_date = lastyear) cv.save()
def cache_construction_indicator(): lastyear = cst.localize(datetime.datetime.now() - datetime.timedelta(days=365.25)) city_geom = Municipality.objects.get(name='Chicago').geom for geom_type,geom_str in \ zip([CensusTract,Municipality,Ward,CommunityArea],\ ['Census Tract', 'Municipality', 'Ward', 'Community Area']): for geom in geom_type.objects.all(): pop = IndicatorCache.objects.get(indicator_name='pop', \ area_type=geom_str, area_id=geom.id).indicator_value if pop == 0: continue if (geom_type == CensusTract): if not city_geom.intersects(geom.loc): continue myinds = BuildingPermit.objects.filter(loc__within=geom.loc).\ filter(timestamp__gte=lastyear) retval = 0 for myind in myinds: if myind.cost is not None: retval += myind.cost cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='construction_pc',\ indicator_value = float(retval)/pop,\ indicator_date = lastyear) cv.save() else: if not city_geom.intersects(geom.geom): continue if geom_type == Municipality and geom.name != 'Chicago': continue myinds = BuildingPermit.objects.filter(loc__within=geom.geom).\ filter(timestamp__gte=lastyear) retval = 0 for myind in myinds: if myind.cost is not None: retval += myind.cost cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='construction_pc',\ indicator_value = float(retval)/pop,\ indicator_date = lastyear) cv.save()
def cache_construction_indicator(): lastyear = cst.localize(datetime.datetime.now() - datetime.timedelta(days=365.25)) city_geom=Municipality.objects.get(name='Chicago').geom for geom_type,geom_str in \ zip([CensusTract,Municipality,Ward,CommunityArea],\ ['Census Tract', 'Municipality', 'Ward', 'Community Area']): for geom in geom_type.objects.all(): pop = IndicatorCache.objects.get(indicator_name='pop', \ area_type=geom_str, area_id=geom.id).indicator_value if pop==0: continue if (geom_type==CensusTract): if not city_geom.intersects(geom.loc): continue myinds = BuildingPermit.objects.filter(loc__within=geom.loc).\ filter(timestamp__gte=lastyear) retval = 0 for myind in myinds: if myind.cost is not None: retval += myind.cost cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='construction_pc',\ indicator_value = float(retval)/pop,\ indicator_date = lastyear) cv.save() else: if not city_geom.intersects(geom.geom): continue if geom_type==Municipality and geom.name!='Chicago': continue myinds = BuildingPermit.objects.filter(loc__within=geom.geom).\ filter(timestamp__gte=lastyear) retval = 0 for myind in myinds: if myind.cost is not None: retval += myind.cost cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='construction_pc',\ indicator_value = float(retval)/pop,\ indicator_date = lastyear) cv.save()
def cache_landuse_indicators(): for geom_type,geom_str in \ zip([CensusTract,Municipality,Ward,CommunityArea],\ ['Census Tract', 'Municipality', 'Ward', 'Community Area']): for geom in geom_type.objects.all(): allpins, sfh, condo, multi, commind = None, None, None, None, None if (geom_type==CensusTract): allpins = Assessor.objects.filter(loc__within=geom.loc) sfh = Assessor.objects.filter(loc__within=geom.loc).\ filter(ptype_id__exact=1) condo = Assessor.objects.filter(loc__within=geom.loc).\ filter(ptype_id__exact=2) multi = Assessor.objects.filter(loc__within=geom.loc).\ filter(ptype_id__in=(3,4)) commind = Assessor.objects.filter(loc__within=geom.loc).\ filter(ptype_id__exact=(5)) else: allpins = Assessor.objects.filter(loc__within=geom.geom) sfh = Assessor.objects.filter(loc__within=geom.geom).\ filter(ptype_id__exact=1) condo = Assessor.objects.filter(loc__within=geom.geom).\ filter(ptype_id__exact=2) multi = Assessor.objects.filter(loc__within=geom.geom).\ filter(ptype_id__in=(3,4)) commind = Assessor.objects.filter(loc__within=geom.geom).\ filter(ptype_id__exact=(5)) tot_pins = len(allpins) if tot_pins == 0: continue cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='pct_sfh',\ indicator_value = float(len(sfh))/len(allpins)*100) cv.save() cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='pct_condo',\ indicator_value = float(len(condo))/len(allpins)*100) cv.save() cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='pct_multifamily',\ indicator_value = float(len(multi))/len(allpins)*100) cv.save() cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='pct_commind',\ indicator_value = float(len(commind))/len(allpins)*100) cv.save()
def cache_landuse_indicators(): for geom_type,geom_str in \ zip([CensusTract,Municipality,Ward,CommunityArea],\ ['Census Tract', 'Municipality', 'Ward', 'Community Area']): for geom in geom_type.objects.all(): allpins, sfh, condo, multi, commind = None, None, None, None, None if (geom_type == CensusTract): allpins = Assessor.objects.filter(loc__within=geom.loc) sfh = Assessor.objects.filter(loc__within=geom.loc).\ filter(ptype_id__exact=1) condo = Assessor.objects.filter(loc__within=geom.loc).\ filter(ptype_id__exact=2) multi = Assessor.objects.filter(loc__within=geom.loc).\ filter(ptype_id__in=(3,4)) commind = Assessor.objects.filter(loc__within=geom.loc).\ filter(ptype_id__exact=(5)) else: allpins = Assessor.objects.filter(loc__within=geom.geom) sfh = Assessor.objects.filter(loc__within=geom.geom).\ filter(ptype_id__exact=1) condo = Assessor.objects.filter(loc__within=geom.geom).\ filter(ptype_id__exact=2) multi = Assessor.objects.filter(loc__within=geom.geom).\ filter(ptype_id__in=(3,4)) commind = Assessor.objects.filter(loc__within=geom.geom).\ filter(ptype_id__exact=(5)) tot_pins = len(allpins) if tot_pins == 0: continue cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='pct_sfh',\ indicator_value = float(len(sfh))/len(allpins)*100) cv.save() cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='pct_condo',\ indicator_value = float(len(condo))/len(allpins)*100) cv.save() cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='pct_multifamily',\ indicator_value = float(len(multi))/len(allpins)*100) cv.save() cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='pct_commind',\ indicator_value = float(len(commind))/len(allpins)*100) cv.save()
def cache_market_indicators(): for geom_type,geom_str in \ zip([CensusTract,Municipality,Ward,CommunityArea],\ ['Census Tract', 'Municipality', 'Ward', 'Community Area']): for geom in geom_type.objects.all(): print '.' retval = get_foreclosure_rate(geoms=geom) for k, v in retval.iteritems(): cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='foreclosure_rate',\ indicator_value = v,\ indicator_date = quarter_to_datetime(k)) cv.save() retval = get_median_price(geoms=geom) for k, v in retval.iteritems(): cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='median_price',\ indicator_value = v,\ indicator_date = quarter_to_datetime(k)) cv.save() retval = get_transactions_per_thousand(geoms=geom) for k, v in retval.iteritems(): cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='transactions_per_thousand',\ indicator_value = v,\ indicator_date = quarter_to_datetime(k)) cv.save() retval = get_mortgages_per_thousand(geoms=geom) for k, v in retval.iteritems(): cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='mortgages_per_thousand',\ indicator_value = v,\ indicator_date = quarter_to_datetime(k)) cv.save() retval = get_percent_lowvalue_transactions(geoms=geom) for k, v in retval.iteritems(): cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='percent_lowvalue',\ indicator_value = v,\ indicator_date = quarter_to_datetime(k)) cv.save() retval = get_percent_business_buyers(geoms=geom) for k, v in retval.iteritems(): cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='percent_businessbuyers',\ indicator_value = v,\ indicator_date = quarter_to_datetime(k)) cv.save()
def cache_market_indicators(): for geom_type,geom_str in \ zip([CensusTract,Municipality,Ward,CommunityArea],\ ['Census Tract', 'Municipality', 'Ward', 'Community Area']): for geom in geom_type.objects.all(): print '.' retval = get_foreclosure_rate(geoms=geom) for k,v in retval.iteritems(): cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='foreclosure_rate',\ indicator_value = v,\ indicator_date = quarter_to_datetime(k)) cv.save() retval = get_median_price(geoms=geom) for k,v in retval.iteritems(): cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='median_price',\ indicator_value = v,\ indicator_date = quarter_to_datetime(k)) cv.save() retval = get_transactions_per_thousand(geoms=geom) for k,v in retval.iteritems(): cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='transactions_per_thousand',\ indicator_value = v,\ indicator_date = quarter_to_datetime(k)) cv.save() retval = get_mortgages_per_thousand(geoms=geom) for k,v in retval.iteritems(): cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='mortgages_per_thousand',\ indicator_value = v,\ indicator_date = quarter_to_datetime(k)) cv.save() retval = get_percent_lowvalue_transactions(geoms=geom) for k,v in retval.iteritems(): cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='percent_lowvalue',\ indicator_value = v,\ indicator_date = quarter_to_datetime(k)) cv.save() retval = get_percent_business_buyers(geoms=geom) for k,v in retval.iteritems(): cv = IndicatorCache(\ area_type=geom_str, area_id = geom.id,\ indicator_name='percent_businessbuyers',\ indicator_value = v,\ indicator_date = quarter_to_datetime(k)) cv.save()