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 sleep(1.3) # ensure we don't hit mapit's usage limit if len(record.pollingplaceaddress7) <= 5: location = None else: try: gridref = geocode(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): # 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 sleep(1.3) # ensure we don't hit mapit's usage limit try: gridref = geocode(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): # postcode if record.postcode == 'rd Lane': postcode = '' else: postcode = record.postcode.strip() # point if record.easting and record.northing: location = Point(float(record.easting), float(record.northing), srid=27700) else: # no points supplied, so attempt to attach one by geocoding sleep(1.3) # ensure we don't hit mapit's usage limit try: gridref = geocode(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 get_type_from_mapit(self, council_type): req = requests.get('%sareas/%s' % (constants.MAPIT_URL, council_type)) for mapit_id, council in list(req.json().items()): council_id = council['codes'].get('gss') if not council_id: council_id = council['codes'].get('ons') print(council_id) contact_info = { 'name': council['name'], } if council_type != "LGD": contact_info.update( self.get_contact_info_from_gov_uk(council_id)) council, created = Council.objects.update_or_create( pk=council_id, mapit_id=mapit_id, council_type=council_type, defaults=contact_info, ) if not council.area: council.area = self.get_wkt_from_mapit(mapit_id) council.save() time.sleep(1) if not council.location: print(council.postcode) try: l = geocode(council.postcode) except: continue time.sleep(1) council.location = Point(l['wgs84_lon'], l['wgs84_lat']) council.save()
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 :( """ sleep(1.3) # ensure we don't hit mapit's usage limit try: gridref = geocode(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 retrieve(self, requst, pk=None, format=None): postcode = pk.replace(' ', '') ret = {} ret['polling_station_known'] = False polling_station = None try: l = geocode(pk) except (PostcodeError, RateLimitError) as e: ret['error'] = e.args[0] return Response(ret) location = Point(l['wgs84_lon'], l['wgs84_lat']) ret['postcode_location'] = PointField().to_representation( location) ret['council'] = CouncilSerializer(Council.objects.get( area__covers=location)).data rh = RoutingHelper(postcode) if rh.route_type == "multiple_addresses": ret['addresses'] = [ ResidentialAddressSerializer(address, context={ 'request': self.request} ).data for address in rh.addresses ] if rh.route_type == "single_address": polling_station = rh.addresses[0] if rh.route_type == "postcode": polling_station = self.get_queryset( location=location, council=ret['council'], ) if polling_station: ret['polling_station_known'] = True ret['polling_station'] = PollingStationSerializer( polling_station, context={'request': self.request}).data if not ret['polling_station_known']: finder = CustomFinder.objects.get_custom_finder(l['gss_codes'], postcode) if finder and finder.base_url: ret['custom_finder'] = {} ret['custom_finder']['base_url'] = finder.base_url ret['custom_finder']['can_pass_postcode'] = finder.can_pass_postcode ret['custom_finder']['encoded_postcode'] = finder.encoded_postcode return Response(ret)
def test_multiple_councils(self): """ We find records for the given postcode in the AddressBase table There are corresponding records in the ONSAD for the UPRNs we found The UPRNs described by this postcode map to more than one local authority Exception of class MultipleCouncilsException should be thrown """ exception_thrown = False try: result = geocode('CC11CC') except MultipleCouncilsException: exception_thrown = True self.assertTrue(exception_thrown)
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 }
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_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 }
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 }
def station_record_to_dict(self, record): # no points supplied, so attempt to attach them by geocoding sleep(1.3) # ensure we don't hit mapit's usage limit if record.postcode: try: gridref = geocode(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): # 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 sleep(1.3) # ensure we don't hit mapit's usage limit try: gridref = geocode(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 get_type_from_mapit(self, council_type, nosleep): req = requests.get('%sareas/%s' % (settings.MAPIT_URL, council_type), headers=self.headers) # Sort here so the fixtures work as expected in tests areas = sorted(req.json().items(), key=lambda data: int(data[0])) for mapit_id, council in areas: council_id = council['codes'].get('gss') if not council_id: council_id = council['codes'].get('ons') print(council_id) defaults = self.get_contact_info_from_yvm(council_id) defaults['council_type'] = council_type defaults['mapit_id'] = mapit_id council, created = Council.objects.update_or_create( pk=council_id, defaults=defaults, ) # Call _save here to ensure it gets written to all databases self._save_council(council) if not council.area: council.area = self.get_wkt_from_mapit(mapit_id) self._save_council(council) if not nosleep or not self.headers: time.sleep(1) if not council.location: print(council.postcode) try: l = geocode(council.postcode) except: continue if not nosleep or not self.headers: time.sleep(1) council.location = Point(l['wgs84_lon'], l['wgs84_lat']) self._save_council(council)
def get_type_from_mapit(self, council_type): req = requests.get('%sareas/%s' % (constants.MAPIT_URL, council_type)) for mapit_id, council in list(req.json().items()): council_id = council['codes'].get('gss') if not council_id: council_id = council['codes'].get('ons') print(council_id) defaults = { 'name': council['name'], } if defaults != "LGD": defaults.update( self.get_contact_info_from_gov_uk(council_id)) defaults['council_type'] = council_type defaults['mapit_id']= mapit_id council, created = Council.objects.update_or_create( pk=council_id, defaults=defaults, ) # Call _save here to ensure it gets written to all databases self._save_council(council) if not council.area: council.area = self.get_wkt_from_mapit(mapit_id) self._save_council(council) time.sleep(1) if not council.location: print(council.postcode) try: l = geocode(council.postcode) except: continue time.sleep(1) council.location = Point(l['wgs84_lon'], l['wgs84_lat']) self._save_council(council)
def get_type_from_mapit(self, council_type): req = requests.get('%sareas/%s' % (settings.MAPIT_URL, council_type)) for mapit_id, council in list(req.json().items()): council_id = council['codes'].get('gss') if not council_id: council_id = council['codes'].get('ons') print(council_id) defaults = { 'name': council['name'], } if defaults != "LGD": defaults.update( self.get_contact_info_from_gov_uk(council_id)) defaults['council_type'] = council_type defaults['mapit_id']= mapit_id council, created = Council.objects.update_or_create( pk=council_id, defaults=defaults, ) # Call _save here to ensure it gets written to all databases self._save_council(council) if not council.area: council.area = self.get_wkt_from_mapit(mapit_id) self._save_council(council) time.sleep(1) if not council.location: print(council.postcode) try: l = geocode(council.postcode) except: continue time.sleep(1) council.location = Point(l['wgs84_lon'], l['wgs84_lat']) self._save_council(council)
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 :( """ sleep(1.3) # ensure we don't hit mapit's usage limit try: gridref = geocode(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 }