示例#1
0
    def station_record_to_dict(self, record):

        # ignore proposed stations
        if record[0] == 'Current':

            address = self.format_address(record)
            postcode = record[9].strip()

            # if postcode is missing, attempt to attach it
            if not postcode:
                gwrapper = GoogleGeocodingApiWrapper(address, self.council_id,
                                                     'DIS')
                try:
                    postcode = gwrapper.address_to_postcode()
                except PostcodeNotFoundException:
                    postcode = ''

            return {
                'internal_council_id': record[15],
                'postcode': postcode,
                'address': address,
                'polling_district_id': record[15]
            }

        else:
            return None
    def station_record_to_dict(self, record):

        # ignore proposed stations
        if record[0] == 'Current':

            address = self.format_address(record)
            postcode = record[9].strip()

            # if postcode is missing, attempt to attach it
            if not postcode:
                gwrapper = GoogleGeocodingApiWrapper(address, self.council_id, 'DIS')
                try:
                    postcode = gwrapper.address_to_postcode()
                except PostcodeNotFoundException:
                    postcode = ''

            return {
                'internal_council_id': record[15],
                'postcode'           : postcode,
                'address'            : address,
                'polling_district_id': record[15]
            }

        else:
            return None
    def station_record_to_dict(self, record):
        location = Point(float(record.easting), float(record.northing), srid=self.get_srid())

        # assemble address and extract postcode if present
        thoroughfare_parts = record.thoroughfare_name.strip().split(', ')
        address_tail = thoroughfare_parts[-1]
        if len(address_tail) >= 6 and len(address_tail) <= 8 and ' ' in address_tail and address_tail != 'Ash Vale':
            address = "\n".join(record.pollingplace.strip().split(', ') + thoroughfare_parts[:-1])
            postcode = address_tail
        else:
            address = "\n".join(record.pollingplace.strip().split(', ') + thoroughfare_parts)

            # attempt to attach postcode if missing
            gwrapper = GoogleGeocodingApiWrapper(address + ', Guildford, UK')
            try:
                postcode = gwrapper.address_to_postcode()
            except PostcodeNotFoundException:
                postcode = ''

        return {
            'internal_council_id': record.register.strip(),
            'postcode':            postcode,
            'address':             address,
            'location':            location
        }
    def station_record_to_dict(self, record):
        location = Point(float(record.east),
                         float(record.north),
                         srid=self.get_srid())

        address = "\n".join(
            [record.addressl1, record.addressl2, record.addressl3])
        if address[-1:] == '\n':
            address = address[:-1]

        # attempt to attach postcodes
        gwrapper = GoogleGeocodingApiWrapper(address + ", Plymouth, UK",
                                             self.council_id, 'UTA')
        try:
            postcode = gwrapper.address_to_postcode()
        except PostcodeNotFoundException:
            postcode = ''

        # These district codes don't exist in the district boundaries
        # so don't import stations for them
        if (record.pdref == 'BC' or record.pdref == 'BH'):
            return None
        else:
            return {
                'internal_council_id': record.statno,
                'postcode': postcode,
                'address': address,
                'location': location,
                'polling_district_id': record.pdref
            }
