Exemplo n.º 1
0
    def __init__(self, key, city='San Francisco'):
        """
        The construct for the YelpCity class.

        Parameters
        ----------
        key : str
            The yelp API key needed to call Yelp Fusion API.
        city : str
            The city to search over.
        """

        api_host = 'https://api.yelp.com'
        search_path = '/v3/businesses/search'
        business_path = '/v3/businesses/'
        zcdb = ZipCodeDatabase()
        url = api_host + search_path
        header = {
            'Authorization': 'Bearer %s' % key,
        }
        param = {
            'location': city,
            'limit': 1,
            'offset': 0,
            'term': 'restaurants',
            'sort_by': 'best_match',
        }
        response = requests.get(url=url, headers=header, params=param)

        self.business_id_set = set()
        self.business_list = []
        for zip_obj in zcdb.find_zip(city=city):
            self.business_list += self.find_businesses(zip_obj, url, header)
        self.region = response.json()['region']
Exemplo n.º 2
0
    def form_valid(request, form):
        zcdb = ZipCodeDatabase()
        agent1_zipcode = form.cleaned_data['zipcode_agent1']
        agent2_zipcode = form.cleaned_data['zipcode_agent2']

        agent1, created = Agent.objects.update_or_create(
            name='Agent 1',
            defaults={'zipcode': agent1_zipcode}
        )

        agent2, created = Agent.objects.update_or_create(
            name='Agent 2',
            defaults={'zipcode': agent2_zipcode},
        )

        nearestzipcode_a1 = []
        nearestzipcode_a2 = []
        try:
            nearestzipcode_a1 = [z.zip for z in zcdb.get_zipcodes_around_radius(agent1_zipcode, DEFAULT_RAIDUS)]
        except ZipNotFoundException:
            pass

        try:
            nearestzipcode_a2 = [z.zip for z in zcdb.get_zipcodes_around_radius(agent2_zipcode, DEFAULT_RAIDUS)]
        except ZipNotFoundException:
            pass

        if nearestzipcode_a1 or nearestzipcode_a2:
            nearestzipcode_a2 = [x for x in nearestzipcode_a2 if not (x in nearestzipcode_a1)]

            contacts_agent1 = Contact.objects.filter(zipcode__in=nearestzipcode_a1).all()
            contacts_agent2 = Contact.objects.filter(zipcode__in=nearestzipcode_a2).all()

            if contacts_agent1 or contacts_agent2:
                data = {
                    'agent1': agent1,
                    'contact_agent1': contacts_agent1,
                    'agent2': agent2,
                    'contact_agent2': contacts_agent2
                }
                content = render_to_string('website/matchs-agent-modal.html', data)
                context = {
                    'success': True,
                    'matchs': content
                }
            else:
                context = {
                    'hasErrors': False,
                    'hasMessage': True,
                    'message': utils.NOT_MATCH_EXIST,
                    'success': False,
                }
        else:
            context = {
                'hasErrors': False,
                'hasMessage': True,
                'message': utils.NOT_MATCH_EXIST,
                'success': False,
            }
        return utils.render_to_json(context)
Exemplo n.º 3
0
def dashboard(request):
    zipcode = request.GET.get('zip_code')
    if zipcode:
        zcdb = ZipCodeDatabase()
        try:
            profiles = Profile.objects.filter(
                is_teacher=True,
                zip_code__in=[
                    z.zip
                    for z in zcdb.get_zipcodes_around_radius(zipcode, 10)
                ])
        except Exception as e:
            logging.error(traceback.format_exc())
            profiles = Profile.objects.filter(phone_number=0)

    # Logs the error appropriately.
    else:
        profiles = Profile.objects.filter(is_teacher=True)
    page = request.GET.get('page', 1)
    paginator = Paginator(profiles, 10)
    try:
        profiles = paginator.page(page)
    except PageNotAnInteger:
        profiles = paginator.page(1)
    except EmptyPage:
        profiles = paginator.page(paginator.num_pages)
    context = {'profiles': profiles}
    return render(request, 'account/dashboard.html', context)
Exemplo n.º 4
0
def fake_df():
    zcdb = ZipCodeDatabase()

    zipcodes = zcdb.get_zipcodes_around_radius(94533, 20)
    cities = [z.city for z in zipcodes]
    zipcodes = [z.zip+"," for z in zipcodes]

    zips_and_cities_df = pd.DataFrame({"zip": zipcodes, "city": cities})
    zips_and_cities_df = zips_and_cities_df.groupby("city", as_index=False).agg(sum)

    df = from_exported(3)
    ints_to_cols = {i: df.columns[i] for i in range(df.shape[1])}

    dfs = []
    for _, row in zips_and_cities_df.iterrows():
        df = fake_pm_for_zipcode(row.zip, row.city)
        dfs.append(df)

    df = pd.concat(dfs)
    df.sort_index(axis=1, inplace=True)

    df.columns = [ints_to_cols[i] for i in range(df.shape[1])]
    df.rename({"Unnamed: 3": ""}, axis=1, inplace=True)

    df.to_csv('/tmp/faked_pms.csv', index=False)
    return df
Exemplo n.º 5
0
def search_caregivers(request, *args, **kwargs):
    context = {}
    # zcdb = ZipCodeDatabase()
    # in_radius = [z.zip for z in zcdb.get_zipcodes_around_radius('90007', 8)] # ('ZIP', radius in miles)
    # radius_utf = [x.encode('UTF-8') for x in in_radius] # unicode list to utf list
    # context['data'] = radius_utf
    # zip, gender, radius
    if request.method == 'POST':
        zip_code = request.POST['zip']
        radius = int(request.POST['radius'])
        zcdb = ZipCodeDatabase()
        in_radius = [
            z.zip for z in zcdb.get_zipcodes_around_radius(zip_code, radius)
        ]  # ('ZIP', radius in miles)
        #radius_utf = [x.encode('UTF-8') for x in in_radius] # unicode list to utf list
        radius_arr = [
            x.encode('utf-8').decode('unicode-escape') for x in in_radius
        ]
        caregivers = Caregiver.objects.filter(zip_code__in=radius_arr)
        context['caregivers'] = caregivers
        context['isPostRequest'] = True
    else:
        context['caregivers'] = []
        context['isPostRequest'] = False
    return render(request, 'search_caregivers.html', context)
