如何通过特定的多义线对点进行分类

时间:2019-02-27 17:34:25

标签: python gis shapefile shapely fiona

中国内部有一个边界,将区域划分为南北。我已将此边界绘制为折线格式shapefile Download link

我想将下图中的这些点分为“北部”和“南部”。 Python中是否有任何有用的功能可以实现这一目标。

fiona 具有 point.within 功能来测试多边形内/外的点,但是我没有搜索合适的功能来按折线划分多个点。

任何建议或技巧将不胜感激!

enter image description here

已更新

根据Prune提出的宝贵建议,我将其解决了。提供的代码如下:

from shapely.geometry import shape
from shapely.geometry import LineString
# loading the boundary layer
import fiona
fname = './N-S_boundary.shp'
line1 = fiona.open(fname)
line1 = shape(line1.next()['geometry']) 
# set a end point which is the southernmost for all stations. 
end_point  = (dy[dy['lat']==dy['lat'].min()]['lon'].values[0],dy[dy['lat']==dy['lat'].min()]['lat'].values[0])
# loop all monitoring stations for classification
dy['NS']= np.nan
for i in range(0,len(dy),1):
    start_point = (dy['lon'].iloc[i],dy['lat'].iloc[i])
    line2       = LineString([start_point, end_point])
    if line1.intersection(line2).is_empty:
        dy["NS"].iloc[i]='S'
    else:
        dy["NS"].iloc[i]='N'     
color_dict= {'N':'steelblue','S':'r'}
dy['site_color']=dy['NS'].map(color_dict)   

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以从拓扑中应用简单的属性。

首先,请确保您的边界对Universe进行了分区(您正在处理的所有可用点)。您可能需要将边界扩展到整个海洋。

现在,选择任何标记有该区域的参考点-要定义“北”和“南”,您必须至少有一个这样的点。 w.l.o.g.假设它是一个称为Z的“南”点。

现在,对于要分类的每个点A,画一条从AZ的连续路径(通常最简单,但不是必需的)。找到该路径与边界的交点。如果交叉点数量相等,则AZ属于同一类(“南部”);否则,它在另一个类别(“北方”)中。

请注意,这需要“分区”的拓扑属性-边界线没有切线:如果路径接触边界,则路径必须完全交叉。