Skip to main content

Map engine for maplibre in django

Project description

Django-Mapengine

django-mapengine is a Django app to provide library for maplibre in backend. This includes

  • html templates and JS files for maplibre
  • creation of maplibre sources and layers including choropleths,
  • provision of multi-vector-tiles from django models,
  • distilling of map source views
  • basic popups

Requirements

Maplibre must be installed (i.e. via npm) and provided as JS framework

Quick start

  1. Add "django_mapengine" to your INSTALLED_APPS setting like this:

    INSTALLED_APPS = [
         "...",
         'django_mapengine',
    ]
    
  2. Install maplibre-gl and pubsub-js dependency by:

    npm install maplibre-gl pubsub-js
    

    and copy JS and CSS to your static folder.

  3. Include URLs from django_mapengine to your project:

    urlpatterns = [
        "...",
        path("map/", include("django_mapengine.urls")),
    ]
    
  4. Configure map engine by setting zoom levels, regions and styles folder in project settings.py. You can see all possible settings by looking into django_mapengine.settings.py. Example settings:

    from django_mapengine import setup
    
    MAP_ENGINE_CENTER_AT_STARTUP = [12.537917858911896, 51.80812518969171]
    MAP_ENGINE_ZOOM_AT_STARTUP = 9
    MAP_ENGINE_MAX_BOUNDS = [[11.280733017118229, 51.22918643452503], [13.616574868700604, 52.35515806663738]]
    
    MAP_ENGINE_IMAGES = [setup.MapImage("wind", "images/icons/i_wind.png")]
    
    MAP_ENGINE_API_MVTS = {
        "municipality":
            [
                setup.MVTAPI("municipality", "map", "Municipality"),
                setup.MVTAPI("municipalitylabel", "map", "Municipality", "label_tiles"),
            ],
        "results": [setup.MVTAPI("results", "map", "Municipality")]
    }
    
    MAP_ENGINE_API_CLUSTERS = [
        setup.ClusterAPI("wind", "map", "WindTurbine"),
        setup.ClusterAPI("pvroof", "map", "PVroof"),
        setup.ClusterAPI("pvground", "map", "PVground"),
        setup.ClusterAPI("hydro", "map", "Hydro"),
        setup.ClusterAPI("biomass", "map", "Biomass"),
        setup.ClusterAPI("combustion", "map", "Combustion"),
    ]
    
    MAP_ENGINE_STYLES_FOLDER = "digiplan/static/config/"
    MAP_ENGINE_ZOOM_LEVELS = {
       "municipality": core.Zoom(8, 12),
    }
    
    MAP_ENGINE_POPUPS = ["results"]
    
  5. Add middleware to your middleware setup before Whitenoise middleware (or other static server middleware):

    MIDDLEWARE = [
        "django.middleware.security.SecurityMiddleware",
        "django_mapengine.django_mapengine.middleware.MapEngineMiddleware",
        "whitenoise.middleware.WhiteNoiseMiddleware",
        ...
    ]
    
  6. Add maplibre-gl, pubsub-js and mapengine JS, CSS and JSONs in template by:

    {% block javascript %}
      {{ block.super }}
      {% compress js %}
        <script src="{% static 'vendors/maplibre/js/maplibre-gl.js' %}"></script>
        <script src="{% static 'vendors/pubsub/js/pubsub.js' %}"></script>
      {% endcompress %}
    {% endblock javascript %}
    
    {%  block inline_javascript %}
      {% include 'django_mapengine/map_json.html' %}
      {% compress js %}
        {% include 'django_mapengine/map_js.html' %}
      {% endcompress %}
    {% endblock %}
    
    {% block css %}
      {% compress css %}
        <link href="{% static 'vendors/maplibre/css/maplibre-gl.css' %}" rel='stylesheet'/>
      {% endcompress %}
    {% endblock css %}
    
  7. If you want to integrate basemaps to your map add the following to the corresponding places:

    <script src="{% static 'django_mapengine/js/basemaps.js' %}" type="text/javascript"></script>
    {% include 'django_mapengine/map_basemaps.html'}
    

User Guides

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_mapengine-2.2.6.tar.gz (33.8 kB view details)

Uploaded Source

Built Distribution

django_mapengine-2.2.6-py3-none-any.whl (45.6 kB view details)

Uploaded Python 3

File details

Details for the file django_mapengine-2.2.6.tar.gz.

File metadata

  • Download URL: django_mapengine-2.2.6.tar.gz
  • Upload date:
  • Size: 33.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.8.0-49-generic

File hashes

Hashes for django_mapengine-2.2.6.tar.gz
Algorithm Hash digest
SHA256 5e3d6e9223d61c2575ef5b9d80110aaa9d4b0bfd837a6e603633d768e12a012f
MD5 be6f029e13492e445ed9c58642327a28
BLAKE2b-256 fbcd26ad3857c81b238c1c53f21631400daf5d5bd6aa8f1760e0c1feb4556fc8

See more details on using hashes here.

File details

Details for the file django_mapengine-2.2.6-py3-none-any.whl.

File metadata

  • Download URL: django_mapengine-2.2.6-py3-none-any.whl
  • Upload date:
  • Size: 45.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.8.0-49-generic

File hashes

Hashes for django_mapengine-2.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 78abb564cd666d9f345a655e87ed7a5aaa05214ed447bfcf7448b644e669e787
MD5 bae2813addd61922a88766e685be3f32
BLAKE2b-256 85e4f43bde7e7725f6ea17c561ed386902f365d4db3b735e2857b2eb1e8b2592

See more details on using hashes here.

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