Get n points on a line

2020-07-24 04:06发布

问题:

If I have a line defined by a start and end coordinates, how do I get n equally spaced points on that line, taking the curvature of the earth into account?

I'm looking for an algorithm, and/or a python library that implements this.

回答1:

Using geographiclib, a python implementation of GeographicLib, I was able to do this:

from geographiclib.geodesic import Geodesic

number_points = 10

gd = Geodesic.WGS84.Inverse(35, 0, 35, 90)
line = Geodesic.WGS84.Line(gd['lat1'], gd['lon1'], gd['azi1'])

for i in range(number_points + 1):
    point = line.Position(gd['s12'] / number_points * i)
    print((point['lat2'], point['lon2']))

output:

(35.0, -7.40353472481637e-21)
(38.29044006500327, 7.8252809205988445)
(41.01134777655358, 16.322054184499173)
(43.056180665524245, 25.451710440063902)
(44.328942450747135, 35.08494460239694)
(44.76147256654079, 45.00000000000001)
(44.328942450747135, 54.91505539760305)
(43.05618066552424, 64.54828955993611)
(41.01134777655356, 73.67794581550085)
(38.290440065003274, 82.17471907940114)
(34.99999999999999, 90.0


回答2:

You can use the npts method from pyproj's Geod class. See https://jswhit.github.io/pyproj/pyproj.Geod-class.html

Given a single initial point and terminus point (specified by python floats lon1,lat1 and lon2,lat2), returns a list of longitude/latitude pairs describing npts equally spaced intermediate points along the geodesic between the initial and terminus points.

Emphasis mine because I missed that at first.

First you create an Geod instance, specifying the ellipsoid you want it to use. Then you can call the method.

from pyproj import Geod
geod = Geod("+ellps=WGS84")
points = geod.npts(lon1=-89.6627, 
                   lat1=39.7658, 
                   lon2=147.2800, 
                   lat2=-42.8500, 
                   npts=100
                  )

points is now a list of tuples on the geodesic line between your start and end point:

[(-91.27649937899028, 39.21278457275204),
 (-92.86468478264302, 38.6377120347621),
 (-94.42723159402209, 38.04136774269571),
 (-95.96421169120758, 37.42453136174509),
 (-97.47578514283185, 36.78797425216882),
...