[PYTHON] GeoDjango + SQLite environment construction on OS X

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.

Select the DB to use

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.

Introduced SQLite and SpatiaLite

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

Introduction of pysqlite

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 .

Changes to settings.py

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',
)

Model definition

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)

DB creation

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.

Verify that the model is displayed on the Admin page

If all goes well, you can edit the map from the Admin page.

Screen Shot 2014-02-13 at 13.54.42 .jpg

It's a little harder to use than Google Map.

Summary

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

GeoDjango + SQLite environment construction on OS X
Mac OS X Mavericks 10.9.5 Development environment construction
Mac OS X Yosemite 10.10 Development environment construction
Mac OS X development environment construction memo
Mac OS X Mountain Lion 10.8.5 Development environment construction
Anaconda environment construction on CentOS7
Building an environment for "Tello_Video" on Mac OS X
Build a Python development environment on Mac OS X
Memo on Mac OS X
[Tensorflow] Tensorflow environment construction on Windows 10
R environment construction with Jupyter (formerly IPython notebook) (on OS X El Capitan 10.11.3)
Linux environment construction (on WSL environment)
Create a Python development environment on OS X Lion
Python environment construction memo on Windows 10
Anaconda python environment construction on Windows 10
Start of self-made OS 1. Environment construction
Anaconda environment construction on Mac (2018 version)
Install Sphinx on Mac OS X
Python environment construction memo on Mac
Python development environment construction on macOS
Install mitmproxy on Mac OS X
[Linux] Docker environment construction on Redhat
Environment construction of python3.8 on mac
Environment construction of "Tello_Video" on Ubuntu
OpenCV3 & Python3 environment construction on Ubuntu
Install pgmagick on Mac OS X 10.9
Continuation ・ Notes on preparing the Python development environment on Mac OS X
Environment construction procedure for the gym "Open AI Gym" that trains AI in games on Mac OS X
Install matplotlib on OS X El Capitan
Building a LaTeX environment on Chrome OS
Installed aws-cli On Mac OS X Lion
Run NASA CEA on Mac OS X
Build an environment with pyenv, pyenv-virtualenv, jupyter on OS X El Capitan
Django environment construction
[Note] Python environment construction on rental server "CORESERVER"
DeepIE3D environment construction
Emacs-based environment construction
Linux environment construction
Python environment construction
I installed Pygame with Python 3.5.1 in the environment of pyenv on OS X
Environment construction (python)
django environment construction
[0] TensorFlow-GPU environment construction built with Anaconda on Ubuntu
Run Zookeeper x python (kazoo) on Mac OS X
CodeIgniter environment construction
python environment construction
Installing TensorFlow 0.11.0rc2 on OS X El Capitan (10.11.6)
Python environment construction
[Environment construction] Oracle DB x Pro * C [Now]
Python 3.x environment construction by Pyenv (CentOS, Ubuntu)
Golang environment construction
python environment construction
Shpinx (Python documentation builder) on Mac OS X
Install LightGBM in an OS X virtualenv environment
Word2vec environment construction
Preparing to use aws cli on Mac OS X
[Environment construction] @anaconda that runs keras / tensorflow on GPU
Build a machine learning Python environment on Mac OS
Very easy to install SciPy on Mac OS X
How to install caffe on OS X with macports
Try using E-Cell 4 on Windows 7 or Mac OS X