def setUp(self): self.api_client = postcodes_io_api.Api() self.latitude = 51.466324 self.longitude = -0.173606 self.VALID_OUTCODES = ['KT19', 'HA9', 'SW19', 'IV2', 'PH16'] self.VALID_POSTCODES = [ 'L40TH', 'SW151JF', 'SW195AG', 'KT185LQ', 'NW87JY', 'RG11LZ' ] self.INVALID_POSTCODES = [ 'HA9997QP', 'SW19OSZ', 'HA899NX', 'RG101LZ', 'SW12EF', 'L500QD' ] self.TERMINATED_POSTCODES = [ 'SW112ZW', 'HA89NX', 'IV23EH', 'BA12QT', 'OX13LA', 'SE109DB' ] self.reverse_geocode_data = { "geolocations": [{ "longitude": 0.629834723775309, "latitude": 51.7923246977375 }, { "longitude": -2.49690382054704, "latitude": 53.5351312861402, "radius": 1000, "limit": 5 }] }
def callAPI(lat, lon, lim, rad): api = postcodes_io_api.Api(debug_http=True) listPostcodes = api.get_nearest_postcodes_for_coordinates( latitude=lat, longitude=lon, limit=lim, radius=rad) onlyPostcodes = [] for i in range(len(listPostcodes["result"])): print(str(i)) onlyPostcodes.append(listPostcodes["result"][i]["postcode"]) return onlyPostcodes
def store(self, name, numberOfMeals, houseNumber, postcode): # check the postcode validity api = postcodes_io_api.Api(debug_http=True) data = api.get_postcode(postcode) if data['status'] != 200: # send back info that its wrong try: frontEnd = Pyro4.Proxy("PYRONAME:front_end") frontEnd.checkOnline() msg = f"For order to: {name}\n{postcode} is not a valid postcode, please try again." self.send(frontEnd, "PostcodeERROR", msg) print() print("Order declined, invalid Postcode.") print() except: print("Front-end server is down.") print() else: postcode += '\n\t\t' + str(data['result']['primary_care_trust']) + '\n\t\t' + str(data['result']['region']) + '\n\t\t' + str(data['result']['country']) # print out details if numberOfMeals == 1: msg = f"\nOrder placed:\nCustomer: {name}\nOrder: {numberOfMeals} meal\nDelivering to: \n\t\tHouse Number {houseNumber}\n\t\t{postcode}" else: msg = f"\nOrder placed:\nCustomer: {name}\nOrder: {numberOfMeals} meals\nDelivering to: \n\t\tHouse Number {houseNumber}\n\t\t{postcode}" print(msg) # send confirmation to front end try: frontEnd = Pyro4.Proxy("PYRONAME:front_end") frontEnd.checkOnline() self.send(frontEnd, "confirmation", msg) print("Order confirmed.") print() # update our store once the order confirmed self.orders[self.count] = [name, numberOfMeals, houseNumber, postcode] self.count += 1 except: print("Front-end server is down.") print() # update data across all servers try: self.attemptServersDataUpdate() print(f"Data updated across active servers.") print() # show the upated list print("Current list of orders confirmed:") for i in range(len(self.orders)): print(f"Customer: {self.orders[i][0]}, Meals: {self.orders[i][1]}, Delivering to: \n\t\tHouse Number {self.orders[i][2]}\n\t\t{self.orders[i][3]}") except: pass print() print("Listening for new requests...") print()
def test_postcode_api(self): postcode = "SO173RZ" api = pst.Api() data = api.get_postcode(postcode) verified_data = { 'postcode': 'SO17 3RZ', 'quality': 1, 'eastings': 442899, 'northings': 115194, 'country': 'England', 'nhs_ha': 'South Central', 'longitude': -1.390884, 'latitude': 50.934605, 'european_electoral_region': 'South East', 'primary_care_trust': 'Southampton City', 'region': 'South East', 'lsoa': 'Southampton 005F', 'msoa': 'Southampton 005', 'incode': '3RZ', 'outcode': 'SO17', 'parliamentary_constituency': 'Romsey and Southampton North', 'admin_district': 'Southampton', 'parish': 'Southampton, unparished area', 'admin_county': None, 'admin_ward': 'Swaythling', 'ced': None, 'ccg': 'NHS Hampshire, Southampton and Isle of Wight', 'nuts': 'Southampton', 'codes': { 'admin_district': 'E06000045', 'admin_county': 'E99999999', 'admin_ward': 'E05002469', 'parish': 'E43000036', 'parliamentary_constituency': 'E14000901', 'ccg': 'E38000253', 'ccg_id': 'D9Y0V', 'ced': 'E99999999', 'nuts': 'TLJ32', 'lsoa': 'E01032756', 'msoa': 'E02003553', 'lau2': 'E06000045' } } self.assertEqual(data['result']['longitude'], verified_data['longitude']) self.assertEqual(data['result']['latitude'], verified_data['latitude'])
def handle_clicked_post(self): api = pst.Api() valid = False retry = False while not valid: if retry: pcode, ok = QInputDialog.getText( self, 'Postcode', 'Postcode Invalid, please try again') else: pcode, ok = QInputDialog.getText(self, 'Postcode', 'Postcode:') retry = True if not ok: return valid = api.is_postcode_valid(pcode) if valid: data = api.get_postcode(pcode) self.coordinate = QGeoCoordinate(data['result']['latitude'], data['result']['longitude']) self._lat_spinbox.setValue(self.coordinate.latitude()) self._lng_spinbox.setValue(self.coordinate.longitude())
def checkPostcode(self, post): PIOFail = False GTDFail = False validPostcode = False # Tries Postcodes.io first try: api = postcodes_io_api.Api() validPostcode = api.is_postcode_valid(post) except: print("There was an error accessing the Postcodes IO API.") PIOFail = True # If Postcodes.io fails, tries GetTheData's postcode checker if PIOFail: try: url = "http://api.getthedata.com/postcode/" + post res = requests.get(url) if res: json = res.json() if json["status"] != "match": validPostcode = False else: validPostcode = True else: print("There was an error accessing the GetTheData API.") GTDFail = True except: print("There was an error accessing the GetTheData API.") GTDFail = True # If both fail, assumes postcode is invalid if PIOFail and GTDFail: print( "Both postcode validation services have failed - assume postcode is invalid" ) validPostcode = False return validPostcode
def spatialPointPuller(outerPC): PCapi = postcodes_io_api.Api(debug_http=False) desiredColumns = ['outcode', 'latitude', 'longitude'] dataforOutput = [] for i in range(len(outerPC.index)): df = outerPC.iloc[[i]] retrievedData = PCapi.get_outcode(df[args.outerPC][i]) try: dataDict = retrievedData['result'] dataList = [dataDict[x] for x in desiredColumns] dataList.insert(0, df[args.sampleID][i]) dataforOutput.append(dataList) except: if df[args.outerPC].notnull()[i] is True: print("Check postcode for:\n" + str(df[[args.sampleID, args.outerPC]])) dataList = [ df[args.sampleID][i], df[args.outerPC][i], np.nan, np.nan ] dataforOutput.append(dataList) outFrame = pd.DataFrame( dataforOutput, columns=[args.sampleID, 'outcode', 'latitude', 'longitude']) return outFrame
def get_locations(lat, long): """ Uses coordinates to determine Country, Region, and Postcode. :param float lat: Latitude :param float long: Longitude :return: Returns a dictionary of locations at different scales :rtype: dict """ pnt = Point(long, lat) api = postcodes_io_api.Api(debug_http=False) resp = api.get_nearest_postcodes_for_coordinates(latitude=lat, longitude=long, limit=1, radius=2000) result = resp['result'] if ('result' in resp) and (resp['result'] != None) else [] item = result[0] if len(result) > 0 else {} postcode = item['postcode'] if 'postcode' in item else str() county_query = CountyBorder.objects.filter(mpoly__contains=pnt) if not county_query: county = str() else: county = county_query[0].name region_query = RegionBorder.objects.filter(mpoly__contains=pnt) if not region_query: region = str() else: region = region_query[0].name country_query = CountryBorder.objects.filter(mpoly__contains=pnt) if not country_query: country = str() else: country = country_query[0].name return {"country": country, "region": region, "county": county, "postcode": postcode}
#!/usr/bin/env python """ A variation on current_weather.py which uses postcodes rather than lon lat. """ import datapoint import postcodes_io_api # Create datapoint connection conn = datapoint.Manager(api_key="aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee") # Get longitude and latitude from postcode postcodes_conn = postcodes_io_api.Api() postcode = postcodes_conn.get_postcode('SW1A 2AA') latitude = postcode['result']['latitude'] longitude = postcode['result']['longitude'] # Get nearest site and print out its name site = conn.get_nearest_forecast_site(latitude, longitude) print(site.name) # Get a forecast for the nearest site forecast = conn.get_forecast_for_site(site.location_id, "3hourly") # Get the current timestep using now() and print out some info now = forecast.now() print(now.weather.text) print("%s%s%s" % ( now.temperature.value, '\xb0', #Unicode character for degree symbol now.temperature.units))
from tradingwebsite import db from flask_wtf import FlaskForm from wtforms import StringField, SelectField, SelectMultipleField, SubmitField, ValidationError from wtforms.validators import DataRequired, Length import postcodes_io_api api = postcodes_io_api.Api(debug_http=True) def postcode_check(form, field): postcode = field.data.replace(" ", "").upper() valid = api.is_postcode_valid(postcode) if valid == False: raise ValidationError('Invalid Postcode') def get_choices(category, filter_type): components = db.components component_dict = components.find_one() posts = db.posts post_list = posts.find() options = [] items = component_dict[category] post_ids = [post["item"] for post in post_list] for item in items: if (option := item[filter_type]) not in options and item["id"] in post_ids: options.append(option) return options
import postcodes_io_api api = postcodes_io_api.Api(debug_http=False) #data = api.get_postcode('SW112EF') postcodes = [] with open('postcodes1.txt', 'w') as f: for i in range(0, 200000): print("Gettings postcode {}".format(i)) data = api.get_random_postcode() postcodes.append(data["result"]["postcode"]) f.write("%s\n" % postcodes[i]) number_of_unique_values = len(postcodes) print(f"postcode1: {number_of_unique_values}")
#test place_list = pd.read_csv("geo_sample.csv")["user_location"] codes = [] for i in place_list: to_code = preprocessing(i) codes.append(to_code) print(codes) ''' #c.execute('''UPDATE users ADD COLUMN geocode VARCHAR(255)''') loginfo = load_token('SQL auth.txt') USER_KEY = load_token('google auth.txt')[0] conn = pymysql.connect(loginfo[0], loginfo[1], loginfo[2], loginfo[3]) gmaps = googlemaps.Client(key=USER_KEY) postcode = postcodes_io_api.Api() cursor = conn.cursor() #c.execute('SET GLOBAL max_allowed_packet=500*1024*1024') geodata = [] coded = {} cursor.execute('''SELECT hash_id FROM users''') id_ = cursor.fetchall() counter = 0 for i in id_: cursor.execute('''SELECT user_location from users WHERE hash_id =''' + "'" + str(i[0].strip()) + "'") location = cursor.fetchone()[0] if location is not None: geo_code = preprocessing(location) try:
def get_location_detail(pc): api = postcodes_io_api.Api(debug_http=True) data = api.get_postcode(pc) return data
# import the api import postcodes_io_api import re # assign the api to a variable api = postcodes_io_api.Api(debug_http=False, timeout=None, base_url=None) def get_a_postcode(postcode): ''' check if the postcode exist, if it does not exist it will give as output 'Invalid Postcode' message to the user, if it exist will give a simple address extract from the postcode ''' if postcode_is_valid(postcode): data = get_data_postcode(postcode) return data, print(data) else: return None def get_data_postcode(postcode): ''' Retrive the postcode data by checking first if the syntax is correct and then validating the code with postcode api to double check if the given postcode eexist and prevent having postcode that does not exist passing true ''' postcode_data = api.get_postcode(postcode) return postcode_data