我试图找出地理位置是否为多边形。我在网上找到了this function。我试图从csv文件传递一个lat和lon列表。
当我尝试单独传递变量时它会起作用,但是当我把它放在循环中时会遇到错误。
我是python的新手,不知道出了什么问题。任何帮助表示赞赏。
data = pd.read_csv('opp.csv')
lat = data['lat'][1]
lon = data['lon'][1]
print lat, lon, type(lat), type(lon)
zone1 = point_in_poly(lat, lon, zone1)
print zone1
#This works and return the following
#41.87639 -87.65328 <type 'numpy.float64'> <type 'numpy.float64'>
OUT
for i in range(2):
print i
lat = data['lat'][i]
lon = data['lon'][i]
print lat, lon,type(lat), type(lon)
zone1 = point_in_poly(lat, lon, zone1)
print zone1
#I got an error as below
File "point_in_poly.py", line 8, in point_in_poly
if (x,y) in poly: return "IN"
TypeError: 'in <string>' requires string as left operand, not tuple
答案 0 :(得分:0)
在第二个代码段中,请查看以下行:
zone1 = point_in_poly(lat, lon, zone1)
您将point_in_poly
(字符串)的返回值分配给您用作方法参数的同一变量。因此,在下一次迭代point_in_poly
尝试在该保存的字符串中查找(lat,lon)元组并产生该错误。
只需将您的代码修改为以下内容:
result = point_in_poly(lat, lon, zone1)
print result