Skip to main content

Django vector tile generation

Project description

Tests Coverage

Python Version Django Version

Generate MapBox VectorTiles from GeoDjango models

Directly with PostgreSQL/PostGIS 2.4+ or python native mapbox_vector_tile

Read full documentation

Installation

Basic

pip install django-vectortiles
  • By default, postgis backend is enabled.
  • Ensure you have psycopg2 set and installed

If you don't want to use Postgis and / or PostgreSQL

pip install django-vectortiles[python]
  • This will incude mapbox_vector_tiles package and its dependencies
  • Set VECTOR_TILES_BACKEND to "vectortiles.backends.python"

Examples

  • assuming you have django.contrib.gis in your INSTALLED_APPS and a gis compatible database backend
# in your app models.py

from django.contrib.gis.db import models


class Feature(models.Model):
    geom = models.GeometryField(srid=4326)
    name = models.CharField(max_length=250)

Simple Example:

from yourapp.models import Feature

# in a vector_layers.py file
from vectortiles import VectorLayer


class FeatureVectorLayer(VectorLayer):
    model = Feature
    vector_tile_layer_name = "features"
    vector_tile_fields = ("name",)

# in your view file

from yourapp.vector_layers import FeatureVectorLayer

from vectortiles.views import MVTView


class FeatureTileView(MVTView):
    layers = [FeatureVectorLayer()]


# in your urls file
from django.urls import path
from yourapp import views

urlpatterns = [
    ...
    path('tiles/<int:z>/<int:x>/<int:y>', views.FeatureTileView.as_view(), name="feature-tile"),
    ...
]

Use TileJSON and multiple domains:

# in your view file

from django.urls import reverse

from vectortiles.views import TileJSONView


class FeatureTileJSONView(TileJSONView):
    """Simple model TileJSON View"""

    name = "My features dataset"
    attribution = "@JEC Data"
    description = "My dataset"

    def get_tile_url(self):
        """ Base MVTView Url used to generates urls in TileJSON in a.tiles.xxxx/{z}/{x}/{y} format """
        return str(reverse("feature-tile", args=(0, 0, 0))).replace("0/0/0", "{z}/{x}/{y}")


# in your urls file
from django.urls import path
from yourapp import views

urlpatterns = [
    ...
    path('tiles/<int:z>/<int:x>/<int:y>', views.FeatureTileView.as_view(), name="feature-tile"),
    path("feature/tiles.json", views.FeatureTileJSONView.as_view(), name="feature-tilejson"),
    ...

    # in your settings file
    ALLOWED_HOSTS = [
    "a.tiles.xxxx",
    "b.tiles.xxxx",
    "c.tiles.xxxx",
    ...
]

VECTOR_TILES_URLS = [
    "https://a.tiles.xxxx",
    "https://b.tiles.xxxx",
    "https://c.tiles.xxxx",
    ...
]

Usage without PostgreSQL / PostGIS

Just import and use vectortiles.mapbox.view.MVTView instead of vectortiles.postgis.view.MVTView

Usage with Django Rest Framework

django-vectortiles can be used with DRF if renderer_classes of the view is overridden (see DRF docs). Simply use the right BaseMixin and action on viewsets, or directly a GET method in an APIView. See documentation for more details.

Development

With docker and docker-compose
docker-compose build
# docker-compose up
docker-compose run /code/venv/bin/python ./manage.py test
Local
  • Install python and django requirements (python 3.6+, django 2.2+)
  • Install geodjango requirements
  • Have a postgresql / postgis 2.4+ enabled database
  • Use a virtualenv
pip install .[dev] -U

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-vectortiles-1.0.0b3.tar.gz (8.2 kB view hashes)

Uploaded Source

Built Distribution

django_vectortiles-1.0.0b3-py3-none-any.whl (11.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page