Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve documentation #22

Open
submarcos opened this issue Feb 26, 2021 · 3 comments
Open

Improve documentation #22

submarcos opened this issue Feb 26, 2021 · 3 comments
Labels
📝 Documentation Improvements or additions to documentation

Comments

@submarcos
Copy link
Owner

submarcos commented Feb 26, 2021

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 submarcos added the 📝 Documentation Improvements or additions to documentation label Feb 26, 2021
@nitrag
Copy link
Contributor

nitrag commented Feb 5, 2023

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).

@submarcos
Copy link
Owner Author

submarcos commented Feb 6, 2023

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
Copy link
Owner Author

submarcos commented Feb 6, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📝 Documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants