Total difference in elevation of a route

Hello,

I am new with openroute service. I was looking for a service to include some routing information in my (hobby) project. I got managed to calcualte a route and get the information for duration and distance.

I am now looking for the total differenc in evaluation of a route (for a hiking route). I guess that is somehow related to the geometry data, but I have no clue how to calculate it.

I am using C# and got following object

    public class Geometry
    {
        public float[][] Coordinates { get; set; }
        public string Type { get; set; }
    }

Any hint how to proceed. Many thanks!

Hi @fisfra
if you pass the "elevation": true parameter in your request body, it will return the total ascent and descent in the summary and segments areas.

Hi Adam,

many thanks for the swift reply.

I tried the following which works fine for the distance / duration but the summary does not include the elevation:

https://api.openrouteservice.org/v2/directions/driving-car?start=12.842420,47.080770&end=8.383194,49.002133&elevation=true

Clipboard Image

You need to use the body and a post to use most of the features of the API - the get version is the very basic start and end points.

So what you would need is something like the following in a POST request:

{
    "coordinates": [[12.842420, 47.08077], [8.383194, 49.002133]],
    "elevation": true
}

And that is a very long hiking route :slight_smile:

Many thanks - I see.

I am just trying to get the “post” c# example running. So far is either gives me a “internal server error” or “Content type ‘text/plain;charset=utf-8’ not supported” error while I thought I set the content type already to

            httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");

Anyway - I will check again if I did a mistake in my code…

Yes, but not.
See Total ascent or descent not in summary in geojson.
If requesting the route in geojson format, total ascent and descent are in the root of properties, not in summary.
I’ts OK in JSON format.

I finally got it work - I think the C# example is not (fully) correct. You need to set the encoding and content type when you create the StringContent object.

Find below a working example

Uri c_baseAddress = new Uri("https://api.openrouteservice.org/");
var profile = "foot-hiking";
 
using (var httpClient = new HttpClient { BaseAddress = c_baseAddress })
{
    // authentification
    httpClient.DefaultRequestHeaders.Clear();
    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8");
    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");
    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", c_apiKey);

    // build string with urls
    var requestString = "v2/directions/" + profile;
              
    var contentString = "{\"coordinates\":[[" + cord1.Longitude.ToString("0.000000", CultureInfo.InvariantCulture) + "," + cord1.Latitude.ToString("0.000000", CultureInfo.InvariantCulture) + "]," +
                        "[" + cord2.Longitude.ToString("0.000000", CultureInfo.InvariantCulture) + "," + cord2.Latitude.ToString("0.000000", CultureInfo.InvariantCulture) + "]],\"elevation\":\"true\"}";

    // send request
    using (var content = new StringContent(contentString, Encoding.UTF8, "application/json"))
    {
        var response = await httpClient.PostAsync(requestString, content);

        // get Json response
        string responseData = await response.Content.ReadAsStringAsync();

        // convert Json response to class
        data = JsonConvert.DeserializeObject<Rootobject>(responseData);
    }
}