Django, a web framework made by Python, comes with a framework called GeoDjango that creates apps that use location information on Django.
I couldn't find much information about using GeoDjango, so I was addicted to it, so I will summarize it.
In order to use GeoDjango, you need to install an extension that can handle Geographic objects in a normally available DB such as PostgreSQL / SQLite / MySQL.
The environment construction with PostgreSQL + PostGIS is officially recommended, but this time SQLite + SpatiaLite is used. Build the way you used it.
Introduce SQLite and Spatia Lite.
The official documentation describes how to bring the package and install it, but it's easier to put it in with Homebrew.
Installing Spatialite | Django documentation | Django
$ brew install sqlite
$ brew install libspatialite
$ brew install spatialite-tools
$ brew install librasterlite
If you try to put it in using pip normally, you will get hooked later.
$ pip install pysqlite #Addictive
This time it is necessary to introduce from the source
$ curl -O https://pypi.python.org/packages/source/p/pysqlite/pysqlite-2.6.3.tar.gz
$ tar xzf pysqlite-2.6.3.tar.gz
$ cd pysqlite-2.6.3
$ $EDITOR setup.cfg
Rewrite setup.cfg
as follows.
[build_ext]
#define=
include_dirs=/usr/local/Cellar/sqlite/3.8.3/include
library_dirs=/usr/local/Cellar/sqlite/3.8.3/lib
libraries=sqlite3
#define=SQLITE_OMIT_LOAD_EXTENSION
$ python setup.py install
$ pip install -e .
I will change the ENGINE of settings.py
used in the project
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.spatialite',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
Change the backend to the one provided by gis.
Also, add contrib.gis
to ʻINSTALLED_APPS`
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.gis',
)
I will create a Model with a suitable Geographic field. anything is fine
from django.contrib.gis.db import models
from django.utils.translation import ugettext as _
class City(models.Model):
name = models.CharField(_('Name'), max_length=32)
location = models.PointField(_('Location'))
Don't forget to add it to ʻINSTALLED_APPS` as you normally would. Also, it is a good idea to create Admin for confirmation.
import django.contrib import admin
admin.site.register(City)
Before you normally syncdb
, you need to create a DB template using SpatiaLite.
$ spatialite db.sqlite3 "SELECT InitSpatialMetaData();"
the SPATIAL_REF_SYS table already contains some row(s)
InitSpatiaMetaData ()error:"table spatial_ref_sys already exists"
$ python manage.py syncdb
Complete when DB can be created as usual
Also, if the following error occurs at the time of syncdb
ImproperlyConfigured: The pysqlite library does not support C extension loading. Both SQLite and pysqlite must be configured to allow the loading of extensions to use SpatiaLite.
The installation of pysqlite
may not be successful. I solved it by building from source as described above.
If all goes well, you can edit the map from the Admin page.
It's a little harder to use than Google Map.
In addition to being able to save GeoDjango and latitude / longitude, it is also equipped with an O / R mapper that allows you to use search conditions such as having polygons on the map and including specific points.
However, it's Django, which is unprecedented in Japan, and its usage is limited, so I've never seen it used. I hope it becomes more fashionable.
Recommended Posts