Exemplo n.º 6
0
def serviceAbilityScoreAlgorithm(target,distance,CollegeEnrollment,GapCompany,GapLibrary):
    from pyzipcode import ZipCodeDatabase
    zcdb = ZipCodeDatabase()
    DF=pd.DataFrame(columns=['Zip','zip'])
    center=target['Zip'].unique()
    for i in center:
        in_radius=[z.zip for z in zcdb.get_zipcodes_around_radius(i,distance)] # ('ZIP', radius in miles)
        df=pd.DataFrame({'zip':in_radius})
        df['Zip']=i
        DF=DF.append(df,ignore_index=True)
    school_college_distance=Distance(target,CollegeEnrollment,DF)
    writerschool_college_distance=pd.ExcelWriter('school_college_distance.xlsx')
    school_college_distance.to_excel(writerschool_college_distance,index=False)
    
    school_company_distance=Distance(target,GapCompany,DF)
    writerschool_company_distance=pd.ExcelWriter('school_company_distance.xlsx')
    school_company_distance.to_excel(writerschool_company_distance,index=False)
    
    
    school_library_distance=Distance(target,GapLibrary,DF)
    writerschool_library_distance=pd.ExcelWriter('school_library_distance.xlsx')
    school_library_distance.to_excel(writerschool_library_distance,index=False)
    
    ServiceAbilityIndexCollegeNearby=serviceAbilityIndexCollegeMap(school_college_distance,0,200,1000)
    ServiceAbilityIndexCompanyNearby=serviceAbilityIndexCompanyMap(school_company_distance,0,200,1000)
    ServiceAbilityIndexHostNearby=serviceAbilityIndexLibraryMap(school_library_distance,1,5)
    ServiceAbilityIndexScore=ServiceAbilityIndexCollegeNearby.merge(ServiceAbilityIndexCompanyNearby,on='Code',left_index=True).merge(ServiceAbilityIndexHostNearby,on='Code',left_index=True)
    ServiceAbilityIndexScore['ServiceAbilityIndexTotalScore']=ServiceAbilityIndexScore.apply(lambda row: row.ServiceabilityIndexCollegeNearby+row.ServiceabilityIndexCompanyNearby+row.ServiceabilityIndexLibraryNearby,axis=1)
    target=ServiceAbilityIndexScore[ServiceAbilityIndexScore.ServiceAbilityIndexTotalScore>=100].sort_values(['ServiceAbilityIndexTotalScore'],ascending=[False]).reset_index(drop=True)
    return target
Exemplo n.º 7
0
def load_location_data(data_dir,group_by='zip3'):
    """Helper function to load lat/long coordinates of each 3-digit zip code.
    Returns data grouped by either 3-digit zip, 5-digit zip, or county FIPS"""
    import us
    sys.path.append('/Users/james/data_science/pyzipcode')
    from pyzipcode import ZipCodeDatabase

    # Build pandas zip code database with lat/long coordinates
    zcdb = ZipCodeDatabase() #create zipcode database object
    zip_dict = dict([('zip_code',[]),('latitude',[]),('longitude',[]),('state',[])])
    for state in us.STATES:
        cur_zips = zcdb.find_zip(state='%s' % state.abbr)
        for cur_zip in cur_zips:
            zip_dict['zip_code'].append(cur_zip.zip)
            zip_dict['latitude'].append(cur_zip.latitude)
            zip_dict['longitude'].append(cur_zip.longitude)
            zip_dict['state'].append(cur_zip.state)
            
    zip_data = pd.DataFrame(zip_dict) #make dict into dataframe
    zip_data['zip3'] = zip_data['zip_code'].apply(lambda x: int(x[:3])) #add a column containing 3-digit zips
    zip_data['zip_code'] = zip_data['zip_code'].astype(int)
    zip_to_fips = make_zip_to_fips_dict(data_dir)
    zip_data['fips'] = zip_data['zip_code'].map(zip_to_fips)
    if group_by == 'zip3':   
        zip_data = zip_data.groupby('zip3').agg({'latitude': np.mean,'longitude':np.mean})
    elif group_by == 'zip':
        zip_data.set_index('zip_code', inplace=True)
    elif group_by == 'fips':
        zip_data = zip_data.groupby('fips').agg({'latitude': np.mean,'longitude':np.mean})
        
    return zip_data