示例#5
0
    def station_record_to_dict(self, record):

        address = "\n".join([record[1]] + record[2].split(', '))

        # remove strange unicode char and replace with space
        postcode = record[3].replace('\xa0', ' ')
        postcode_parts = postcode.split(' ')

        # if postcode is invalid, attempt to fix it
        if len(postcode_parts[1]) == 2:
            gwrapper = GoogleGeocodingApiWrapper(address, self.council_id, 'DIS')
            try:
                suggested_postcode = gwrapper.address_to_postcode()
                """
                In this case we have a partial postcode we can use to verify, so:
                If the first 6 characters of the suggested postcode match
                with the partial postcode we already have then accept the suggestion
                otherwise, discard both and insert blank postcode
                """
                if suggested_postcode[:-1] == postcode:
                    postcode = suggested_postcode
                else:
                    postcode = ''
            except PostcodeNotFoundException:
                postcode = ''

        return {
            'internal_council_id': record[0],
            'postcode'           : postcode,
            'address'            : address
        }
    def station_record_to_dict(self, record):
        location = Point(float(record.east), float(record.north), srid=self.get_srid())

        address = "\n".join([record.addressl1, record.addressl2, record.addressl3])
        if address[-1:] == '\n':
            address = address[:-1]

        # attempt to attach postcodes
        gwrapper = GoogleGeocodingApiWrapper(address + ", Plymouth, UK", self.council_id, 'UTA')
        try:
            postcode = gwrapper.address_to_postcode()
        except PostcodeNotFoundException:
            postcode = ''

        # These district codes don't exist in the district boundaries
        # so don't import stations for them
        if (record.pdref == 'BC' or record.pdref == 'BH'):
            return None
        else:
            return {
                'internal_council_id': record.statno,
                'postcode':            postcode,
                'address':             address,
                'location':            location,
                'polling_district_id': record.pdref
            }
    def station_record_to_dict(self, record):
        location = Point(float(record.easting),
                         float(record.northing),
                         srid=self.get_srid())

        # assemble address and extract postcode if present
        thoroughfare_parts = record.thoroughfare_name.strip().split(', ')
        address_tail = thoroughfare_parts[-1]
        if len(address_tail) >= 6 and len(
                address_tail
        ) <= 8 and ' ' in address_tail and address_tail != 'Ash Vale':
            address = "\n".join(record.pollingplace.strip().split(', ') +
                                thoroughfare_parts[:-1])
            postcode = address_tail
        else:
            address = "\n".join(record.pollingplace.strip().split(', ') +
                                thoroughfare_parts)

            # attempt to attach postcode if missing
            gwrapper = GoogleGeocodingApiWrapper(address + ', Guildford, UK',
                                                 self.council_id, 'DIS')
            try:
                postcode = gwrapper.address_to_postcode()
            except PostcodeNotFoundException:
                postcode = ''

        return {
            'internal_council_id': record.register.strip(),
            'postcode': postcode,
            'address': address,
            'location': location
        }
示例#8
0
    def station_record_to_dicts(self, record):

        address = "\n".join([record[1]] + record[2].split(', '))

        # remove strange unicode char and replace with space
        postcode = record[3].replace('\xa0', ' ')
        postcode_parts = postcode.split(' ')

        # if postcode is invalid, attempt to fix it
        if len(postcode_parts[1]) == 2:
            gwrapper = GoogleGeocodingApiWrapper(address, self.council_id,
                                                 'DIS')
            try:
                suggested_postcode = gwrapper.address_to_postcode()
                """
                In this case we have a partial postcode we can use to verify, so:
                If the first 6 characters of the suggested postcode match
                with the partial postcode we already have then accept the suggestion
                otherwise, discard both and insert blank postcode
                """
                if suggested_postcode[:-1] == postcode:
                    postcode = suggested_postcode
                else:
                    postcode = ''
            except PostcodeNotFoundException:
                postcode = ''
        """
        In this data, sometimes a single polling station serves several
        districts. For simplicity, if record[4] is something like "AA4, AD3" 
        return the same polling station address/point twice with 2 different IDs
        """
        internal_ids = record[4].split(", ")
        if (len(internal_ids) == 2):
            return [{
                'internal_council_id': internal_ids[0],
                'postcode': postcode,
                'address': address
            }, {
                'internal_council_id': internal_ids[1],
                'postcode': postcode,
                'address': address
            }]
        else:
            return [{
                'internal_council_id': internal_ids[0],
                'postcode': postcode,
                'address': address
            }]
