Skip to content

Commit

Permalink
Merge pull request #35 from submarcos/v1
Browse files Browse the repository at this point in the history
v1
  • Loading branch information
submarcos authored Feb 8, 2024
2 parents 686e906 + 1b78807 commit e00abb4
Show file tree
Hide file tree
Showing 51 changed files with 1,766 additions and 872 deletions.
5 changes: 5 additions & 0 deletions .env.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SECRET_KEY=
POSTGRES_PASSWORD=
POSTGRES_USER=vectortiles
POSTGRES_NAME=vectortiles
POSTGRES_HOST=postgres
8 changes: 4 additions & 4 deletions .github/workflows/python-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
flake8:
runs-on: ubuntu-latest
container:
image: python:3.7
image: python:3.8
env:
LANG: C.UTF-8
steps:
Expand All @@ -28,7 +28,7 @@ jobs:
isort:
runs-on: ubuntu-latest
container:
image: python:3.7
image: python:3.8
env:
LANG: C.UTF-8

Expand All @@ -45,7 +45,7 @@ jobs:
black:
runs-on: ubuntu-latest
container:
image: python:3.7
image: python:3.8
env:
LANG: C.UTF-8

Expand All @@ -62,7 +62,7 @@ jobs:
doc:
runs-on: ubuntu-latest
container:
image: python:3.7
image: python:3.8
env:
LANG: C.UTF-8

Expand Down
18 changes: 12 additions & 6 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ on:
release:
types: [created]
workflow_dispatch:


permissions:
id-token: write

jobs:
deploy:
runs-on: ubuntu-latest
Expand All @@ -17,15 +20,18 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.7
python-version: 3.8

