如何使用Python检查一个经纬度坐标是否在多边形内(考虑大圆线)

5

我有一个包含纬度/经度坐标的数据框:

      Lat       Lon
 29.39291 -98.50925
 29.39923 -98.51256
 29.40147 -98.51123
 29.38752 -98.52372
 29.39291 -98.50925
 29.39537 -98.50402
 29.39343 -98.49707
 29.39291 -98.50925
 29.39556 -98.53148

这些是构成多边形的坐标:

       Lat        Lon
 29.392945 -98.507696
 29.406167 -98.509074
 29.407234 -98.517039
 29.391325 -98.517166

我想使用Python检查第一个数据框的每个坐标是否在多边形内,并考虑大圆。
期望结果:
      Lat       Lon  Within
 29.39291 -98.50925       1
 29.39923 -98.51256       1
 29.40147 -98.51123       1
 29.38752 -98.52372       0
 29.39291 -98.50925       1
 29.39537 -98.50402       0
 29.39343 -98.49707       0
 29.39291 -98.50925       1
 29.39556 -98.53148       0
1个回答

4

从这里开始 What's the fastest way of checking if a point is inside a polygon in python ,假设您的多边形数据框为df_poly,点为df_points

from shapely.geometry import Point
from shapely.geometry.polygon import Polygon

polygon = Polygon([tuple(x) for x in df_poly[['Lat', 'Lon']].to_numpy()])
df_points['Within'] = df_points.apply(lambda x: polygon.contains(Point(x['Lat'], x['Lon'])), axis=1)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接