Skip to main content

Django vector tile generation

Project description

Tests Coverage Python Version Django Version

Generate MapBox VectorTiles from GeoDjango models

image

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 psycopg installed

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

pip install django-vectortiles[python]
  • This will include mapbox_vector_tiles package and its dependencies
  • Set VECTOR_TILES_BACKEND="vectortiles.backends.python" in your project settings.

Examples

Let's create vector tiles with your city geometries.

  • 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 City(models.Model):
    name = models.CharField(max_length=250)
    city_code = models.CharField(max_length=10, unique=True)
    population = models.IntegerField(default=0)
    geom = models.MultiPolygonField(srid=4326)

Simple Example:

from yourapp.models import City

# in a vector_layers.py file
from vectortiles import VectorLayer


class CityVL(VectorLayer):
    model = City
    id = "cities"  # layer id / name in tile
    tile_fields = ("name", "city_code")  # add name and city_code properties in each tile feature
    min_zoom = 9  # don't embed city borders at low zoom levels

# in your view file

from yourapp.vector_layers import CityVL

from vectortiles.views import MVTView


class CityTileView(MVTView):
    layer_classes = [CityVL]


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

urlpatterns = [
    ...
    CityTileView.get_url(),  # serve tiles at default /tiles/<int:z>/<int:x>/<int:y>. You can override url prefix and tile scheme in class attributes.
    ...
]

Example with multiple layers

Suppose you want to make a map with your city borders, and a point in each city center that shows a popup with city name, population an area.

from django.contrib.gis.db.models.functions import Centroid, Area
from yourapp.models import City

# in a vector_layers.py file
from vectortiles import VectorLayer


class CityVectorLayer(VectorLayer):
    model = City
    id = "cities"
    tile_fields = ('city_code', "name")
    min_zoom = 10

    
class CityCentroidVectorLayer(VectorLayer):
    queryset = City.objects.annotate(
        centroid=Centroid("geom"), # compute the city centroïd
        area=Area("geom"), # compute the city area
    )  
    geom_field = "centroid"  # use the centroid field as geometry feature
    id = "city_centroïds"
    tile_fields = ('name', 'city_code', 'area', 'population')  # add area and population properties in each tile feature
    min_zoom = 7  # let's show city name at zoom 7


    
# in your view file

from yourapp.vector_layers import CityVectorLayer, CityCentroidVectorLayer

from vectortiles.views import MVTView


class CityTileView(MVTView):
    layer_classes = [CityVectorLayer, CityCentroidVectorLayer]


# in your urls file
from yourapp import views

urlpatterns = [
    ...
    views.CityTileView.get_url(),  # serve tiles at default /tiles/<int:z>/<int:x>/<int:y>
    ...
]

Now, any tile requested at http://you_url/tiles/{z}/{x}/{y} that intersects a city will return a vector tile with two layers, cities with border geometries and city_code property, and city_centroïds with center geometry and city_name property. image

Read full documentation for examples, as multiple layers, cache policy, mapblibre integration, etc.

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.1.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

django_vectortiles-1.0.1-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file django_vectortiles-1.0.1.tar.gz.

File metadata

  • Download URL: django_vectortiles-1.0.1.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for django_vectortiles-1.0.1.tar.gz
Algorithm Hash digest
SHA256 5f2145fd47010200ca1bdeefe5faf20a3389f700ea9fc822cb7f7c7a4d8721f7
MD5 3aea96c6cb4323ab5316824865959a83
BLAKE2b-256 9179eb841fd243c336962e5964d04b2c1e325ebf4f90335b38d508ce2abca63e

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_vectortiles-1.0.1.tar.gz:

Publisher: python-publish.yml on submarcos/django-vectortiles

Attestations:

File details

Details for the file django_vectortiles-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_vectortiles-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 37e08d23a525a8cf052e2e9bf7011fa7152c7dd2ad1a3c73d1ca668c6e38e739
MD5 d25ac0fa81efc766c2f13d1fc5adf7c1
BLAKE2b-256 3e58f5cdeb2142a1258dc09246dd55dc88a8016806ab9861990c0011ce5b3ebf

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_vectortiles-1.0.1-py3-none-any.whl:

Publisher: python-publish.yml on submarcos/django-vectortiles

Attestations:

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