たぶん動く...

多分GIS系の人。あくまで個人的見解であり、所属団体を代表するものではありません。

GeoDjangoをUbuntu環境で構築する

TTY6335は一応、GIS系の人です。
家でADSBの収集をしており、1秒ごとの受信データをgeojsonファイルで保存しているのですが、ファイル数が数百万ファイルになり、データベースで管理することを思いつきました。

GUIベースで管理できそうなので、GeoDjangoを選定しました。
ネット記事のGeoDjangoの環境構築はみんなWindowsで構築しており、Ubuntuで構築している記事がほとんどありませんでした。 公式のガイドも微妙....

そこで今回はUbuntu環境でGeoDjangoを構築したので、そのメモ。

dockerのubuntuにインストールしていきます。

構築環境

Ubuntu 20.04.2 LTS (Focal Fossa)
PostgreSQL 12.6
plpgsql 1.0
Python 3.8.5
django 3.1.7

Ubuntuのコンテナをつくる

geodjangoという名前のコンテナをつくり、この中にgeodjango環境を構築していきます。

docker pull ubuntu:latest   
docker run -dit -v /home/saga/workplace:/media/workplace:Z -p 8080:8000 --name geodjango ubuntu:latest  

最初の設定

とりあえずupdateとupgradeしてvimを入れる

apt-get update && apt-get upgrade
apt-get install vim  

pythonとpipを入れる

apt-get install python3  
apt-get install python3-pip  
root@2c60898901b6:/# python3 -V
Python 3.8.5

日本語でおk

localectl set-locale LANG=ja_JP.UTF-8

GDALを入れる

apt-get install python3-gdal gdal-bin gdal-data

postgisと関連ソフトを入れる。 postgisの依存関係をインストールすればpostgresもインストールされる。

apt-get install postgis
apt-get install binutils libproj-dev gdal-bin
apt-get install python3-psycopg2 python3-psycopg2-dbg

postgresの設定

postgres起動 データベース関係はこちら を参照

/etc/init.d/postgresql start

postgresの認証方法をpeerからtrustに変更する。この辺詳しくないのですべてpeer->trustにしてパスワードなしでデータベースにアクセスできるようにする。

/etc/postgresql/12/main/pg_hda.conf
# Database administrative login by Unix domain socket
local   all             postgres                               trust

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust
host    replication     all             ::1/128                 trust

postgres再起動

/etc/init.d/postgresql restart
postgresにpostgresというユーザーをつくる。
su postgres -c 'psql --username=postgres'

パスワードはpostgresにする

postgresというユーザーでgeodjangodbというロールを作る

createdb -U postgres -E UTF-8 geodjangodb

postgisのEXTENSIONを入れる

psql -U postgres -d geodjangodb -c "CREATE EXTENSION postgis;"

django_adminというユーザーを作る。

postgres=#CREATE USER django_admin WITH PASSWORD 'django_admin';
postgres=#GRANT ALL PRIVILEGES ON DATABASE geodjangodb TO django_admin;

GeoDjangoのプロジェクトを作成

ホーム直下にtutorialというプロジェクトを作る

django-admin startproject tutorial
python manage.py startapp map

tutorial/settings.py の設定

#dockerコンテナで環境構築しているのですべてのIPからアクセスを許可する。
ALLOWED_HOSTS = ['*']

NSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.gis',  #add
    'map.apps.MapConfig',  #add
]

# setting for postgis
DATABASES = {
   'default': {
       'ENGINE': 'django.contrib.gis.db.backends.postgis',
       'NAME': 'geodjangodb',
       'USER': 'django_admin',
       'HOST':'localhost',
       'PASSWORD': 'django_admin',
   }
}
LANGUAGE_CODE = 'ja'

TIME_ZONE = 'UTC'

GeoDjango起動

python manage.py makemigrations
python manage.py migrate
python3 manage.py createsuperuser #ユーザーはroot パスワードはgeodjangoで設定。
python3  manage.py runserver 0.0.0.0:8000

起動をブラウザで確認

f:id:TTY6335:20210227222511p:plain

参考文献

GeoDjango Installation | Django documentation | Django

GeoDjango入門チュートリアル

Pythonのインストール · HonKit