Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 70d815d

Browse files
committedAug 25, 2024··
add command to correct duplicate areas
1 parent 717913b commit 70d815d

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
for a in aa:
32+
a0 = AdministrativeArea.objects.get(id=a['ids'][0])
33+
a1 = AdministrativeArea.objects.get(id=a['ids'][1])
34+
d0 = a0.get_depth()
35+
d1 = a1.get_depth()
36+
d0parent = a0.get_parent()
37+
d1parent = a1.get_parent()
38+
d0childs = a0.get_children_count()
39+
d1childs = a1.get_children_count()
40+
if d0childs > 0 or d1childs > 0:
41+
print(f'Cant choose {a0.osm_type} {a0.osm_id} {a0.name} because has childs')
42+
return None
43+
if d0 > d1:
44+
print(f'Choosing less depth')
45+
a1.delete()
46+
elif d0 < d1:
47+
print(f'Choosing less depth')
48+
a0.delete()
49+
else:
50+
areadiff = d0parent.geometry.area - d1parent.geometry.area
51+
if areadiff > 0.00001:
52+
print(f'Choosing bigger parent area by a margin of 0.00001')
53+
a0.delete()
54+
elif areadiff < -0.00001:
55+
print(f'Choosing bigger parent area by a margin of 0.00001')
56+
a1.delete()
57+
else:
58+
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)
Please sign in to comment.