- name: Install dependencies
run: |
python -m pip install setuptools wheel twine -U
- name: Build and publish
python -m pip install setuptools wheel -U
- name: Build packages
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: |
python setup.py sdist
twine upload dist/*
python setup.py sdist bdist_wheel
- name: Publish package distributions to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
21 changes: 14 additions & 7 deletions .github/workflows/python-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,24 @@ on:

jobs:
test:
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
env:
LANG: C.UTF-8
SECRET_KEY: secret-for-ci-only
POSTGRES_HOST: localhost
POSTGRES_NAME: ci_test
POSTGRES_PASSWORD: ci_test
POSTGRES_USER: ci_test
POSTGRES_DB: ci_test
strategy:
matrix:
python-version: ['3.7', '3.8', '3.11']
python-version: ['3.8', '3.11']
django-version: ['3.2.*', '4.2.*']
psycopg: ['psycopg2-binary']
postgis-image: ['postgis/postgis:10-2.5', 'postgis/postgis:11-2.5', 'postgis/postgis:latest']
exclude:
- postgis-image: 'postgis/postgis:11-2.5'
django-version: '3.2.*' # test only with 10-2.5 and latest
- python-version: '3.7'
django-version: '4.2.*' # Django 4.2 supports only python >= 3.8
- postgis-image: 'postgis/postgis:10-2.5'
django-version: '4.2.*' # Django 4.2 supports only postgres >= 12
- postgis-image: 'postgis/postgis:11-2.5'
Expand All @@ -36,9 +42,10 @@ jobs:
postgres:
image: ${{ matrix.postgis-image }}
env:
POSTGRES_PASSWORD: travis_ci_test
POSTGRES_USER: travis_ci_test
POSTGRES_DB: travis_ci_test
POSTGRES_NAME: ci_test
POSTGRES_PASSWORD: ci_test
POSTGRES_USER: ci_test
POSTGRES_DB: ci_test
ports:
- 5432:5432
steps:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,4 @@ venv.bak/
.mypy_cache/
.idea/
/docs/_build/
cache
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM makinacorpus/geodjango:bionic-3.6
FROM makinacorpus/geodjango:focal-3.8

RUN mkdir -p /code/src

Expand All @@ -7,7 +7,7 @@ RUN chown -R django:django /code

USER django

RUN python3.6 -m venv /code/venv
RUN python3.8 -m venv /code/venv
RUN /code/venv/bin/pip install --no-cache-dir pip setuptools wheel -U

COPY . /code/src
Expand Down
90 changes: 54 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,101 +17,117 @@
pip install django-vectortiles
```

* Without any other option, use only vectortiles.postgis
* By default, postgis backend is enabled.
* Ensure you have psycopg2 set and installed

#### If you don't want to use Postgis
#### If you don't want to use Postgis and / or PostgreSQL
```bash
pip install django-vectortiles[mapbox]
pip install django-vectortiles[python]
```
* This will incude mapbox_vector_tiles package and its dependencies
* Use only vectortiles.mapbox
* 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
* assuming you have ```django.contrib.gis``` in your ```INSTALLED_APPS``` and a gis compatible database backend

```python
# in your app models.py

from django.contrib.gis.db import models


class Layer(models.Model):
name = models.CharField(max_length=250)


class Feature(models.Model):
geom = models.GeometryField(srid=4326)
name = models.CharField(max_length=250)
layer = models.ForeignKey(Layer, on_delete=models.CASCADE, related_name='features')
```


#### Simple model:
#### Simple Example:

```python
# in your view file

from django.views.generic import ListView
from vectortiles.postgis.views import MVTView
from yourapp.models import Feature

# in a vector_layers.py file
from vectortiles import VectorLayer


class FeatureTileView(MVTView, ListView):
class FeatureVectorLayer(VectorLayer):
model = Feature
vector_tile_layer_name = "features"
vector_tile_fields = ('other_field_to_include', )
vector_tile_fields = ("name",)

# in your view file

from yourapp.vector_layers import FeatureVectorLayer

from vectortiles.views import MVTView


class FeatureTileView(MVTView):
layer_classes = [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"),
...
]
```

#### Related model:
#### Use TileJSON and multiple domains:

```python
# in your view file

from django.views.generic import DetailView
from vectortiles.mixins import BaseVectorTileView
from vectortiles.postgis.views import MVTView
from yourapp.models import Layer
from django.urls import reverse

from vectortiles.views import TileJSONView
from yourapp.vector_layers import FeatureVectorLayer

class LayerTileView(MVTView, DetailView):
model = Layer
vector_tile_fields = ('other_field_to_include', )

def get_vector_tile_layer_name(self):
return self.get_object().name
class FeatureTileJSONView(TileJSONView):
"""Simple model TileJSON View"""

def get_vector_tile_queryset(self):
return self.get_object().features.all()
name = "My features dataset"
attribution = "@JEC Data"
description = "My dataset"
layer_classes = [FeatureVectorLayer]

def get(self, request, *args, **kwargs):
self.object = self.get_object()
return BaseVectorTileView.get(self,request=request, z=kwargs.get('z'), x=kwargs.get('x'), y=kwargs.get('y'))
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('layer/<int:pk>/tile/<int:z>/<int:x>/<int:y>', views.LayerTileView.as_view(), name="layer-tile"),
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
Expand All @@ -126,8 +142,9 @@ django-vectortiles can be used with DRF if `renderer_classes` of the view is ove

##### With docker and docker-compose

Copy ```.env.dist``` to ```.env``` and fill ```SECRET_KEY``` and ```POSTGRES_PASSWORD```

```bash
docker pull makinacorpus/geodjango:bionic-3.6
docker-compose build
# docker-compose up
docker-compose run /code/venv/bin/python ./manage.py test
Expand All @@ -139,6 +156,7 @@ docker-compose run /code/venv/bin/python ./manage.py test
* Install geodjango requirements
* Have a postgresql / postgis 2.4+ enabled database
* Use a virtualenv

```bash
pip install .[dev] -U
```
12 changes: 6 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
version: "3"
services:
postgres:
image: postgis/postgis:10-2.5
image: postgis/postgis:12-2.5
volumes:
- postgres:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=travis_ci_test
- POSTGRES_USER=travis_ci_test
- POSTGRES_DB=travis_ci_test
env_file:
- .env
ports:
- "5432:5432"

Expand All @@ -17,7 +15,9 @@ services:
depends_on:
- postgres
environment:
- POSTGRES_HOST=postgres
- DJANGO_SETTINGS_MODULE=test_vectortiles.settings.dev
env_file:
- .env
volumes:
- .:/code/src
ports:
Expand Down
7 changes: 6 additions & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
CHANGELOG
=========

0.2.0+dev
1.0.0-beta
----------

* Drop python 3.6 and Django 2.2
* Add python 3.11 and Django 4.2

** Breaking changes **

* Refactor PostGIS and Python (old named MapBox) backends usage. Use setting to set (default postgis)
* No DetailView anymore. As Tile can have many layers, declare VectorLayer on MTVView (one or many).

* Enhancements

* Add compatibility to use with psycopg v3
Expand Down
Loading

0 comments on commit e00abb4

Please sign in to comment.