Upgrade to Pro — share decks privately, control downloads, hide ads and more …

GeoDjangoでWeb地図をつくろう

mamix1116
November 04, 2018
140

 GeoDjangoでWeb地図をつくろう

FOSS4Gの発表です。

mamix1116

November 04, 2018
Tweet

Transcript

  1. Code for CAT $.$'  ! ( WS - ),*

    $%"  NPO PTA#&  +   
  2.   G (myenv)$ django-admin startproject geodjango # G shell

    .- . G P .- < G U _ .- . G S < < W UL G P - . < G S P < G LI R S P _ . < G RI S P
  3.  # A C UI = # LANGUAGE_CODE = 'en-us’

    LANGUAGE_CODE = ‘ja’ E= = ' ' # / geodjango/settings.py
  4.  # PS E DATABASES = { 'default’: { #

    ‘ENGINE’: ‘Django.db.backends.sqlite3’ # ‘NAME’: os.path.join(BASE_DIR, ‘db.sqlite3’), 'ENGINE': 'django.contrib.gis.db.backends.postgis’, 'NAME': 'geodjango’, 'USER': 'geo’, }, } L NT E , ' , . : : = :A : : = A # : : = A _[ A I ] D L _[ geodjango/settings.py
  5.    5@ %A>2! C&>2 6C&>2 9; 20052016 1,8453

    *?B%$ B-.,)1C&1 BG(I"#"/84+E" *?B suinanjiko.geojson {"type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": { "type": "Point", "coordinates": [ 131.491361 ,34.186589 ] }, "properties": { "no":1, "ym":200504, "pref":" 7)", "river":"J&", "jiko":"C' F  HD 0* & <=", "accurate":"01.:” } }, :
  6. geojson     (myenv)$ python manage.py ogrinspect --srid=4326

    jiko.geojson Suinanjiko # This is an auto-generated Django model module created by ogrinspect. from django.contrib.gis.db import models class Suinanjiko(models.Model): no = models.IntegerField() ym = models.IntegerField() pref = models.CharField(max_length=0) river = models.CharField(max_length=0) jiko = models.CharField(max_length=0) accurate = models.CharField(max_length=0) geom = models.PointField(srid=4326) shell $ python manage.py ogrinspect <> < >
  7. Geojson models.py from django.contrib.gis.db import models class Suinanjiko(models.Model): no =

    models.IntegerField() ym = models.IntegerField() pref = models.CharField(max_length=50) river = models.CharField(max_length=50) jiko = models.CharField(max_length=256) accurate = models.CharField(max_length=256) geom = models.PointField(srid=4326) def __str__(self): return self.jiko # max_length= # str()  
  8.     load_suinanjiko.py import os from django.contrib.gis.utils import

    LayerMapping from map.models import Suinanjiko # Model     mapping = { 'no' : 'no’, 'ym' : 'ym’, : 'geom' : 'POINT’, } #   geojson_file = os.path.abspath( os.path.join(os.path.dirname(__file__), 'data’, 'suinanjiko.geojson’)) #  def run(verbose=True): lm = LayerMapping(Suinanjiko, geojson_file, mapping,transform=False, encoding='UTF-8’) lm.save(strict=True, verbose=verbose)
  9.     (myenv)$ python manage.py shell >>> from

    map import load_Hokkaido >>> load_suinanjiko.run()  Saved : ~~~~ Saved : ~~~~ : >>> exit() shell Django load_suinanjiko.py  
  10.    map/admin.py from django.contrib.gis import admin from map.models

    import Suinanjiko admin.site.register(Suinanjiko, admin.OSMGeoAdmin) #OpenStreetMap geodjango/urls.py # from django.contrib import admin from django.contrib.gis import admin from django.urls import path urlpatterns = [ path('admin/', admin.site.urls), ]
  11. Django REST framework(DRF) (myenv)$ pip install djangorestframework-gis (myenv)$ pip install

    django-filter (myenv)$ pip install markdown (myenv)$ pip freeze : djangorestframework==3.8.2 djangorestframework-gis==0.13 django-filter==1.1.0 Markdown==2.6.11 shell    djangorestframework-gis : DRF    django-filter : "  markdown : Markdown! 
  12.  geodjango/settings.py INSTALLED_APPS = [ 'django.contrib.admin’, 'django.contrib.auth’, : 'django.contrib.gis', 'map',

    'django_filters’, 'rest_framework’, 'rest_framework_gis’, 'markdown', ]     settings.py
  13.  map/ serializers.py from rest_framework_gis import serializers from world.models import

    Suinanjiko Class SuinanjikoSerializer(serializers.GeoFeatureModelSerializer): class Meta: model = Suinanjiko geo_field = 'geom’ fields = ('__all__')    API