Django vector tile generation
Project description
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 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.gisin yourINSTALLED_APPSand 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.
Read full documentation for examples, as multiple layers, cache policy, mapblibre integration, etc.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_vectortiles-1.0.2.tar.gz.
File metadata
- Download URL: django_vectortiles-1.0.2.tar.gz
- Upload date:
- Size: 9.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5ea1eb69eb40a0f0fac8979d7b483312145a0af3ac30f7769ee6542e2456214d
|
|
| MD5 |
b413cb0b6c69365d40c37706101b1aef
|
|
| BLAKE2b-256 |
631efffa26b857739e8f182737d7774fd34ddd5930881c4609a0d3fc54479cbf
|
Provenance
The following attestation bundles were made for django_vectortiles-1.0.2.tar.gz:
Publisher:
python-publish.yml on submarcos/django-vectortiles
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_vectortiles-1.0.2.tar.gz -
Subject digest:
5ea1eb69eb40a0f0fac8979d7b483312145a0af3ac30f7769ee6542e2456214d - Sigstore transparency entry: 268628437
- Sigstore integration time:
-
Permalink:
submarcos/django-vectortiles@4c9b812c259d949b75f18dbbd72142ce0dc73247 -
Branch / Tag:
refs/tags/1.0.2 - Owner: https://github.com/submarcos
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@4c9b812c259d949b75f18dbbd72142ce0dc73247 -
Trigger Event:
release
-
Statement type:
File details
Details for the file django_vectortiles-1.0.2-py3-none-any.whl.
File metadata
- Download URL: django_vectortiles-1.0.2-py3-none-any.whl
- Upload date:
- Size: 11.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
579e721b26f6168a029c112c42e2b9290c2209c80f669caf7ea4be446e202715
|
|
| MD5 |
c918b69b1ed096afedacc2d0b604e3ab
|
|
| BLAKE2b-256 |
1592c32b0460a321f3cf018b5bc11a2cb07bf4ef627ed4cf98871360e5d8c4d8
|
Provenance
The following attestation bundles were made for django_vectortiles-1.0.2-py3-none-any.whl:
Publisher:
python-publish.yml on submarcos/django-vectortiles
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_vectortiles-1.0.2-py3-none-any.whl -
Subject digest:
579e721b26f6168a029c112c42e2b9290c2209c80f669caf7ea4be446e202715 - Sigstore transparency entry: 268628442
- Sigstore integration time:
-
Permalink:
submarcos/django-vectortiles@4c9b812c259d949b75f18dbbd72142ce0dc73247 -
Branch / Tag:
refs/tags/1.0.2 - Owner: https://github.com/submarcos
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@4c9b812c259d949b75f18dbbd72142ce0dc73247 -
Trigger Event:
release
-
Statement type: