Skip to content

Commit

Permalink
Changed tile lookups to be pk based on views.
Browse files Browse the repository at this point in the history
  • Loading branch information
yellowcap committed Oct 20, 2016
1 parent a40051c commit dcd533c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
1 change: 1 addition & 0 deletions raster/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
EXPORT_MAX_PIXELS = 10000 * 10000
MAX_EXPORT_NAME_LENGTH = 100
DEFAULT_LEGEND_BREAKS = 7
PK_FORMAT = '{layer}-{z}-{x}-{y}'
README_TEMPLATE = """Django Raster Algebra Export
============================
{description}
Expand Down
37 changes: 31 additions & 6 deletions raster/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from django.db.models import Max, Min
from django.db.models.signals import post_delete, post_save, pre_save
from django.dispatch import receiver
from raster.const import PK_FORMAT
from raster.mixins import ValueCountMixin
from raster.tiles.const import WEB_MERCATOR_SRID
from raster.utils import hex_to_rgba
Expand Down Expand Up @@ -371,6 +372,25 @@ def statistics(self):
return (self.min, self.max, self.mean, self.std)


class RasterTileManager(models.Manager):

def filter_on_pk(self, rasterlayer_id, tilez, tilex, tiley):
"""
A wrapper function that filters on the primary key based on the input
parameters, instead of the individual column values directly.
This is useful for sharded databases, which can be sharded by the
verbose primary key.
"""
pk = PK_FORMAT.format(
layer=rasterlayer_id,
z=tilez,
x=tilex,
y=tiley,
)
return self.filter(id=pk)


class RasterTile(models.Model):
"""
Store individual tiles of a raster data source layer.
Expand All @@ -387,14 +407,19 @@ class RasterTile(models.Model):
tiley = models.IntegerField(db_index=True)
tilez = models.IntegerField(db_index=True, choices=ZOOMLEVELS)

objects = RasterTileManager()

def __str__(self):
return '{} {}'.format(self.rid, self.rasterlayer.name)

def save(self, *args, **kwargs):
self.id = '{0}-{1}-{2}-{3}'.format(
self.rasterlayer_id,
self.zoom,
self.tilex,
self.tiley,
)
self.id = self.construct_pk()
super(RasterTile, self).save(*args, **kwargs)

def construct_pk(self):
return PK_FORMAT.format(
layer=self.rasterlayer_id,
z=self.tilez,
x=self.tilex,
y=self.tiley,
)
4 changes: 2 additions & 2 deletions raster/tiles/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ def get_raster_tile(layer_id, tilez, tilex, tiley):
# Compute multiplier to find parent raster
multiplier = 2 ** (tilez - zoom)
# Fetch tile
tile = RasterTile.objects.filter(
tile = RasterTile.objects.filter_on_pk(
tilex=tilex / multiplier,
tiley=tiley / multiplier,
tilez=zoom,
rasterlayer_id=layer_id
rasterlayer_id=layer_id,
)

if tile.exists():
Expand Down

0 comments on commit dcd533c

Please sign in to comment.