繁体   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