Ejemplo n.º 1
0
def PostcodeInfo(Postcode):
	Postcode = Postcode.upper()
	pc = PostCoder()
	result = pc.get(Postcode)
		
	if str(result) != "None":
		location = result['geo']
		for key, value in location.items():
			if (key == "lat"):
				loc_lat = value
			if (key == "lng"):
				loc_long = value

		location = result['administrative']
		for key, value in location.items():
			if (key == "constituency"):
				for key1, value1 in value.items():
					if (key1 == "title"):
						loc_const = value1
			if (key == "district"):
				for key2, value2 in value.items():
					if (key2 == "title"):
						loc_dist = value2
					
		return(json.dumps({"Postcode": Postcode, "lat": loc_lat, "lng": loc_long, "const": loc_const, "coun": loc_dist, "IP": socket.gethostbyname(socket.gethostname())}, sort_keys=True))
		# write_to_file = Postcode + ", " + str(loc_lat) + ", " + str(loc_long) + ", " + str(loc_const) + ", " + str(loc_dist) + "\n"
	else:
		return(json.dumps({"Postcode": Postcode, "lat": "N/A"}))
def main():
    FIRST = 1256
    LAST = 3091.0  
    csv_in = "Survey Data.csv"       
    reader = csv.reader(open(csv_in, "rU"))
    
    #(record_id, data_source,allocated_NN,postcode, Xcord, Ycord , last_Modified, geography)
    reader.next() #skip header
    #new_rows = []
    for row in reader:       
        data = list(row)[0:4]
        cur = int(data[0])                
        if cur < FIRST:
            continue 
        postcode = data[3]
        pc = PostCoder()
        gdict = pc.get(postcode)
        try:
            lat = gdict['geo']['lat']
            lng = gdict['geo']['lng']
            data.extend([lat, lng])
            percent = (cur/LAST) * 100
            print ("%s [%f" % (cur, percent)) + "%]"                   
            FIRST = FIRST + 1
        except TypeError:
            print "Failed to geo-code"
        
        
        with open('nn.csv', mode='a') as csv_out:
            writer = csv.writer(csv_out, dialect='excel')
            writer.writerow(data)
Ejemplo n.º 3
0
 def __init__(self, endpoint=None):
     self.pc = PostCoder()
     self._last_updated = 0
     self._etree = None
     self._stations_lst = []
     self._stations_map = {}
     self.endpoint = endpoint or TFL_DATA_LOC
def get_info(postcode):
    """Get info from postcode

    Key arguments:
    postcode   --  The postcode given (string)

    Return a dict with the info, and error message if the code is invalid
    """

    if is_valid_postcode(postcode):
        return PostCoder().get(postcode)
    return {'error': 'Your postcode is invalid'}
Ejemplo n.º 5
0
def PostcodeInfo(Postcode):
    Postcode = Postcode.upper()
    pc = PostCoder()
    result = pc.get(Postcode)

    if str(result) != "None":
        location = result['geo']
        for key, value in location.items():
            if (key == "lat"):
                loc_lat = value
            if (key == "lng"):
                loc_long = value

        location = result['administrative']
        for key, value in location.items():
            if (key == "constituency"):
                for key1, value1 in value.items():
                    if (key1 == "title"):
                        loc_const = value1
            if (key == "district"):
                for key2, value2 in value.items():
                    if (key2 == "title"):
                        loc_dist = value2

        return (json.dumps(
            {
                "Postcode": Postcode,
                "lat": loc_lat,
                "lng": loc_long,
                "const": loc_const,
                "coun": loc_dist,
                "IP": socket.gethostbyname(socket.gethostname())
            },
            sort_keys=True))
        # write_to_file = Postcode + ", " + str(loc_lat) + ", " + str(loc_long) + ", " + str(loc_const) + ", " + str(loc_dist) + "\n"
    else:
        return (json.dumps({"Postcode": Postcode, "lat": "N/A"}))
Ejemplo n.º 6
0
def get_geo(postcode):
	pc = PostCoder()
	result = pc.get(postcode)
	return result['geo']['lat'], result['geo']['lng'], result['administrative']['council']['title']
Ejemplo n.º 7
0
 def setUp(self):
     self.pc = PostCoder()
Ejemplo n.º 8
0
class TestPostCoder(unittest.TestCase):
    def setUp(self):
        self.pc = PostCoder()

    def tearDown(self):
        pass

    def test__check_point(self):
        """ Tests postcodes._check_point """
        f = self.pc._check_point
        self.assertRaises(IllegalPointException, f, -91, 181)               
        self.assertRaises(IllegalPointException, f, 90.1, -180.1)
        self.assertIsNone(f(0,0))

    @patch('postcodes.get')
    def test_get(self, mock):
        """ Tests PostCoder.get """
        self.pc.get("F0 0BA")
        self.pc.get("F00BA") # should use cache
        self.pc.get("f00b a") # should use cache
        mock.assert_called_once_with("f00ba")
        self.pc.get("f00b a", skip_cache=True)
        mock.assert_has_calls([call("f00ba"), call("f00ba")])

    @patch('postcodes.get_nearest')
    def test_get_nearest(self, mock):
        """ Tests PostCoder.get_nearest """
        self.pc.get_nearest(0, 0)
        self.pc.get_nearest(0, 0, skip_cache=True)
        mock.assert_has_calls([call(0, 0), call(0, 0,)]) 
        self.assertRaises(IllegalPointException, self.pc.get_nearest, -91, 0)

    @patch('postcodes.get_from_postcode')
    def test_get_from_postcode(self, mock):
        """ Tests PostCoder.get_from_postcode """
        self.pc.get_from_postcode("F0 0BA", 1.1)
        self.pc.get_from_postcode("F00BA", "12.32")
        self.pc.get_from_postcode("F00BA", 12.32) # use cache
        mock.assert_has_calls([call("f00ba", 1.1), call("f00ba", 12.32)])
        
        mock.reset_mock()
        self.pc.get_from_postcode("F00BA", 12.32, skip_cache=True)
        mock.assert_called_once_with("f00ba", 12.32)
        self.assertRaises(IllegalDistanceException, self.pc.get_from_postcode, 
                          None, -11)

    @patch('postcodes.get_from_geo')
    def test_get_from_geo(self, mock):
        """ Tests PostCoder.get_from_geo """
        self.pc.get_from_geo(-40, 100, 1)
        self.pc.get_from_geo("-30", "20", "12")
        self.pc.get_from_geo(-30, 20, 12)
        mock.assert_has_calls([call(-40, 100, 1), call(-30, 20, 12)])
        
        mock.reset_mock()
        self.pc.get_from_geo(-30, 20, 12, skip_cache=True)
        mock.assert_called_once_with(-30, 20, 12)
        f = self.pc.get_from_geo
        self.assertRaises(IllegalPointException, f, 91, 0, 0)
        self.assertRaises(IllegalDistanceException, f, -30, 20, -11)
Ejemplo n.º 9
0
def get_location_from_postcode(post_code):
    pc = PostCoder()
    result = pc.get(post_code).get('geo')
    return result.get('lng'), result.get('lat')
Ejemplo n.º 10
0
from postcodes import PostCoder

filename = 'trusts.csv'

#Open the file and read out data
with open(filename, 'rb') as f:
    reader = csv.reader(f)
    name_list = []
    postcode_list = []

    for row in reader:
        name_list.append(row[1].replace(',', ''))
        postcode_list.append(row[9])

#Take postcodes and obtain lat long data
pc = PostCoder()
lat_list = []
lng_list = []

for postcode in postcode_list:
    info = pc.get(postcode)
    lat_list.append(info.get('geo').get('lat'))
    lng_list.append(info.get('geo').get('lng'))

#Write new data to csv
writefile = 'trust_loc.csv'
with open(writefile, 'wb') as f:
    writer = csv.writer(f, delimiter=',')

    for i in range(len(name_list)):
        writer.writerow([name_list[i], str(lat_list[i]), str(lng_list[i])])
