4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【GIS】geojsonをpython で作る

Last updated at Posted at 2022-06-26

はじめに

地図上に緯度経度で指定した点や線分をプロットするために、geojson という形でデータをやり取りすることがあります。おじさんは以前に使用したことがあるのですが、今回、また少し触るので、思い出しながらメモに書きます。

内容

GeoJson

どこかに良き導入資料はないかと探しますが、WikiPediaやpython module のindtroduction にしかたどり着けませんでした。まぁ、それでよいか。

こちら、定義はなんとなく分かったのですが、authorize されているドキュメントはどれかな。できれば読みやすいものがよいけれど。

以下メモ。

  • データは三層構成
    • geometry(点、線、ポリゴン等)
    • feature (geometry + properties)
    • featureをまとめたもの
  • properties には、線の太さとか、国名だとか追加できる。地図表示用の要素は CSS に準拠?するらしい??
  • WEBサービスを使って確認できる

Feature に表示のための属性をもたせます。

{
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [125.6, 10.1]
  },
  "properties": {
    "name": "Dinagat Islands"
  }
}

が一つのFeature です。ここで、geomtry の type は Point, LineString, Polygon および、それらを複数持つ MultiPoint, MultiLineString, MultiPolygon が定義されています。properties には、点や線の大きさ、太さ、色を指定することもできます。

		"properties": {
			"stroke": "#555555",
			"stroke-width": 2,
			"stroke-opacity": 1,
			"fill": "#00aa22",
			"fill-opacity": 0.5
		},

GeoJson CSS Style というようです。Authorizeされたページを探そうと思ったのですが見つかりませんでした。

Python geojson

python module があるので、それを利用します。

$ pip install geojson

問題なくインストールできました。

例えば、移動軌跡を状態によって色分けして表示する、という場合には、複数の線分を下記の過程をそのままpython で実行できました。

  1. MultiLineString をgeometory として持つ、Feature を作る
  2. 1のFeatureを配列として持つ。FeatureCollection という型として持つ。
  3. FeatureCollection を geojson として dump する

ここでは、以下の方法で二つの Point にそれぞれ属性を付けたFeature を持つ geojson を作ってみます。

緯度経度と場所の名前、地図上に表示したい色を事前に用意しておきます。
それらを、

  • geometry には緯度経度を指定した Point
  • properties には、名前の他、表示用の項目を設定
    としてFeature を作り、それらを束ねてFeatureCollection としました。
test_geojson.py
from geojson import Point, Feature, FeatureCollection, dump
outfile = "out.geojson"

PointsData = [{'name': 'Ho Chi Minh City Opera House',
               'latitude': 10.777406835725145,
               'longitude': 106.70299858740313,
               'color': '#ff2600'},
              {'name': 'Independence Palace',
               'latitude': 10.778137451508647,
               'longitude': 106.69531332149265,
               'color': '#00ff26'}]

# Prepare Geojson FeatureCollection
ft_all = []
for i, p in enumerate(PointsData):
    lat, lon = p['latitude'], p['longitude']
    ft = Feature(geometry = Point((lon, lat,)),
                 properties = {'name': p['name'], 'marker-color': p['color'],
                               'marker-size': 'medium','marker-symbol': ''})
    ft_all.append(ft)
ft_colct = FeatureCollection(ft_all)

with open(outfile, 'w') as f:
    dump(ft_colct, f, indent=2)

これをpython3 test_geojson.pyなどで実行していただくと、以下のgeojson が生成されます。

out.geojson
{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          106.702999,
          10.777407
        ]
      },
      "properties": {
        "name": "Ho Chi Minh City Opera House",
        "marker-color": "#ff2600",
        "marker-size": "medium",
        "marker-symbol": ""
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          106.695313,
          10.778137
        ]
      },
      "properties": {
        "name": "Independence Palace",
        "marker-color": "#00ff26",
        "marker-size": "medium",
        "marker-symbol": ""
      }
    }
  ]
}

確認方法

地図上に表示してくれるサイトがいくつかありました。

ここで、特に登録は必要ありません。Open でFile を選択し、生成したgeojson のファイルをアップロードすると下記のように表示してくれました。

image.png

またサイゴン行きたいな。

まとめ

geojson を python で使ってみた。できたら、java script?を使って地図を表示するところも作ってみたい。
1時間くらいのつもりが3時間以上かけてしまった。。。
今後やってみたいのは、

  • 設定する緯度軽度の小数点以下の桁数の設定。precision という引数で設定した気がしたが。
  • open data から地形情報をとってきて、これに変換する
    (2022/06/26)
4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?