How does the Google Elevation API 'explode' a polyline into equidistant points?

164 Views Asked by At

The Elevation API provided by Google, uses two parameters for requesting height along a path: path and samples. The path is the lat and lng of the starting and the finish points and the samples is the sample points along a path for which to return elevation data. So the path is divided into an ordered set of equidistant points along the path, which are sent back to the client as an array of lat, lng and elevation for each point. This can be useful for visualizing elevation profiles or such.

I have seen other open-source alternatives for the Google Elevation API, however, none of them has an easy way to provide a samples parameter, so if one wants to achieve the same, you simply have to somehow 'explode' the path yourself into multiple equidistant points and send this array as part of the URL to the back-end.

Is there a library or algorithm I should consider when splitting the path (which is usually of type LineString / polyline) into equidistant points? In general, how should I approach this problem?

1

There are 1 best solutions below

0
On BEST ANSWER

I run Open Topo Data and GPXZ, which are open and paid (respectively) alternatives to the Google Elevation API, and both support sampled paths!

The algorithm I use is detailed in Sampling points along a lat,lon path, and the Python code for doing this in Open Topo Data is on here on GitHub.

But in general, here's how to approach this problem:

  • Calculate the distance (in linear units like metres) of each segment of the path.
  • Add all of that up to get a total path distance.
  • For each sample s_i:
    • The distance d_i of sample i along the path is total_path_distance * i / n_samples.
    • Find the segment that contains the sample point as d_segment_start <= d_i < d_segment_end.
    • The location of sample s_i is the result of travelling along the segment identified in the previous step by a distance of d_segment_start - d_i

Some practical considerations

  • You need to work in linear units (ft, km) rather than angular units (lats, lons) when dealing with distance.
  • The calculations for distances are complicated due to the earth's roundness and lumpiness, so use a 3rd-party library. I'm not familiar with javascript so much, but after a quick google it looks like the geodesy package would be a good place to start.
  • Be careful about lons wrapping around the date line from +180 to -180, ideally this something the library would handle.
  • To travel along a segment by a certain distance, you can project a bearing from the starting point by the angle between the two points and get pretty close, but a 3rd-party geodesy library might have slightly more accurate methods for handling this step.