簡體   English   中英

從多個坐標創建線並以百分比從該線獲取點

[英]Create line from multiple coorinates and get points from that line with a percentage

我有多個 GPS 坐標點我想從 python 創建一條線。 這些點不在一條直線上,但足夠精確,可以用直線連接它們。 我知道如何將一個點連接到另一個點,但不知道如何將這些奇異線中的多條連接到更長的一條,然后根據整條線的百分比得到一個點。

我使用此代碼來獲取單數行的百分比:

def pointAtPercent(p0, p1, percent):
    if p0.x != p1.x:
        x = p0.x + percent * (p1.x - p0.x)
    else:
        x = p0.x;

    if p0.y != p1.y:
        y = p0.y + percent * (p1.y - p0.y)
    else:
        y = p0.y

    p = point()
    p.x = x
    p.y = y

    return p;

這是一個示例列表:

[ 10.053417,
53.555737,
10.053206,
53.555748,
10.052497,
53.555763,
10.051125,
53.555757,
10.049193,
53.555756,
10.045511,
53.555762,
10.044863,
53.555767,
10.044319,
53.555763,
10.043685,
53.555769,
10.042765,
53.555759,
10.04201,
53.555756,
10.041919,
53.555757,
10.041904,
53.555766
]

您可以創建x,y對的列表並根據列表的長度訪問 GPS 點:

points = [
    10.053417, 53.555737, 10.053206, 53.555748, 10.052497, 53.555763, 10.051125,
    53.555757, 10.049193, 53.555756, 10.045511, 53.555762, 10.044863, 53.555767,
    10.044319, 53.555763, 10.043685, 53.555769, 10.042765, 53.555759, 10.04201,
    53.555756, 10.041919, 53.555757, 10.041904, 53.555766
]

points = [(points[i], points[i + 1]) for i in range(0, len(points) - 1, 2)]


def pointAtPercent(points, percent):
    lstIndex = int(
        len(points) / 100. * percent
    )  # possibly creates some rounding issues !
    print(points[lstIndex - 1])


pointAtPercent(points, 10)
pointAtPercent(points, 27.5)
pointAtPercent(points, 50)
pointAtPercent(points, 100)

出去:

(10.053417, 53.555737)
(10.052497, 53.555763)
(10.045511, 53.555762)
(10.041904, 53.555766)

基本算法是這樣的:

  • 確定每個段的長度。 您處於球極坐標中(假設地球是一個球體,但事實並非如此(如果您需要更高的精度,請參閱 WGS 84)),所以您可以這樣做
def great_circle_distance(lat_0, lon_0, lat_1, lon_1):
    return math.acos(
        math.sin(lat_0) * math.sin(lat_1)
        + math.cos(lat_0) * math.cos(lat_1) * math.cos(lon_1 - lon_0)
    )


radian_points = [(math.radians(p.x), math.radians(p.y)) for p in points]
lengths = [
    great_circle_distance(*p0, *p1) for p0, p1 in zip(radian_points, radian_points[1:])
]
path_length = sum(lengths)
  • 給定一個百分比,您可以計算出它沿着路徑有多遠。
distance_along = percentage * path_length
  • 找到正確段的索引。
# Inefficient but easy (consider bisect.bisect with a stored list of sums)
index = max(
    next(i for i in range(len(lengths) + 1) if sum(lengths[:i]) >= distance_along) - 1,
    0,
)

  • 然后使用您的原始算法。
point = pointAtPercent(
    points[index],
    points[index + 1],
    (distance_along - sum(lengths[:index])) / lengths[index],
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM