不明白,索引器错误:数组的索引太多了

2024-04-19 20:56:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我的任务是删除经纬度坐标,如果点之间的距离在一个特定的距离内(5公里、10公里或30公里)。这是为了建模和避免点的聚集。我用哈弗辛方程来测量距离。在

以下是我的初始代码:

load the geometry record from points,  
then convert it to an array, 
compare each coordinate pairs and measure distance. 
After that, remove the longitude and latitude pairs that are   
close to each other, 

但被困在这一步。在

我计划更新坐标对的项目列表,并使用新的坐标对再次迭代。在

运行以下脚本会出现以下错误:

IndexError: too many indices for array

迭代中的索引似乎没有更新。它在第一次通过时仍然得到指数。在

^{pr2}$

Traceback

File "<ipython-input-3-8e88eba2ab54>", line 1,  in <module>  
  dist_haversine(filepath,input_dist,input_crop)
File "<ipython-input-2-d43a1f1da26a>", line 20, in 
  dist_haversine  
    for i,j in itertools.izip(acoords[:,0],acoords[:,1]):  
IndexError: too many indices for array

Tags: andthetoin距离forinputthat
1条回答
网友
1楼 · 发布于 2024-04-19 20:56:06

这是我用来过滤点的初始解决方案。它可以工作,而且对于1000-3000点的数据集来说有点快。然而,尝试过滤50000个点,花了2.5-3个小时才完成。在

def dist_haversine(filepath,input_dist,input_crop):
    """
    Calculate the great circle distance between two points 
    on the earth (specified in decimal degrees)
    """

    r = shapefile.Reader(filepath)
    idx = np.arange(len(r.records()))
    coordinates = []
    for i in idx:
        geom = r.shape(i)
        coordinates.append(geom.points[0])       

    acoords = np.array(coordinates)

    index = []        
    for r,n,l in itertools.izip(acoords[:,0],acoords[:,1],idx):
        if l in index:
            continue
        else:
            for i,j,k in itertools.izip(acoords[:,0],acoords[:,1], idx):
                if k in index:
                    continue

                else:

                    lon1=r
                    lat1=n
                    lon2=i
                    lat2=j

                    coord_check = ((lon1 == lon2) & (lat1 == lat2))*1

                    if coord_check == 1:
                        continue

                    else:
                        lon1, lat1, lon2, lat2 = map(math.radians, [lon1, lat1, lon2, lat2])

                        # haversine formula
                        dlon = lon2 - lon1 
                        dlat = lat2 - lat1 
                        a = math.sin(dlat/2)**2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon/2)**2
                        c = 2 * math.asin(math.sqrt(a)) 
                        km = c*6371 #/1000.0

                    if km < input_dist:
                        if k in index:
                            continue
                        else:
                            index.append(k)

    filterList = [i for j, i in enumerate(coordinates) if j not in index]

    df_coords = pd.DataFrame(filterList, columns=['Lon','Lat'])

    df_coords.insert(0, 'Crop', input_crop)  

    return df_coords.to_csv(directory + "\\" + "%s_distFilter_%skm.csv" % (input_crop, input_dist), sep=",", index=None)

相关问题 更多 >