nursinghomes.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:correctiv-nursinghomes 作者: correctiv 项目源码 文件源码
def assign_brandenburg(self, *args, **options):
        brandenburg_state = State.objects.get(name='Brandenburg')
        excel_file = pd.ExcelFile(options['filename'])
        df = excel_file.parse('Brandenburg')
        assigned_auths = defaultdict(list)
        locations = {}
        for _, row in df.iterrows():
            auth = SupervisionAuthority.objects.get(state=brandenburg_state, name=row['name'])
            locations[auth] = GEOSGeometry('POINT(%f %f)' % (row['lng'], row['lat']), srid=4326)
            assigned_districts = row[u'Landkreis-Zuständigkeit'].splitlines()
            for district_name in assigned_districts:
                districts = District.objects.filter(part_of=brandenburg_state, name=district_name)
                if len(districts) != 1:
                    print(district_name)
                    print(districts)
                else:
                    assigned_auths[districts[0]].append(auth)

        for nursinghome in NursingHome.objects.filter(supervision_authority__isnull=True,
                state=brandenburg_state):
            district = District.objects.get(geom__covers=nursinghome.geo)
            auths = assigned_auths[district]
            if len(auths) == 1:
                nursinghome.supervision_authority = auths[0]
                nursinghome.save()
            else:
                min_distance = None
                best_auth = None
                for auth, point in locations.items():
                    if auth not in auths:
                        continue
                    dist = NursingHome.objects.filter(pk=nursinghome.pk
                            ).annotate(distance=Distance('geo', point))
                    dist = dist[0].distance.m
                    if min_distance is None or dist < min_distance:
                        min_distance = dist
                        best_auth = auth
                nursinghome.supervision_authority = best_auth
                nursinghome.save()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号