2017-03-07 51 views
-1

我想写一个程序来创建线条和点使用匀称,当我尝试创建一个点从Linestring我得到以下错误,这仍然存在,即使我把整数:TypeError:__init __()需要正好4个参数(给出3)Point Shapely

为我的节目以下错误:

TypeError 
Traceback (most recent call last) 
    <ipython-input-356-a717c6058dbb> in <module>() 
    ----> 1 naive_2_meeting(floor_1,floor_2,floor_3,'b5ce04fe','b5ce04fe') 

    <ipython-input-355-cb9e84a49a> in naive_2_meeting(floor_1, floor_2, floor_3, uid1, uid2) 
    30     p = p.astype(float) 
    31     print(p[0][0],p[0][1]) 
---> 32     point = Point(1.0,2.0) 
    33     # check if intersection a line or a point 
    34     # line 1 

TypeError: __init__() takes exactly 4 arguments (3 given) 

和输出我得到的(这是线串,我需要的第一个坐标的点):

LINESTRING (62.45216400000002 25.25002557370135, 62.34162800000001 25.33145349735835) 

# need: (62.452164000000018, 25.25002557370135) 

我已经指出了问题区域,所以你不必阅读整个代码,我的问题是我不能创建一个点,我不知道为什么。 这里是我的代码:

def naive_2_meeting(floor_1,floor_2,floor_3,uid1,uid2): 
    flag = 0 
    # check floor 1, convert to numpy array for speed 
    floor_1_uid1 = floor_1[floor_1['uid']==uid1].sort_values(['epoch','x','y'], 
     ascending=[True,True,True]).as_matrix() 
    floor_1_uid2 = floor_1[floor_1['uid']==uid2].sort_values(['epoch','x','y'], 
     ascending=[True,True,True]).as_matrix() 

    # compare each line segment to each other 
    for i in range(0,len(floor_1_uid1)-1): 
     a = floor_1_uid1[i][0] 
     b = floor_1_uid1[i][1] 
     c = floor_1_uid1[i+1][0] 
     d = floor_1_uid1[i+1][1] 
     for j in range(0,len(floor_1_uid2)-1): 
      a1 = floor_1_uid2[j][0] 
      b1 = floor_1_uid2[j][1] 
      c1 = floor_1_uid2[j+1][0] 
      d1 = floor_1_uid2[j+1][1] 
      line1 = LineString([(a,b),(c,d)]) 
      line2 = LineString([(a1,b1),(c1,d1)]) 

      if line1.intersects(line2): 
       # find intersection point 
       point = line1.intersection(line2) 
       print (point) 
       if point.geom_type == 'LineString': 
        # need to extract the begining of the intersecting lineString 
        print('linestring intersection') 

        p = np.array(point) 
        p = p.astype(float) 
        print(p[0][0],p[0][1]) 

问题区域是在这里:

    point = Point(1.0,2.0) 
       # check if intersection a line or a point 
       # line 1 
       # length of segment 
       length_total = Point(a,b).distance(Point(c,d)) 
       length_first_mid = Point(a,b).distance(point) 
       #length_last_mid = Point(c,d).distance(point) 
       # calculate the time from first to last 
       epoch_diff = floor_1_uid1[i+1][4] - floor_1_uid1[i][4] 

       time_add = (epoch_diff * length_first_mid)/length_total 
       time_meet = floor_1_uid1[i][4] + time_add 

       # line 2 
       # length of segment 
       length_total_1 = Point(a1,b1).distance(Point(c1,d1)) 
       length_first_mid_1 = Point(a1,b1).distance(point) 
       #length_last_mid = Point(c,d).distance(point) 
       # calculate the time from first to last 
       # assume uniform velocity along an interpolated line 
       epoch_diff_1 = floor_1_uid1[j+1][4] - floor_1_uid1[j][4] 

       time_add_1 = (epoch_diff_1 * length_first_mid_1)/length_total_1 
       time_meet_1 = floor_1_uid1[j][4] + time_add_1 

       #is the time within a certain time range 
       if np.absolute(time_meet_1-time_meet)<20: 
        print ('They both intersect at ',line1.intersection(line2)) 
        flag = 1 

       # else it means they cross paths at different times 
      elif line1.distance(line2) < 5: 
       # find the point 
       print('They both intersect') 
       flag = 1 
       return 

    if flag == 0: 
     print('They do not intersect') 

任何意见是值得欢迎的,我使用的是从身材匀称错误的呢?

+0

@downvoter谢谢你downvoting没有提到我可以改进。 – LoveMeow

+1

您是否使用'from shapely.geometry import Point'导入Point? – James

+0

是的,我做过,适用于交点(点)不是线条的情况 – LoveMeow

回答

0

尝试Point((1.0,2.0))(加上括号)。此外,请添加print Point并检查显示的整齐的导入类(只是为了确保您没有通过重新定义Point来覆盖该类)。

0

@bruno desthuilliers发表的评论让我检查了所有最近安装的库,问题是我最近安装的矢量库,一旦删除问题就不见了!谢谢!

相关问题