Exemplo n.º 8
0
def get_nct_list_from_zip(input_zip, mile_range=50):
    zcdb = ZipCodeDatabase()
    zip_list = [
        z.zip for z in zcdb.get_zipcodes_around_radius(input_zip, mile_range)
    ]  # default mile range set at 100
    conn = general_pool_criteria.connection()
    cur = conn.cursor()
    # conn.execute("USE trial_knowledge_base_COVID19") ##
    sql = '''
            ;with cte (code, DeclarationItem, Declaration) as
            (
              select nct_id,
                cast(left(zip_codes, charindex('|',zip_codes+'|')-1) as varchar(50)) DeclarationItem,
                     stuff(zip_codes, 1, charindex('|',zip_codes+'|'), '') Declaration
              from dbo.aact_trial_info
              union all
              select code,
                cast(left(Declaration, charindex('|',Declaration+'|')-1) as varchar(50)) DeclarationItem,
                stuff(Declaration, 1, charindex('|',Declaration+'|'), '') Declaration
              from cte
              where Declaration > ''
            ) 
            select code as nct_id, DeclarationItem as zip
            from cte
            order by nct_id asc
            option (maxrecursion 0);
        '''
    cur.execute(sql)
    trial_zips = cur.fetchall()
    cur.close()
    conn.close()

    # compare nearby zip codes to trial zip codes
    nearby_nct_list = []
    for item in trial_zips:
        test_nct = item[0]
        if item[1] is not None:
            test_zip = item[1].split('-')[0]
        else:
            test_zip = 00000
        if test_zip in zip_list:
            nearby_nct_list.append(
                test_nct
            )  # some zip codes stored with '-xxxx' after primary 5 digit, pyzipcode no like that
    nearby_nct_list = list(set(nearby_nct_list))

    # temp loop to add trial number, to be removed in the future
    temp_return_list = []
    i = 1
    for item in nearby_nct_list:
        temp_return_list.append('%s;%d' % (item, i))
        i += 1

    # return temp_return_list
    # fengyang: also return the nearby zipcode list
    return [temp_return_list, zip_list]
Exemplo n.º 9
0
 def get_zipcodes_in_radius():
     zipcode_input = get_zipcode_input() #getting zipcode from input
     if not zipcode_input:
         return None
     radius = get_search_radius() #getting radius from input
     zcdb = ZipCodeDatabase() #using zipCodeDatabase
     try:
         in_radius = [z.zip for z in zcdb.get_zipcodes_around_radius(zipcode_input, radius)] #('ZIP', radius in miles)
         return in_radius
     except Exception: #catch when zipcode is not found in database
         show_error_message(self, "Inputted zipcode was not found")
Exemplo n.º 10
0
def find_neighbors_str(store_zip1, radius=5):
    zcdb = ZipCodeDatabase()

    in_radius = []

    for neighbors in zcdb.get_zipcodes_around_radius(store_zip1, radius):
        in_radius.append(neighbors.zip)

    zipcode_reach2 = find_neighbors_list(store_zip, radius=radius)

    return (list(set(in_radius).difference(set(zipcode_reach2))))
Exemplo n.º 11
0
def search_caregivers(request, *args, **kwargs):
    context = {}
    context['user_type'] = request.session['user_type']
    # zcdb = ZipCodeDatabase()
    # in_radius = [z.zip for z in zcdb.get_zipcodes_around_radius('90007', 8)] # ('ZIP', radius in miles)
    # radius_utf = [x.encode('UTF-8') for x in in_radius] # unicode list to utf list
    # context['data'] = radius_utf
    # zip, gender, radius
    if request.method == 'POST':
        zip_code = request.POST['zip']
        radius = int(request.POST['radius'])
        zcdb = ZipCodeDatabase()
        # start_date = datetime.datetime.strptime(request.POST['start_date'], "%Y-%m-%d").date()
        # end_date = datetime.datetime.strptime(request.POST['end_date'], "%Y-%m-%d").date()
        # availability = request.POST['availability']
        # in_radius = [z.zip for z in zcdb.get_zipcodes_around_radius(zip_code, radius)] # ('ZIP', radius in miles)
        # # radius_utf = [x.encode('UTF-8') for x in in_radius] # unicode list to utf list
        # radius_arr = [x.encode('utf-8').decode('unicode-escape') for x in in_radius]
        # # radius_arr = 10 #added 28/10
        # # caregivers = Caregiver.objects.filter(zip_code__in = radius_arr)
        # caregivers = Caregiver.objects.filter(zip_code__in = radius_arr, availability=availability, start_date__lte=start_date, end_date__gte=end_date)

        start_date = datetime.datetime.strptime(
            request.POST['start_date'],
            "%Y-%m-%d").date() if request.POST['start_date'] != '' else ''
        end_date = datetime.datetime.strptime(
            request.POST['end_date'],
            "%Y-%m-%d").date() if request.POST['end_date'] != '' else ''
        availability = request.POST[
            'availability'] if request.POST['availability'] != '' else ''
        if zip_code.strip() != '':
            in_radius = [
                z.zip
                for z in zcdb.get_zipcodes_around_radius(zip_code, radius)
            ]  # ('ZIP', radius in miles)
            radius_arr = [
                x.encode('utf-8').decode('unicode-escape') for x in in_radius
            ]
        else:
            radius_arr = []
        print(start_date, end_date, "datessssss")
        # caregivers = Caregiver.objects.filter(zip_code__in = radius_arr) | Caregiver.objects.filter(availability=availability) | Caregiver.objects.filter(start_date__lte=start_date,end_date__gte=end_date )

        caregivers = Caregiver.objects.filter(zip_code__in=radius_arr,
                                              availability=availability,
                                              start_date__lte=start_date,
                                              end_date__gte=end_date)

        context['caregivers'] = caregivers
        context['isPostRequest'] = True
    else:
        context['caregivers'] = []
        context['isPostRequest'] = False
    return render(request, 'search_caregivers.html', context)
Exemplo n.º 12
0
def find_neighbors_list(store_zip, radius=5):
    zcdb = ZipCodeDatabase()

    in_radius = []

    for sz in store_zip:
        for neighbors in zcdb.get_zipcodes_around_radius(sz, radius):
            in_radius.append(neighbors.zip)

    zipcode_reach = list(set(in_radius))  # remove dupplicate zipcode
    return (zipcode_reach)
