概要
GeoDjango + postGIS でRESTfulなAPIを作って、GeoJsonを含むjsonデータをpost/getしたいと考えています。
そこで、初期データとしてjsonファイルをロード
参考サイト:Django Rest Framework GISで誰でも簡単RESTful Geo API - monomotiの日記
してデータベースに保存された情報をDjangoのコンソール画面で確認したところ、jsonデータのうちGeoJsonの部分だけEWKTの形式に置換されてしまいました。
GeoJsonの状態で保持するにはどうしたら良いでしょうか。
Djangoの管理画面で確認されたpostGISの保持データ
ソースコード
ロードしたい初期データ
geojsondata.json
json
1{ 2 "type": "FeatureCollection", 3 "crs": { "type": "label", "properties": { "label": "urn:ogc:def:crs:OGC:1.3:CRS84" } }, 4 "features": [ 5 { 6 "properties": { 7 "category": "バー", 8 "label": "Live bar Teasin'" 9 }, 10 "geometry": { 11 "coordinates": [ 12 135.5118818, 13 34.6845511 14 ], 15 "type": "Point" 16 }, 17 "type": "Feature", 18 "id": 439 19 }, 20 { 21 "properties": { 22 "category": "カフェ", 23 "label": "伽奈泥庵" 24 }, 25 "geometry": { 26 "coordinates": [ 27 135.5156035, 28 34.6695098 29 ], 30 "type": "Point" 31 }, 32 "type": "Feature", 33 "id": 440 34 }, 35 { 36 "properties": { 37 "category": "パブ", 38 "label": "Marciero" 39 }, 40 "geometry": { 41 "coordinates": [ 42 135.502859, 43 34.703484 44 ], 45 "type": "Point" 46 }, 47 "type": "Feature", 48 "id": 441 49 }, 50 51 { 52 "properties": { 53 "category": "レストラン", 54 "label": "浜勝" 55 }, 56 "geometry": { 57 "coordinates": [ 58 129.8814474, 59 32.7435253 60 ], 61 "type": "Point" 62 }, 63 "type": "Feature", 64 "id": 545 65 }, 66 67 { 68 "properties": { 69 "category": "レストラン", 70 "label": "二刀流" 71 }, 72 "geometry": { 73 "coordinates": [ 74 135.415729, 75 34.719976 76 ], 77 "type": "Point" 78 }, 79 "type": "Feature", 80 "id": 547 81 } 82 ], 83 "type": "FeatureCollection" 84 }
初期データを読み込むためのスクリプト
load_data.py
python
1import os 2from django.contrib.gis.utils import LayerMapping 3from my_app.models import Geometry 4 5geojson_mapping = { 6 'label' : 'label', 7 'category' : 'category', 8 'geom' : 'POINT', 9} 10 11geojson_data = os.path.abspath(os.path.join(os.path.dirname(__file__), 'data', 'geojsondata.json')) 12 13def run(verbose=True): 14 lm = LayerMapping(Geometry, geojson_data, geojson_mapping, 15 transform=False, encoding='UTF-8') 16 17 lm.save(strict=True, verbose=verbose)
モデル
models.py
python
1from django.contrib.gis.db import models 2 3class Geometry(models.Model): 4 label = models.CharField(max_length=50,null=False,blank=True,default="") 5 category = models.CharField(max_length=50,null=False,blank=True,default="") 6 geom = models.PointField(srid=4326) 7 8 def __str__(self): 9 return self.label
シリアライザ
serializers.py
python
1from rest_framework import serializers 2from my_app.models import Geometry 3 4class GeometrySerializer(serializers.ModelSerializer): 5 class Meta: 6 model=Geometry 7 fields='__all__' 8 read_only_fields=('__all__',)
備考
データベースのシェルでCREATE EXTENSION postgis;
してあります
環境
- python3.6.6
- django2.1.2
- postgreSQL10.5
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。