# -*- coding: utf-8 -*-
# Generated by Django 1.10.7 on 2017-05-12 20:59
from __future__ import unicode_literals

from django.db import migrations, models
from shapely.ops import unary_union


def set_space_outside(apps, schema_editor):
    Section = apps.get_model('mapdata', 'Section')
    for section in Section.objects.all():
        building_geometries = unary_union(tuple(building.geometry for building in section.buildings.all()))
        for space in section.spaces.all():
            if space.geometry.intersection(building_geometries).area / space.geometry.area < 0.5:
                space.outside = True
                space.save()


class Migration(migrations.Migration):

    dependencies = [
        ('mapdata', '0002_locationredirect'),
    ]

    operations = [
        migrations.AddField(
            model_name='space',
            name='outside',
            field=models.BooleanField(default=False, verbose_name='is outside of building'),
        ),
        migrations.RunPython(set_space_outside),
    ]
