//private static Microsoft.Maps.MapControl.WPF.LocationCollection GetListOfLocations() //{ // string text = File.ReadAllText(@"C:\GIT\private\Mapsui_mytest\Itinero_test\bin\x64\Debug\route.geojson"); // GeoJsonModel parsed = JsonConvert.DeserializeObject<GeoJsonModel>(text); // var locs = new Microsoft.Maps.MapControl.WPF.LocationCollection(); // foreach (Feature feature in parsed.features) // { // if (feature.geometry.coordinates[0] is Newtonsoft.Json.Linq.JArray startingCoordinates) // { // // // locs.Add(new Microsoft.Maps.MapControl.WPF.Location((float)startingCoordinates.Last(), (float)startingCoordinates.First())); // } // ///var startingPoint = new Point((double)startingCoordinates.Last(), (double)startingCoordinates.First()); // if (feature.geometry.coordinates[1] is Newtonsoft.Json.Linq.JArray endingCoordinates) // { // locs.Add(new Microsoft.Maps.MapControl.WPF.Location((float)endingCoordinates.Last(), (float)endingCoordinates.First())); // } // if (feature.geometry.coordinates[0] is double longitude && feature.geometry.coordinates[1] is double latitude) // { // locs.Add(new Microsoft.Maps.MapControl.WPF.Location(latitude, longitude)); // } // // // ///var endingPoint = new Point((float)endingCoordinates.Last(), (float)endingCoordinates.First()); // //polyline.Points.Add(startingPoint); // //polyline.Points.Add(endingPoint); // } // return locs; //} private static Itinero.Route GetRoute() { RouterDb routerDb = null; using (FileStream stream = new FileInfo(@"C:\GIT\private\map\quebec.routerdb").OpenRead()) { routerDb = RouterDb.Deserialize(stream); } var router = new Itinero.Router(routerDb); //get a profile Itinero.Profiles.Profile profile = Vehicle.Car.Fastest(); //the default OSM car profile //routerDb.AddContracted(profile); //dodawanie tego trwa bardzo długo, może się opłacać zrobić to przed wyznaczaniem wielu tras var from = new Itinero.LocalGeo.Coordinate(45.532400f, -73.622885f); var to = new Itinero.LocalGeo.Coordinate(45.545841f, -73.623474f); //create a routerpoint from a location //snaps the given location to the nearest routable edge RouterPoint start = router.TryResolve(profile, from, 200).Value; RouterPoint end = router.TryResolve(profile, to, 200).Value; var points = new List <RouterPoint>() { router.TryResolve(profile, from, 200).Value, router.TryResolve(profile, to, 200).Value }; //calculate a route //var route = router.Calculate(profile, start, end); Result <Itinero.Route> route = router.TryCalculate(profile, points.ToArray()); return(route.Value); }
/// <summary> /// Builds the shortest path between the two coordinates as a referenced line. /// </summary> public static ReferencedLine BuildLine(this Coder coder, Itinero.LocalGeo.Coordinate coordinate1, Itinero.LocalGeo.Coordinate coordinate2, out Route route) { // calculate raw path. var weightHandler = coder.Router.GetDefaultWeightHandler(coder.Profile.Profile); var source = coder.Router.Resolve(coder.Profile.Profile, coordinate1, 100); var target = coder.Router.Resolve(coder.Profile.Profile, coordinate2, 100); var path = coder.Router.TryCalculateRaw(coder.Profile.Profile, weightHandler, source, target, coder.Profile.RoutingSettings); if (path.IsError) { throw new InvalidOperationException("No route found."); } var pathDistance = path.Value.Weight; // build route. route = coder.Router.BuildRoute(coder.Profile.Profile, weightHandler, source, target, path.Value).Value; // build referenced line by building vertices and edge list. var pathAsList = path.Value.ToList(); var edges = new List <long>(); var vertices = new List <uint>(); for (var i = 0; i < pathAsList.Count; i++) { vertices.Add(pathAsList[i].Vertex); if (i > 0) { if (pathAsList[i].Edge != Itinero.Constants.NO_EDGE && pathAsList[i].Edge != -Itinero.Constants.NO_EDGE) { edges.Add(pathAsList[i].Edge); } else { var edgeEnumerator = coder.Router.Db.Network.GeometricGraph.Graph.GetEdgeEnumerator(); float best; var edge = edgeEnumerator.FindBestEdge(weightHandler, vertices[vertices.Count - 2], vertices[vertices.Count - 1], out best); edges.Add(edge); } } } // makersure first and last are real vertices. var sourceOffset = 0f; if (vertices[0] == Constants.NO_VERTEX) { var edge = coder.Router.Db.Network.GetEdge(edges[0]); if (edge.From == vertices[1]) { sourceOffset = Itinero.LocalGeo.Coordinate.DistanceEstimateInMeter(coordinate1, coder.Router.Db.Network.GetVertex(edge.To)); vertices[0] = edge.To; } else if (edge.To == vertices[1]) { sourceOffset = Itinero.LocalGeo.Coordinate.DistanceEstimateInMeter(coordinate1, coder.Router.Db.Network.GetVertex(edge.From)); vertices[0] = edge.From; } else { throw new Exception("First edge does not match first vertex."); } } var targetOffset = 0f; if (vertices[vertices.Count - 1] == Constants.NO_VERTEX) { var edge = coder.Router.Db.Network.GetEdge(edges[edges.Count - 1]); if (edge.From == vertices[vertices.Count - 2]) { targetOffset = Itinero.LocalGeo.Coordinate.DistanceEstimateInMeter(coordinate2, coder.Router.Db.Network.GetVertex(edge.To)); vertices[vertices.Count - 1] = edge.To; } else if (edge.To == vertices[vertices.Count - 2]) { targetOffset = Itinero.LocalGeo.Coordinate.DistanceEstimateInMeter(coordinate2, coder.Router.Db.Network.GetVertex(edge.From)); vertices[vertices.Count - 1] = edge.From; } else { throw new Exception("Last edge does not match last vertex."); } } var totalDistance = pathDistance + sourceOffset + targetOffset; return(new ReferencedLine() { Edges = edges.ToArray(), Vertices = vertices.ToArray(), NegativeOffsetPercentage = 100.0f * (targetOffset / totalDistance), PositiveOffsetPercentage = 100.0f * (sourceOffset / totalDistance), StartLocation = coder.Router.Db.CreateRouterPointForEdgeAndVertex(edges[0], vertices[0]), EndLocation = coder.Router.Db.CreateRouterPointForEdgeAndVertex(edges[edges.Count - 1], vertices[vertices.Count - 1]) }); }
/// <summary> /// Builds the shortest path between the two coordinates as a referenced line. /// </summary> public static ReferencedLine BuildLine(this Coder coder, Itinero.LocalGeo.Coordinate coordinate1, Itinero.LocalGeo.Coordinate coordinate2) { Route route; return(coder.BuildLine(coordinate1, coordinate2, out route)); }
/// <summary> /// Builds a point along line location. /// </summary> public static ReferencedPointAlongLine BuildPointAlongLine(this Coder coder, Itinero.LocalGeo.Coordinate coordinate, out RouterPoint resolvedPoint) { return(coder.BuildPointAlongLine(coordinate.Latitude, coordinate.Longitude, out resolvedPoint)); }
/// <summary> /// Returns an estimate of the distance between the two given coordinates. /// </summary> /// <remarks>Accuraccy decreases with distance.</remarks> public static float DistanceEstimateInMeter(Coordinate coordinate1, Coordinate coordinate2) { return(Coordinate.DistanceEstimateInMeter(coordinate1.Latitude, coordinate1.Longitude, coordinate2.Latitude, coordinate2.Longitude)); }