Exemplo n.º 13
0
def tz_ids_for_address(country, state=None, city=None, zipcode=None, **kwargs):
    """ Get the TZ identifiers for a given address, e.g.:

    >>> tztrout.tz_ids_for_address('US', state='CA', city='Palo Alto')
    [u'America/Los_Angeles']
    >>> tztrout.tz_ids_for_address('PL')
    [u'Europe/Warsaw']
    >>> tztrout.tz_ids_for_address('CN')
    [
        u'Asia/Shanghai',
        u'Asia/Harbin',
        u'Asia/Chongqing',
        u'Asia/Urumqi',
        u'Asia/Kashgar'
    ]
    """

    if country == 'US':
        if zipcode:
            if not isinstance(zipcode, basestring):
                zipcode = str(zipcode)

            # If an extended zipcode in a form of XXXXX-XXXX is provided,
            # only use the first part
            zipcode = zipcode.split('-')[0]

            return td.us_zip_to_tz_ids.get(zipcode)
        elif state or city:
            if city and 'city:%s' % city.lower() in data_exceptions:
                return data_exceptions['city:%s' % city.lower()]['include']
            if len(state) != 2:
                state = td.normalized_states['US'].get(state.lower(), state)
            zcdb = ZipCodeDatabase()
            zipcodes = zcdb.find_zip(city=city,
                                     state=state,
                                     exact=True,
                                     limit=1)
            if zipcodes:
                return td.us_zip_to_tz_ids.get(zipcodes[0].zip)
            elif city is not None:
                zipcodes = zcdb.find_zip(state=state, exact=True, limit=1)
                if zipcodes:
                    return td.us_zip_to_tz_ids.get(zipcodes[0].zip)
    elif country == 'CA' and state:
        if len(state) != 2:
            state = td.normalized_states['CA'].get(state.lower(), state)
        return td.ca_state_to_tz_ids.get(state)
    elif country == 'AU' and state:
        if len(state) != 2:
            state = td.normalized_states['AU'].get(state.lower(), state)
        return td.au_state_to_tz_ids.get(state)

    return pytz.country_timezones.get(country)
Exemplo n.º 14
0
def create_data_table(state_choose='MA',
                      radius=5,
                      Number=0,
                      tran_type=['Sale', 'Return'],
                      min_date=min_date1,
                      max_date=max_date1):
    zip_dict = {}

    sea_zipcode = result['ShipToZipCode'][result['State'] ==
                                          state_choose].unique().tolist()

    try:
        sea_zipcode.remove('00000')
    except:
        pass

    zipcode_reach2 = find_neighbors_list(store_zip, radius=radius)

    # add more parameter here
    ecom_filter = result[(result['TransactionType'].isin(tran_type))
                         & (result['TheDate'] >= min_date) &
                         (result['TheDate'] <= max_date)][[
                             'ShipToZipCode', 'Transactions'
                         ]].groupby('ShipToZipCode').sum()

    try:
        for i in sea_zipcode:
            zcdb = ZipCodeDatabase()
            in_radius = []
            for neighbors in zcdb.get_zipcodes_around_radius(i, radius):
                in_radius.append(neighbors.zip)
            zipcode_reach_search = list(
                set(in_radius).difference(set(zipcode_reach2)))
            zip_dict[i] = ecom_filter[ecom_filter.index.isin(
                zipcode_reach_search)]['Transactions'].sum()
    except:
        pass

    predict1 = pd.DataFrame.from_dict(zip_dict,
                                      orient='index',
                                      columns=['Transactions'
                                               ]).sort_values('Transactions',
                                                              ascending=False)

    store_future_zip = predict1.index

    store_future = table_from_zipcodelist(list(store_future_zip))

    store_future['Transactions'] = predict1['Transactions'].tolist()

    return ColumnDataSource(store_future)
Exemplo n.º 15
0
def get_zips_list(center_zipcode, distance):
    # use the ZipCodeDatabase package to get a list of zipcodes within the provided radius
    try:
        zcdb = ZipCodeDatabase()
        zip_objects = zcdb.get_zipcodes_around_radius(center_zipcode, distance)
    except ZipNotFoundException:
        error = "Zip code not found. Please check your zip code and try again."
        return error

    # grab the .zip property from each returned zipcode object
    def get_zip(zipObj):
        return zipObj.zip

    return list(map(get_zip, zip_objects))
Exemplo n.º 16
0
    def clean(self):
        super().clean()
        # validate applicant date of birth
        zip_code = self.cleaned_data.get('zip_code', None)
        applicant_dob = self.cleaned_data.get('dob', None)
        policy = self.cleaned_data.get('policy_type',None)
        if applicant_dob and policy:
            applicant_age = self._process_dob_for_age(applicant_dob)
            if applicant_age < 18 and self.cleaned_data['policy_type'] == 'Family':
                self.cleaned_data['dob'] = ''
                self.add_error('dob', forms.ValidationError(
                    _("for policy type family, minimum age must be 18"),
                    code='invalid'
                ))
            if applicant_age < 2 and self.cleaned_data['policy_type'] == 'Self':
                self.cleaned_data['dob'] = ''
                self.add_error('dob', forms.ValidationError(
                    _("for policy type Self, minimum age must be 2"),
                    code='invalid'
                ))

        if zip_code:
            try:
                self.cleaned_data['state'] = ZipCodeDatabase()[zip_code].state
            except IndexError as err:
                pass

        return self.cleaned_data
