def test_get_code_by_uprn_valid(self): """ valid get_code() by UPRN queries """ with self.assertNumQueries(FuzzyInt(0, 4)): addressbase = AddressBaseGeocoder("CC1 1CC") self.assertEqual("B01000001", addressbase.get_code("lad", "00000008")) self.assertIsInstance(addressbase.get_point("00000008"), Point) self.assertEqual("B01000002", addressbase.get_code("lad", "00000009")) self.assertIsInstance(addressbase.get_point("00000009"), Point)
def post_import(self): filepath = os.path.join(self.base_folder_path, "local.2018-05-03/Version 2 - UPRN/uprns.csv") self.csv_delimiter = "," uprns = self.get_data("csv", filepath) print("Updating UPRNs...") for record in uprns: stations = PollingStation.objects.filter( council_id=self.council_id, internal_council_id=record.polling_place_id) if len(stations) == 1: station = stations[0] try: uprn = record.uprn.lstrip("0") g = AddressBaseGeocoder(record.polling_place_postcode) location = g.get_point(uprn) except (ObjectDoesNotExist, AddressBaseException): location = None station.location = location self.check_station_point({ "location": station.location, "council_id": station.council_id }) station.save() else: print("Could not find station id " + self.get_station_hash(record)) print("...done")
def get_station_point(self, record): location = None if (hasattr(record, self.easting_field) and hasattr(record, self.northing_field) and getattr(record, self.easting_field) != "0" and getattr(record, self.easting_field) != "" and getattr(record, self.northing_field) != "0" and getattr(record, self.northing_field) != ""): # if we've got points, use them location = Point( float(getattr(record, self.easting_field)), float(getattr(record, self.northing_field)), srid=27700, ) elif (self.station_uprn_field and getattr(record, self.station_uprn_field).strip()): # if we have a UPRN, try that try: uprn = getattr(record, self.station_uprn_field) uprn = uprn.lstrip("0") g = AddressBaseGeocoder(self.get_station_postcode(record)) location = g.get_point(getattr(record, self.station_uprn_field)) except (ObjectDoesNotExist, AddressBaseException): # otherwise, don't set a point location = None else: # otherwise, don't set a point location = None return location
def get_station_point(self, record): if record.uprn and record.uprn != "0": try: g = AddressBaseGeocoder(record.postcode) return g.get_point(record.uprn) except (ObjectDoesNotExist, AddressBaseException): return self.geocode_from_postcode(record) else: return self.geocode_from_postcode(record)
def test_get_code_by_uprn_no_onsud(self): """ '00000006' is a valid UPRN in AddressBase but not in ONSUD """ with self.assertNumQueries(FuzzyInt(0, 4)): addressbase = AddressBaseGeocoder("BB1 1BB") with self.assertRaises(get_onsud_model().DoesNotExist): result = addressbase.get_code("lad", "00000006") self.assertIsInstance(addressbase.get_point("00000006"), Point)
def test_get_code_by_uprn_invalid_uprn(self): """ 'foo' is not a valid UPRN in our DB """ with self.assertNumQueries(FuzzyInt(0, 4)): addressbase = AddressBaseGeocoder("CC1 1CC") with self.assertRaises(get_address_model().DoesNotExist): result = addressbase.get_code("lad", "foo") with self.assertRaises(get_address_model().DoesNotExist): result = addressbase.get_point("foo")
def get_station_point(self, record): location = None if (hasattr(record, self.easting_field) and hasattr(record, self.northing_field) and getattr(record, self.easting_field) != "0" and getattr(record, self.easting_field) != "" and getattr(record, self.northing_field) != "0" and getattr(record, self.northing_field) != ""): # if we've got points, use them location = Point( float(getattr(record, self.easting_field)), float(getattr(record, self.northing_field)), srid=27700, ) self.logger.log_message( logging.INFO, "using grid reference for station %s", getattr(record, self.station_id_field), ) elif (self.station_uprn_field and getattr(record, self.station_uprn_field).strip()): # if we have a UPRN, try that try: uprn = getattr(record, self.station_uprn_field) uprn = uprn.lstrip("0") g = AddressBaseGeocoder(self.get_station_postcode(record)) location = g.get_point(getattr(record, self.station_uprn_field)) self.logger.log_message( logging.INFO, "using UPRN for station %s", getattr(record, self.station_id_field), ) except (ObjectDoesNotExist, AddressBaseException): # if that fails, fall back to postcode location = self.geocode_from_postcode(record) self.logger.log_message( logging.INFO, "using postcode for station %s", getattr(record, self.station_id_field), ) else: # otherwise, geocode using postcode location = self.geocode_from_postcode(record) self.logger.log_message( logging.INFO, "using postcode for station %s", getattr(record, self.station_id_field), ) return location
def test_get_code_by_uprn_invalid_uprn_for_postcode(self): """ '00000001' is a valid UPRN in our DB, but for a different postcode than the one we constructed with """ with self.assertNumQueries(FuzzyInt(0, 4)): addressbase = AddressBaseGeocoder("CC1 1CC") with self.assertRaises(get_address_model().DoesNotExist): result = addressbase.get_code("lad", "00000001") with self.assertRaises(get_address_model().DoesNotExist): result = addressbase.get_point("00000001")