Created
May 31, 2014 13:32
-
-
Save xivk/70c61418ab90f4729d2a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
using OsmSharp.Math.Geo; | |
using OsmSharp.Routing; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Text; | |
namespace OsmSharp.Service.Client.PCL | |
{ | |
public static class RoutingFacade | |
{ | |
public static OsmSharp.Routing.Route Calculate(OsmSharp.Routing.Vehicle vehicle, GeoCoordinate from, GeoCoordinate to) | |
{ | |
var requestData = "{ " + | |
"\"Type\": \"Regular\", " + | |
"\"ReturnType\": \"Route\", " + | |
"\"Vehicle\": \"{vehicle}\", " + | |
"\"Hooks\": [ " + | |
"{ " + | |
"\"Id\": 1, " + | |
"\"Latitude\": {lat_from}, " + | |
"\"Longitude\": {lon_from}, " + | |
"\"Tags\": [ " + | |
"{ " + | |
" \"Key\": \"key\", " + | |
" \"Value\": \"value\" " + | |
"} " + | |
"] " + | |
"}, " + | |
"{ " + | |
"\"Id\": 2, " + | |
"\"Latitude\": {lat_to}, " + | |
"\"Longitude\": {lon_to}, " + | |
"\"Tags\": [ " + | |
"{ " + | |
" \"Key\": \"key\", " + | |
" \"Value\": \"value\" " + | |
"} " + | |
"] " + | |
"} " + | |
"] " + | |
"} "; | |
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://build.osmsharp.com:666/routing?format=json"); | |
httpWebRequest.ContentType = "text/json"; | |
httpWebRequest.Method = "POST"; | |
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) | |
{ | |
var json = requestData.Replace("{vehicle}", vehicle.UniqueName).Replace("{lat_from}", from.Latitude.ToInvariantString()).Replace("{lon_from}", from.Longitude.ToInvariantString()) | |
.Replace("{lat_to}", to.Latitude.ToInvariantString()).Replace("{lon_to}", to.Longitude.ToInvariantString()); | |
streamWriter.Write(json); | |
streamWriter.Flush(); | |
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); | |
string routeJSON = string.Empty; | |
using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) | |
{ | |
routeJSON = streamReader.ReadToEnd(); | |
} | |
var routingResponse = JsonConvert.DeserializeObject<RoutingResponse>(routeJSON,new JsonSerializerSettings | |
{ | |
TypeNameHandling = TypeNameHandling.Objects | |
}); | |
return routingResponse.Route.ConvertTo(); | |
} | |
} | |
public class RoutingResponse | |
{ | |
public string Status { get; set; } | |
public string StatusMessage { get; set; } | |
public Route Route { get; set; } | |
} | |
public class Route | |
{ | |
public Vehicle Vehicle { get; set; } | |
public OsmSharp.Routing.RoutePointEntry[] Entries { get; set; } | |
public DateTime TimeStamp { get; set; } | |
public double TotalDistance { get; set; } | |
public double TotalTime { get; set; } | |
//"TimeStamp":"\/Date(1401542840838+0100)\/", | |
//"TotalDistance":3551.93346688342, | |
//"TotalTime":479.775044906941 | |
public OsmSharp.Routing.Route ConvertTo() | |
{ // converts to and actual route. | |
return new OsmSharp.Routing.Route() | |
{ | |
Vehicle = OsmSharp.Routing.Vehicle.GetByUniqueName(this.Vehicle.UniqueName), | |
Entries = this.Entries, | |
TimeStamp = this.TimeStamp, | |
TotalDistance = this.TotalDistance, | |
TotalTime = this.TotalTime | |
}; | |
} | |
} | |
public class Vehicle | |
{ | |
public string UniqueName { get; set; } | |
} | |
//class DTOJsonConvertor: Newtonsoft.Json.JsonConverter | |
//{ | |
// public override bool CanConvert(Type objectType) | |
// { | |
// if(objectType.FullName == ) | |
// } | |
// public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | |
// { | |
// throw new NotImplementedException(); | |
// } | |
//} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment