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

added bounding box for project_add_extents map
  • Loading branch information
linzjax authored and seav committed Jun 27, 2016
1 parent 01e8520 commit 9b872f5
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
1 change: 1 addition & 0 deletions cadasta/core/static/js/map_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ function add_map_controls(map) {
});

map.addControl(new Geolocate());
return map
}
30 changes: 30 additions & 0 deletions cadasta/organization/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,36 @@ 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:
point[0] += 360
while point[0] > 180:
point[0] -= 360
extent += ' '.join([str(point[0]), str(point[1])])
if i != len(points) - 1:
extent += ','
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
3 changes: 2 additions & 1 deletion cadasta/templates/organization/project_add_extents.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
<script>
$(document).ready(function () {
$(window).on('map:init', function(e) {
add_map_controls(e.originalEvent.detail.map);
map = add_map_controls(e.originalEvent.detail.map);
map.fitBounds([[-45.0, -180.0], [45.0, 180.0]]);
});
});
</script>
Expand Down
2 changes: 0 additions & 2 deletions functional_tests/pages/ProjectAdd.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ def set_geometry(self, country):
zoom_in = self.BY_CLASS('leaflet-control-zoom-in')
zoom_in.click() # Zoom 1
time.sleep(0.5) # Give zoom animation time to settle
zoom_in.click() # Zoom 2
time.sleep(0.5) # Give zoom animation time to settle

if country == 'AU':
self.draw_rectangle(840, 210, 20, 20)
Expand Down

0 comments on commit 9b872f5

Please sign in to comment.