检查点是否在作为csv文件的多边形内

时间:2017-11-15 02:05:06

标签: python python-3.5

首先我得到的是一个例子

poly = [190, 50, 500, 310]
bbPath = mplPath.Path(np.array([[poly[0], poly[1]],
                 [poly[1], poly[2]],
                 [poly[2], poly[3]],
                 [poly[3], poly[0]]]))

bbPath.contains_point((200, 100))

coordinate.csv文件有两列,第一列'经度190 50 500 310',第二列'纬度50 500 310 190'

我试过的是

with open('coordinate.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
for row in readCSV:
    print(row)

我不知道如何将csv数据导入路径并检查点是否在多边形内

1 个答案:

答案 0 :(得分:0)

您需要对csv文件的每一行执行一些简单的操作:

from matplotlib import path as mplPath
import numpy as np

with open('coordinate.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
        lon_list = row[0].split(' ') #split first column into words
        lat_list = row[1].split(' ') #split second column into words
        lonlat_zip = zip(lon_list[1:], lat_list[1:]) #zip lists together (skipping first word)
        lonlat_list = list(lonlat_zip) #create list
        lonlat_arr = np.array(lonlat_list) #create array
        bbPath = mplPath.Path(lonlat_arr) #create path
        result = bbPath.contains_point((200, 100))
        print(result)

我把这个过程分解成小步骤,你可以把它们拉到一起。显然,如果csv文件与描述不完全相同,则需要对其进行更改以适应。