示例#1
0
    def get_record(self, identifier):
        # use code to find a matching OrganisationGeography
        # or OrganisationDivision record

        _, code = split_code(identifier)
        try:
            return OrganisationGeography.objects.all().get(gss=code)
        except OrganisationGeography.DoesNotExist:
            return OrganisationDivision.objects.all().get(
                official_identifier=identifier)
示例#2
0
    def validate_identifier(self, identifier):
        # throw an exception if we're going to have a bad time with this id
        # return True if it looks good
        code_type, code = split_code(identifier)

        if code_type == "unit_id" and re.match(r"^\d+$", code):
            # FIXME before 2021
            error = (
                "Importing boundaries from BoundaryLine against CEDs is "
                "not yet available because unit_id is not a stable identifier")
            raise ValueError(error)

        if code_type == "gss" and re.match(r"^[A-Z][0-9]{8}$", code):
            return True

        raise ValueError(
            "Unknown code type. Expected 'gss:X01000001' or 'unit_id:12345'")
示例#3
0
    def import_div_geography(self, div):
        area_type = div.division_type
        bl = self.open_boundaryline(area_type)
        code_type, code = split_code(div.official_identifier)
        fieldname = "code" if code_type == "gss" else code_type
        geom = self.get_geography_from_feature(bl.get_feature_by_field(fieldname, code))

        try:
            div.geography.geography = geom.ewkb
            div.geography.source = self.source
            div.geography.save()
        except DivisionGeography.DoesNotExist:
            dg = DivisionGeography(
                division_id=div.id, geography=geom.ewkb, source=self.source
            )
            dg.save()
        self.stdout.write("..saved {}".format(str(div)))
示例#4
0
    def filter_records(self, identifier):
        # use code to find matching OrganisationGeography
        # or OrganisationDivision records

        _, code = split_code(identifier)

        orgs = OrganisationGeography.objects.all().filter(gss=code)
        if orgs.exists():
            return orgs

        divs = OrganisationDivision.objects.all().filter(
            official_identifier=identifier)
        if divs.exists():
            return divs

        raise ObjectDoesNotExist(
            ("Couldn't find any OrganisationGeography or OrganisationDivision "
             "objects matching {}".format(identifier)))