I have a little function that parse a nested list of coordinates into a flat list of compressed coordinates. By compressed coordinates, I mean that only the delta (distance) between each coordinates are stored in the list and the float coordinates are transformed in integer.
input = [[-8081441,5685214], [-8081446,5685216], [-8081442,5685219], [-8081440,5685211], [-8081441,5685214]]
output = [-8081441, 5685214, 5, -2, -4, -3, -2, 8, 1, -3]
def parseCoords(coords):
#keep the first x,y coordinates
parsed = [int(coords[0][0]), int(coords[0][1])]
for i in xrange(1, len(coords)):
parsed.extend([int(coords[i-1][0]) - int(coords[i][0]), int(coords[i-1][1]) - int(coords[i][1])])
return parsed
parsedCoords = parseCoords(input)
As the input list is really big, is there a way to improve the function, maybe by using generators or list comprehension?