/// <summary> /// Converts the given coordinate to and OpenLR coordinate. /// </summary> public static OpenLR.Model.Coordinate ToCoordinate(this Itinero.LocalGeo.Coordinate coordinate) { return(new OpenLR.Model.Coordinate() { Latitude = coordinate.Latitude, Longitude = coordinate.Longitude }); }
/// <summary> /// Encodes the edge that the given location resolves on. /// </summary> public static string EncodeClosestEdge(this Coder coder, Itinero.LocalGeo.Coordinate location, float searchDistanceInMeter = 50) { return(coder.Encode(coder.BuildEdge( coder.Router.Resolve(coder.Profile.Profile, location, searchDistanceInMeter).EdgeIdDirected()))); }
/// <summary> /// Encodes the edge that the given location resolves on. /// </summary> public static string EncodeClosestEdge(this Coder coder, Itinero.LocalGeo.Coordinate location) { return(coder.Encode(coder.BuildEdge( coder.Router.Resolve(coder.Profile.Profile, location).EdgeIdDirected()))); }
/// <summary> /// Finds candidate vertices for a location reference point. /// </summary> public static IEnumerable <CandidateLocation> FindCandidateLocationsFor(this Coder coder, LocationReferencePoint lrp, float maxVertexDistanceInMeter = 40, float candidateSearchBoxSize = 0.01f) { // build candidates list. var scoredCandidates = new List <CandidateLocation>(); var lrpCoordinate = new Itinero.LocalGeo.Coordinate((float)lrp.Coordinate.Latitude, (float)lrp.Coordinate.Longitude); // get vertices and check their edges. var vertices = coder.Router.Db.Network.GeometricGraph.Search((float)lrp.Coordinate.Latitude, (float)lrp.Coordinate.Longitude, candidateSearchBoxSize); var candidates = new HashSet <long>(); var edgeEnumerator = coder.Router.Db.Network.GetEdgeEnumerator(); foreach (var v in vertices) { if (candidates.Contains(v)) { // vertex was already considered. continue; } var vertexLocation = coder.Router.Db.Network.GetVertex(v); var distance = Itinero.LocalGeo.Coordinate.DistanceEstimateInMeter(vertexLocation, lrpCoordinate); if (distance < maxVertexDistanceInMeter) { candidates.Add(v); // check if there are edges that can be used for the given profile. edgeEnumerator.MoveTo(v); RouterPoint location; if (coder.Router.Db.TryCreateRouterPointForVertex(v, coder.Profile.Profile, out location)) { scoredCandidates.Add(new CandidateLocation() { Score = Score.New(Score.VERTEX_DISTANCE, string.Format("The vertex score compare to max distance {0}", maxVertexDistanceInMeter), (float)System.Math.Max(0, (1.0 - (distance / maxVertexDistanceInMeter))), 1), // calculate scoring compared to the fixed max distance. Location = location }); } } } var candidatesQualityOK = false; foreach (var candidate in scoredCandidates) { if (candidate.Score.Value / candidate.Score.Reference >= System.Math.Min(coder.Profile.ScoreThreshold + coder.Profile.ScoreThreshold, 0.7)) { // at least one above threshold, keep it. candidatesQualityOK = true; } } if (!candidatesQualityOK) { // no candidates, create a virtual candidate. var routerPoints = coder.Router.ResolveMultiple(new Itinero.Profiles.Profile[] { coder.Profile.Profile }, lrpCoordinate.Latitude, lrpCoordinate.Longitude, maxVertexDistanceInMeter); if (routerPoints.Count == 0) { throw new Exception("No candidates found for LRP: Could not resolve a point at the location."); } foreach (var routerPoint in routerPoints) { var distance = Itinero.LocalGeo.Coordinate.DistanceEstimateInMeter(routerPoint.LocationOnNetwork(coder.Router.Db), lrpCoordinate); scoredCandidates.Add(new CandidateLocation() { Score = Score.New(Score.VERTEX_DISTANCE, string.Format("The vertex score compare to max distance {0}", maxVertexDistanceInMeter), (float)System.Math.Max(0, (1.0 - (distance / maxVertexDistanceInMeter))), 1), // calculate scoring compared to the fixed max distance. Location = routerPoint }); } } return(scoredCandidates); }
static void Main(string[] args) { Itinero.Logging.Logger.LogAction = (origin, level, message, parameters) => { Console.WriteLine(string.Format("[{0}-{3}] {1} - {2}", origin, level, message, DateTime.Now.ToString())); }; var trafficSigns = new List <MaxSpeedSign>(); using (var stream = File.OpenText(@"../data/verkeersbordpt.csv")) { var csv = new CsvReader(stream, new CsvHelper.Configuration.CsvConfiguration() { Delimiter = ";", CultureInfo = System.Globalization.CultureInfo.InvariantCulture }); while (csv.Read()) { var type = csv.GetField <string>("type"); switch (type) { case "C43": var opschrift = int.Parse(csv.GetField <string>("opschrift").Replace("|", string.Empty)); var lat = (float)csv.GetField <double>("Y"); var lon = (float)csv.GetField <double>("X"); trafficSigns.Add(new MaxSpeedSign() { Id = csv.GetField <string>("OBJECTID"), Latitude = lat, Longitude = lon, Speed = opschrift }); break; } } } RouterDb routerDb = null; using (var stream = File.OpenRead(@"belgium.c.routerdb")) { routerDb = RouterDb.Deserialize(stream); } var router = new Router(routerDb); var profile = routerDb.GetSupportedProfile("car"); var coordinates = new Itinero.LocalGeo.Coordinate[trafficSigns.Count]; for (var i = 0; i < coordinates.Length; i++) { coordinates[i] = new Itinero.LocalGeo.Coordinate() { Latitude = trafficSigns[i].Latitude, Longitude = trafficSigns[i].Longitude }; } var massResolver = new Itinero.Algorithms.Search.MassResolvingAlgorithm(router, new Itinero.Profiles.IProfileInstance[] { profile }, coordinates); massResolver.Run(); var features = new FeatureCollection(); for (var i = 0; i < massResolver.RouterPoints.Count; i++) { var point = massResolver.RouterPoints[i]; var sign = trafficSigns[i]; var oi = massResolver.LocationIndexOf(i); var coordinate = coordinates[oi]; var coordinateOnNetwork = point.LocationOnNetwork(routerDb); //var shape = new List<GeographicPosition> //{ // new GeographicPosition(coordinate.Latitude, coordinate.Longitude), // new GeographicPosition(coordinateOnNetwork.Latitude, coordinateOnNetwork.Longitude) //}.ToList<IPosition>(); var signAttributes = new Dictionary <string, object>(); signAttributes["speed"] = sign.Speed; signAttributes["id"] = sign.Id; //features.Features.Add(new Feature(new LineString(shape))); //features.Features.Add(new Feature(new Point(new GeographicPosition(coordinate.Latitude, coordinate.Longitude)), // signAttributes)); var edge = routerDb.Network.GeometricGraph.GetEdge(point.EdgeId); var dir = point.Direction(routerDb); var to = edge.To; if (dir == Itinero.Navigation.Directions.RelativeDirectionEnum.Left) { to = edge.From; } var toCoordinate = routerDb.Network.GetVertex(to); var speedFeatures = new FeatureCollection(); var speedShape = point.ShapePointsTo(routerDb, to); var shape = new List <IPosition>(); shape.Add(new GeographicPosition(coordinateOnNetwork.Latitude, coordinateOnNetwork.Longitude)); for (var s = 0; s < speedShape.Count; s++) { shape.Add(new GeographicPosition(speedShape[s].Latitude, speedShape[s].Longitude)); } shape.Add(new GeographicPosition(toCoordinate.Latitude, toCoordinate.Longitude)); speedFeatures.Features.Add(new Feature(new LineString(shape), signAttributes)); speedFeatures.Features.Add(new Feature(new Point(new GeographicPosition(coordinate.Latitude, coordinate.Longitude)), signAttributes)); features.Features.AddRange(speedFeatures.Features); File.WriteAllText("decoded_" + i + ".geojson", JsonConvert.SerializeObject(speedFeatures, Formatting.Indented)); } File.WriteAllText("decoded_all.geojson", JsonConvert.SerializeObject(features, Formatting.Indented)); }
/// <summary> /// Converts the given coordinate to corresponding GeoAPI coordinate. /// </summary> public static Coordinate ToGeoAPICoordinate(this Itinero.LocalGeo.Coordinate coordinate) { return(new Coordinate(coordinate.Longitude, coordinate.Latitude)); }