def handle(self, *args, **options): CENSUS_BLOCK_2010 = pickle.load(open(BOUNDARY_DATA + "census.p", 'rb')) NEIGHBORHOOD = pickle.load(open(BOUNDARY_DATA + "community.p", 'rb')) NEIGHBORHOOD_INDEX = create_spatial_index(NEIGHBORHOOD) aggregate_results = {} for i, (aggregate_id, aggregate_shape) in \ enumerate(CENSUS_BLOCK_2010.iteritems(), 1): # print out a nice message to know how it's going if i % 1000 == 0: print >> sys.stderr, 'Aggregating %i of %i' % \ (i, len(CENSUS_BLOCK_2010)) if not CensusBlocks.objects.filter( census_id=aggregate_id, building_subtype="All").exists(): print >> sys.stderr, "Updating census_id: %s" % (aggregate_id) neighborhood_count = 0 for j, item_2010 in enumerate( NEIGHBORHOOD_INDEX.intersection(aggregate_shape.bounds, objects=True)): # get the element id raw_id = item_2010.object # get the shape from the shape dictionary raw_shape = NEIGHBORHOOD[raw_id] # calculate the intersection of the polygons intersection = aggregate_shape.intersection(raw_shape) # calculate the fraction of the area of the aggregate shape that # is in the raw shape frac_raw = float(intersection.area) / aggregate_shape.area # fix rawid in those two cases for mckliney and ohare raw_id = raw_id.title() if raw_id == 'Mckinley Park': raw_id = 'McKinley Park' elif raw_id == 'Ohare': raw_id = "O'hare" # if there is any area above tolerance, then add it up if neighborhood_count == 0: neighborhood, created = Neighborhoods.objects.get_or_create( name=raw_id) census_block, created = CensusBlocks.objects.get_or_create( census_id=aggregate_id, building_type="Residential", building_subtype="All", neighborhood=neighborhood, shape=wkt.dumps(aggregate_shape)) neighborhood_count += 1 else: continue
def handle(self, *args, **options): CENSUS_BLOCK_2010 = pickle.load(open(BOUNDARY_DATA + "census.p", 'rb')) NEIGHBORHOOD = pickle.load(open(BOUNDARY_DATA + "community.p",'rb')) NEIGHBORHOOD_INDEX = create_spatial_index(NEIGHBORHOOD) aggregate_results = {} for i, (aggregate_id, aggregate_shape) in \ enumerate(CENSUS_BLOCK_2010.iteritems(), 1): # print out a nice message to know how it's going if i % 1000 == 0: print >> sys.stderr, 'Aggregating %i of %i' % \ (i, len(CENSUS_BLOCK_2010)) if not CensusBlocks.objects.filter(census_id=aggregate_id, building_subtype="All").exists(): print >> sys.stderr, "Updating census_id: %s" %(aggregate_id) neighborhood_count = 0 for j,item_2010 in enumerate(NEIGHBORHOOD_INDEX.intersection(aggregate_shape.bounds, objects=True)): # get the element id raw_id = item_2010.object # get the shape from the shape dictionary raw_shape = NEIGHBORHOOD[raw_id] # calculate the intersection of the polygons intersection = aggregate_shape.intersection(raw_shape) # calculate the fraction of the area of the aggregate shape that # is in the raw shape frac_raw = float(intersection.area) / aggregate_shape.area # fix rawid in those two cases for mckliney and ohare raw_id = raw_id.title() if raw_id == 'Mckinley Park': raw_id = 'McKinley Park' elif raw_id == 'Ohare': raw_id = "O'hare" # if there is any area above tolerance, then add it up if neighborhood_count == 0: neighborhood, created = Neighborhoods.objects.get_or_create(name = raw_id) census_block, created = CensusBlocks.objects.get_or_create(census_id=aggregate_id, building_type="Residential", building_subtype="All", neighborhood = neighborhood, shape = wkt.dumps(aggregate_shape)) neighborhood_count += 1 else: continue
def handle(self, *args, **options): CENSUS_BLOCK_2010 = pickle.load(open(BOUNDARY_DATA + "census.p", 'rb')) NEIGHBORHOOD = pickle.load(open(BOUNDARY_DATA + "neighborhood.p",'rb')) NEIGHBORHOOD_INDEX = create_spatial_index(NEIGHBORHOOD) aggregate_results = {} for i, (aggregate_id, aggregate_shape) in \ enumerate(CENSUS_BLOCK_2010.iteritems(), 1): # print out a nice message to know how it's going # print >> sys.stderr, 'Aggregating %i of %i' % \ # (i, len(CENSUS_BLOCK_2010)) # try: # census_block = CensusBlocks.objects.get(census_id=aggregate_id, # building_type='Residential') # except CensusBlocks.DoesNotExist: # print aggregate_id # continue neighborhood_count = 0 for j,item_2010 in enumerate(NEIGHBORHOOD_INDEX.intersection(aggregate_shape.bounds, objects=True)): # get the element id raw_id = item_2010.object # get the shape from the shape dictionary raw_shape = NEIGHBORHOOD[raw_id] # calculate the intersection of the polygons intersection = aggregate_shape.intersection(raw_shape) # calculate the fraction of the area of the aggregate shape that # is in the raw shape frac_raw = float(intersection.area) / aggregate_shape.area # if there is any area above tolerance, then add it up if frac_raw >= .05: neighborhood_count += 1 if neighborhood_count > 2: print aggregate_id
def handle(self, *args, **options): CENSUS_BLOCK_2010 = pickle.load(open(BOUNDARY_DATA + "census.p", 'rb')) NEIGHBORHOOD = pickle.load(open(BOUNDARY_DATA + "neighborhood.p", 'rb')) NEIGHBORHOOD_INDEX = create_spatial_index(NEIGHBORHOOD) total = {} for i, (aggregate_id, aggregate_shape) in \ enumerate(CENSUS_BLOCK_2010.iteritems(), 1): # print out a nice message to know how it's going print >> sys.stderr, 'Aggregating %i of %i' % \ (i, len(CENSUS_BLOCK_2010)) # find all intersecting 2000 census blocks, along with the # fraction of the 2010 census block that will be assigned to # the 2000 census block # print dir(raw_spatial_index.intersection(aggregate_shape.bounds, # objects=True)).__sizeof__() try: matched_neighborhood = list( NEIGHBORHOOD_INDEX.intersection(aggregate_shape.bounds, objects=True))[0] except IndexError: continue neighborhood = Neighborhoods.objects.get( name=matched_neighborhood.object) try: census_blocks = CensusBlocks.objects.filter( census_id=aggregate_id) except CensusBlocks.DoesNotExist: print aggregate_id continue for census_block in census_blocks: census_block.neighborhood = neighborhood census_block.save()
def handle(self, *args, **options): CENSUS_BLOCK_2010 = pickle.load(open(BOUNDARY_DATA + "census.p", 'rb')) NEIGHBORHOOD = pickle.load(open(BOUNDARY_DATA + "neighborhood.p",'rb')) NEIGHBORHOOD_INDEX = create_spatial_index(NEIGHBORHOOD) total = {} for i, (aggregate_id, aggregate_shape) in \ enumerate(CENSUS_BLOCK_2010.iteritems(), 1): # print out a nice message to know how it's going print >> sys.stderr, 'Aggregating %i of %i' % \ (i, len(CENSUS_BLOCK_2010)) # find all intersecting 2000 census blocks, along with the # fraction of the 2010 census block that will be assigned to # the 2000 census block # print dir(raw_spatial_index.intersection(aggregate_shape.bounds, # objects=True)).__sizeof__() try: matched_neighborhood = list(NEIGHBORHOOD_INDEX.intersection(aggregate_shape.bounds, objects=True))[0] except IndexError: continue neighborhood = Neighborhoods.objects.get(name = matched_neighborhood.object) try: census_blocks = CensusBlocks.objects.filter(census_id=aggregate_id) except CensusBlocks.DoesNotExist: print aggregate_id continue for census_block in census_blocks: census_block.neighborhood = neighborhood census_block.save()
# --------------------------------------------- read in all of the boundary data # CENSUS_BLOCK_2000 = read_shapefile( # os.path.join(BOUNDARY_DATA, 'Census Blocks.shp'), # id_fieldname = "CENSUS_B_1", # simplification = SIMPLIFICATION, # ) # CENSUS_BLOCK_2000_INDEX = create_spatial_index(CENSUS_BLOCK_2000) print "getting 2010 census" CENSUS_BLOCK_2010 = read_shapefile( os.path.join(BOUNDARY_DATA, 'CensusBlockTIGER2010.shp'), id_fieldname = "GEOID10", simplification = SIMPLIFICATION, ) pickle.dump(CENSUS_BLOCK_2010, open("../web/apps/main/management/commands/census.p", 'wb')) CENSUS_BLOCK_2010_INDEX = create_spatial_index(CENSUS_BLOCK_2010) # print CENSUS_BLOCK_2010 # SCHOOL = read_shapefile( # os.path.join(BOUNDARY_DATA, 'BoundaryGrades10.shp'), # id_fieldname = "SCHOOLID", # simplification = SIMPLIFICATION, # ) # CENSUS_TRACT_2000 = read_shapefile( # os.path.join(BOUNDARY_DATA, 'Census_Tracts.shp'), # id_fieldname = "CENSUS_T_1", # simplification = SIMPLIFICATION, # )
def handle(self, *args, **options): g = geocoders.GeoNames() CENSUS_BLOCK_2010 = pickle.load(open(BOUNDARY_DATA + "census.p", 'rb')) NEIGHBORHOOD = pickle.load(open(BOUNDARY_DATA + "community.p",'rb')) NEIGHBORHOOD_INDEX = create_spatial_index(NEIGHBORHOOD) aggregate_results = {} for i, (aggregate_id, aggregate_shape) in \ enumerate(CENSUS_BLOCK_2010.iteritems(), 1): # print out a nice message to know how it's going if i % 100 == 0: print >> sys.stderr, 'Aggregating %i of %i' % \ (i, len(CENSUS_BLOCK_2010)) # try: # census_blocks = CensusBlocks.objects.filter(census_id=aggregate_id) # except CensusBlocks.DoesNotExist: # continue neighborhood_count = 0 for j,item_2010 in enumerate(NEIGHBORHOOD_INDEX.intersection(aggregate_shape.bounds, objects=True)): # get the element id raw_id = item_2010.object # get the shape from the shape dictionary raw_shape = NEIGHBORHOOD[raw_id] # calculate the intersection of the polygons intersection = aggregate_shape.intersection(raw_shape) # calculate the fraction of the area of the aggregate shape that # is in the raw shape frac_raw = float(intersection.area) / aggregate_shape.area # fix rawid in those two cases for mckliney and ohare raw_id = raw_id.title() if raw_id == 'Mckinley Park': raw_id = 'McKinley Park' elif raw_id == 'Ohare': raw_id = "O'hare" # if there is any area above tolerance, then add it up if frac_raw >= .05 and neighborhood_count == 0: neighborhood, created = Neighborhoods.objects.get_or_create(name = raw_id) # Neighborhoods.objects.filter(name = neighborhood.name)\ # .update(shape = wkt.dumps(raw_shape)) #json.dumps(list(raw_shape.exterior.coords))) # neighborhood.shape = json.dumps(list(raw_shape.exterior.coords)) # neighborhood.save() # Add the average value to the non-existing rows if not CensusBlocks.objects.filter(census_id=aggregate_id, building_type = "Residential", building_subtype = "Single Family").exists(): CensusBlocks.objects.create(census_id=aggregate_id, building_type = "Residential", building_subtype = "Single Family", kwh_efficiency = neighborhood.kwh_efficiency, therm_efficiency = neighborhood.therm_efficiency, neighborhood = neighborhood, shape = wkt.dumps(aggregate_shape)) if not CensusBlocks.objects.filter(census_id=aggregate_id, building_type = "Residential", building_subtype = "Multi 7+").exists(): CensusBlocks.objects.create(census_id=aggregate_id, building_type = "Residential", building_subtype = "Multi 7+", kwh_efficiency = neighborhood.kwh_efficiency, therm_efficiency = neighborhood.therm_efficiency, neighborhood = neighborhood, shape = wkt.dumps(aggregate_shape)) if not CensusBlocks.objects.filter(census_id=aggregate_id, building_type = "Residential", building_subtype = "Multi < 7").exists(): CensusBlocks.objects.create(census_id=aggregate_id, building_type = "Residential", building_subtype = "Multi < 7", kwh_efficiency = neighborhood.kwh_efficiency, therm_efficiency = neighborhood.therm_efficiency, neighborhood = neighborhood, shape = wkt.dumps(aggregate_shape)) # try: # shape = aggregate_shape # if shape != None: # lat, lon = list(shape.representative_point().coords)[0] # try: # (place, point) = g.reverse((lon,lat)) # except ValueError: # nice_address = "" # else: # # Get the address because we are rounding it out # address = place.split(",")[0] # broken = address.split() # try: # new_street = str(int(round(int(broken[0]) ,-2))) # except ValueError: # print address, place # nice_address = address # else: # broken = ' '.join(broken[1:]) # nice_address = new_street + " " + broken # else: # nice_address = "" # except (AttributeError) as e: # print "attribute error", aggregate_id # nice_address = "" # CensusBlocks.objects.filter(census_id=aggregate_id)\ # .update(neighborhood = neighborhood, # shape = wkt.dumps(aggregate_shape)) # nice_address = nice_address) #json.dumps(list(aggregate_shape.exterior.coords))) neighborhood_count += 1