简体   繁体   中英

Comparing X Y coordinates of the same list

I have a list of XY tuple coordinates. I am trying to eliminate the coordinates that are very close to each other using the euclidean distance. However, the code so far does not perform as expected, especially as the number of coordinates increases.

So far, I have found online how to compare two lists of coordinates, but not the elements within the same list. Hence, what i have done is slice the list in the first element and the remainder of the list and so the euclidean distance comparison. If within the proximity, it is element value is removed from the list. Then the list is updated and the procedure repeated. However, it does not perform as expected.

from scipy.spatial import distance

# List of coordinates.
xy = [(123, 2191), (44, 2700), (125, 2958), (41, 3368), (33, 4379), (78, 4434), (75, 5897), (50, 6220), (75, 7271), (80, 7274), (58, 8440), (60, 8440), (59, 8441), (32, 9699), (54, 9758), (58, 9759), (43, 10113), (64, 10252), (57, 12118), (61, 12120), (60, 14129), (61, 14129), (66, 15932), (68, 15933), (53, 17302), (57, 17304), (84, 20012), (84, 20013), (102, 20222), (49, 21257), (47, 21653), (56, 27042), (51, 28200), (48, 28201), (55, 28202), (65, 29366), (43, 29484), (67, 29808), (32, 30840), (31, 30842), (48, 36368), (48, 36369), (49, 36369), (21, 37518), (102, 37519)]

uni = []
for x in xy[:]:
    for i, j in enumerate(xy):
        if i == 0:
            new_xy = j # New List comprising of first element of the list
    remaining_xy = list(set(xy) - set(new_xy)) # rest of list converted into a separate list
    for m in remaining_xy:
        print(new_xy , m, distance.euclidean(new_xy , m))
        if distance.euclidean(new_xy ,m) < 1000: # If distance less then threshold, remove. 
            remaining_xy.remove(m)

    xy = remaining_xy #reset xy
    remaining_xy = [] #reset remaining_xy
    uni.append(new_xy) # append unique values. 

print(len((uni)), uni)

However, for example, the output shows

..., (53, 17302), (57, 17304), ...

Which does not satisfy the threshold.

For me your code is actually working. Maybe just change your last print statement to:

print(len(set(uni)), set(uni))

These outputs seem right for me. All coordinates in the set(uni) are more than 1000 apart from each other.

I get the following:

23 {(68, 15933), (58, 8440), (75, 7271), (51, 28200), (21, 37518), (61, 14129), (84, 20012), (65, 29366), (50, 6220), (49, 21257), (53, 17302), (41, 3368), (33, 4379), (64, 10252), (58, 9759), (56, 27042), (57, 12118), (78, 4434), (32, 30840), (31, 30842), (48, 36369), (48, 28201), (123, 2191)}

Update:

Unfortunately I haven't tested the complete output... I cannot directly find the issue in your code, but with a recursive function you will get the correct result you are looking for:

def recursiveCoord(_coordinateList):
    if len(_coordinateList) > 1:
        xy_0 = _coordinateList[0]
        remaining_xy = list(set(_coordinateList) - set(xy_0))

        new_xy_list = []

        for coord in remaining_xy:
            dist = distance.euclidean(xy_0 ,coord)

            if dist >= 1000:
                new_xy_list.append(coord)

         return [xy_0] + recursiveCoord(new_xy_list)
    else:
        return []

Call it like that:

uni = recursiveCoord(xy)

and you will get a list with all unique coordinates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM