Skip to content

Commit

Permalink
get mappable neighborhoods by hood_id, not hood name
Browse files Browse the repository at this point in the history
allows for mapped points attached to neighborhoods with case-insensitively different names to get mapped, such as "NOPA" vs "NoPa" vs "nopa"
  • Loading branch information
glyphobet committed Oct 18, 2015
1 parent b19a015 commit fa05449
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
14 changes: 7 additions & 7 deletions hood/db.py
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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:]
Expand All @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions map.py
Expand Up @@ -58,15 +58,15 @@
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):
chop = int((len(colors) - len(hoods)) / 2)
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)]


Expand Down Expand Up @@ -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='')

Expand Down
2 changes: 1 addition & 1 deletion webpage/web.py
Expand Up @@ -175,7 +175,7 @@ def add_location(req, database):
def make_page(req, database, data, status, errors):
error_str = '\n'.join(['<b>Error:</b> %s<br>'%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()))
Expand Down

0 comments on commit fa05449

Please sign in to comment.