def attachOrphanRedistrictingGroupsToClosestNeighbor(neighborsToAttach): tqdm.write('\n') tqdm.write( '*** Attaching Orphan Redistricting Groups To Closest Neighbor ***') contiguousRegions = findContiguousGroupsOfGraphObjects(neighborsToAttach) while len(contiguousRegions) > 1: tqdm.write(' *** Found {0} Isolated Regions ***'.format( len(contiguousRegions))) isolatedRegion = contiguousRegions[0] closestRegion = findClosestGeometry(originGeometry=isolatedRegion, otherGeometries=[ region for region in contiguousRegions if region is not isolatedRegion ]) closestGroupInIsolatedRegion = findClosestGeometry( originGeometry=closestRegion, otherGeometries=isolatedRegion) closestGroupInClosestRegion = findClosestGeometry( originGeometry=isolatedRegion, otherGeometries=closestRegion) closestGroupInIsolatedRegion.addNeighbors( neighbors=[closestGroupInClosestRegion]) closestGroupInClosestRegion.addNeighbors( neighbors=[closestGroupInIsolatedRegion]) contiguousRegions = findContiguousGroupsOfGraphObjects( neighborsToAttach) tqdm.write(' *** No More Orphaned Redistricting Groups ***')
def attachOrphanBlocksToClosestNeighbor(self): orphanBlocks = self.findOrphanBlocks() for orphanBlock in orphanBlocks: otherBlocks = [block for block in self.children if block is not orphanBlock] closestBlock = findClosestGeometry(originGeometry=self, otherGeometries=otherBlocks) orphanBlock.addNeighbors(neighbors=[closestBlock]) closestBlock.addNeighbors(neighbors=[orphanBlock])
def reorganizeAtomicBlockBetweenRedistrictingGroups(redistrictingGroups): for redistrictingGroup in redistrictingGroups: for borderBlock in redistrictingGroup.borderChildren: borderBlock.removeNonIntersectingNeighbors() atomicBlockGroupDict = {} for redistrictingGroup in redistrictingGroups: atomicBlockGroupDict[redistrictingGroup.graphId] = redistrictingGroup.children.copy() atomicBlockGroups = atomicBlockGroupDict.values() for atomicBlockGroup in atomicBlockGroups: contiguousRegions = findContiguousGroupsOfGraphObjects(atomicBlockGroup) while len(contiguousRegions) > 1: smallestContiguousRegion = min(contiguousRegions, key=lambda contiguousRegion: len(contiguousRegion)) smallestContiguousRegionPolygon = polygonFromMultipleGeometries(smallestContiguousRegion) otherRegion = None otherSplitChildrenList = [x for x in atomicBlockGroups if x is not atomicBlockGroup] for otherSplitChildren in otherSplitChildrenList: otherSplitChildrenPolygon = polygonFromMultipleGeometries(otherSplitChildren) if intersectingPolygons(smallestContiguousRegionPolygon, otherSplitChildrenPolygon): otherRegion = otherSplitChildren break if otherRegion is None: allBlocksInOtherSplits = [block for blockList in otherSplitChildrenList for block in blockList] closestBlock = findClosestGeometry(smallestContiguousRegionPolygon, allBlocksInOtherSplits) closestSplit = next((blockList for blockList in otherSplitChildrenList if closestBlock in blockList), None) otherRegion = closestSplit for childBlock in smallestContiguousRegion: atomicBlockGroup.remove(childBlock) childBlock.removeNeighborConnections() otherRegion.append(childBlock) assignNeighborBlocksFromCandidateBlocks(block=childBlock, candidateBlocks=otherRegion) contiguousRegions = findContiguousGroupsOfGraphObjects(atomicBlockGroup) for key, value in atomicBlockGroupDict.items(): groupWithId = next((redistrictingGroup for redistrictingGroup in redistrictingGroups if redistrictingGroup.graphId == key), None) groupWithId.children = value for redistrictingGroup in redistrictingGroups: redistrictingGroup.attachOrphanBlocksToClosestNeighbor() return redistrictingGroups