Ejemplo n.º 11
0
from pprint import PrettyPrinter
from postcodes import PostCoder

fileList = []
postcodeList = []
os.chdir("/Users/leo/Documents/MyCode/walesCSVs/")
for counter, files in enumerate(glob.glob("*.csv")):
    fileList.append(files)

for fileIn in fileList:
    f = open(fileIn)
    lines = f.readlines()
    for line in lines[1:]:
        postCode = []
        postCode = re.findall(r'[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][A-Z]{2}', line)
        if postCode:
            postcodeList.append(postCode[0])
    f.close()

latLongList = []
pc = PostCoder()
count = 0
for postcode in postcodeList:
    try:
        result = pc.get(postcode)
        latLongList.append([result['geo']['lat'], result['geo']['lng']])
        count += 1
        if count % 25 == 0:
            print "Processed %d of %d results" % (count, len(postcodeList))
    except:
        pass
Ejemplo n.º 12
0
 def setUp(self):
     self.pc = PostCoder()
Ejemplo n.º 13
0
class BikeChecker(object):
    """
    The BikeChecker object allows you to access Barclay's Bike 
    availabily.

    It maintains the most recent availabily data, so you don't need 
    to worry about refreshing the data source; simply use the methods 
    provided to access bike data and the :class:`boris.BikeChecker` 
    class will take care of whether to request new bike availabily data.

    :param url: optional web-service url. You _may_ need to change this
                if `TFL`_ change the endpoint in the future.

    .. _TFL: http://www.tfl.gov.uk/

    :returns: a list of dictionaries containing bike station data
    """

    def __init__(self, endpoint=None):
        self.pc = PostCoder()
        self._last_updated = 0
        self._etree = None
        self._stations_lst = []
        self._stations_map = {}
        self.endpoint = endpoint or TFL_DATA_LOC

    def _process_stations(self):
        stations = _parse_feed(self.endpoint)
        self._last_updated = long(stations.get("lastUpdate"))
        self._stations_lst = [dict(_convert(e) for e in st) for st in stations] 
        if not self._stations_lst:
            raise InvalidDataException("No Station data available")
        for station in self._stations_lst:
            self._stations_map[station['name'].lower()] = station

    @property
    def last_updated(self):
        if self._last_updated:
            return datetime.datetime.fromtimestamp(self._last_updated / 1000)

    def all(self, skip_cache=False):
        """
        Gets all available bike data.

        :param skip_cache: optional argument specifying whether to 
                           check the cache (default) or skip it and 
                           explicitly request fresh data.

        :returns: a list of dictionaries describing current status of 
                  bike stations
        """
        now = _time_ms(datetime.datetime.utcnow())
        if skip_cache or now - self._last_updated > CACHE_LIMIT:
            self._process_stations()
        return self._stations_lst

    def get(self, name, fuzzy_matches=0, skip_cache=False):
        """
        Availability information for the station(s) matching `name`.

        `get` allows fuzzy matching of stations based on their name, and 
        returns up to `fuzzy_matches` stations; a station's inclusion 
        is dependent on how close its name is to `name`, based on the 
        result of the `Ratcliff/Obershelp`_ algorithm.

        .. _Ratcliff/Obershelp: http://xlinux.nist.gov/dads/HTML/ratcliffObershelp.html

        :param name: the name of the bike station

        :param fuzzy_matches: optional argument specifying how many 
                              fuzzy matches to return. By default 
                              `fuzzy_matches` is 0 and so `get` will 
                              return an empty list if `name` does not 
                              exactly match a station name.

        :param skip_cache: optional argument specifying whether to 
                           check the cache (default) or skip it and 
                           explicitly request fresh data.

        :returns: a list of station availability data ordered by how 
                  closely the station name matches `name`.
        """
        now = _time_ms(datetime.datetime.utcnow())
        if skip_cache or now - self._last_updated > CACHE_LIMIT:
            self._process_stations()

        name = name.strip().lower()
        station = self._stations_map.get(name, None)
        if station is None:
            if not fuzzy_matches:
                return []
            names = self._stations_map.keys()
            matches = difflib.get_close_matches(name, names, n=fuzzy_matches, 
                                                cutoff=0)
            if matches:
                return [self._stations_map[x] for x in matches]
        else:
            return [station]

    def find_with_geo(self, lat, lng, predicate=None, skip_cache=False):
        """
        Availability information for the nearest station to 
        (`lat`, `lng`). Using `predicate` you can ensure that any 
        returned stations also satisfy certain properties.

        For example, ensuring there are at least five bikes available:

        >>> bc = BikeChecker()
        >>> bike_predicate = lambda x: x['nbBikes'] >= 4 
        >>> bc.find_with_geo(51.49, -0.19, predicate=bike_predicate)
        >>> # results here.

        :param lat: latidude of position

        :param lng: longitude of position

        :parma predicate: optional argument specifying a predicate 
                          which must be satisfied by any station 
                          returned.

        :param skip_cache: optional argument specifying whether to 
                           check the cache (default) or skip it and 
                           explicitly request fresh data.

        :returns: a `dict` containing an availability `dict` for the 
                  nearest station, as well as the distance to that 
                  station in kilometres. If no stations satisfy 
                  `predicate`, and empty `dict` is returned.
        """
        now = _time_ms(datetime.datetime.utcnow())
        if skip_cache or now - self._last_updated > CACHE_LIMIT:
            self._process_stations()

        if predicate is None: 
            predicate = lambda x: True
        if not self._stations_lst: 
            self._process_stations()

        near, near_dist = None, None
        for station in self._stations_lst:
            st_geo = (station['lat'], station['long'])
            station_dist = _haversine((lat, lng), st_geo)
            if predicate(station) and \
               (not near_dist or station_dist < near_dist):
                near, near_dist = station, station_dist
        return {'station': near, 'distance': near_dist} if near else {}

    def find_with_postcode(self, postcode, predicate=None, skip_cache=False):
        """ 
        Availability information for the nearest station to `postcode`.

        Using `predicate` you can ensure that any returned stations 
        also satisfy certain properties, for example that they have a 
        certain number of bikes available.

        :param postcode: the postcode to search

        :parma predicate: optional argument specifying a predicate 
                          which must be satisfied by any station 
                          returned.

        :param skip_cache: optional argument specifying whether to 
                           check the cache (default) or skip it and 
                           explicitly request fresh data.

        :returns: a `dict` containing an availability `dict` for the 
                  nearest station, as well as  the distance to that 
                  station in kilometres. If no stations satisfy 
                  `predicate`, and empty `dict` is returned.
        """
        now = _time_ms(datetime.datetime.utcnow())
        if skip_cache or now - self._last_updated > CACHE_LIMIT:
            self._process_stations()

        info = self.pc.get(postcode)
        if not info:
            raise InvalidPostcodeException("No known postcode %s" % postcode)
        if 'geo' not in info or not set(['lat', 'lng']) <= set(info['geo']):
            raise InvalidDataException("Missing latitude and/or longitude")
        lat, lng = float(info['geo']['lat']), float(info['geo']['lng'])
        return self.find_with_geo(lat, lng, predicate=predicate)
Ejemplo n.º 14
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import responses
import cgi
from postcodes import PostCoder
from geolocation.google_maps import GoogleMaps
g_m = GoogleMaps(api_key='AIzaSyAGwOOqFQPqt6xyG3UPVkh9dXGeNN4_kpg')
#gmaps = googlemaps.Client(key='AIzaSyAGwOOqFQPqt6xyG3UPVkh9dXGeNN4_kpg')
#from osmapi import OsmApi

pc = PostCoder()
#Userpc = raw_input("Enter Postcode")
print "Content-Type: text/html"
print
print """
<html>
  <head>
<style>
body {
  background-color: #d3d3d3;
  margin: 0 15%;
  font-family: Roboto;
  }
h1 {
  background-color: #4169E1;
  text-align: center;
  color: #ffffff;
  font-family: Roboto Condensed;
  font-weight: Bold;
  border-bottom: 20px solid #4169E1;
Ejemplo n.º 15
0
def locationOfPostcode(postcode):
	pc = PostCoder()
	result = pc.get(postcode)
	x = result['geo']['easting']
	y = result['geo']['northing']
	return x,y
Ejemplo n.º 16
0
import os
from postcodes import PostCoder

pc = PostCoder()


def postcode_to_coords(n):  #Find coordinates from postcode
    result = pc.get(n)
    if result is None:
        return None, None
    result = result['geo']
    latitude = result['lat']
    longitude = result['lng']
    return latitude, longitude


for file in os.listdir('.'):
    if file.endswith('.csv'):
        handle = open(file, 'r')
        data = ''
        latitude = longitude = price = 0
        for row in handle:
            split = row.split(',')
            price = split[1]
            postcode = split[3]
            latitude, longitude = postcode_to_coords(postcode)
            print price, latitude, longitude
            if (not latitude is None) and (not longitude is None) and (
                    not price is None):
                data += price + ',' + latitude + ',' + longitude + '\n'
        if data != '':
Ejemplo n.º 17
0
def get_geo(postcode):
    pc = PostCoder()
    result = pc.get(postcode)
    return result['geo']['lat'], result['geo']['lng'], result[
        'administrative']['council']['title']
Ejemplo n.º 18
0
class TestPostCoder(unittest.TestCase):
    def setUp(self):
        self.pc = PostCoder()

    def tearDown(self):
        pass

    def test__check_point(self):
        """ Tests postcodes._check_point """
        f = self.pc._check_point
        self.assertRaises(IllegalPointException, f, -91, 181)
        self.assertRaises(IllegalPointException, f, 90.1, -180.1)
        self.assertIsNone(f(0, 0))

    @patch('postcodes.get')
    def test_get(self, mock):
        """ Tests PostCoder.get """
        self.pc.get("F0 0BA")
        self.pc.get("F00BA")  # should use cache
        self.pc.get("f00b a")  # should use cache
        mock.assert_called_once_with("f00ba")
        self.pc.get("f00b a", skip_cache=True)
        mock.assert_has_calls([call("f00ba"), call("f00ba")])

    @patch('postcodes.get_nearest')
    def test_get_nearest(self, mock):
        """ Tests PostCoder.get_nearest """
        self.pc.get_nearest(0, 0)
        self.pc.get_nearest(0, 0, skip_cache=True)
        mock.assert_has_calls([call(0, 0), call(
            0,
            0,
        )])
        self.assertRaises(IllegalPointException, self.pc.get_nearest, -91, 0)

    @patch('postcodes.get_from_postcode')
    def test_get_from_postcode(self, mock):
        """ Tests PostCoder.get_from_postcode """
        self.pc.get_from_postcode("F0 0BA", 1.1)
        self.pc.get_from_postcode("F00BA", "12.32")
        self.pc.get_from_postcode("F00BA", 12.32)  # use cache
        mock.assert_has_calls([call("f00ba", 1.1), call("f00ba", 12.32)])

        mock.reset_mock()
        self.pc.get_from_postcode("F00BA", 12.32, skip_cache=True)
        mock.assert_called_once_with("f00ba", 12.32)
        self.assertRaises(IllegalDistanceException, self.pc.get_from_postcode,
                          None, -11)

    @patch('postcodes.get_from_geo')
    def test_get_from_geo(self, mock):
        """ Tests PostCoder.get_from_geo """
        self.pc.get_from_geo(-40, 100, 1)
        self.pc.get_from_geo("-30", "20", "12")
        self.pc.get_from_geo(-30, 20, 12)
        mock.assert_has_calls([call(-40, 100, 1), call(-30, 20, 12)])

        mock.reset_mock()
        self.pc.get_from_geo(-30, 20, 12, skip_cache=True)
        mock.assert_called_once_with(-30, 20, 12)
        f = self.pc.get_from_geo
        self.assertRaises(IllegalPointException, f, 91, 0, 0)
        self.assertRaises(IllegalDistanceException, f, -30, 20, -11)