Skip to content

Commit

Permalink
Simplification by zoom level
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Mar 25, 2014
1 parent 2b93cb1 commit 56301a4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
18 changes: 18 additions & 0 deletions djgeojson/tests.py
Expand Up @@ -507,6 +507,24 @@ def test_some_tiles_have_empty_queryset(self):
self.view.args = [4, 6, 8]
self.assertEqual(0, len(self.view.get_queryset()))

def test_simplification_depends_on_zoom_level(self):
self.view.simplifications = {6: 100}
self.view.args = [6, 8, 4]
self.view.get_queryset()
self.assertEqual(self.view.simplify, 100)

def test_simplification_is_default_if_not_specified(self):
self.view.simplifications = {}
self.view.args = [0, 8, 4]
self.view.get_queryset()
self.assertEqual(self.view.simplify, None)

def test_simplification_takes_the_closest_upper_level(self):
self.view.simplifications = {3: 100, 6: 200}
self.view.args = [4, 8, 4]
self.view.get_queryset()
self.assertEqual(self.view.simplify, 200)


class Address(models.Model):
geom = GeoJSONField()
Expand Down
12 changes: 12 additions & 0 deletions djgeojson/views.py
Expand Up @@ -67,6 +67,8 @@ class TiledGeoJSONLayerView(GeoJSONLayerView):
height = 256
tile_srid = 3857
trim_to_boundary = True
"""Simplify geometries by zoom level (dict <int:float>)"""
simplifications = None

def tile_coord(self, xtile, ytile, zoom):
"""
Expand Down Expand Up @@ -95,11 +97,21 @@ def get_queryset(self):
qs = qs.filter(**{
'%s__intersects' % self.geometry_field: bbox
})

# Simplification dict by zoom level
simplifications = self.simplifications or {}
z = self.z
self.simplify = simplifications.get(z)
while self.simplify is None and z < 32:
z += 1
self.simplify = simplifications.get(z)

# Won't trim point geometries to a boundary
model_field = qs.model._meta.get_field(self.geometry_field)
self.trim_to_boundary = (self.trim_to_boundary and
not isinstance(model_field, PointField))
if self.trim_to_boundary:
qs = qs.intersection(bbox)
self.geometry_field = 'intersection'

return qs

0 comments on commit 56301a4

Please sign in to comment.