def station_record_to_dict(self, record): # format address address = "\n".join([ record.pollingplaceaddress1, record.pollingplaceaddress2, record.pollingplaceaddress3, record.pollingplaceaddress4, record.pollingplaceaddress5, record.pollingplaceaddress6, ]) while "\n\n" in address: address = address.replace("\n\n", "\n").strip() # no points supplied, so attempt to attach them by geocoding if len(record.pollingplaceaddress7) <= 5: location = None else: try: gridref = geocode_point_only(record.pollingplaceaddress7) location = Point(gridref['wgs84_lon'], gridref['wgs84_lat'], srid=4326) except PostcodeError: location = None return { 'internal_council_id': record.pollingdistrictreference, 'postcode' : record.pollingplaceaddress7, 'address' : address, 'location' : location }
def station_record_to_dict(self, record): # format address address = record.pollingstation while "\n\n" in address: address = address.replace("\n\n", "\n").strip() postcode = " ".join(address.split(" ")[-2:]) location = None if float(record.lat) and float(record.lng): location = Point(float(record.lng), float(record.lat), srid=4326) else: # no points supplied, so attempt to attach them by geocoding try: location_data = geocode_point_only(postcode) except PostcodeError: pass if location_data: location = Point(location_data["wgs84_lon"], location_data["wgs84_lat"], srid=4326) return { "internal_council_id": record.stationcode, "postcode": postcode, "address": address, "location": location, }
def station_record_to_dict(self, record): address = record.situation_of_polling_station while "\n\n" in address: address = address.replace("\n\n", "\n").strip() location = None location_data = None if record.postcode_if_available: try: location_data = geocode_point_only(postcode_if_available) except PostcodeError: pass if location_data: location = Point( location_data['wgs84_lon'], location_data['wgs84_lat'], srid=4326) desc = record.description_of_persons_entitled_to_vote district = desc.split('-')[0].strip() return { 'internal_council_id': district, 'polling_district_id': district, 'postcode' : None, 'address' : address, 'location' : location }
def station_record_to_dict(self, record): # format address address = "\n".join([ record.polling_pl, record.pol_addre, ]) while "\n\n" in address: address = address.replace("\n\n", "\n").strip() postcode = " ".join(address.split(' ')[-2:]).strip().split('\n')[-1] location = None location_data = None # no points supplied, so attempt to attach them by geocoding try: location_data = geocode_point_only(postcode) except PostcodeError: pass if location_data: location = Point( location_data['wgs84_lon'], location_data['wgs84_lat'], srid=4326) return { 'internal_council_id': record.poll_ref, 'postcode' : postcode, 'address' : address, 'location' : location }
def station_record_to_dict(self, record): # format address address = "\n".join([ record.pollingplaceaddress1, record.pollingplaceaddress2, record.pollingplaceaddress3, record.pollingplaceaddress4, record.pollingplaceaddress5, record.pollingplaceaddress6, ]) while "\n\n" in address: address = address.replace("\n\n", "\n") """ No grid references were supplied, so attempt to derive a grid ref from postcode Unfortunately some of these postcodes cover quite large areas, so the postcode centroid may be some distance from the polling station :( """ try: gridref = geocode_point_only(record.pollingplaceaddress7) location = Point(gridref['wgs84_lon'], gridref['wgs84_lat'], srid=4326) except PostcodeError: location = None return { 'internal_council_id': record.pollingplaceid, 'postcode' : record.pollingplaceaddress7, 'address' : address, 'location' : location }
def station_record_to_dict(self, record): address = record.pollingstationdetails while "\n\n" in address: address = address.replace("\n\n", "\n").strip() postcode = " ".join(address.split(' ')[:2]) location = None location_data = None if float(record.pollingstationxref) and float(record.pollingstationyref): location = Point( float(record.pollingstationxref), float(record.pollingstationyref), srid=27700) else: # no points supplied, so attempt to attach them by geocoding try: location_data = geocode_point_only(postcode) except PostcodeError: pass if location_data: location = Point( location_data['wgs84_lon'], location_data['wgs84_lat'], srid=4326) return { 'internal_council_id': record.pollingstationid, 'postcode' : postcode, 'address' : address, 'location' : location }
def station_record_to_dict(self, record): # No IDs/codes were supplied, so we'll just assign each # district/station a sequential ID as we parse the file self.station_count = self.station_count + 1 # No grid references were supplied, # so attempt to derive a grid ref from postcode try: gridref = geocode_point_only(record[6]) location = Point(gridref['wgs84_lon'], gridref['wgs84_lat'], srid=4326) except PostcodeError: location = None # format address address_parts = [] for i in range(0,6): if record[i].strip(): address_parts.append(record[i].strip()) address = "\n".join(address_parts) return { 'internal_council_id': self.station_count, 'postcode': record[6], 'address': address, 'location': location }
def station_record_to_dict(self, record): # format address address = "\n".join([ record.premise_name, record.premise_add1, record.premise_add2, record.premise_add3, record.premise_add4, record.premise_add5, ]) while "\n\n" in address: address = address.replace("\n\n", "\n").strip() postcode = record.premise_p_code location = None location_data = None # no points supplied, so attempt to attach them by geocoding if len(postcode) > 5: try: location_data = geocode_point_only(postcode) except PostcodeError: pass if location_data: location = Point( location_data['wgs84_lon'], location_data['wgs84_lat'], srid=4326) return { 'internal_council_id': record.stationuprn, 'postcode' : postcode, 'address' : address, 'location' : location }
def station_record_to_dict(self, record): # format address address = "\n".join( [ record.pollingplaceaddress1, record.pollingplaceaddress2, record.pollingplaceaddress3, record.pollingplaceaddress4, record.pollingplaceaddress5, record.pollingplaceaddress6, ] ) while "\n\n" in address: address = address.replace("\n\n", "\n").strip() # no points supplied, so attempt to attach them by geocoding if len(record.pollingplaceaddress7) <= 5: location = None else: try: gridref = geocode_point_only(record.pollingplaceaddress7) location = Point(gridref["wgs84_lon"], gridref["wgs84_lat"], srid=4326) except PostcodeError: location = None return { "internal_council_id": record.pollingplaceid, "postcode": record.pollingplaceaddress7, "address": address, "location": location, }
def station_record_to_dict(self, record): # format address address = "\n".join([ record.pollingstationaddress_1, record.pollingstationaddress_2, record.pollingstationaddress_3, record.pollingstationaddress_4, record.pollingstationaddress_5, ]) while "\n\n" in address: address = address.replace("\n\n", "\n").strip() postcode = record.pollingstationpostcode if postcode == "n/a": return location_data = None try: location_data = geocode_point_only(postcode) except PostcodeError: pass if location_data: location = Point( location_data['wgs84_lon'], location_data['wgs84_lat'], srid=4326) return { 'internal_council_id': record.pollingstationnumber, 'postcode' : postcode, 'address' : address, 'location' : location }
def station_record_to_dict(self, record): # format address address = "\n".join([ record.polling_place_address_1, record.polling_place_address_2, record.polling_place_address_3, record.polling_place_address_4, record.polling_place_postcode, ]) while "\n\n" in address: address = address.replace("\n\n", "\n").strip() postcode = record.polling_place_postcode.strip() location = None location_data = None # no points supplied, so attempt to attach them by geocoding try: location_data = geocode_point_only(postcode) except PostcodeError: pass if location_data: location = Point( location_data['wgs84_lon'], location_data['wgs84_lat'], srid=4326) return { 'internal_council_id': self._mk_place_id(record), 'postcode' : postcode, 'address' : address, 'location' : location, }
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: gridref = geocode_point_only(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 }
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 }
def station_record_to_dict(self, record): # postcode if record.postcode == 'rd Lane': postcode = '' else: postcode = record.postcode.strip() # point if record.easting and record.northing: if 'connaught' in record.name.lower(): location = Point( 0.004481, 51.564568, srid=4326 ) else: location = Point( float(record.easting), float(record.northing), srid=27700 ) else: # no points supplied, so attempt to attach one by geocoding try: gridref = geocode_point_only(postcode) location = Point(gridref['wgs84_lon'], gridref['wgs84_lat'], srid=4326) except PostcodeError: location = None # format address address = record.address address_parts = address.split(', ') address_parts = [x.strip(' ') for x in address_parts] if address_parts[-1] == postcode: del(address_parts[-1]) address = "%s\n%s" % (record.name, "\n".join(address_parts)) return { 'internal_council_id': record.district, 'postcode' : postcode, 'address' : address, 'location' : location }
def station_record_to_dict(self, record): # no points supplied, so attempt to attach them by geocoding if record.postcode: try: gridref = geocode_point_only(record.postcode) location = Point(gridref['wgs84_lon'], gridref['wgs84_lat'], srid=4326) except PostcodeError: location = None else: location = None return { 'internal_council_id': record.district_code.strip(), 'postcode' : record.postcode.strip(), 'address' : record.address.strip(), 'location' : location }
def station_record_to_dict(self, record): # format address address = "\n".join([f.strip() for f in [ record.name, record.address1, record.address2, record.address3, record.address4, record.address5, ] if f != "NULL"]) while "\n\n" in address: address = address.replace("\n\n", "\n").strip() postcode = record.postcode location = None if int(record.easting) and int(record.northing): location = Point( int(record.easting), int(record.northing), srid=27700) else: # no points supplied, so attempt to attach them by geocoding try: location_data = geocode_point_only(postcode) except PostcodeError: pass if location_data: location = Point( location_data['wgs84_lon'], location_data['wgs84_lat'], srid=4326) return { 'internal_council_id': record.districtreference, 'polling_district_id': record.districtreference, 'postcode' : postcode, 'address' : address, 'location' : location }
def station_record_to_dict(self, record): # Don't import the Carmarthenshire polling stations # We don't have the address data to go with them :( if record.local_auth == 'W06000010': return None # format address address_parts = [] if record.bldg_name: address_parts.append(record.bldg_name) if record.bldg_add1: address_parts.append(record.bldg_add1) if record.bldg_add2: address_parts.append(record.bldg_add2) if record.bldg_add3: address_parts.append(record.bldg_add3) if record.bldg_add4: address_parts.append(record.bldg_add4) if record.bldg_add5: address_parts.append(record.bldg_add5) address = "\n".join(address_parts) # convert postcode to upper case postcode = record.bldg_pcode.upper() if record.kml_lng and record.kml_lat: location = Point(float(record.kml_lng), float(record.kml_lat), srid=4326) else: # there is one station we don't have a point for - geocode it try: gridref = geocode_point_only(postcode) location = Point(gridref['wgs84_lon'], gridref['wgs84_lat'], srid=4326) except PostcodeError: location = None return { 'internal_council_id': record.district, 'postcode' : postcode, 'address' : address, 'location' : location }
def station_record_to_dict(self, record): if not record.polling_na: return # format address address = record.station_na while "\n\n" in address: address = address.replace("\n\n", "\n").strip() postcode = " ".join(address.split(' ')[-2:]).strip().split('\n')[-1] location = None if float(record.polling_station_x) and float(record.polling_station_y): if "Shapla Primary School" in address: location = Point( -0.066990, 51.510020, srid=4326 ) else: location = Point( float(record.polling_station_x), float(record.polling_station_y), srid=27700) else: # no points supplied, so attempt to attach them by geocoding try: location_data = geocode_point_only(postcode) except PostcodeError: pass if location_data: location = Point( location_data['wgs84_lon'], location_data['wgs84_lat'], srid=4326) return { 'internal_council_id': record.code, 'postcode' : postcode, 'address' : address, 'location' : location }
def station_record_to_dict(self, record): # format address address = "\n".join([ record.pollingplaceaddress1, record.pollingplaceaddress2, record.pollingplaceaddress3, record.pollingplaceaddress4, record.pollingplaceaddress5, record.pollingplaceaddress6, ]) while "\n\n" in address: address = address.replace("\n\n", "\n").strip() location = None location_data = None if int(record.pollingplaceeasting) and int(record.pollingplacenorthing): location = Point( float(record.pollingplaceeasting), float(record.pollingplacenorthing), srid=27700) else: # no points supplied, so attempt to attach them by geocoding try: location_data = geocode_point_only(record.pollingplaceaddress7) except PostcodeError: pass if location_data: location = Point( location_data['wgs84_lon'], location_data['wgs84_lat'], srid=4326) return { 'internal_council_id': record.pollingplaceid, 'postcode' : record.pollingplaceaddress7, 'address' : address, 'location' : location }
def station_record_to_dict(self, record): # format address address = "\n".join([ record.ps_address_1, record.ps_address_2, record.ps_address_3, record.ps_address_4, ]) while "\n\n" in address: address = address.replace("\n\n", "\n").strip() postcode = record.post_code location = None if record.northings and record.eastings: location = Point( float(record.eastings), float(record.northings), srid=27700) else: # no points supplied, so attempt to attach them by geocoding try: location_data = geocode_point_only(postcode) except PostcodeError: pass if location_data: location = Point( location_data['wgs84_lon'], location_data['wgs84_lat'], srid=4326) return { 'internal_council_id': record.poll_stn_number, 'postcode' : postcode, 'address' : address, 'location' : location }
def geocode_from_postcode(self, record): try: location_data = geocode_point_only(record.postcode) return location_data.centroid except PostcodeError: return None