示例#9
0
    def station_record_to_dict(self, record):

        # format address
        address = "\n".join([
            record.address1,
            record.address2,
            record.address3,
            record.address4,
            record.address5
        ])
        while "\n\n" in address:
            address = address.replace("\n\n", "\n")
        # remove trailing "\n" if present
        if address[-1:] == '\n':
            address = address[:-1]


        # attempt to attach postcode if missing
        postcode = record.postcode
        if not postcode:
            gwrapper = GoogleGeocodingApiWrapper(address, self.council_id, 'UTA')
            try:
                postcode = gwrapper.address_to_postcode()
            except PostcodeNotFoundException:
                postcode = ''


        """
        No grid references were supplied, so attempt to
        derive a grid ref from postcode if we have that
        """
        sleep(1.3) # ensure we don't hit mapit's usage limit
        if postcode:
            try:
                gridref = geocode(postcode)
                location = Point(gridref['wgs84_lon'], gridref['wgs84_lat'], srid=4326)
            except PostcodeError:
                location = None
        else:
            location = None


        return {
            'internal_council_id': record.polling_district,
            'postcode'           : postcode,
            'address'            : address,
            'location'           : location
        }
示例#10
0
    def station_record_to_dict(self, record):

        # format address
        address = "\n".join([
            record.address1,
            record.address2,
            record.address3,
            record.address4,
            record.address5
        ])
        while "\n\n" in address:
            address = address.replace("\n\n", "\n")
        # remove trailing "\n" if present
        if address[-1:] == '\n':
            address = address[:-1]


        # attempt to attach postcode if missing
        postcode = record.postcode
        if not postcode:
            gwrapper = GoogleGeocodingApiWrapper(address, self.council_id, 'UTA')
            try:
                postcode = gwrapper.address_to_postcode()
            except PostcodeNotFoundException:
                postcode = ''


        """
        No grid references were supplied, so attempt to
        derive a grid ref from postcode if we have that
        """
        sleep(1.3) # ensure we don't hit mapit's usage limit
        if postcode:
            try:
                gridref = geocode(postcode)
                location = Point(gridref['wgs84_lon'], gridref['wgs84_lat'], srid=4326)
            except KeyError:
                location = None
        else:
            location = None


        return {
            'internal_council_id': record.polling_district,
            'postcode'           : postcode,
            'address'            : address,
            'location'           : location
        }
    def station_record_to_dicts(self, record):

        address = "\n".join([record[1]] + record[2].split(', '))

        # remove strange unicode char and replace with space
        postcode = record[3].replace('\xa0', ' ')
        postcode_parts = postcode.split(' ')

        # if postcode is invalid, attempt to fix it
        if len(postcode_parts[1]) == 2:
            gwrapper = GoogleGeocodingApiWrapper(address, self.council_id, 'DIS')
            try:
                suggested_postcode = gwrapper.address_to_postcode()
                """
                In this case we have a partial postcode we can use to verify, so:
                If the first 6 characters of the suggested postcode match
                with the partial postcode we already have then accept the suggestion
                otherwise, discard both and insert blank postcode
                """
                if suggested_postcode[:-1] == postcode:
                    postcode = suggested_postcode
                else:
                    postcode = ''
            except PostcodeNotFoundException:
                postcode = ''

        """
        In this data, sometimes a single polling station serves several
        districts. For simplicity, if record[4] is something like "AA4, AD3" 
        return the same polling station address/point twice with 2 different IDs
        """
        internal_ids = record[4].split(", ")
        if (len(internal_ids) == 2):
            return [
                { 'internal_council_id': internal_ids[0],
                  'postcode'           : postcode,
                  'address'            : address },
                { 'internal_council_id': internal_ids[1],
                  'postcode'           : postcode,
                  'address'            : address }
            ]
        else:
            return [{
                'internal_council_id': internal_ids[0],
                'postcode'           : postcode,
                'address'            : address
            }]
    def station_record_to_dict(self, record):
        unique_station_id = "-".join((
            record.polling_district,
            record.polling_station,
            record.polling_station_postcode,
        ))

        if unique_station_id in self.known_stations:
            return None

        address = record.polling_station
        postcode = record.polling_station_postcode

        if not postcode:
            gwrapper = GoogleGeocodingApiWrapper(address, self.council_id, 'UTA')
            try:
                postcode = gwrapper.address_to_postcode()
            except PostcodeNotFoundException:
                postcode = ''


        """
        No grid references were supplied, so attempt to
        derive a grid ref from postcode if we have that
        """
        if postcode:
            try:
                sleep(1.3) # ensure we don't hit mapit's usage limit
                gridref = geocode(postcode)
                location = Point(
                    gridref['wgs84_lon'],
                    gridref['wgs84_lat'],
                    srid=4326
                )
            except PostcodeError:
                location = None
        else:
            location = None

        self.known_stations.add(unique_station_id)

        return {
            'internal_council_id': record.polling_district,
            'postcode'           : postcode,
            'address'            : address,
            'location'           : location
        }
