def compute(self, **kwargs):
        """
        Determine if a district is contiguous.

        @keyword district: A L{District} whose contiguity should be 
            evaluated.
        @keyword plan: A L{Plan} whose set of districts should be 
            evaluated for contiguity.
        @keyword version: Optional. The version of the plan, defaults to 
            the most recent version.
        """
        districts = []

        if 'district' in kwargs:
            districts = [kwargs['district']]

        elif 'plan' in kwargs:
            plan = kwargs['plan']
            version = kwargs['version'] if 'version' in kwargs else plan.version
            districts = plan.get_districts_at_version(version, include_geom=True)

        else:
            return

        if 'allow_single_point' in self.arg_dict:
            allow_single = int(self.arg_dict['allow_single_point'][1]) == 1
        else:
            allow_single = False

        count = 0
        for district in districts:
            if len(district.geom) == 1 and district.district_id != 0:
                count += 1
            else:
                if district.district_id == 0:
                    # for plan summations of contiguity, ignore the unassigned in the tally
                    if 'plan' in kwargs:
                        continue
                    elif len(district.geom) == 1:
                        count += 1
                        continue

                # if the district has no geometry, i.e. an empty unassigned district,
                # treat it as contiguous
                if len(district.geom) == 0:
                    count += 1
                    continue
                    
                # obtain the contiguity overrides that need to be applied
                overrides = district.get_contiguity_overrides()

                if allow_single or len(overrides) > 0:
                    # create a running union of of polygons that are linked, seeded with the first.
                    # loop through remaining polygons and add any that either touch the union,
                    # or do so virtually with a contiguity override. repeat until either:
                    #   - the remaining list is empty: contiguous
                    #   - no matches were found in a pass: discontiguous
                    union = district.geom[0]
                    remaining = district.geom[1:]
                    contiguous = True
    
                    while (len(remaining) > 0):
                        if len(district.geom) == 0:
                            continue

                        match_in_pass = False
                        for geom in remaining:
                            linked = False
                            if allow_single and geom.touches(union):
                                linked = True
                            else:
                                for override in overrides:
                                    o = override.override_geounit.geom
                                    c = override.connect_to_geounit.geom
                                    if (geom.contains(o) and union.contains(c)) or (geom.contains(c) and union.contains(o)):
                                        linked = True
                                        overrides.remove(override)
                                        break

                            if linked:
                                remaining.remove(geom)
                                union = geom.union(union)
                                match_in_pass = True
                                    
                        if not match_in_pass:
                            contiguous = False
                            break
    
                    if contiguous:
                        count += 1

        self.result = { 'value': count }
        try:
            target = self.get_value('target')
            if target != None:
                self.result = { 'value':_('%(value)d (of %(target)s)') % {'value': count, 'target': target} }
        except:
            pass
dataset/ETHPy150Open PublicMapping/DistrictBuilder/django/publicmapping/redistricting/calculators.py/Contiguity.compute/original