Exemplo n.º 17
0
def main(zips):

    featfile='../../../../Data/Model_Runs/Model_15sept.pkl'
    df=pd.read_pickle(featfile)
    #load model
    regzips=df['zip'].values
    slope=df['slope'].values
    ints=df['intercept'].values

    idzips=[np.where(regzips==z)[0][0] for z in zips if z in regzips]

    clf = joblib.load('../../../../Data/Model_Runs/LinearRegression.pkl')

    #build inputs
    ip1=slope[idzips]
    flag=np.where(np.isnan(ip1))
    ip=ip1[~np.isnan(ip1)]
    ip=np.reshape(ip,[ip.shape[0],1])
    #run model
    op=clf.predict(list(ip))
    op=samNormalize(op)

    #return ZipCodeDatabase
    zipnew=np.asarray(zips)
    zips=regzips[idzips][np.where(op>1.1)]
    zjson=[]

    zcdb=ZipCodeDatabase()
    for zid in zips:
        z=zcdb[zid]
        temp= {'lat': z.latitude, 'lng': z.longitude, 'name':z.city}#'zip':int(z.zip),
        zjson.append(temp)
        # zjson=zjson.append(dict({'zip':z.zip, 'lat': z.latitude, 'lon': z.longitude}))
    # print str(json.dumps(zjson))
    return json.dumps(zjson)
def get_zip(b):
 
    zcdb = ZipCodeDatabase()
    zipcode = zcdb[b]
    longi = zipcode.longitude
    Lati = zipcode.latitude
    cord = str(longi) + ',' +  str(Lati)
    return cord
Exemplo n.º 19
0
    def search_distance(self, search_zip):
        zcdb = ZipCodeDatabase()
        customer_zip = zcdb[search_zip]
        customer_location = [customer_zip.latitude, customer_zip.longitude]
        location_distance = distance.distance(self.location,
                                              customer_location).mi
        rounded_distance = round(location_distance, 1)

        self.search_distance = rounded_distance
Exemplo n.º 20
0
def search_results(request):
    query = request.GET.get('query')  # grab user query
    zcdb = ZipCodeDatabase()  # create instance of zip code database object
    zipcode = zcdb[
        query]  # not too sure. Maybe this is a really long list of zip codes and this is how you access one?
    zip_codes_in_radius = [
        z.zip for z in zcdb.get_zipcodes_around_radius(query, 15)
    ]
    jobs = []
    for zip in zip_codes_in_radius:
        jobs_in_zip = Job.objects.filter(business__zip_code=zip)
        if jobs_in_zip:
            jobs += jobs_in_zip
    form = JobSeekerEmailForm()  # pass email subscription from to template
    return render(request, 'job/search_results.html', {
        'query': query,
        'jobs': jobs,
        'form': form
    })
Exemplo n.º 21
0
def hospital(request):
    g = geocoders.GoogleV3()
    place, (lat, lng) = g.geocode('Charlotte')
    g = geocoder.google([request.GET['latitude'], request.GET['longitude']], method='reverse')
    zcdb = ZipCodeDatabase()


    conn=connect()
    cur=conn.cursor()
    zip_list = zcdb.get_zipcodes_around_radius(g.postal, 35)    
    results=[]
    
    for z in zip_list:
#    	print z.zip

    	cur.execute("select f1.provider_id,f1.hospital_name,f2.score from (select * from findhospital_hospitalgeneralinformation where hospital_type='"+ request.GET['type']+" ' and zip_code = '"+ z.zip +"') as f1, findhospital_hospitalperformance f2  where f1.provider_id=f2.provider_id");

	for row in cur.fetchall():
		results.append(hospitals(row[0],row[1],row[2]))
		
    conn.close()


    for i in range( 1, len( results ) ):
    	obj = results[i]
    	tmp = results[i].score
    	print results[i].score
	k = i
	while k > 0 and tmp > results[k - 1].score:
        	results[k] = results[k - 1]
        	k -= 1
	results[k]=obj
    #for z in zip_list:
    #	print z.zip
       																																																							
    

    code = read('/home/sairam/Desktop/healthapp/findhospital/templates/results.html')
    t= Template(code)
    c = Context({'results':results})
    return HttpResponse(t.render(c))
Exemplo n.º 22
0
def get_city_data(zipc):
    """Given a zip code, return latlon and city name"""
    geolocator = ZipCodeDatabase()
    try:
        zip_data = geolocator[str(zipc)]
        lat = zip_data.latitude
        lon = zip_data.longitude
        name = '%s, %s' % (zip_data.city, zip_data.state)
        return lat, lon, name

    except:
        return 'None', 'None', 'None'
Exemplo n.º 23
0
def eventbrite_api(request):
    profile = Profile.objects.get(user=request.user)
    interests = profile.interests.filter(interests__in=['MUSIC', 'TECHNOLOGY', 'COMEDY', 'CAR', 'FOOD', 'SPORTS'])
    zcdb = ZipCodeDatabase()

    #geocodes users zipcode to use lat and lng in search
    zipcode = zcdb[profile.zipcode]
    free_times = FreeTimes.objects.filter(user=request.user)
    eventbrite_url='https://www.eventbriteapi.com/v3/events/search/?'
    for free_time in free_times:

        # formats time to match eventbrite api
        start_time =  "{}Z".format(free_time.free_time_start[:-6])
        end_time = "{}Z".format(free_time.free_time_end[:-6])
        for interest in interests:
            eventbrite_params = {
            "token": eventbrite_token,
            'popular': True,
            'q': str(interest.interests),
            'location.latitude': zipcode.latitude,
            'location.longitude': zipcode.longitude,
            'location.within': '20mi',
            'start_date.range_start': start_time,
            'start_date.range_end': end_time
            }
            eventbrite_resp = get(url=eventbrite_url, params=eventbrite_params)
            eventbrite_data = json.loads(eventbrite_resp.text)

            # Saves returned events to database
            for event in eventbrite_data['events']:
                formatted_start = event['start']['utc'][:-1] + '-7:00'
                formatted_end = event['end']['utc'][:-1] + '-7:00'
                # Creates a datetime object from the time returned by Api
                datetime_start = dateutil.parser.parse(event['start']['utc'])
                datetime_end = dateutil.parser.parse(event['end']['utc'])
                Event.objects.create(
                    name=event['name']['text'],
                    category=interest.interests,
                    venue=event['venue']['name'],
                    description=event['description']['text'],
                    latitude=event['venue']['latitude'],
                    longitude=event['venue']['longitude'],
                    start_time=formatted_start,
                    end_time=formatted_end,
                    picture=event['logo_url'],
                    event_url=event['url'],
                    user=request.user,
                    start_dateTime=datetime_start,
                    end_dateTime=datetime_end
                )
    success = {'success': 'success'}
    return HttpResponse(json.dumps(success), content_type="application/json")
Exemplo n.º 24
0
def state_from_zip(zipcode):
    try:
        zip_info = ZipCodeDatabase()[zipcode]
    except IndexError:
        return None

    if zip_info.city in ('APO', 'FPO', 'DPO'):
        # we cannot lookup state names for APO/FPO/DPO zipcodes;
        # probably we need better handling in audb for military PO addresses
        return zip_info.state

    state = us.states.lookup(zip_info.state)
    return state.name if state else None
Exemplo n.º 25
0
def load_location_data(data_dir, group_by='zip3'):
    """Helper function to load lat/long coordinates of each 3-digit zip code.
    Returns data grouped by either 3-digit zip, 5-digit zip, or county FIPS"""
    import us
    sys.path.append('/Users/james/data_science/pyzipcode')
    from pyzipcode import ZipCodeDatabase

    # Build pandas zip code database with lat/long coordinates
    zcdb = ZipCodeDatabase()  #create zipcode database object
    zip_dict = dict([('zip_code', []), ('latitude', []), ('longitude', []),
                     ('state', [])])
    for state in us.STATES:
        cur_zips = zcdb.find_zip(state='%s' % state.abbr)
        for cur_zip in cur_zips:
            zip_dict['zip_code'].append(cur_zip.zip)
            zip_dict['latitude'].append(cur_zip.latitude)
            zip_dict['longitude'].append(cur_zip.longitude)
            zip_dict['state'].append(cur_zip.state)

    zip_data = pd.DataFrame(zip_dict)  #make dict into dataframe
    zip_data['zip3'] = zip_data['zip_code'].apply(
        lambda x: int(x[:3]))  #add a column containing 3-digit zips
    zip_data['zip_code'] = zip_data['zip_code'].astype(int)
    zip_to_fips = make_zip_to_fips_dict(data_dir)
    zip_data['fips'] = zip_data['zip_code'].map(zip_to_fips)
    if group_by == 'zip3':
        zip_data = zip_data.groupby('zip3').agg({
            'latitude': np.mean,
            'longitude': np.mean
        })
    elif group_by == 'zip':
        zip_data.set_index('zip_code', inplace=True)
    elif group_by == 'fips':
        zip_data = zip_data.groupby('fips').agg({
            'latitude': np.mean,
            'longitude': np.mean
        })

    return zip_data
Exemplo n.º 26
0
def zipcode_coordinates(zipcd):
    print zipcd
    zcdb = ZipCodeDatabase()
    try:
        zipcode = zcdb[zipcd]
        coordinates = (zipcode.latitude, zipcode.longitude)
    except IndexError:
        if zipcd == '60290':
            coordinates = (41.881, -87.6247)
        else:
            coordinates = (np.nan, np.nan)

    return coordinates
Exemplo n.º 27
0
    def generate_zip_to_tz_id_map(self):
        """ Generate the map of US zip codes to time zone identifiers. The
        method finds all the possible time zone identifiers for each zip code
        based on a UTC offset stored for that zip in pyzipcode.ZipCodeDatabase.
        """
        zcdb = ZipCodeDatabase()
        zips = list(zcdb.find_zip())
        zips_len = len(zips)
        tz_ids_to_zips = defaultdict(list)
        for cnt, zip in enumerate(zips):
            ids = tuple(self._get_tz_identifiers_for_us_zipcode(zip))

            # apply the data exceptions
            exceptions = data_exceptions.get(
                'zip:' + zip.zip) or data_exceptions.get('state:' +
                                                         zip.state) or {}
            exceptions['include'] = exceptions.get(
                'include', []) + data_exceptions['all'].get(
                    'include', []) if 'all' in data_exceptions else []
            exceptions['exclude'] = exceptions.get(
                'exclude', []) + data_exceptions['all'].get(
                    'exclude', []) if 'all' in data_exceptions else []
            if exceptions:
                ids = tuple((set(ids) - set(exceptions['exclude']))
                            | set(exceptions['include']))

            tz_ids_to_zips[ids].append(zip.zip)

            stdout.write('\r%d/%d' % (cnt + 1, zips_len))
            stdout.flush()

        zips_to_tz_ids = {
            tuple(zips): json.dumps(ids)
            for ids, zips in tz_ids_to_zips.iteritems()
        }

        file = open(US_ZIPS_TO_TZ_IDS_MAP_PATH, 'w')
        file.write(pickle.dumps(zips_to_tz_ids))
        file.close()
Exemplo n.º 28
0
 def getCiti(self):
     """ write the city and state given the postcode"""
     if self.postcode:
         zcdb = ZipCodeDatabase()
         regexp = re.compile(r'-\d*')
         try:
             postcode = re.sub(regexp, "", self.postcode)
             zipcode = zcdb[postcode]
             self.citi = zipcode.city
             self.state = zipcode.state
         except:
             self.citi = "NA-citi"
             self.state = "NA-state"
