Skip to main content

Reusable django-app for outdoor and indoor mapping

Project description

https://travis-ci.org/openwisp/django-loci.svg https://coveralls.io/repos/openwisp/django-loci/badge.svg Requirements Status https://img.shields.io/gitter/room/nwjs/nw.js.svg?style=flat-square https://badge.fury.io/py/django-loci.svg downloads code style: black

Reusable django-app for storing GIS and indoor coordinates of objects.

Indoor coordinates Map coordinates Mobile coordinates

Dependencies

Compatibility Table

django-loci

Python version

0.2

2.7 or >=3.4

0.3

>=3.6

Install stable version from pypi

Install from pypi:

pip install django-loci

Install development version

First of all, install the dependencies of GeoDjango:

Install tarball:

pip install https://github.com/openwisp/django-loci/tarball/master

Alternatively you can install via pip using git:

pip install -e git+git://github.com/openwisp/django-loci#egg=django_loci

If you want to contribute, install your cloned fork:

git clone git@github.com:<your_fork>/django-loci.git
cd django_loci
python setup.py develop

Setup (integrate in an existing django project)

First of all, set up your database engine to one of the spatial databases suppported by GeoDjango.

Add django_loci and its dependencies to INSTALLED_APPS in the following order:

INSTALLED_APPS = [
    # ...
    'django.contrib.gis',
    'django_loci',
    'django.contrib.admin',
    'leaflet',
    'channels'
    # ...
]

Configure CHANNEL_LAYERS according to your needs, a sample configuration can be:

ASGI_APPLICATION = "django_loci.channels.routing.channel_routing"
CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels.layers.InMemoryChannelLayer",
    },
}

Now run migrations:

./manage.py migrate

Troubleshooting

Common issues and solutions when installing GeoDjango.

Unable to load the SpatiaLite library extension

If you get the following exception:

django.core.exceptions.ImproperlyConfigured: Unable to load the SpatiaLite library extension

You need to specify the SPATIALITE_LIBRARY_PATH in your settings.py as explained in the django documentation regarding how to install and configure spatialte.

Issues with other geospatial libraries

Please refer to the geodjango documentation on troubleshooting issues related to geospatial libraries.

Settings

LOCI_FLOORPLAN_STORAGE

type:

str

default:

django_loci.storage.OverwriteStorage

The django file storage class used for uploading floorplan images.

The filestorage can be changed to a different one as long as it has an upload_to class method which will be passed to FloorPlan.image.upload_to.

To understand the details of this statement, take a look at the code of django_loci.storage.OverwriteStorage.

DJANGO_LOCI_GEOCODER

type:

str

default:

ArcGIS

Service used for geocoding and reverse geocoding.

Supported geolocation services:

  • ArcGIS

  • Nominatim

  • GoogleV3 (Google Maps v3)

DJANGO_LOCI_GEOCODE_STRICT_TEST

type:

bool

default:

True

Indicates whether the system should raise an ImproperlyConfigured exception in case geocoding doesn’t work when the application is started.

DJANGO_LOCI_GEOCODE_FAILURE_DELAY

type:

int

default:

1

Amount of seconds between geocoding retry API calls when geocoding requests fail.

DJANGO_LOCI_GEOCODE_RETRIES

type:

int

default:

3

Amount of retry API calls when geocoding requests fail.

DJANGO_LOCI_GEOCODE_API_KEY

type:

str

default:

None

API key if required (eg: Google Maps).

Extending django-loci

django-loci provides a set of models and admin classes which can be imported, extended and reused by third party apps.

To extend django-loci, you MUST NOT add it to settings.INSTALLED_APPS, but you must create your own app (which goes into settings.INSTALLED_APPS), import the base classes of django-loci and add your customizations.

Extending models

This example provides an example of how to extend the base models of django-loci by adding a relation to another django model named Organization.

# models.py of your app
from django.db import models
from django_loci.base.models import (AbstractFloorPlan,
                                     AbstractLocation,
                                     AbstractObjectLocation)

# the model ``organizations.Organization`` is omitted for brevity
# if you are curious to see a real implementation, check out django-organizations


class OrganizationMixin(models.Model):
    organization = models.ForeignKey('organizations.Organization')

    class Meta:
        abstract = True


class Location(OrganizationMixin, AbstractLocation):
    class Meta(AbstractLocation.Meta):
        abstract = False

    def clean(self):
        # your own validation logic here...
        pass


class FloorPlan(OrganizationMixin, AbstractFloorPlan):
    location = models.ForeignKey(Location)

    class Meta(AbstractFloorPlan.Meta):
        abstract = False

    def clean(self):
        # your own validation logic here...
        pass


class ObjectLocation(OrganizationMixin, AbstractObjectLocation):
    location = models.ForeignKey(Location, models.PROTECT,
                                 blank=True, null=True)
    floorplan = models.ForeignKey(FloorPlan, models.PROTECT,
                                  blank=True, null=True)

    class Meta(AbstractObjectLocation.Meta):
        abstract = False

    def clean(self):
        # your own validation logic here...
        pass

Extending the admin

Following the previous Organization example, you can avoid duplicating the admin code by importing the base admin classes and registering your models with them.

But first you have to change a few settings in your settings.py, these are needed in order to load the admin templates and static files of django-loci even if it’s not listed in settings.INSTALLED_APPS.

