Skip to content

Commit

Permalink
Fixes #213: Points are now centered on the map.
Browse files Browse the repository at this point in the history
added logic into model to check if project extent is within the map bounds
  • Loading branch information
linzjax committed Jun 23, 2016
1 parent 375c330 commit 28c3de0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
32 changes: 32 additions & 0 deletions cadasta/organization/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
13 changes: 13 additions & 0 deletions cadasta/organization/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 28c3de0

Please sign in to comment.