簡體   English   中英

如何在兩個經緯度點之間插入緯度/經度點(路線)

[英]How to Interpolate Lat/Long Points (Route) between Two Lat Long Points

假設我們提供了兩個地址。 我從下面的網上隨機抓取了兩個:

# Google headquarters:
google_lat = 37.422131
google_lon = -122.084801

# Apple headquarters
apple_lat = 37.33467267707233
apple_lon = -122.0089722675975

現在,假設我想在這兩個地址之間插入一系列點。 也許對於初學者來說,一個簡單的線性插值(或考慮到地球的球形性質)會起作用。 在實踐中,如果我可以獲得每個點之間每 n 英里的緯度/經度數組,那么通過谷歌地圖提供的道路路線就可以了。 可能有一個 REST API 來獲取兩個地址之間的點嗎?

只是為了開始一些想法,如果我不能采用上面的 API 方法,我怎么能開始處理這樣的任務? 我想要的最低限度是每個地址之間的緯度/經度數組以及其中的點。

Python 中的示例會很好,但也歡迎與語言無關的討論。 謝謝。

您提供了一對 (x1, y1) 和 (x2, y2) 位置,用一條線段連接它們。

計算一對增量:

  • delta_x = x2 - x1
  • delta_y = y2 - y1

現在使用參數形式的方程對線段進行插值。 t從 0 .. 1 變化到你喜歡的步長,然后發出

(x1 + t * delta_x, y1 + t * delta_y)

為您的插值點。

這是初學者的基本線性插值示例。

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()

# Google headquarters:
google_lat = 37.422131
google_lon = -122.084801

# Apple headquarters
apple_lat = 37.33467267707233
apple_lon = -122.0089722675975

# latitude is the x-coordinate
# longitude is the y-coordinate
# lets say we wanted to figure out a series of points between the two given with linear interpolation

latitudes = np.linspace(apple_lat, google_lat, 10)  # ten points
longitudes = (google_lon - apple_lon)/(google_lat - apple_lat)*(latitudes - apple_lat) + apple_lon

ax.plot(latitudes, longitudes, marker='o')
ax.set_xlabel('Latitude')
ax.set_ylabel('Longitude')

for x, y in zip(latitudes, longitudes):
    print(x, y)

plt.show()

輸出:

在此處輸入圖像描述

點之間:

37.33467267707233 -122.0089722675975
37.34439026850873 -122.01739768230888
37.354107859945145 -122.02582309702028
37.36382545138155 -122.03424851173166
37.37354304281796 -122.04267392644306
37.38326063425437 -122.05109934115444
37.392978225690776 -122.05952475586584
37.40269581712718 -122.06795017057722
37.412413408563594 -122.07637558528862
37.422131 -122.084801

但就像你說的,你需要考慮地球的曲率,這意味着有一個額外的 z 軸需要處理。 這需要進一步研究和更多地磨練你的線性代數技能,因為這肯定需要從 xy 平面到三維空間的某種變換。

如果您要大量使用幾何圖形,Shapely 可能是一個值得研究的庫:

您還可以將這些點投影到地圖上,以便考慮地球的曲率。 可以在此處找到有關如何完成的文檔。

import shapely
from shapely.geometry import Point, LineString
from shapely.ops import interpolate
import numpy as np

# Google headquarters:
google_lat = 37.422131
google_lon = -122.084801

# Apple headquarters
apple_lat = 37.33467267707233
apple_lon = -122.0089722675975

google = Point(google_lat, google_lon)
apple = Point(apple_lat, apple_lon)

line = LineString([google, apple])

pts = []
for div in np.arange(0.1,1,0.1):
   pts.extend(line.interpolate(div, normalized=True).coords[:])

print(pts)

輸出:

[(37.41338516770723, -122.07721812675975), (37.404639335414466, -122.0696352535195),
 (37.3958935031217, -122.06205238027925), (37.38714767082893, -122.054469507039),
 (37.37840183853616, -122.04688663379875), (37.369656006243396, -122.0393037605585),
 (37.36091017395063, -122.03172088731824), (37.35216434165786, -122.024138014078),
 (37.343418509365094, -122.01655514083775)]

暫無
暫無

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

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