diff --git a/hood/db.py b/hood/db.py index e1cbdd4..f6169f1 100644 --- a/hood/db.py +++ b/hood/db.py @@ -56,14 +56,14 @@ def get(self, select, d): def get_by_dict(self, d): select = """ - SELECT * FROM location WHERE + SELECT * FROM location WHERE """ + \ ' AND '.join( self._key_tuple(d.keys()) ) return self.get(select, d) - def get_mappable_by_hood(self, hood, distinct=True): + def get_mappable_by_hood_id(self, hood_id, distinct=True): distinct = distinct and 'DISTINCT' or '' select_hood = """SELECT %s long, lat FROM location """ % distinct select_stat = """SELECT @@ -75,7 +75,7 @@ def get_mappable_by_hood(self, hood, distinct=True): where = """ WHERE - hood = %(hood)s + hood_id = %(hood_id)s AND long IS NOT NULL AND lat IS NOT NULL AND @@ -85,8 +85,8 @@ def get_mappable_by_hood(self, hood, distinct=True): ; """ - hoods = self.get(select_hood+where, {'hood':hood}) - stats = self.get(select_stat+where, {'hood':hood})[0] + hoods = self.get(select_hood+where, {'hood_id':hood_id}) + stats = self.get(select_stat+where, {'hood_id':hood_id})[0] avg = stats[0:2] stddev = stats[2:] @@ -109,9 +109,9 @@ def get_mappable_no_hood(self, hood, distinct=True): def get_neighborhoods(self): - select = """SELECT name FROM neighborhood ORDER BY lower(name);""" + select = """SELECT id, name FROM neighborhood ORDER BY lower(name);""" hoods = self.get(select, {}) - return [h[0] for h in hoods] + return [(h[0], h[1]) for h in hoods] def get_by_url(self, url): diff --git a/map.py b/map.py index 051ee8a..1f91f9e 100644 --- a/map.py +++ b/map.py @@ -58,7 +58,7 @@ colors.sort(lambda a,b: cmp(sum(b[:3]), sum(a[:3]))) database = db.DB(config) -hoods = database.get_neighborhoods() +hoods = database.get_neighborhoods() # chop off lightest and darkest colors if we have more colors than neighborhoods if len(colors) > len(hoods): @@ -66,7 +66,7 @@ colors = colors[chop:-chop] hood_colors = {} -for i, hood in enumerate(hoods): +for i, (hood_id, hood) in enumerate(hoods): hood_colors[hood] = colors[i%len(colors)] @@ -138,8 +138,8 @@ def to_image(self, lng, lat): font = ImageFont.truetype(config['font_path'], config['font_size']) all_image = Image.new('RGBA', m.image_size, (0xff, 0xff, 0xff, 0x00)) -for h, hood in enumerate(hoods): - average, stddev, points = database.get_mappable_by_hood(hood) +for h, (hood_id, hood) in enumerate(hoods): + average, stddev, points = database.get_mappable_by_hood_id(hood_id) print("Mapping {:31s} {:4d} points, ".format('"'+hood+'",', len(points)), end='') diff --git a/webpage/web.py b/webpage/web.py index 13dffbe..5be33b8 100644 --- a/webpage/web.py +++ b/webpage/web.py @@ -175,7 +175,7 @@ def add_location(req, database): def make_page(req, database, data, status, errors): error_str = '\n'.join(['Error: %s
'%e for e in errors]) - db_hoods = database.get_neighborhoods() + db_hoods = [h[1] for h in database.get_neighborhoods()] hoods_set = config['neighborhoods'] | set(db_hoods) hoods = list(hoods_set) hoods.sort( lambda a,b: cmp(a.lower(), b.lower()))