LoginSignup
5
5

More than 3 years have passed since last update.

[Python]多角形の座標で内側か外側か判定

Posted at

地図上の緯度経度で特定の設定領域内か、外か判定

メモ。
結果的には便利なライブラリを使ったほうが早かった
四角形、五角形などで地図上に座標でプロットしてその内側なら特定の処理をしたいという状況になったのでメモ。

車輪の再発明はしない。地理空間データ分析ライブラリを使う。

$ pip install turfpy

pointに現在点。
polygonは多角形という意味。点を指定。

領域内なら、boolean_point_in_polygonで、名前の通り、TureかFalseを返してくれる。便利。

from turfpy.measurement import boolean_point_in_polygon
from geojson import Point, Polygon, Feature

point = Feature(geometry=Point((0.5, 1.1)))
polygon = Polygon(
    [
        [
            (0.0, 0.0),
            (1.0, 2.0),
            (3.0, 2.0),
            (2.0, 0.0)
        ]
    ]
)

if boolean_point_in_polygon(point, polygon):
    print('領域内')
else:
    print('領域外')
領域外

あとは、緯度経度で指定すればOK

5
5
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
5
5