diff --git a/cadasta/organization/models.py b/cadasta/organization/models.py index 21e18acd2..e8e952718 100644 --- a/cadasta/organization/models.py +++ b/cadasta/organization/models.py @@ -234,6 +234,38 @@ def public(self): return self.access == 'public' +def reassign_project_extent(instance): + if instance.extent: + points = [list(x) for x in list(instance.extent.boundary.coords)] + extent_visible = True + for point in points: + if point[0] < -180 or point[0] > 180: + extent_visible = False + break + if extent_visible: + return + extent = '((' + for i, point in enumerate(points): + while point[0] < -180 or point[0] > 180: + if point[0] < -180: + point[0] += 360 + elif point[0] > 180: + point[0] -= 360 + if i != len(points) - 1: + extent += ' '.join([str(point[0]), str(point[1]), ',']) + else: + extent += ' '.join([str(point[0]), str(point[1])]) + polygon = str(instance.extent).split(' ')[0] + new_extent = ('{} {}))'.format(polygon, extent)) + instance.extent = new_extent + + +@receiver(models.signals.pre_save, sender=Project) +def check_extent(sender, instance, **kwargs): + if instance.extent: + reassign_project_extent(instance) + + class ProjectRole(RandomIDModel): project = models.ForeignKey(Project) user = models.ForeignKey('accounts.User') diff --git a/cadasta/organization/tests/test_models.py b/cadasta/organization/tests/test_models.py index 17fd56d56..419e5af68 100644 --- a/cadasta/organization/tests/test_models.py +++ b/cadasta/organization/tests/test_models.py @@ -114,6 +114,19 @@ def test_country_assignment_for_invalid_geometry(self): ) assert project.country == '' + def test_reassign_extent(self): + project = ProjectFactory.create( + extent='SRID=4326;POLYGON((' + '211.36667 47.25000, ' + '211.41667 47.25000, ' + '211.41667 47.28333, ' + '211.36667 47.28333, ' + '211.36667 47.25000))' + ) + assert project.extent.boundary.coords == ( + (-148.63333, 47.25), (-148.58333, 47.25), (-148.58333, 47.28333), + (-148.63333, 47.28333), (-148.63333, 47.25)) + def test_defaults_to_public(self): project = ProjectFactory.create() assert project.public()