Package: djangostreetmap
DjangoStreetMapDjangSstreetMap is a Django application to load OSM data into a postgis database and deliver OSM data as
MVT tiles.Openstreetmap Vector Tiles"A vector tile is a lightweight data format for storing geospatial vector data"For an introduction to MVT (Mapbox Vector Tiles) see themapbox docsFor an introduction to OSM seeopenstreetmap.orgPurpose of this projectThis is a Django application toImport OSM data as Django modelsExpose Django models as MVT (Mapbox Vector Tile) geographic format dataTile generation is much faster when geometry is in srid=3857 (or maybe with an index in that SRID?)PrerequisitesYou need thegdallibraries installedOn Ubuntu:sudo apt install binutils libproj-dev gdal-binOtherwise refer to the Django docs "Installing geospatial libraries"Adding to a ProjectIf necessary install psycopg2 in your envExtend installed_apps with the following apps:pip install osmflex["django.contrib.gis","djangostreetmap","osmflex",](Recommended) Set your cacheYou likely want to set a fast cache for your tiles like Memcached. If this is not found
the default cache will be used; this can be a bit slower and is very much non persistent
This assumes you're running memcached (Linux:apt install memcached) and installed memcached(pip install python-memcached)CACHES={'default':{'BACKEND':'django.core.cache.backends.memcached.MemcachedCache','LOCATION':'127.0.0.1:11211',}}Running faster in testingRunpoetry installTo run pytest, you need to have an appropriate postgis database
If you use docker one option is to run the following:dockerrun\--rm\-p49155:5432\--name=djangostreetmap\-ePOSTGRES_PASSWORD=post1234\postgis/postgis:14-3.2\-cfsync=off\-cshared_buffers=4096MBRunpoetry run pytestRunserver is "ok" but this recipe will give faster performance for demonstration purposespipinstallgunicorn
gunicorn-w8djangostreetmap.wsgi:applicationBuildingpoetry version patch
poetry build
poetry publishWriting ViewsTo set up a new View, create a subclass of TileLayerView with someMvtQueryinstances as layers:classRoadLayerView(TileLayerView):layers=[MvtQuery(table=OsmHighway._meta.db_table,attributes=["name"],filters=[f"\"highway\"='{road_class}'"],layer=road_class)forroad_classin("primary","secondary","tertiary")]Append the URL to your urls.py as follows. Note the zoom, x and y are mandatory.path("highways/<int:zoom>/<int:x>/<int:y>.pbf",RoadLayerView.as_view()),Running in DevelopmentSet up postgisdockerrun--name=osm\-ePOSTGRES_DB=postgres\-ePOSTGRES_USER=postgres\-ePOSTGRES_PASSWORD=post1234\-p49155:5432\postgis/postgis:12-3.1Find your port: if you do not use49155as above:(env)josh@m4800:~/github/joshbrooks/djangostreetmap$dockerps
CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES
c619232fe38apostgis/postgis:12-3.1"docker-entrypoint.s…"33secondsagoUp32seconds0.0.0.0:49155->5432/tcp,:::49155->5432/tcposm
...OSM is on port 49155To apply this to your project:DATABASES={"default":{"ENGINE":"django.contrib.gis.db.backends.postgis","USER":"postgres","PASSWORD":"post1233","HOST":"localhost","PORT":"49154","NAME":"postgres",}}Fetch your datawgethttps://download.geofabrik.de/australia-oceania/papua-new-guinea-latest.osm.pbforwgethttps://download.geofabrik.de/asia/east-timor-latest.osm.pbfInstalling osm2psqlTo run the management command below you'll need anosm2pgsqlversion of around 1.3 or greater. This is not available in the ubuntu package manager (yet)...Import DataThe "osmflex" app has two management commands to run which will populate osmflex models./manage.pyrun_osm2pgsql/media/josh/blackgate/osm/asia/east-timor-latest.osm.pbf./manage.pyimport_from_pgosmflexExploring dataSee the Django admin for osmflex:http://localhost:8000/admin/osmflexpsql --host localhost --username postgres --port 49159QgisAdd a new Postgres Connection with the following settings:Name: DjangoStreetMap
Host: localhost
Port: 49155
Database: postgresAuthentication: Basic
postgres / post1233DevelopmentCode is blacked, flaked, isorted and mypy'd.pip install pre-commit