简体   繁体   English

将坐标列表排序成条状

[英]Sort list of coordinates into strips

I am reading in lat and long coordinates from a csv file. 我正在从CSV文件读取纬度和经度坐标。 My goal is to take the coordinates and sort them into their respective stips as a list of lists ie [[strip 1 coordiantes],[strip 2 coordiantes],[strip 3 coordinates]] 我的目标是获取坐标并将其分类为各自的提示,作为列表的列表,例如[[strip 1 coordiantes],[strip 2 coordiantes],[strip 3 coordinates]]

The ultimate goal is to use these strips to identify the outer most corner waypoints to be used for other work. 最终目标是使用这些条带来确定要用于其他工作的最外面的角点。 Simply getting standard max, min x and y of all coordinates doesn't work as the orientation of the points is not fixed. 由于各点的方向不固定,仅获取所有坐标的标准max,min x和y无效。

Go from this: 从此:

[['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']]

To this: 对此:

[[['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']]]

My plan was to use rise/run calculation between the points. 我的计划是在两点之间使用上升/运行计算。 However as the points slope changes as the coordinate moves to another strip and again when comparing points on same strip I am unsure how to proceed. 但是,由于点的斜率随坐标移动到另一个条带而变化,因此当比较同一条带上的点时,我又不确定如何继续。

在此处输入图片说明

Curr Code: 当前代码:

# 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)

Any help is appreciated. 任何帮助表示赞赏。

EDIT: Here is the slopes that I have currently calculated. 编辑:这是我目前计算的斜率。 Not sure why they are all slightly different. 不知道为什么它们都略有不同。

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

Solution: 解:

# 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

I think you are on the right track. 我认为您在正确的轨道上。 Assuming that points that belong to one "strip" are already together in the list, you just have to remember the slope of the current group and keep adding points while their slope wrt the last point is (roughly) the same as the last slope. 假设属于一个“条带”的点已在列表中在一起,您只需要记住当前组的斜率并继续添加点,而它们的斜率与最后一个点(大约)与最后一个斜率大致相同。

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

The tricky part is when to consider the slopes "close enough". 棘手的部分是何时考虑坡度“足够近”。 For your example, any epsilon from 1e-4 to 1e-7 seems to work, resulting in the groups 以您的示例为例,从1e-41e-7任何epsilon似乎都起作用,这导致了组

[(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)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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