I'm trying to filter coordinates from 2 lists taken from a txt file by distance, I can't figure out what is going wrong with it since it is not deleting every coordinate that doesn't pass the if statement if distance is greater than 12 meters, delete item in list.
code:
x = [] # contains a list of x coordinates in EPGS: 2202
y = [] # contains a list of y coordinates in EPGS: 2202
keepItWorking = 0 # this is supposed to avoid offset once a coordinate is deleted.
xStore = x[0] # Stores x variable to evaluate distance and delete coordinates.
yStore = y[0] # Stores y variable to evaluate distance and delete coordinates.
def distance(x1, x2, y1, y2):
return (math.sqrt(((x2 - x1)**2) + ((y2 - y1)**2)))
for i in range(1, len(x)):
try:
if distance(x[i - keepItWorking], xStore, y[i - keepItWorking], yStore) > 12 #if distance is greater than 12 store coordinates values and proceed to delete next coordinates with the new evaluation.
xStore = x[i - keepItWorking]
yStore = y[i - keepItWorking]
elif distance(x[i - keepItWorking], xStore, y[i - keepItWorking], yStore) < 12 # if distance is lower than 12 meters delete values from list.
del x[i - keepItWorking]
del y[i - keepItWorking]
keepItWorking = keepItWorking + 1
except IndexError: # avoids error when index gets out of range becouse of the items eliminations.
continue
Apparently I fixed the issue I was having while rewriting the code in here... It is working perfectly.