Add django.forms to INSTALLED_APPS, now it should look like the following:

INSTALLED_APPS = [
    # ...
    'django.contrib.gis',
    'django_loci',
    'django.contrib.admin',
    #      ↓
    'django.forms', # <-- add this
    #      ↑
    'leaflet',
    'channels'
    # ...
]

Now add EXTENDED_APPS after INSTALLED_APPS:

INSTALLED_APPS = [
    # ...
]

EXTENDED_APPS = ('django_loci',)

Add openwisp_utils.staticfiles.DependencyFinder to STATICFILES_FINDERS:

STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'openwisp_utils.staticfiles.DependencyFinder',
]

Add openwisp_utils.loaders.DependencyLoader to TEMPLATES:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'OPTIONS': {
            'loaders': [
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader',
                # add the following line
                'openwisp_utils.loaders.DependencyLoader'
            ],
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    }
]

Last step, add FORM_RENDERER:

FORM_RENDERER = 'django.forms.renderers.TemplatesSetting'

Then you can go ahead and create your admin.py file following the example below:

# admin.py of your app
from django.contrib import admin

from django_loci.base.admin import (AbstractFloorPlanAdmin, AbstractFloorPlanForm,
                                    AbstractFloorPlanInline, AbstractLocationAdmin,
                                    AbstractLocationForm, AbstractObjectLocationForm,
                                    AbstractObjectLocationInline)
from django_loci.models import FloorPlan, Location, ObjectLocation


class FloorPlanForm(AbstractFloorPlanForm):
    class Meta(AbstractFloorPlanForm.Meta):
        model = FloorPlan


class FloorPlanAdmin(AbstractFloorPlanAdmin):
    form = FloorPlanForm


class LocationForm(AbstractLocationForm):
    class Meta(AbstractLocationForm.Meta):
        model = Location


class FloorPlanInline(AbstractFloorPlanInline):
    form = FloorPlanForm
    model = FloorPlan


class LocationAdmin(AbstractLocationAdmin):
    form = LocationForm
    inlines = [FloorPlanInline]


class ObjectLocationForm(AbstractObjectLocationForm):
    class Meta(AbstractObjectLocationForm.Meta):
        model = ObjectLocation


class ObjectLocationInline(AbstractObjectLocationInline):
    model = ObjectLocation
    form = ObjectLocationForm


admin.site.register(FloorPlan, FloorPlanAdmin)
admin.site.register(Location, LocationAdmin)

Extending channel consumers

Extend the channel consumer of django-loci in this way:

from django_loci.channels.base import BaseLocationBroadcast
from ..models import Location  # your own location model


class LocationBroadcast(BaseLocationBroadcast):
    model = Location

Extending AppConfig

You may want to reuse the AppConfig class of django-loci too:

from django_loci.apps import LociConfig


class MyConfig(LociConfig):
    name = 'myapp'
    verbose_name = _('My custom app')

    def __setmodels__(self):
        from .models import Location
        self.location_model = Location

Installing for development

Install sqlite:

sudo apt-get install sqlite3 libsqlite3-dev libsqlite3-mod-spatialite gdal-bin

Install your forked repo:

git clone git://github.com/<your_fork>/django-loci
cd django-loci/
python setup.py develop

Install test requirements:

pip install -r requirements-test.txt

Create database:

cd tests/
./manage.py migrate
./manage.py createsuperuser

Launch development server and SMTP debugging server:

./manage.py runserver

You can access the admin interface at http://127.0.0.1:8000/admin/.

Run tests with:

# pytests is used to test django-channels
./runtests.py && pytest

Contributing

  1. Announce your intentions in the OpenWISP Mailing List

  2. Fork this repo and install it

  3. Follow PEP8, Style Guide for Python Code

  4. Write code

  5. Write tests for your code

  6. Ensure all tests pass

  7. Ensure test coverage does not decrease

  8. Document your changes

  9. Send pull request

Changelog

See CHANGES.

License

See LICENSE.

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-loci-0.4.tar.gz (40.2 kB view details)

Uploaded Source

Built Distribution

django_loci-0.4-py2.py3-none-any.whl (43.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-loci-0.4.tar.gz.

File metadata

  • Download URL: django-loci-0.4.tar.gz
  • Upload date:
  • Size: 40.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for django-loci-0.4.tar.gz
Algorithm Hash digest
SHA256 d0d9451efbdd246740702e6a7628db455f558456fc024ffce6eac45cb017bd41
MD5 48e667ed465904e5e6dd2f5b22a617ff
BLAKE2b-256 6d759f643c96004992c5fc2addb1a3fd819d10ace86c2d69a55645b0e849d62e

See more details on using hashes here.

Provenance

File details

Details for the file django_loci-0.4-py2.py3-none-any.whl.

File metadata

  • Download URL: django_loci-0.4-py2.py3-none-any.whl
  • Upload date:
  • Size: 43.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for django_loci-0.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5ab7b5881bcad20c8a008874329b7a20006b01336bf172b4b755443ad6a41213
MD5 260db029c4d7a7f81c4f71cd9ab8fd69
BLAKE2b-256 528edbac48a3d04d35245268b3477cdd790e90660dff595b979b5b6337d9707c

See more details on using hashes here.

Provenance

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