ajnisbet / opentopodata

Open alternative to the Google Elevation API!
https://www.opentopodata.org
MIT License
314 stars 68 forks source link

Support samples along a path #37

Closed ajnisbet closed 3 years ago

ajnisbet commented 3 years ago

The google maps api lets you sample along a path. I don't have plans to support this any time soon in Open Topo Data, but a user submitted a patch for v1.5.0 to add this functionality!

Update 2021-09-04: The patch below has some issues, here's a better method that I'm adding: Sampling points along a lat,lon path

Old patch ``` --- opentopodata.orig/api.py 2021-03-10 16:36:58.761755648 +0100 +++ opentopodata/api.py 2021-03-21 09:43:48.369460334 +0100 @@ -190,6 +190,44 @@ return _parse_polyline_locations(locations, max_n_locations) +def _create_path_with_samples(lats, lons, samples): + """Create a path with the requested samples starting from given path + + + Args: + lats: The latitudes + lons: The longitudes + samples: Number of point in the segments of the path. + + Returns: + lats: List of latitude floats. + lons: List of longitude floats. + """ + + if(len(lats) < 2): + return lats, lons + if(int(samples) < 2): + samples = 2; + lats1 = [] + lons1 = [] + lat1 = lats[0] + lon1 = lons[0] + for i in range(1, len(lats)): + lat2 = lats[i] + lon2 = lons[i] + dlat = (lat2 - lat1) / (int(samples) - 1) + dlon = (lon2 - lon1) / (int(samples) - 1) + lat = lat1 + lon = lon1 + for i in range(0, int(samples)): + lats1.append(lat) + lons1.append(lon) + lat += dlat + lon += dlon + + return lats1, lons1 + + def _parse_polyline_locations(locations, max_n_locations): """Parse and validate locations in Google polyline format. @@ -399,6 +437,11 @@ request.args.get("locations"), _load_config()["max_locations_per_request"] ) + samples = request.args.get("samples") + if not samples: + samples = 2 + lats, lons = _create_path_with_samples(lats, lons, samples) + # Get the z values. datasets = _get_datasets(dataset_name) elevations, dataset_names = backend.get_elevation( ```
ajnisbet commented 3 years ago

Closing for now, will consider reopening if there's more demand

dansku commented 3 years ago

This would be extremely valuable for LOS calculations.

ajnisbet commented 3 years ago

This is a well-timed reminder, I implemented this for a different project recently so I'll work on adding it to Open Topo Data. I also notice now that the code I shared above is incorrect, it doesn't account for projection properly.

@dansku out of curiosity, what are LOS calculations?

bluthen commented 3 years ago

LOS is line of sight I think. So if you are planning radio or free space optic networks, maybe other applications.

dansku commented 3 years ago

Sorry indeed that is line of sight. I would like to know how can I get the altitude map between two points, for that i would need a few samples between points. Any idea how to add the correct implementation? Thank you

ajnisbet commented 3 years ago

This is now live: https://api.opentopodata.org/v1/ned10m?locations=36.3407,-121.7061|36.4212,-121.4990&samples=10