将坐标列表排序成条状

时间:2019-05-06 15:23:03

标签: python csv coordinates nested-lists

我正在从csv文件中读取纬度和经度坐标。我的目标是获取坐标并将其按列表的列表(即[[strip 1 coordiantes],[strip 2 coordiantes],[strip 3 coordinates]]

)分类到各自的提示中。

最终目标是使用这些条带识别要用于其他工作的最外面的拐角航路点。仅仅获取所有坐标的标准max,min x和y不起作用,因为这些点的方向不固定。

从这里开始:

[['41.55275997', '21.97765353'], ['41.55273016', '21.97763498'], ['41.55270034', '21.97761643'], ['41.55267052', '21.97759788'], ['41.55264071', '21.97757933'], ['41.55270455', '21.97740142'], ['41.55273436', '21.97741997'], ['41.55276418', '21.97743852'], ['41.55279399', '21.97745707'], ['41.55282381', '21.97747562'], ['41.55275997', '21.97765353'], ['41.55273016', '21.97763498'], ['41.55270034', '21.97761643'], ['41.55267052', '21.97759788'], ['41.55264071', '21.97757933']]

对此:

[[['41.55275997', '21.97765353'], ['41.55273016', '21.97763498'], ['41.55270034', '21.97761643'], ['41.55267052', '21.97759788'], ['41.55264071', '21.97757933']], [['41.55270455', '21.97740142'], ['41.55273436', '21.97741997'], ['41.55276418', '21.97743852'], ['41.55279399', '21.97745707'], ['41.55282381', '21.97747562']], [['41.55275997', '21.97765353'], ['41.55273016', '21.97763498'], ['41.55270034', '21.97761643'], ['41.55267052', '21.97759788'], ['41.55264071', '21.97757933']]]

我的计划是在两点之间使用上升/运行计算。但是,随着点的倾斜度随坐标移动到另一条带而变化,并且再次比较同一条带上的点时,我不确定如何继续。

enter image description here

邮政编码:

# get waypoint coordiantes
coordinateList = []
csv_file.seek(0)
next(csv_reader)
#add all coordinates in csv to a single list
for line in csv_reader:
    coordinateList.append([line[0],line[1]])
print(coordinateList)

#Take coordinate list (list of lists) and add coordinates to lists reprenting a s ingle stip

#Get the rise over run from the first two coordinates.
rise = float(coordinateList[0][1]) - float(coordinateList[1][1])
run = float(coordinateList[0][0]) - float(coordinateList[1][0])
print(rise,run)
#add first two coordiantes to a strip
coordStips = [[coordinateList[0],coordinateList[1]]]

#iterate through remaining coordiantes and compare
for coord1,coord2 in zip(coordinateList[1:-1:], coordinateList[2::]):
    #print(coord1,coord2)
    rise = float(coord2[1]) - float(coord1[1])
    run = float(coord2[0]) - float(coord1[0])
    print(rise,run)

感谢您的帮助。

编辑:这是我当前计算的斜率。不知道为什么它们都略有不同。

0.622065727665411
0.622065727665411
0.6222744045453561
-2.7868107768422306
0.6222744045453561
0.6220657278136351
0.6222744045453561
0.622065727665411
-2.7868107768422306
0.6222744046936797
0.622065727665411
0.622065727665411
0.6222744045453561

解决方案:

# get waypoint coordiantes
        coordinateList = []
        csv_file.seek(0)
        next(csv_reader)
        #add all coordinates in csv to a single list
        for line in csv_reader:
            coordinateList.append([line[0],line[1]])
        print(coordinateList)

        #Take coordinate list (list of lists) and add coordinates to lists reprenting a s ingle stip

        #Get the rise over run from the first two coordinates.
        rise = float(coordinateList[0][1]) - float(coordinateList[1][1])
        run = float(coordinateList[0][0]) - float(coordinateList[1][0])
        masterslope = rise/run

        #---Strip List set Up
        #add first two coordiantes to a strip
        coordStrips = [[coordinateList[0],coordinateList[1]]]
        stripCount = 0
        switch = False


        #----------Iteration
        #iterate through remaining coordiantes and compare
        for coord1,coord2 in zip(coordinateList[1:-1:], coordinateList[2::]):
            #if previous waypoint was found to be on a new strip
            if switch == True:
                coordStrips[stripCount].append(coord2)

                rise = float(coord2[1]) - float(coord1[1])
                run = float(coord2[0]) - float(coord1[0])
                masterslope = rise/run

                switch = False
                continue
            #print(coord1,coord2)
            rise = float(coord2[1]) - float(coord1[1])
            run = float(coord2[0]) - float(coord1[0])
            slope = rise/run
            diff = abs(masterslope-slope)
            #they are in the same strip, add to current strip
            if diff < 0.5:
                coordStrips[stripCount].append(coord2)
            #new strip
            else:

                stripCount+= 1
                coordStrips.append([coord2])
                switch = True

1 个答案:

答案 0 :(得分:2)

我认为您的做法正确。假设属于一个“条带”的点已经在列表中在一起,您只需要记住当前组的斜率并在点的斜率为w.r.t的情况下继续添加点即可。最后一点(大约)与最后一个斜率相同。

points = [tuple(map(float, t)) for t in coordinateList]

x2, y2 = points[0]
groups = [[(x2, y2)]]
slope = None
close = lambda a, b: abs(a - b) < epsilon

for x, y in points[1:]:
    if slope is None or close(x-x2, slope[0]) and close(y-y2, slope[1]):
        groups[-1].append((x,y))
        slope = (x-x2, y-y2)
    else:
        groups.append([(x,y)])
        slope = None
    x2, y2 = x, y

棘手的部分是何时考虑坡度“足够近”。以您的示例为例,从epsilon1e-4的任何1e-7似乎都有效,从而导致了分组

[(41.55275997, 21.97765353), (41.55273016, 21.97763498), (41.55270034, 21.97761643), (41.55267052, 21.97759788), (41.55264071, 21.97757933)]
[(41.55270455, 21.97740142), (41.55273436, 21.97741997), (41.55276418, 21.97743852), (41.55279399, 21.97745707), (41.55282381, 21.97747562)]
[(41.55275997, 21.97765353), (41.55273016, 21.97763498), (41.55270034, 21.97761643), (41.55267052, 21.97759788), (41.55264071, 21.97757933)]