def get_k_nearest_zipcodes_locations(self, ip_zipcode, radius=50, k_neigh=20): """ Find the zipcodes near to the provided zipcodes. """ search = ZipcodeSearchEngine() lat_long_inf = search.by_zipcode(str(ip_zipcode)) lat, longi = lat_long_inf["Latitude"], lat_long_inf["Longitude"] try: result = search.by_coordinate(lat, longi, radius=radius, returns=k_neigh) except: return None if len(result) == 0: return None else: nearest_zip_list = [] for res in result: nearest_zip_list.append(int(res["Zipcode"])) # Check which all zipcodes are present in the given data. avl_zipcode = set(nearest_zip_list) & set(self._zip_code_list) if avl_zipcode is not None: zip_index_list = [] for code in avl_zipcode: zip_index_list.append(self._zip_code_list.index(code)) return zip_index_list else: return None
def init_randomzips(): while True: try: print( 'Please enter the zip code you would like to find accounts around' ) searchzip = int(input('---> ')) print( 'Please enter the radius you would like to find accounts with') searchradius = int(input('---> ')) search = ZipcodeSearchEngine() zipcode = search.by_zipcode(str(searchzip)) mylat = re.findall('"Latitude": (\S+),', str(zipcode)) mylong = re.findall('"Longitude": (\S+),', str(zipcode)) res = search.by_coordinate(zipcode.Latitude, zipcode.Longitude, radius=searchradius, returns=100) searchresults = [] for zipcode in res: searchresults.append(zipcode.Zipcode) searchcity = zipcode.City searchstate = zipcode.State except: print("Sorry, I didn't understand that.") continue else: break print(searchresults)
def create_user_params(self): """Hold data about the user. We've collected all of the information we need from the user. The last thing that needs to be done is to find out what state they live in, and which district they are from. Then we can find their Presenent reps from that info.""" search = ZipcodeSearchEngine() zipcode = search.by_zipcode(str(self.zip_code)) df = pd.DataFrame(columns=[['email', 'password', 'first_name', 'last_name', 'gender', 'dob', 'street', 'zip_code', 'city', 'state_short', 'state_long', 'district']]) df.loc[0, 'email'] = self.email df.loc[0, 'password'] = user_info.hash_password(self) df.loc[0, 'first_name'] = self.first_name.lower().title() df.loc[0, 'last_name'] = self.last_name.lower().title() df.loc[0, 'gender'] = self.gender.lower().title() df.loc[0, 'dob'] = pd.to_datetime(self.dob) df.loc[0, 'street'] = self.street.lower().title() df.loc[0, 'zip_code'] = str(self.zip_code) df.loc[0, 'city'] = str(zipcode['City'].lower().title()) df.loc[0, 'state_short'] = str(zipcode['State']) df.loc[0, 'state_long'] = str(us.states.lookup(df.loc[0, 'state_short'])) df.loc[0, 'district'] = user_info.get_district_from_address(self, df.loc[0, 'city'], df.loc[0, 'state_short'], df.loc[0, 'state_long']) return df
def normalize_city_state(city=None, state=None): """ Utility function that accepts string representations of city and state, and returns a tuple of a validated city, state and zipcode value. :param city: Type: str Description: Name of a city to lookup. Default is None :param state: Type: str Description: Name or abbreviation of a US state to lookup. Default is None :return: Function accepts a city and state combination to lookup. The city and state combination is matched against a database of all US cities and states. If a valid match is found, a tuple with the city and state name are returned. If just one zipcode exists for that city and state combination, a zipcode is also returned. If multiple zipcodes match the city and state combination, then None is returned as the zipcode value in the tuple. The tuple is returned as ("cityname", "statename", "zipcode"). If lookup fails for one or all three items a None object is returned in the tuple. """ zipcode_search = ZipcodeSearchEngine() if state and city: try: city_state_zips = zipcode_search.by_city_and_state(city, state) if city_state_zips: zip_object = city_state_zips[0] single_zipcode = None if len(city_state_zips) == 1: single_zipcode = zip_object.Zipcode return zip_object.City, zip_object.State, single_zipcode except ValueError: return city, validate_state(state), None elif state: return None, validate_state(state), None
def get_weatherInfos(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 uszipcode import ZipcodeSearchEngine geolocator = Nominatim() (lat, lon) = get_station_coordinates(stationName, stationData) location = geolocator.reverse((lat, lon)) zipcode = location.raw['address']['postcode'] search = ZipcodeSearchEngine() zipcode_infos = search.by_zipcode(zipcode) stationWeather = pd.DataFrame() radius = 0 while radius < 10 and stationWeather.shape[0] == 0: zipNearby = [ int(z.Zipcode) for z in search.by_coordinate(lat, lon, radius=radius, returns=5) ] 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 person_searching_teams_result(request): error_message = "" # get and normalize dist dist = int(request.POST['distance']) if dist == None or dist < 1 or dist > max_travel_distance: error_message += "Must specify a distance between 1 and %s miles. " % max_travel_distance dist = 15 zipcode = request.POST['zipcode'] zipcode_search_engine = ZipcodeSearchEngine() info = zipcode_search_engine.by_zipcode(zipcode) latitude = info.Latitude longitude = info.Longitude if latitude == None or longitude == None: error_message += "Must specify a valid US zipcode. " jfll = False if "jfll" in request.POST: jfll = True fll = False if "fll" in request.POST: fll = True ftc = False if "ftc" in request.POST: ftc = True frc = False if "frc" in request.POST: frc = True if not (jfll or fll or ftc or frc): error_message += "Must select at least one of jFLL, FLL, FTC, or FRC type of teams. " new_members = False if "new_members" in request.POST: new_members = True return render_person_searching_teams(request, zipcode, dist, latitude, longitude, new_members, jfll, fll, ftc, frc, error_message)
def get_cities(self): ''' This function uses the zipcodes API to return the city name for each cluster centroid, based on latitude and longitude ''' midpoints_df = self.midpoints_df search = ZipcodeSearchEngine() midpoints_df["City"] = midpoints_df[["latitude", "longitude"]].apply(lambda x:\ search.by_coordinate(\ x[0]\ , x[1]\ , radius=30\ , returns=1)[0].City\ , axis=1) midpoints_df["State"] = midpoints_df[["latitude", "longitude"]].apply(lambda x:\ search.by_coordinate(\ x[0]\ , x[1]\ , radius=30\ , returns=1)[0].State\ , axis=1) midpoints_df[ "City_State"] = midpoints_df["City"] + ", " + midpoints_df["State"] cities_dict = midpoints_df.set_index("geo_cluster").to_dict("index") self.cities_dict = cities_dict
def validate_state(state): """ Utility function that accepts any valid string representation of a US state and returns a normalized two character abbreviation of the state. :param state: Type: String Default: None Description: String representation of a US state :return: If a valid US state is found, the two character state abbreviation is returned. Otherwise, a ValueError is raised """ zipcode_search = ZipcodeSearchEngine() try: state_zips = zipcode_search.by_state(state) if state: state = state_zips[0] return state.State else: raise ValueError( 'Could not find a valid US state with the given input: {}'. format(state)) except TypeError: raise ValueError( 'Could not find a valid US state with the given input: {}'.format( state))
def validZip(self,zip): search = ZipcodeSearchEngine() zipcode = search.by_zipcode(zip) if zipcode['Zipcode'] is not None: self.calculatepressure(zipcode=zipcode['Zipcode']) else: print('Please enter a valid zip code')
def isvalid(self, zipcode): from uszipcode import ZipcodeSearchEngine search = ZipcodeSearchEngine() if zipcode < 90000 or zipcode > 99999 or zipcode == 91980: return False lat = search.by_zipcode(zipcode)['Latitude'] lon = search.by_zipcode(zipcode)['Longitude'] return self.check_SD_lat_lon(lat, lon)
def get_distance_between_zips(zip1, zip2): "Return the distance in miles between two zips" search = ZipcodeSearchEngine() my_zip1 = search.by_zipcode(zip1) loc1 = (my_zip1['Latitude'], my_zip1['Longitude']) my_zip2 = search.by_zipcode(zip2) loc2 = (my_zip2['Latitude'], my_zip2['Longitude']) return vincenty(loc1, loc2).miles
def findCityState(zipcode): search = ZipcodeSearchEngine() zip = search.by_zipcode(zipcode) city = zip.City state = zip.State return (city,state)
def search_city(state): search_engine = ZipcodeSearchEngine() state = search_engine.by_city(str(state)) cities = [] for i in state: cities.append(i.City) return cities
def addusertodatabase(): ########## GET ACCOUNT ########## while True: try: print( 'Please enter your account name as it appears in the URL after "https://soundcloud.com/" ' ) accname = str(input('acc---> ')) url = str("https://soundcloud.com/" + accname) request = requests.get(url) if request.status_code == 200: #makes sure that the soundcloud account name is real by checking the url link to see if it exists/opens break else: print( '\nSorry, this was an invalid response, please try again. Souncloud URL names must only use numbers, lowercase letters, underscores or hyphens, and they must start with a letter or number. \nPlease try again. And make sure you are connected to the internet.' ) continue except: print( '\nSorry, this was an invalid response, please try again. Souncloud URL names must only use numbers, lowercase letters, underscores or hyphens, and they must start with a letter or number. \nPlease try again. And make sure you are connected to the internet.' ) continue else: break readyaccname = accname readyfullurl = url ########## GET ZIP CODE ########## print( 'Please enter the permanent zip code you would like to associate with your account' ) while True: try: acczip = int(getzip()) search = ZipcodeSearchEngine() zipcode = search.by_zipcode(str(acczip)) except ValueError: print( "\nSorry, I didn't understand that. Please enter a valid zip code" ) continue else: break readyacczip = acczip ########## ADD TO DATABASE ########## accounts = {} accounts[readyacczip] = readyaccname with open('accounts.json', 'a') as fp: #opens the existing file, rather than creating a new one json.dump(accounts, fp, indent=4) #adds the individual account zip and name in an organized format print( '\nThank you, your account has been added as \"' + readyaccname + '\" (Full URL:', readyfullurl + ")", 'with Zip Code', readyacczip) options()
def zipFromCityState(city, state): """ :param city: :param state: :return: """ search = ZipcodeSearchEngine() return search.by_city_and_state(city, state)
def zipToCoord(zipCode): """ take zip code and convert them to a zip code :param zipCode: :return: """ search = ZipcodeSearchEngine() results = search.by_zipcode(zipCode) return results
def extract_location(s): states = utils.states z_search = ZipcodeSearchEngine() possible_locations = find_possible_locations(s) keys = ['State', 'City', 'Zipcode'] for state, zipcode in possible_locations: zip_info = z_search.by_zipcode(zipcode) if states[state] == zip_info['State']: return {key: zip_info[key] for key in keys} return {'State': None, 'City': None, 'Zipcode': None}
def search_zip(city): search_engine = ZipcodeSearchEngine() city = search_engine.by_city(str(city)) zipcodes = [] for i in city: zipcodes.append(i.Zipcode) return zipcodes
def filter_posts(self, service, location, company, rating): search = ZipcodeSearchEngine() if service != "None" and location != "None" and company == 'None' and rating == 'None': res = search.by_city(location) city_zips = [] for i in range(0, len(res)): city_zips.append(res[i].Zipcode) return Post.query.join(Company).filter( Company.company_zipcode.in_(city_zips), Post.service_id == service)
def filter_desired_columns_from_ip_records(self): """ Filter only desired columns from the records. """ filtered_data = [] print("LOG: [Filtering Engine] Filtering desired columns.") # Get categories and convert to list. for row in self._input_data_records: cat_list = [] for k in row['categories']: cat_list.append(k.encode('ascii')) # Parse zipcode from the full_address value. zip_code = row['full_address'].split(' ')[len(row['full_address'].split(' ')) - 1] # Check if zipcode is available and a valid one. try: zip_code = int(zip_code) # Sometimes we get invalid zipcode such as 891118, we need to get # the zipcode from latitude and longitude if (zip_code > 99999): raise Exception("ERROR: [Filtering Engine] Invalid zip_code") except: # Get the closest zipcode for the given lat-long # Help link: https://pypi.python.org/pypi/uszipcode # Search engine for zipcode to lat-long and vice-versa conversions. This returns # top 3 matching zipcodes. search = ZipcodeSearchEngine() result = search.by_coordinate(row['latitude'], row['longitude'], radius=20, returns=3) if len(result) == 0: continue zip_code = int(result[0]['Zipcode']) # Filter out rows that belong to some invalid locations. if (zip_code < 100): continue # Create record row with desired columns. a = (cat_list, '', row['state'], row['city'], row['full_address'], zip_code, row['longitude'], row['latitude'], row['stars'], row['type'], row['review_count'] ) # Append to final data. filtered_data.append(a) print ("LOG: [Filtering Engine] Number of filtered final records: %d" % len(filtered_data)) self._input_filtered_cols_records = filtered_data
def getZips(): # get variables passed stName = request.args.get('Store') stRad = request.args.get('Scope') sqlStr = ("SELECT \ njstores.LATITUDE_MEAS,\ njstores.LONGITUDE_MEAS\ FROM where_are_your_stores.njstores\ WHERE LOCATION_NAME = '" + stName + "'") # Grab the file and return all store locations results = conn.execute(sqlStr) resDict = {} resList = [] for row in results: resDict["Lat"] = row.LATITUDE_MEAS resDict["Long"] = row.LONGITUDE_MEAS stLat = row.LATITUDE_MEAS stLong = row.LONGITUDE_MEAS resList.append(resDict) stRad = int(stRad) # stRad = request.args.get('Scope') search = ZipcodeSearchEngine() res = search.by_coordinate(stLat, stLong, radius=stRad, returns=0) # print(res) allZips = [] for aRec in res: zDict = {} # allZips.append(aRec.Zipcode) # allPop.append(aRec.Population) # allIncome.append(aRec.Total) zDict["City"] = aRec.City zDict["Density"] = aRec.Density zDict["HouseOfUnits"] = aRec.HouseOfUnits zDict["LandArea"] = aRec.LandArea zDict["Latitude"] = aRec.Latitude zDict["Longitude"] = aRec.Longitude zDict["NEBoundLatitude"] = aRec.NEBoundLatitude zDict["NEBoundLongitude"] = aRec.NEBoundLongitude zDict["Population"] = aRec.Population zDict["SWBoundLatitude"] = aRec.SWBoundLatitude zDict["SWBoungLongitude"] = aRec.SWBoungLongitude zDict["State"] = aRec.State zDict["TotalWages"] = aRec.TotalWages zDict["WaterArea"] = aRec.WaterArea zDict["Wealthy"] = aRec.Wealthy zDict["Zipcode"] = aRec.Zipcode zDict["ZipcodeType"] = aRec.ZipcodeType allZips.append(zDict) return jsonify(allZips)
def get_zipcode_crime_map(self, zipcode): """ Generate a html heatmap for the zipcode. """ from uszipcode import ZipcodeSearchEngine search = ZipcodeSearchEngine() zip_lat = search.by_zipcode(zipcode)['Latitude'] zip_lon = search.by_zipcode(zipcode)['Longitude'] self.generate_past_week_html(self.crime_lat_lon_list, center_lat=zip_lat, center_lon=zip_lon)
def assign_zipcode(steps): search = ZipcodeSearchEngine() for step in steps: zipcodes = search.by_coordinate(step.latitude, step.longitude, returns=1) if len(zipcodes) > 0: zipcode_dict = next(iter(zipcodes)) step.zipcode = zipcode_dict['Zipcode'] step.city = zipcode_dict['City'] step.state = zipcode_dict['State']
def city_to_zipcodes(city, state): zipcodeEngineSearch = ZipcodeSearchEngine() try: zipcodes = zipcodeEngineSearch.by_city_and_state(city, state) if not zipcodes: # A zipcode that is known to return null return [zipcodeEngineSearch.by_zipcode(97003)] return zipcodes except ValueError: # A zipcode that is known to return null return [zipcodeEngineSearch.by_zipcode(97003)]
def zipcode_to_latlong(zipcode): """ Takes in zipcode and returns the corresponding latitude and longitude :param zipcode: String or Int of zipcode (either work) :return: Latutude, longitude """ # TODO: this works for some zip codes but not all # (pasadena doesn't work, but LA does). Fix to make it work # for everything search = ZipcodeSearchEngine() loc_data = search.by_zipcode(zipcode) return loc_data['Latitude'], loc_data['Longitude']
def init_addusertodatabase(): ########## GET ACCOUNT ########## x = 0 while x < 300: while True: try: accname = random.choice(randomusers) url = str("https://soundcloud.com/" + accname) request = requests.get(url) if request.status_code == 200: #print(accname) break else: print( '\nSorry, this was an invalid response, please try again. Souncloud URL names must only use numbers, lowercase letters, underscores or hyphens, and they must start with a letter or number. \nPlease try again. And make sure you are connected to the internet.' ) continue except: print( '\nSorry, this was an invalid response, please try again. Souncloud URL names must only use numbers, lowercase letters, underscores or hyphens, and they must start with a letter or number. \nPlease try again. And make sure you are connected to the internet.' ) continue else: break readyaccname = accname readyfullurl = url ########## GET ZIP CODE ########## while True: try: #acczip = random.choice(morezips) acczip = random.choice(randomzips) search = ZipcodeSearchEngine() zipcode = search.by_zipcode(str(acczip)) except ValueError: print( "Sorry, I didn't understand that. Please enter a valid zip code" ) continue else: break readyacczip = acczip ########## ADD TO DATABASE ########## accounts = {} accounts[readyacczip] = readyaccname with open('accounts.json', 'a') as fp: json.dump(accounts, fp, indent=4) print( '\nThank you, your account has been added as \"' + readyaccname + '\"', 'with Zip Code', readyacczip, '\n ') #options() x += 1
def process_form_data(form_list, request): #ImageFormSet = modelformset_factory(Images,form=ImagesForm, extra=3) if request.method == 'POST': form_data = [form.cleaned_data for form in form_list] form = Posting_Form_Final() instance = form.save(commit=False) instance.user = request.user instance.title = form_data[0]['title'] instance.image = form_data[0]['image'] instance.condition = form_data[1]['condition'] instance.description = form_data[1]['description'] instance.price = form_data[2]['price'] instance.email = form_data[2]['email'] instance.phone_number = form_data[2]['phone_number'] instance.street = form_data[3]['street'] instance.city = form_data[3]['city'] instance.state = form_data[3]['state'] instance.zipcode = form_data[3]['zipcode'] instance.height_field = 100 instance.width_field = 100 search = ZipcodeSearchEngine() zipcode = search.by_zipcode(instance.zipcode) instance.longitude = zipcode['Longitude'] instance.latitude = zipcode['Latitude'] instance.save() #Save images (multiple possible) per form #images_form_instance.post = instance #images_form_instance.image images_form = ImagesForm(request.POST, request.FILES) if images_form.is_valid(): photo = images_form.save() data = {'is_valid': True, 'name': photo.image.name, 'url': photo.image.url} else: data = {'is_valid': False} """ import pdb; pdb.set_trace() formset = ImageFormSet(request.POST, request.FILES, queryset=Images.objects.none()) if formset.is_valid() and form.is_valid(): for form in formset.cleaned_data: image = form['image'] photo = Images(post=instance, image=image) photo.save() messages.success(request,"Yeeew, check it out on the home page!") else: print(formset.errors, form.errors) formset = ImageFormSet(queryset=Images.objects.none()) """ return form_list
def get_xy_crimeRate_popDensity(self, df): """ Get the relationship between population density and Crime rate, plot in x, y coordinates form """ from uszipcode import ZipcodeSearchEngine search = ZipcodeSearchEngine() crime_rate = [] annual_wage = [] population_dens = [] valid_zip = df['zip'].unique().astype(np.int) crime_num = df.groupby(['zip']).size() for i in valid_zip: if not self.isvalid(i): continue else: wage = search.by_zipcode(i)['Wealthy'] population_den = search.by_zipcode(i)['Density'] rate = crime_num.get(i) / 180.0 if wage is None: continue if rate > 20: print("highest rate: ", i, "rate: ", rate, ", wage: ", wage, " pop desity: ", population_den) if population_den > 12000: print("highest density: ", i, "rate: ", rate, ", wage: ", wage, " pop desity: ", population_den) # a way of measuring good community - dense population, but small crime rate if population_den > 10000 and rate < 4: print("low wage and low crime: ", i, "rate: ", rate, ", wage: ", wage, " pop desity: ", population_den) annual_wage.append(int(wage) * 0.01) crime_rate.append(rate) population_dens.append(population_den) # print(curr_population) plt.scatter(crime_rate, population_dens, s=annual_wage, alpha=0.4) plt.xlabel('Crime Numbers (times / day)') plt.ylabel('Population Density (people / km$^2$)') for income in [10000, 20000, 40000]: plt.scatter([], [], c='k', alpha=0.4, s=income * 0.01, label=str(income) + ' $ / year') plt.legend(scatterpoints=1, frameon=False, labelspacing=1, title='Annual wage') plt.title( 'Crime Rate and Population Density for San Diego Area Zipcodes') plt.show()
def on_message(self, client, userdata, message): payload = json.loads(message.payload) if payload["postal_code"] == None or payload["postal_code"] == "": return if (self.zips == None): self.zips = ZipcodeSearchEngine() zcode = self.zips.by_zipcode(payload["postal_code"]) if zcode["Longitude"] == None or zcode["Latitude"] == None: return (x, y) = self.project(zcode["Longitude"], zcode["Latitude"]) x = int(x) y = int(y) logging.warning(zcode["City"] + " at " + str(x) + ", " + str(y)) self.pings.append(Ping(x, y))
def _convert_zip(city, state, idx, errors): try: search = ZipcodeSearchEngine() result = search.by_city_and_state(city, state) zipcode = result[0]['Zipcode'] # print('Zipcode for {} , {} is: {}'.format(city, state, zipcode)) return zipcode, errors except: print( 'Error! Could not find city {} in state {} for id#: {}.'.format( city, state, idx)) zipcode = None errors[idx] = (city, state) return zipcode, errors
def get_rich_zipcode(self) -> Optional[Zipcode]: if not self.zipcode: return None search = ZipcodeSearchEngine() return search.by_zipcode(self.zipcode)