django-vectortiles icon indicating copy to clipboard operation
django-vectortiles copied to clipboard

Improve documentation

Open submarcos opened this issue 4 years ago • 3 comments

Add examples:

  • Usages of annotated fields in tiles (and JSONField cases)
  • Usages of get_vector_tile_queryset to simplify geometries by zoom level
  • Usage of cache policies on get_tile

submarcos avatar Feb 26 '21 16:02 submarcos

Discovered this repo today and all I can say is great work! It was super easy to implement!

  • I am curious how simplification works per zoom level, ST_ASMVT() must be doing it.
  • I am interested how I could cache tiles up to a certain zoom level (to not overwhelm my cache database).

nitrag avatar Feb 05 '23 05:02 nitrag

Hi ! Thank you for your interest.

For cache, you can override get_tile method :

    def get_tile(self, x, y, z):
        if z < 10:
            # cache under zoom level 10
            cache_key = hashlib.md5(f"vectortiles:feature:{x}:{y}:{z}".encode("utf-8")).hexdigest()
            if cache.get(cache_key):
                return cache.get(cache_key)
            else:
                tile = super().get_tile(x, y, z)
                cache.set(cache_key, tile)
        else:
            tile = super().get_tile(x, y, z)
        return tile

or something like that (sorry this code example is not optimized or tested. I will add example in documentation) you can implement your own system, for example different expiration duration according zoom level

submarcos avatar Feb 06 '23 13:02 submarcos

For simplification, ST_ASMVTGEOM prepare geometry in specific mapbox vector tile format. You should implement your own system.

from django.contrib.gis.db.models.functions import GeomOutputGeoFunc


class SimplifyPreserveTopology(GeomOutputGeoFunc):
    """ ST_SimplifyPreserveTopology postgis function """

class FeatureTileView(MVTView, ListView):
    model = Feature
    vector_tile_layer_name = "features"  # name for data layer in vector tile
    vector_tile_fields = ('name',)  # model fields or queryset annotates to include in tile
    vector_tile_geom_name = "simple_geom"

    def get_queryset():
        return Feature.objects.all().annotate(simple_geom=SimplifyPreserveTopology('geom', int(self.request.kwargs.get('z')))

take care about simplification param it's just an example. You should simplify in low zoom level, not high

submarcos avatar Feb 06 '23 13:02 submarcos