Skip to content

Commit 1c9f06a

Browse files
committed
add command to correct duplicate areas
1 parent 717913b commit 1c9f06a

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from django.core.management.base import BaseCommand
2+
from apps.catastro.models import AdministrativeArea
3+
from django.contrib.postgres.aggregates import ArrayAgg
4+
from django.db.models import Count
5+
6+
7+
class Command(BaseCommand):
8+
help = 'remove duplicated areas'
9+
10+
"""
11+
select
12+
osm_type,
13+
osm_id,
14+
count(*),
15+
array_agg(id),
16+
array_agg(st_area(geometry)),
17+
array_agg(name)
18+
from
19+
catastro_administrativearea
20+
group by
21+
osm_type,
22+
osm_id
23+
having
24+
count(*)>1
25+
;
26+
"""
27+
28+
def handle(self, *args, **options):
29+
aa = AdministrativeArea.objects.values('osm_type', 'osm_id').order_by().annotate(ids=ArrayAgg('id'), count_id=Count('id')).filter(count_id__gt=1)
30+
31+
total = len(aa)
32+
i = 0
33+
for a in aa:
34+
i = i + 1
35+
print(f'[{i*100/total:2f}] {i}/{total}: ,', end='', flush=True)
36+
a0 = AdministrativeArea.objects.get(id=a['ids'][0])
37+
a1 = AdministrativeArea.objects.get(id=a['ids'][1])
38+
print(f'{a0.osm_type} {a0.osm_id} {a0.name} - {a1.osm_type} {a1.osm_id} {a1.name}', end='', flush=True)
39+
d0 = a0.get_depth()
40+
d1 = a1.get_depth()
41+
d0parent = a0.get_parent()
42+
d1parent = a1.get_parent()
43+
d0childs = a0.get_children_count()
44+
d1childs = a1.get_children_count()
45+
if d0childs > 0 or d1childs > 0:
46+
print(f'Cant choose {a0.osm_type} {a0.osm_id} {a0.name} because has childs')
47+
continue
48+
if d0 > d1:
49+
print(f'Choosing less depth')
50+
a1.delete()
51+
elif d0 < d1:
52+
print(f'Choosing less depth')
53+
a0.delete()
54+
else:
55+
areadiff = d0parent.geometry.area - d1parent.geometry.area
56+
if areadiff > 0.00001:
57+
print(f'Choosing bigger parent area by a margin of 0.00001')
58+
a0.delete()
59+
elif areadiff < -0.00001:
60+
print(f'Choosing bigger parent area by a margin of 0.00001')
61+
a1.delete()
62+
else:
63+
print(f'Cant choose {a0.osm_type} {a0.osm_id} {a0.name} because has same area by a margin of 0.00001')

0 commit comments

Comments
 (0)