Exemplo n.º 29
0
 def _get_zipcode_list(self, location):
     if location.lower() == "all":
         with file("yelp-city-USA-list.txt") as f:
             return f.read().split()
     zip_db = ZipDB()
     if re.findall(r'[0-9,]+', location):
         zips = location.split(',')
     elif location.find(",") < 0 and len(location) > 2:
         print "search city ..."
         zips = [z.zip for z in zip_db.find_zip(city=location) or []]
     elif len(location) == 2:
         print "search state ..."
         zips = [z.zip for z in zip_db.find_zip(state=location) or []]
     else:
         print "search city: %s state: %s" % tuple(location.split(","))
         zips = [
             z.zip
             for z in zip_db.find_zip(city=location.split(',')[0],
                                      state=location.split(',')[1]) or []
         ]
     return list(
         set(["%s,%s" % (zip_db[i].city, zip_db[i].state) for i in zips]))
Exemplo n.º 30
0
class UrlBuilder(object):
    """Build URL list for CareerBuilder searches"""
    zcdb = ZipCodeDatabase()

    def __init__(self, zipcode, search=None):
        """Validate zipcode, prompt user for all query parameters"""
        self.search = search
        try:
            # Check zipcode DB for valid zip
            self.zcdb[zipcode]
        except IndexError:
            # Default to Baltimore Catalyte office zipcode
            print 'Invalid zipcode code. Defaulting to 21201'
            self.zipcode = '21201'
        else:
            self.zipcode = zipcode
        self.base = 'http://www.careerbuilder.com/'
        self.params = []
        self.params.append(Posted.cmd_append())
        self.params.append(Radius.cmd_append())
        self.params.append(Pay.cmd_append())
        self.params.append(Emp.get_formatted())

    def build_url(self):
        """Add path and query parameters based on filters specified by user"""
        if not self.has_filters():
            return '{}{}?cat1=JN008'.format(self.base, self.build_path())
        else:
            return '{}{}{}&cat1=JN008'.format(self.base, self.build_path(), self.format_filters())

    def build_path(self):
        """Compose URL substring for endpoint path"""
        if not self.search or not self.search.split():
            return 'jobs-in-{}'.format(self.zipcode)
        else:
            return 'jobs-{}-in-{}'.format('-'.join(self.search.split()), self.zipcode)

    def has_filters(self):
        """Check for user input query parameters"""
        if any(param is not None for param in self.params):
            return True
        else:
            return False

    def format_filters(self):
        """Compose URL substring for all query parameters"""
        base = '?'
        for param in self.params:
            if param is not None:
                base += param + '&'
        return base
    def post(self):

        # force to accept only JSON posted data
        try:
            data = request.get_json(force=True)
        except Exception as err:
            return {'error': settings.INVALID_JSON}, 400

        # make sure user has submitted JSON data i.e list of zip codes. Format: {"zip_codes": ["44000", "10008"]}
        if data and data.get('zip_codes') and isinstance(
                data.get('zip_codes'), list):

            try:
                # get user's Geo Data from user's IP address
                g = geocoder.ip(request.remote_addr)
            except Exception as err:
                return {'error': settings.CONNECTION_ERROR}, 400

            # if user is from local machine instead of a real IP (server machine) then get user's real IP address
            if g.ip == '127.0.0.1':
                user_ip = urlopen('http://ip.42.pl/raw').read()
                # get user's Geo Data from user's IP address
                g = geocoder.ip(user_ip)

            user_lat_lng = g.lat, g.lng
            result = dict()

            # iterate through each zip code and calculate distance relative to user's lat, long
            for zip_code in data.get('zip_codes', []):

                # This package contains geo data for US zip codes only.
                zcdb = ZipCodeDatabase()
                try:

                    # get zip code's Geo Data for lat, long
                    zip_code_data = zcdb[zip_code]
                    zip_code_lat_lng = zip_code_data.latitude, zip_code_data.longitude
                    """
                    Calculate the geodesic distance between two points using the formula devised by Thaddeus Vincenty,
                    with an accurate ellipsoidal model of the earth. using geopy package
                    """
                    distance = round(
                        vincenty(zip_code_lat_lng, user_lat_lng).miles, 2)
                    result[zip_code] = '{0} Miles'.format(distance)

                except IndexError as err:
                    result[zip_code] = settings.INVALID_ZIP_CODE

            return result, 200
        else:
            return {'error': settings.INVALID_JSON}, 400
Exemplo n.º 32
0
def zipcode_state(df):
    from pyzipcode import ZipCodeDatabase
    zip=ZipCodeDatabase()
    for i in df[pd.isnull(df['State'])&pd.notnull(df['ZIP code'])].index:
    try:
        df['State'][i]=str(zip[df['ZIP code'][i]].state)
    except:
        continue

    # fill the rest of the missing values of state and zipcode with string "Not provided"
    df['State'].fillna('Not provided',inplace=True)
    df['ZIP code'].fillna('Not Provided',inplace=True)

    return df
Exemplo n.º 33
0
    def generate_zip_to_tz_id_map(self):
        """ Generate the map of US zip codes to time zone identifiers. The
        method finds all the possible time zone identifiers for each zip code
        based on a UTC offset stored for that zip in pyzipcode.ZipCodeDatabase.
        """
        zcdb = ZipCodeDatabase()
        zips = list(zcdb.find_zip())
        tz_ids_to_zips = defaultdict(list)
        for zip in _progressbar(zips):
            ids = tuple(self._get_tz_identifiers_for_us_zipcode(zip))

            # apply the data exceptions
            exceptions = data_exceptions.get('zip:' + zip.zip) or data_exceptions.get('state:' + zip.state) or {}
            exceptions['include'] = exceptions.get('include', []) + data_exceptions['all'].get('include', []) if 'all' in data_exceptions else []
            exceptions['exclude'] = exceptions.get('exclude', []) + data_exceptions['all'].get('exclude', []) if 'all' in data_exceptions else []
            if exceptions:
                ids = tuple((set(ids) - set(exceptions['exclude'])) | set(exceptions['include']))

            tz_ids_to_zips[ids].append(zip.zip)

        zips_to_tz_ids = {zip: ids for ids, zips in tz_ids_to_zips.items() for zip in zips}

        _dump_json_data(US_ZIPS_TO_TZ_IDS_MAP_PATH, zips_to_tz_ids)
