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; }
}
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.
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
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);
}
}