示例#13
0
    def station_record_to_dict(self, record):

        # format address
        address = "\n".join(
            [
                record.address1,
                record.address2,
                record.address3,
                record.address4,
                record.address5,
            ]
        )
        while "\n\n" in address:
            address = address.replace("\n\n", "\n")
        # remove trailing "\n" if present
        if address[-1:] == "\n":
            address = address[:-1]

        # attempt to attach postcode if missing
        postcode = record.postcode
        if not postcode:
            gwrapper = GoogleGeocodingApiWrapper(address, self.council_id, "UTA")
            try:
                postcode = gwrapper.address_to_postcode()
            except PostcodeNotFoundException:
                postcode = ""

        """
        No grid references were supplied, so attempt to
        derive a grid ref from postcode if we have that
        """
        if postcode:
            try:
                location_data = geocode_point_only(postcode)
                location = location_data.centroid
            except PostcodeError:
                location = None
        else:
            location = None

        return {
            "internal_council_id": record.polling_district,
            "postcode": postcode,
            "address": address,
            "location": location,
        }
    def station_record_to_dict(self, record):
        unique_station_id = "-".join((
            record.polling_district,
            record.polling_station,
            record.polling_station_postcode,
        ))

        if unique_station_id in self.known_stations:
            return None

        address = record.polling_station
        postcode = record.polling_station_postcode

        if not postcode:
            gwrapper = GoogleGeocodingApiWrapper(address, self.council_id,
                                                 'UTA')
            try:
                postcode = gwrapper.address_to_postcode()
            except PostcodeNotFoundException:
                postcode = ''
        """
        No grid references were supplied, so attempt to
        derive a grid ref from postcode if we have that
        """
        if postcode:
            try:
                sleep(1.3)  # ensure we don't hit mapit's usage limit
                gridref = geocode(postcode)
                location = Point(gridref['wgs84_lon'],
                                 gridref['wgs84_lat'],
                                 srid=4326)
            except PostcodeError:
                location = None
        else:
            location = None

        self.known_stations.add(unique_station_id)

        return {
            'internal_council_id': record.polling_district,
            'postcode': postcode,
            'address': address,
            'location': location
        }
示例#15
0
    def station_record_to_dict(self, record):
        location = Point(float(record.east), float(record.north), srid=self.get_srid())

        address = "\n".join([record.addressl1, record.addressl2, record.addressl3])
        if address[-1:] == '\n':
            address = address[:-1]

        # attempt to attach postcodes
        gwrapper = GoogleGeocodingApiWrapper(address + ", Plymouth, UK", self.council_id, 'UTA')
        try:
            postcode = gwrapper.address_to_postcode()
        except PostcodeNotFoundException:
            postcode = ''

        return {
            'internal_council_id': record.statno,
            'postcode':            postcode,
            'address':             address,
            'location':            location
        }
    def station_record_to_dict(self, record):
        unique_station_id = "-".join((record.polling_district, record.polling_station, record.polling_station_postcode))

        if unique_station_id in self.known_stations:
            return None

        address = record.polling_station
        postcode = record.polling_station_postcode

        if not postcode:
            gwrapper = GoogleGeocodingApiWrapper(address, self.council_id, "UTA")
            try:
                postcode = gwrapper.address_to_postcode()
            except PostcodeNotFoundException:
                postcode = ""

        """
        No grid references were supplied, so attempt to
        derive a grid ref from postcode if we have that
        """
        if postcode:
            try:
                gridref = geocode_point_only(postcode)
                location = Point(gridref["wgs84_lon"], gridref["wgs84_lat"], srid=4326)
            except PostcodeError:
                location = None
        else:
            location = None

        self.known_stations.add(unique_station_id)

        return {
            "internal_council_id": record.polling_district,
            "postcode": postcode,
            "address": address,
            "location": location,
        }