Skip to content

Support samples along a path #37

Description

@ajnisbet

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(

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions