Open9

GeoDjangoをやるかも知れない

boiledorange73boiledorange73
% pkg install py39-django42
  • py39-asgiref: 3.7.2_1
  • py39-django42: 4.2.6
  • py39-sqlparse: 0.4.4
% pip freeze | grep -i django
Django==4.2.6
% pip freeze | grep -i pytz
pytz==2023.3
% mkdir ~/temp/geodjango
% cd ~/temp/geodjango
% django-admin startproject gdproject1
% ls
gdproject1/
% cd gdproject1
% pwd
% ~/temp/geodjango/gdproject1
% vi gdproject1/settings.py
#LANGUAGE_CODE = "en-us"
LANGUAGE_CODE = "ja-JP"

#TIME_ZONE = "UTC"
TIME_ZONE = "Asia/Tokyo"
% vi gdproject1/settings.py
DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": "gddb1",
        "USER": "geodjango",
        "PASSWORD": "(パスワード)",
        "HOST": "127.0.0.1",
        "PORT": "5432",
    }
}

(https://docs.djangoproject.com/en/4.2/ref/settings/#databases)

% createdb gddb1
% createuser geodjango
% psql -d gddb1
geodjango=# GRANT ALL ON DATABASE geodjango TO geodjango;
GRANT
geodjango=# ALTER USER geodjango WITH PASSWORD '(パスワード)';
ALTER ROLE
geodjango=# 
% python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK
% 
% python manage.py createsuperuser
ユーザー名 (leave blank to use 'foobar'): gdadmin
メールアドレス:  gdamin@localhost
Password: 
Password (again): 
このパスワードは短すぎます。最低 8 文字以上必要です。
このパスワードは一般的すぎます。
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.
% ./manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
October 30, 2023 - 18:55:25
Django version 4.2.6, using settings 'geodjango.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

外部からアクセスできない。

boiledorange73boiledorange73

https://blog.masasuzu.net/entry/20100731/1280588504 から。

% ./manage.py runserver 0.0.0.0:8000

WindowsのEdgeからアクセスしようとすると、いちおうサーバが応答したものの、次のエラー。

Invalid HTTP_HOST header: '(サーバIP):8000'. You may need to add '(サーバIP)' to ALLOWED_HOSTS.

これはエラーメッセージを読んだ通り。

% vi gdproject1/settings.py 
#ALLOWED_HOSTS = []
ALLOWED_HOSTS = ['(サーバIP)']

(たぶん引用符要る)

で、立ち上げ直し。

% ./manage.py runserver 0.0.0.0:8000

これでアクセスできるようになりました。

boiledorange73boiledorange73

アプリケーション作成

https://docs.djangoproject.com/ja/4.2/intro/tutorial01/

% ./manage.py startapp testapp1
% vi testapp1/views.py

元のファイルは次の通り。

from django.shortcuts import render

# Create your views here.

次のように変更。

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")
% vi testapp1/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path("", views.index, name="index"),
]
% vi gdproject1/urls.py
...

from django.contrib import admin
from django.urls import path
# 追加
from django.urls import include

urlpatterns = [
    # 追加
    path("testapp1/", include("testapp1.urls")),
    path("admin/", admin.site.urls),
]

自動で再起動します。

~/temp/geodjango/gdproject1/gdproject1/urls.py changed, reloading.
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
November 07, 2023 - 18:00:32
Django version 4.2.6, using settings 'gdproject1.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

トップページが Page Not Found に

/testapp1 → /testapp1/ にHTML表示
(index.html でない)

[02/Jan/2023 12:34:56] "GET /testapp1/ HTTP/1.1" 200 40
boiledorange73boiledorange73

国土数値情報(行政区域)を叩き込む

mncpl_table.sql

BEGIN;

CREATE TABLE mncpl (
  gid SERIAL PRIMARY KEY,
  objectid NUMERIC,
  n03_001 TEXT,
  n03_002 TEXT,
  n03_003 TEXT,
  n03_004 TEXT,
  n03_007 TEXT,
  shape_leng DOUBLE PRECISION,
  shape_area DOUBLE PRECISION,
  geom GEOMETRY(MULTIPOLYGON, 6668)
);

CREATE INDEX ON mncpl USING GiST (geom);

END;
% psql -d gddb1 -U postgres
gddb1=# create extension postgis;
CREATE EXTENSION
gddb1=# 
% shp2pgsql -s 6668 -a -D -W cp932 -i N03-23_230101.shp mncpl > mncpl_data.sql
...
% psql -d gddb1 -f mncpl_table.sql
% psql -d gddb1 -f mncpl_data.sql

以下は参考

% shp2pgsql -s 6668 -D -W cp932 -I -i N03-23_230101.shp mncpl > mncpl_data.sql
% psql -d gddb1 -f mncpl.sql
boiledorange73boiledorange73

https://homata.gitbook.io/geodjango/geodjango/geodjango

% vi gdproject1/settings.py
DATABASES = {
    "default": {
        # "ENGINE": "django.db.backends.postgresql",
        "ENGINE": "django.contrib.gis.db.backends.postgis",
...
INSTALLED_APPS = [
    ...
    "django.contrib.gis",
]
~/temp/geodjango/gdproject1/gdproject1/settings.py changed, reloading.
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
November 07, 2023 - 18:05:12
Django version 4.2.6, using settings 'gdproject1.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
boiledorange73boiledorange73
%  vi testapp1/models.py
from django.db import models

# Create your models here.
from django.contrib.gis.db import models

class Municipality(models.Model):
    n03_001 = models.CharField(max_length=255)
    n03_002 = models.CharField(max_length=255)
    n03_003 = models.CharField(max_length=255)
    n03_004 = models.CharField(max_length=255)
    n03_007 = models.CharField(max_length=255)
    geom     = models.MultiPolygonField(srid=6668)
% vi testapp1/admin.py
from django.contrib import admin

# Register your models here.
from .models import Mncpl

#admin.site.register(Mncpl, admin.GeoModelAdmin)
admin.site.register(Mncpl, admin.OSMGeoAdmin)
% vi gdproject1/urls.py
from django.contrib import admin
from django.urls import path
# 追加
from django.urls import include
# さらに追加
from django.contrib.gis import admin


urlpatterns = [
    # 追加
#    path("testapp1/", include("testapp1.urls")),
#    path("admin/", admin.site.urls),
    path("admin/", django.contrib.gis.admin),
]