def get_weatherInfos_py27(weatherData, stationData, stationName):
    ## find weather available at the station zipcode, if not available in data, find weather at the closest zipcode(s) nearby
    from geopy.geocoders import Nominatim
    from pyzipcode import ZipCodeDatabase
    geolocator = Nominatim()
    (lat, lon) = get_station_coordinates(stationName, stationData)
    location = geolocator.reverse((lat, lon))
    #print("Address station from coordinates: "+location.address)
    zipcode = location.raw['address']['postcode']
    #print("Station post code: ", zipcode)
    zcDB = ZipCodeDatabase()

    stationWeather = pd.DataFrame()
    radius = 0
    while radius < 10 and stationWeather.shape[0] == 0:
        zipNearby = [
            int(z.zip)
            for z in zcDB.get_zipcodes_around_radius(zipcode, radius)
        ]
        #print(zipNearby)
        stationWeather = weatherData[weatherData['Zip'].isin(zipNearby)]
        #print("radius: ", radius)
        radius += 0.05  ## ?? 50m?, 0.05 miles?
    #print("post codes of neighborhood: ", zipNearby)
    def fixPrecip(x):
        try:
            return float(x)
        except:
            return 0.005  # maybe 0.01 or something?

    precipitation_inch = stationWeather[u'PrecipitationIn'].apply(fixPrecip)
    temperature_fahrenheit = stationWeather[u'Mean TemperatureF']
    temperature_celcius = (temperature_fahrenheit - 32.) / 1.8
    precipitation_mm = 25.4 * precipitation_inch  ## in millimeters
    #sfPrecipitation.max() #[sfPrecipitation != 0.0]
    #sfTemp.head
    return (precipitation_mm, temperature_celcius)
def getZipCodes(source, radius):
    from pyzipcode import ZipCodeDatabase
    zcdb = ZipCodeDatabase()
    zipcodes = [z.zip for z in zcdb.get_zipcodes_around_radius(source, radius)]
    return zipcodes
Exemplo n.º 36
0
import re
import sys
from pyzipcode import ZipCodeDatabase
import reverse_geocoder as rg
import random
import string

flist=str(sys.argv)
tfile= flist[12:len(flist)-2]
#f = open(tfile, 'r')
f = open("sent9.txt", 'r')
g = open("t9.txt", 'w')

zcdb = ZipCodeDatabase()
c=0
for line in f:
    fcoords=()
    tweet = line.split("|")
    coords = re.search(r"\[(.*)\]", tweet[0]).group(1)
    x, y = map(float, re.findall(r'[+-]?[0-9.]+', coords))
    location = rg.search([x,y])
    if location[0]['cc'] == "US":
        state = location[0]['admin1']
        city = location[0]['name']
        zlist=zcdb.find_zip(city=city)
        if zlist>0:
            zipcode = random.choice(zlist)
            s = tweet[-1].strip('\n')+","+zipcode.zip+","+state+"\n"
#    print s.encode("UTF-8")
            g.write(s.encode('utf8'))
    c+=1
from pyzipcode import ZipCodeDatabase
from scipy.spatial import distance
import pandas as pd

zcdb = ZipCodeDatabase()
sf_zipcodes = zcdb.find_zip(city="San Francisco", state="CA")
# print(len(sf_zipcodes))

def long_lat_to_zipcode(input_longitude,input_latitude):
    """
    function to convert latitude and longitude to zipcode:
    find euclidean distance between user provided latitude and longitude and
    all latitudes, longitudes from sf_zipcodes and select the row from latter
    which has minimum distance. then extract its zip code

    :param input_longitude,input_latitude: latitude and longitude that you want to convert to closest zipcode
    :return closest_zip: zip code
    """

    closest_zip = None
    euc_dist = None

    for i, sf_zipcode in enumerate(sf_zipcodes):
        # extract latitude and longitude from each row in sf_zipcode
        lat = sf_zipcode.latitude
        long = sf_zipcode.longitude
        # calculate euclidean distance between lat and long from sf_zipcode and input latitude and longitude value
        euclidean_dist = round( distance.euclidean((long,lat), (input_longitude,input_latitude)), 4)
        # assign the euclidean distance calculated for first row as euc_dist
        if i == 0:
            euc_dist = euclidean_dist
import us
from geopy.geocoders import Nominatim
from easydict import EasyDict as edict

print("starting webdriver")
driver = webdriver.Firefox()
print("getting webpage")
driver.get("https://www.allareacodes.com/")
result = driver.find_elements(By.XPATH, "//select[@style='width: 100%; margin-right: 2px']")
area_code_and_place = result[0].text.split("\n")
prefixes = [
    "New", "Los", "San", "Baton", "Fort",
    "Bowling", "Lake", "Grand", "Saint",
    "Charlotte"
]
zcdb = ZipCodeDatabase()
geolocator = Nominatim()
for area_code in area_code_and_place:
    state = area_code.split("-")[1].split("(")[0].strip()
    if "DC" in state:
        state = us.states.lookup("DC").abbr
    else:
        state = us.states.lookup(state).abbr
    city = area_code.split("-")[1].split("(")[1].rstrip(")")
    city = city.strip()
    if "," in city:
        city = city.split(",")[0]
    if " " in city:
        if [prefix for prefix in prefixes if prefix in city] == []:
            city = city.split(" ")[0]
    if isinstance(zcdb.find_zip(city=city,state=state),list):