def generate_user_home_work(user): user_home=detect_home(user) # print user_home zip_is_valid = _check_zip_validity(user_home, user) logging.debug('starting for %s' % user) if Profiles.find({'user_id':user}).count()==0: profile_todo={'source':'Shankari','user_id': user,'home':user_home} Profiles.insert(profile_todo) else: #TODO: make detect_home return something better than a N/A string Profiles.update({"$and":[{'source':'Shankari'}, {'user_id':user}]},{"$set":{'home':user_home}}) user_work=detect_work_office(user) Profiles.update({"$and":[{'source':'Shankari'},{'user_id':user}]},{"$set":{'work_place':user_work}}) user_zip=get_userZipcode(user, zip_is_valid) Profiles.update({"$and":[{'source':'Shankari'},{'user_id':user}]},{"$set":{'zip':user_zip}}) if user_zip!='N/A': geoinfo= Geocoder.geocode(user_zip) # geocoder returns data in lat,lng format. # we convert to lng,lat internally, since that is the format that the # rest of our code is in zipCen=[geoinfo[0].coordinates[1],geoinfo[0].coordinates[0]] else: zipCen='N/A' Profiles.update({"$and":[{'source':'Shankari'},{'user_id':user}]},{"$set":{'zip_centroid':zipCen}}) for day in range(1,6): key='work'+str(day) Profiles.update({"$and":[{'source':'Shankari'}, {'user_id':user}]},{"$set":{key:detect_daily_work_office(user,day)}})
def detect_daily_work_office(user_id,day): # say should be from 1 to 5 Sections=edb.get_section_db() office_candidate=[] home=eamh.detect_home(user_id) if home == 'N/A': return 'N/A' # print(list_first_pnt) for section in Sections.find({"$and":[{"user_id": user_id},{ "section_start_point": { "$ne": None }}]}): section_start_pnt=section['section_start_point'] section_end_pnt=section['section_end_point'] if ec.Is_date(section['section_start_time'],day)==True: # parameter that the distance away from home: away_home=400 if not ec.Is_place(section_start_pnt,home,away_home) and not ec.Is_place(section_end_pnt,home,away_home): office_candidate.append(section['track_points'][0]) office_candidate.append(section['track_points'][-1]) elif ec.Is_place(section_start_pnt,home,away_home) and not ec.Is_place(section_end_pnt,home,away_home): office_candidate.append(section['track_points'][-1]) elif not ec.Is_place(section_start_pnt,home,away_home) and ec.Is_place(section_end_pnt,home,away_home): office_candidate.append(section['track_points'][0]) if len(office_candidate)>0: office_candidate = sorted(office_candidate, key=lambda k: parser.parse(k['time'])) # print(office_candidate) weighted_office_candidate=ec.get_static_pnts(office_candidate) office_location=ec.most_common(weighted_office_candidate,200) # print(len(office_candidate)) # print(len(weighted_office_candidate)) # print(ec.calculate_appearance_rate(office_candidate,office_location)) # print(ec.calculate_appearance_rate(weighted_office_candidate,office_location)) return office_location else: return 'N/A'
def display_trip(user, date, option): user_id = UUID(user) user_home = detect_home(user_id) gmap = pygmaps.maps(user_home[1], user_home[0], 14) start, end = Date(date) for section in get_section_db().find({"$and":[{'user_id':user_id},{"section_start_datetime": {"$gte": start, "$lt": end}}]}): drawSection(section, option, gmap) gmap.draw('gmap_display/' + str(start)[:10] + '_' + user + '.html')
def display_home(): mymap_home = pygmaps.maps(37.8556475757, -122.248774009,14) for user in get_section_db().distinct('user_id'): print(user) user_home=detect_home(user) print(user_home) if user_home!='N/A': mymap_home.addpoint(user_home[1], user_home[0], "#FF0000") mymap_home.draw('mymap_home.html')
def display_home(): mymap_home = pygmaps.maps(37.8556475757, -122.248774009, 14) for user in get_section_db().distinct('user_id'): print(user) user_home = detect_home(user) print(user_home) if user_home != 'N/A': mymap_home.addpoint(user_home[1], user_home[0], "#FF0000") mymap_home.draw('mymap_home.html')
def detect_work_office(user_id): Sections = edb.get_section_db() office_candidate = [] home = eamh.detect_home(user_id) if home == 'N/A': return 'N/A' # Else, get home locations # print(list_first_pnt) for section in Sections.find({ "$and": [{ "user_id": user_id }, { "section_start_point": { "$ne": None } }] }): section_start_pnt = section['section_start_point'] section_end_pnt = section['section_end_point'] if ec.Is_weekday(section['section_start_time']) == True: # parameter that the distance away from home: away_home = 400 if not ec.Is_place(section_start_pnt, home, away_home) and not ec.Is_place( section_end_pnt, home, away_home): office_candidate.append(section['track_points'][0]) office_candidate.append(section['track_points'][-1]) elif ec.Is_place(section_start_pnt, home, away_home) and not ec.Is_place( section_end_pnt, home, away_home): office_candidate.append(section['track_points'][-1]) elif not ec.Is_place(section_start_pnt, home, away_home) and ec.Is_place( section_end_pnt, home, away_home): office_candidate.append(section['track_points'][0]) if len(office_candidate) > 0: office_candidate = sorted(office_candidate, key=lambda k: parser.parse(k['time'])) # print(office_candidate) weighted_office_candidate = ec.get_static_pnts(office_candidate) office_location = ec.most_common(weighted_office_candidate, 200) # print(len(office_candidate)) # print(len(weighted_office_candidate)) # print(ec.calculate_appearance_rate(office_candidate,office_location)) # print(ec.calculate_appearance_rate(weighted_office_candidate,office_location)) return office_location else: return 'N/A'
def get_daily_evening_commute_sections(user_id, day): # say should be from 1 to 5 # get a list of all the sections for Mon, or ... earlist_start = 5 earlist_end = 15 Sections = edb.get_section_db() list_of_commute = [] candidate_sections = [] home = eamh.detect_home(user_id) work = eamw.detect_daily_work_office(user_id, day) if work == "N/A": return [] else: # print(list_first_pnt) for section in Sections.find( {"$and": [{"user_id": user_id}, {"section_start_point": {"$ne": None}}, {"commute": {"$exists": False}}]} ): time2 = ec.parse_time(section["section_start_time"]) if ( ec.Is_date(section["section_start_time"], day) and (time2 - time2.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds() / 3600 > earlist_end ) or ( ec.Is_date(section["section_start_time"], day + 1) and (time2 - time2.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds() / 3600 < earlist_start ): candidate_sections.append(section) if len(candidate_sections) > 0: candidate_sections = sorted(candidate_sections, key=lambda k: parser.parse(k["section_start_time"])) min_sec = len(candidate_sections) for i in range(len(candidate_sections) - 1, -1, -1): if i <= min_sec: if ec.Is_place(candidate_sections[i]["section_end_point"], home, 200): for j in range(i, -1, -1): if ( ec.Is_place(candidate_sections[j]["section_start_point"], work, 200) and ec.travel_time( candidate_sections[j]["section_start_time"], candidate_sections[i]["section_end_time"], ) <= 24 * 60 * 60 ): sections_todo = [] sections_todo.extend(candidate_sections[j : i + 1]) list_of_commute.append(sections_todo) min_sec = j - 1 break return list_of_commute
def display_trip(user, date, option): user_id = UUID(user) user_home = detect_home(user_id) gmap = pygmaps.maps(user_home[1], user_home[0], 14) start, end = Date(date) for section in get_section_db().find({ "$and": [{ 'user_id': user_id }, { "section_start_datetime": { "$gte": start, "$lt": end } }] }): drawSection(section, option, gmap) gmap.draw('gmap_display/' + str(start)[:10] + '_' + user + '.html')
def get_daily_evening_commute_sections(user_id, day): # say should be from 1 to 5 # get a list of all the sections for Mon, or ... earlist_start = 5 earlist_end = 15 Sections = edb.get_section_db() list_of_commute = [] candidate_sections = [] home = eamh.detect_home(user_id) work = eamw.detect_daily_work_office(user_id, day) if work == 'N/A': return [] else: # print(list_first_pnt) for section in Sections.find({"$and":[{"user_id": user_id},{ "section_start_point": { "$ne": None }},\ {'commute':{ "$exists": False }}]}): time2 = ec.parse_time(section['section_start_time']) if (ec.Is_date(section['section_start_time'],day) and (time2 - time2.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds()/3600>earlist_end) or \ (ec.Is_date(section['section_start_time'],day+1) and (time2 - time2.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds()/3600<earlist_start): candidate_sections.append(section) if len(candidate_sections) > 0: candidate_sections = sorted( candidate_sections, key=lambda k: parser.parse(k['section_start_time'])) min_sec = len(candidate_sections) for i in range(len(candidate_sections) - 1, -1, -1): if i <= min_sec: if ec.Is_place(candidate_sections[i]['section_end_point'], home, 200): for j in range(i, -1, -1): if ec.Is_place(candidate_sections[j]['section_start_point'],work,200) and \ ec.travel_time(candidate_sections[j]['section_start_time'],\ candidate_sections[i]['section_end_time'])<=24*60*60: sections_todo = [] sections_todo.extend(candidate_sections[j:i + 1]) list_of_commute.append(sections_todo) min_sec = j - 1 break return list_of_commute
def searchTrip(user, period, startpoint, endpoint, mode, option): user_id = UUID(user) user_home = detect_home(user_id) gmap = pygmaps.maps(user_home[1], user_home[0], 14) start, end = Date(period) sectionList = [] startpoint = Geocoder.geocode(startpoint)[0].coordinates endpoint = Geocoder.geocode(endpoint)[0].coordinates for section in get_section_db().find({ "$and": [{ "mode": mode }, { "mode": { "$ne": 'airplane' } }, { "mode": { "$ne": 7 } }, { "section_start_point": { "$ne": None } }, { "section_end_point": { "$ne": None } }] }): point_start = section['section_start_point']['coordinates'] point_end = section['section_end_point']['coordinates'] if calDistance(startpoint, point_start) < 100 and calDistance( endpoint, point_end) < 100: sectionList.append(section['_id']) gmap.addpoint(point_end[1], point_end[0], COLOR[1]) gmap.addpoint(point_start[1], point_start[0], COLOR[1]) drawSection(section, option, gmap) gmap.draw('gmap_display/' + 'SearchResult' + str(start)[:10] + '-' + str(end)[:10] + '_' + user + '.html')
def get_daily_morning_commute_sections(user_id, day): # say should be from 1 to 5 # get a list of all the sections for Mon, or ... Sections = edb.get_section_db() list_of_commute = [] candidate_sections = [] home = eamh.detect_home(user_id) work = eamw.detect_daily_work_office(user_id, day) if work == "N/A": return [] else: # print(list_first_pnt) for section in Sections.find( {"$and": [{"user_id": user_id}, {"section_start_point": {"$ne": None}}, {"commute": {"$exists": False}}]} ): if ec.Is_date(section["section_start_time"], day): candidate_sections.append(section) if len(candidate_sections) > 0: candidate_sections = sorted(candidate_sections, key=lambda k: parser.parse(k["section_start_time"])) max_sec = 0 for i in range(len(candidate_sections)): if i >= max_sec: if ec.Is_place(candidate_sections[i]["section_start_point"], home, 200): for j in range(i, len(candidate_sections)): if ( ec.Is_place(candidate_sections[j]["section_end_point"], work, 200) and ec.travel_time( candidate_sections[i]["section_start_time"], candidate_sections[j]["section_end_time"], ) <= 24 * 60 * 60 ): sections_todo = [] sections_todo.extend(candidate_sections[i : j + 1]) list_of_commute.append(sections_todo) max_sec = j + 1 break return list_of_commute
def get_daily_morning_commute_sections(user_id, day): # say should be from 1 to 5 # get a list of all the sections for Mon, or ... Sections = edb.get_section_db() list_of_commute = [] candidate_sections = [] home = eamh.detect_home(user_id) work = eamw.detect_daily_work_office(user_id, day) if work == 'N/A': return [] else: # print(list_first_pnt) for section in Sections.find({"$and":[{"user_id": user_id},{ "section_start_point": { "$ne": None }},\ {'commute':{ "$exists": False }}]}): if ec.Is_date(section['section_start_time'], day): candidate_sections.append(section) if len(candidate_sections) > 0: candidate_sections = sorted( candidate_sections, key=lambda k: parser.parse(k['section_start_time'])) max_sec = 0 for i in range(len(candidate_sections)): if i >= max_sec: if ec.Is_place( candidate_sections[i]['section_start_point'], home, 200): for j in range(i, len(candidate_sections)): if ec.Is_place(candidate_sections[j]['section_end_point'],work,200) and \ ec.travel_time(candidate_sections[i]['section_start_time'],\ candidate_sections[j]['section_end_time'])<=24*60*60: sections_todo = [] sections_todo.extend(candidate_sections[i:j + 1]) list_of_commute.append(sections_todo) max_sec = j + 1 break return list_of_commute
def searchTrip(user, period, startpoint, endpoint, mode, option): user_id = UUID(user) user_home = detect_home(user_id) gmap = pygmaps.maps(user_home[1], user_home[0], 14) start, end = Date(period) sectionList = [] startpoint = Geocoder.geocode(startpoint)[0].coordinates endpoint = Geocoder.geocode(endpoint)[0].coordinates for section in get_section_db().find({"$and":[ {"mode": mode}, {"mode": {"$ne": 'airplane'}}, {"mode": {"$ne":7}}, {"section_start_point": {"$ne": None}}, {"section_end_point": {"$ne": None}}]}): point_start = section['section_start_point']['coordinates'] point_end = section['section_end_point']['coordinates'] if calDistance(startpoint, point_start) < 100 and calDistance(endpoint, point_end) < 100: sectionList.append(section['_id']) gmap.addpoint(point_end[1], point_end[0], COLOR[1]) gmap.addpoint(point_start[1], point_start[0], COLOR[1]) drawSection(section, option, gmap) gmap.draw('gmap_display/' + 'SearchResult' + str(start)[:10] + '-' + str(end)[:10] + '_' + user + '.html')
def generate_user_home_work(user): user_home = detect_home(user) # print user_home zip_is_valid = _check_zip_validity(user_home, user) logging.debug('starting for %s' % user) if Profiles.find({'user_id': user}).count() == 0: profile_todo = { 'source': 'Shankari', 'user_id': user, 'home': user_home } Profiles.insert(profile_todo) else: #TODO: make detect_home return something better than a N/A string Profiles.update({"$and": [{ 'source': 'Shankari' }, { 'user_id': user }]}, {"$set": { 'home': user_home }}) user_work = detect_work_office(user) Profiles.update({"$and": [{ 'source': 'Shankari' }, { 'user_id': user }]}, {"$set": { 'work_place': user_work }}) user_zip = get_userZipcode(user, zip_is_valid) Profiles.update({"$and": [{ 'source': 'Shankari' }, { 'user_id': user }]}, {"$set": { 'zip': user_zip }}) if user_zip != 'N/A': geoinfo = Geocoder.geocode(user_zip) # geocoder returns data in lat,lng format. # we convert to lng,lat internally, since that is the format that the # rest of our code is in zipCen = [geoinfo[0].coordinates[1], geoinfo[0].coordinates[0]] else: zipCen = 'N/A' Profiles.update({"$and": [{ 'source': 'Shankari' }, { 'user_id': user }]}, {"$set": { 'zip_centroid': zipCen }}) for day in range(1, 6): key = 'work' + str(day) Profiles.update({"$and": [{ 'source': 'Shankari' }, { 'user_id': user }]}, {"$set": { key: detect_daily_work_office(user, day) }})