def dist_join(db, t1, t2, r, limit=7000):
    " given radius r1, r2, find overlapping lat lons "
    
    t1cols = get_numeric_columns(db, t1, ignore=['id'])
    t2cols = get_numeric_columns(db, t2, ignore=['id'])
    sels1 = map(lambda s: '%s.%s' % (t1, s), t1cols)
    sels2 = map(lambda s: '%s.%s' % (t2, s), t2cols)
    sels = sels1 + sels2
    
    if not sels:
        return []
    
    cols = [(t1, c) for c in t1cols] + [(t2, c) for c in t2cols]

    q = """select count(*) from %s, %s
           where (%s._latlon <-> %s._latlon) < %%s and
           %s._latlon is not null and %s._latlon is not null
           """ % (t1, t2, t1, t2, t1, t2)
    count = db.execute(q, (r,)).fetchone()[0]
    if count == 0:
        return []
    thresh = float(limit) / count
    print "\tjoincount\t%d" % count, t1, t2
    
    q = """select %s from %s, %s
           where (%s._latlon <-> %s._latlon) < %%s and
           %s._latlon is not null and %s._latlon is not null and
           random() <= %%s
           """ % (','.join(sels), t1, t2, t1, t2, t1, t2)
    res = db.execute(q, (r, thresh)).fetchall()
    return [dict(zip(cols, row)) for row in res]
    



### Same zoom level
dataset/ETHPy150Open sirrice/dbtruck/dbtruck/analyze/fuzzyjoin.py/dist_join/original