submarcos / django-vectortiles

Mapbox VectorTiles for django, with PostGIS or Python
https://django-vectortiles.readthedocs.io
MIT License
38 stars 11 forks source link

Improve documentation #22

Open submarcos opened 3 years ago

submarcos commented 3 years ago

Add examples:

nitrag commented 1 year ago

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

submarcos commented 1 year ago

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 commented 1 year ago

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