def getNeighborhoodsByAreas(conn, areaids, user): print 'getting votes' (blocks, allVotes) = vote_utils.getVotes(conn, areaids, user) print 'got votes' blocks_by_hoodid = defaultdict(list) id_to_label = {} for block in blocks: votes = allVotes[block['geoid10']] #print block['geoid10'] maxVotes = vote_utils.pickBestVotes(votes) for maxVote in maxVotes: blocks_by_hoodid[maxVote['id']].append(block) id_to_label[maxVote['id']] = maxVote['label'] hoods = {} print 'doing unions' for (id, blocks) in blocks_by_hoodid.iteritems(): geoms = [asShape(eval(block['geojson_geom'])) for block in blocks] blockids = [block['geoid10'] for block in blocks] pop10 = sum([block['pop10'] for block in blocks]) housing10 = sum([block['housing10'] for block in blocks]) geom = cascaded_union(geoms) hoods[id] = NeighborhoodArea(geom, blockids, pop10, housing10) return (hoods, id_to_label)
def citydata(): conn = psycopg2.connect("dbname='gis' user='******' host='localhost' password='******'") areaid = request.args.get('areaid', False) apikey = request.args.get('key', '') user = findUserByApiKey(apikey) (rows, votes) = vote_utils.getVotes(conn, areaid, user) response = { "type": "FeatureCollection", "features": makeFeatures(rows, votes, user) } return jsonify(response)
def citydata(): conn = getPostgresConnection() areaid = request.args.get('areaid', False) apikey = request.args.get('key', '') user = findUserByApiKey(conn, apikey) (rows, votes) = vote_utils.getVotes(conn, areaid, user) response = { "type": "FeatureCollection", "features": makeFeatures(rows, votes, user) } return jsonify(response)
def citydata(): conn = psycopg2.connect( "dbname='gis' user='******' host='localhost' password='******'") areaid = request.args.get('areaid', False) apikey = request.args.get('key', '') user = findUserByApiKey(apikey) (rows, votes) = vote_utils.getVotes(conn, areaid, user) response = { "type": "FeatureCollection", "features": makeFeatures(rows, votes, user) } return jsonify(response)
def getNeighborhoodsByArea(conn, areaid, user): (blocks, allVotes) = vote_utils.getVotes(conn, areaid, user) blocks_by_hoodid = defaultdict(list) blockids_by_hoodid = defaultdict(list) id_to_label = {} for block in blocks: geom = asShape(eval(block['geojson_geom'])) votes = allVotes[block['geoid10']] #print block['geoid10'] maxVotes = vote_utils.pickBestVotes(votes) for maxVote in maxVotes: blocks_by_hoodid[maxVote['id']].append(geom) blockids_by_hoodid[maxVote['id']].append(block['geoid10']) id_to_label[maxVote['id']] = maxVote['label'] hoods = {} for (id, geoms) in blocks_by_hoodid.iteritems(): hoods[id] = NeighborhoodArea(cascaded_union(geoms), blockids_by_hoodid[id]) return (hoods, id_to_label)
def processArea(areaid): hoodDict = {} (hoods, id_to_label) = geo_utils.getNeighborhoodsByArea(conn, areaid, None) for (id, shape) in hoods.iteritems(): hoodIndex.add(int(id), shape.bounds) hoodDict[int(id)] = shape (rows, voteDict) = vote_utils.getVotes(conn, areaid, None) bestVoteDict = {} print 'building vote dict' for k,v in voteDict.iteritems(): bestVoteDict[str(k).zfill(15)] = vote_utils.pickBestVote(v) print 'building rtree' shapeDict = {} for r in rows: id = r['geoid10'] shape = asShape(geojson.loads(r['geojson_geom'])) shapeDict[id] = shape index.add(int(id), shape.bounds) print 'smoothin' for r in rows: id = str(r['geoid10']).zfill(15) bestId = None if id in bestVoteDict: bestId = bestVoteDict[id]['id'] shape = shapeDict[id] # find all the blocks it maybe touches candidate_ids = set([n for n in index.intersection(shape.bounds)]) touches = [] for cid in candidate_ids: if cid != id: cshape = shapeDict[str(cid).zfill(15)] if shape.touches(cshape): touches.append(cid) print '%s touches %s' % (id, touches) smearDict = defaultdict(lambda: 0) totalWithVotes = 0 for t in touches: tid = str(t).zfill(15) if tid in bestVoteDict: cbestId = bestVoteDict[tid]['id'] smearDict[cbestId] += 1 totalWithVotes += 1 candidate_hood_ids = set([n for n in hoodIndex.intersection(shape.bounds)]) for hood_id in candidate_hood_ids: hoodShape = hoodDict[hood_id] if hoodShape.contains(shape): print 'block %s was contained by neighborhood %s' % (id, hood_id) smearDict[hood_id] += 1000000 print smearDict if len(smearDict) > 0: maxVotes = {x for x in itertools.groupby(smearDict.iteritems(), operator.itemgetter(1))} maxCount = max(maxVotes.keys()) isAmbiguous = len(maxVotes[maxCount]) > 1 maxVote = maxVotes[maxCount][0] threshold = totalWithVotes * (5.0/8.0) if maxVote[1] >= threshold and (maxVote[0] != bestId or isAmbiguous): print 'very likely that block %s should be in %s, is in %s' % (id, maxVote[0], bestId) cur.execute("""DELETE FROM votes WHERE source=%s AND id=%s""", ('smear', id)) cur.execute("""INSERT INTO votes (id, label, count, source) values (%s, %s, %s, 'smear')""", ( id, maxVote[0], 1)) conn.commit()