예제 #1
0
        /// <summary>
        /// Generates instructions for the given route.
        /// </summary>
        public static IList <Instruction> GenerateInstructions(this Route route, ILanguageReference languageReference)
        {
            if (route.IsMultimodal())
            {
                throw new NotSupportedException("Generate instruction for multimodal routes is not supported.");
            }
            var profile = Profiles.Profile.GetRegistered(route.Profile);

            return(profile.InstructionGenerator.Generate(route, languageReference));
        }
예제 #2
0
 public InstructionCarGenerator(Route route, ILanguageReference languageReference)
     : this(route, languageReference, new InstructionCarGenerator.TryGetDelegateWithLanguageReference[4]
 {
     new InstructionCarGenerator.TryGetDelegateWithLanguageReference(InstructionCarGenerator.GetStartInstruction),
     new InstructionCarGenerator.TryGetDelegateWithLanguageReference(InstructionCarGenerator.GetStopInstruction),
     new InstructionCarGenerator.TryGetDelegateWithLanguageReference(InstructionCarGenerator.GetRoundaboutInstruction),
     new InstructionCarGenerator.TryGetDelegateWithLanguageReference(InstructionCarGenerator.GetTurnInstruction)
 })
 {
 }
        private Table BuildLanguageReferenceTable(ILanguageReference language)
        {
            var table = new Table(_script);

            table["get"] = (Func <string, string>)((key) =>
            {
                return(language[key]);
            });
            return(table);
        }
예제 #4
0
        public static List <Instruction> Generate(Route route, ILanguageReference languageReference)
        {
            InstructionCarGenerator instructionCarGenerator = new InstructionCarGenerator(route, languageReference);

            instructionCarGenerator.Run();
            if (instructionCarGenerator.HasSucceeded)
            {
                return(instructionCarGenerator.Instructions);
            }
            return(new List <Instruction>());
        }
예제 #5
0
        /// <summary>
        /// Generates instructions for the given route.
        /// </summary>
        public static List <Instruction> Generate(Route route, ILanguageReference languageReference)
        {
            var algorithm = new InstructionCarGenerator(route, languageReference);

            algorithm.Run();

            if (algorithm.HasSucceeded)
            {
                return(algorithm.Instructions);
            }
            return(new List <Instruction>());
        }
예제 #6
0
        //gets a turn
        private Instruction GetTurn(RoutePosition position, ILanguageReference language)
        {
            var instruction = new Instruction
            {
                Shape = position.Shape,
                Type  = "turn"
            };
            var relativeDirection = position.RelativeDirection().Direction;
            var turnRelevant      = false;
            var branches          = position.Branches();

            if (branches != null)
            {
                var traversedBranches =
                    GetTraversable(branches).ToList();

                if (relativeDirection == RelativeDirectionEnum.StraightOn &&
                    traversedBranches.Count >= 2)
                {
                    turnRelevant = true;// straight on at cross road
                }
                if (relativeDirection != RelativeDirectionEnum.StraightOn &&
                    traversedBranches.Count > 0)
                {
                    turnRelevant = true;// an actual normal turn
                }
            }
            if (turnRelevant)
            {
                var    next = position.Next();
                string name = null;
                if (next != null)
                {
                    name = next.Value.GetMetaAttribute("name");
                }
                if (!String.IsNullOrEmpty(name))
                {
                    instruction.Text = string.Format(language["Go {0} on {1}."],
                                                     language[relativeDirection.ToString()
                                                              .ToLower()], name);
                    instruction.Shape = position.Shape;
                }
                else
                {
                    instruction.Text = String.Format(language["Go {0}."],
                                                     language[relativeDirection.ToString().ToLower()]);
                    instruction.Shape = position.Shape;
                }
                return(instruction);
            }
            return(null);
        }
예제 #7
0
        private Instruction GetStart(RoutePosition position, ILanguageReference language)
        {
            var instruction = new Instruction
            {
                Shape = position.Shape,
                Type  = "start"
            };
            var direction = position.Direction();

            instruction.Text = String.Format(language
                                             ["Start {0}."], language[direction.ToString()
                                                                      .ToLower()]);
            return(instruction);
        }
예제 #8
0
        //gets a roundabout instruction
        private Instruction GetRoundabout(RoutePosition position, ILanguageReference language)
        {
            position.Next();
            if (String.IsNullOrEmpty(position.GetMetaAttribute("junction")))
            {
                var instruction = new Instruction
                {
                    Shape = position.Shape,
                    Type  = "roundabout"
                };

                var exit     = 1;
                var count    = 1;
                var previous = position.Previous();
                while (previous != null && previous.Value.GetMetaAttribute("junction") == "roundabout")
                {
                    var branches = previous.Value.Branches();
                    if (branches != null)
                    {
                        branches = GetTraversable(branches);
                        if (branches.Any())
                        {
                            exit++;
                        }
                    }
                    count++;
                    previous = previous.Value.Previous();
                }

                instruction.Text = string.Format(language["Take the {0}th exit at the next roundabout."], exit);
                if (exit == 1)
                {
                    instruction.Text = string.Format(language["Take the first exit at the next roundabout."]);
                }
                else if (exit == 2)
                {
                    instruction.Text = string.Format(language["Take the second exit at the next roundabout."]);
                }
                else if (exit == 3)
                {
                    instruction.Text = string.Format(language["Take the third exit at the next roundabout."]);
                }

                instruction.Shape = position.Shape;
                return(instruction);
            }

            return(null);
        }
예제 #9
0
 public static int GetStopInstruction(Route r, int i, ILanguageReference languageReference, out Instruction instruction)
 {
     instruction = (Instruction)null;
     if (i != r.Segments.Count - 1 || r.Segments.Count <= 0)
     {
         return(0);
     }
     instruction = new Instruction()
     {
         Text    = languageReference["Arrived at destination."],
         Type    = "stop",
         Segment = r.Segments.Count - 1
     };
     return(1);
 }
예제 #10
0
 /// <summary>
 /// Gets the stop instruction.
 /// </summary>
 public static int GetStopInstruction(RoutePosition position, ILanguageReference languageReference,
                                      out Instruction instruction)
 {
     instruction = null;
     if (position.IsLast())
     {
         instruction = new Instruction()
         {
             Text  = languageReference["Arrived at destination."],
             Type  = "stop",
             Shape = position.Shape
         };
         return(1);
     }
     return(0);
 }
예제 #11
0
 /// <summary>
 /// Gets the start instruction.
 /// </summary>
 public static int GetStartInstruction(RoutePosition position, ILanguageReference languageReference,
                                       out Instruction instruction)
 {
     instruction = null;
     if (position.IsFirst())
     {
         instruction = new Instruction()
         {
             Text = string.Format(languageReference["Start {0}."],
                                  languageReference[position.Direction().ToInvariantString()]),
             Type  = "start",
             Shape = 0
         };
         return(1);
     }
     return(0);
 }
예제 #12
0
        public static int GetStartInstruction(Route r, int i, ILanguageReference languageReference, out Instruction instruction)
        {
            instruction = (Instruction)null;
            if (i != 0 || r.Segments.Count <= 0)
            {
                return(0);
            }
            DirectionEnum directionEnum = DirectionCalculator.Calculate(new GeoCoordinate((double)r.Segments[0].Latitude, (double)r.Segments[0].Longitude), new GeoCoordinate((double)r.Segments[1].Latitude, (double)r.Segments[1].Longitude));
            string        str           = languageReference[directionEnum.ToInvariantString()];

            instruction = new Instruction()
            {
                Text    = string.Format(languageReference["Start {0}."], (object)str),
                Type    = "start",
                Segment = 0
            };
            return(1);
        }
예제 #13
0
        public IList <Instruction> Generate(Route route, ILanguageReference language)
        {
            if (route.IsMultimodal())
            {
                throw new ArgumentException("Cannot use a unimodal instruction generator on multimodal route.");
            }
            if (_profile.FullName.ToLowerInvariant() != route.Profile)
            {
                throw new ArgumentException(string.Format("Cannot generate instructions with a generator for profile {0} for a route with profile {1}.",
                                                          _profile.FullName, route.Profile));
            }
            var instructions = new List <Instruction>();

            foreach (var position in route)
            {
                if (position.IsFirst())
                {
                    instructions.Add(GetStart(position, language));
                }
                else if (position.IsLast())
                {
                    instructions.Add(GetStop(position, language));
                }
                else if (position.GetMetaAttribute("junction") ==
                         "roundabout")
                {
                    var instruction = GetRoundabout(position, language);
                    if (instruction != null)
                    {
                        instructions.Add(instruction);
                    }
                }
                else
                {
                    var instruction = GetTurn(position, language);
                    if (instruction != null)
                    {
                        instructions.Add(instruction);
                    }
                }
            }
            return(instructions);
        }
        /// <summary>
        /// Generates instructions for the given route assuming it's using the profile in this generator.
        /// </summary>
        public IList <Instruction> Generate(Route route, ILanguageReference languageReference, CancellationToken cancellationToken)
        {
            if (route.IsMultimodal())
            {
                throw new ArgumentException("Cannot use a unimodal instruction generator on multimodal route.");
            }
            if (_profile.FullName.ToLowerInvariant() != route.Profile)
            {
                throw new ArgumentException(string.Format("Cannot generate instructions with a generator for profile {0} for a route with profile {1}.",
                                                          _profile.FullName, route.Profile));
            }

            var instructionGenerator = new UnimodalInstructionGenerator(route, _getInstructionFunctions, languageReference);

            instructionGenerator.Run(cancellationToken);
            if (!instructionGenerator.HasSucceeded)
            {
                throw new Exception(string.Format("Failed to generate instructions: {0}", instructionGenerator.ErrorMessage));
            }
            return(instructionGenerator.Instructions);
        }
예제 #15
0
        public static int GetRoundaboutInstruction(Route r, int i, ILanguageReference languageReference, out Instruction instruction)
        {
            if (r.Segments[i].Tags != null)
            {
                RouteTags[]            tags1 = r.Segments[i].Tags;
                Func <RouteTags, bool> func1 = (Func <RouteTags, bool>)(x =>
                {
                    if (x.Key == "junction")
                    {
                        return(x.Value == "roundabout");
                    }
                    return(false);
                });
                if (((IEnumerable <RouteTags>)tags1).Any <RouteTags>(func1) && i < r.Segments.Count)
                {
                    if (r.Segments[i + 1].Tags != null)
                    {
                        RouteTags[]            tags2 = r.Segments[i + 1].Tags;
                        Func <RouteTags, bool> func2 = (Func <RouteTags, bool>)(x =>
                        {
                            if (x.Key == "junction")
                            {
                                return(x.Value == "roundabout");
                            }
                            return(false);
                        });
                        if (((IEnumerable <RouteTags>)tags2).Any <RouteTags>(func2))
                        {
                            goto label_19;
                        }
                    }
                    int num1 = 1;
                    int num2 = 1;
                    for (int index = i - 1; index >= 0; --index)
                    {
                        ++num2;
                        if (r.Segments[index].Tags != null)
                        {
                            RouteTags[]            tags2 = r.Segments[index].Tags;
                            Func <RouteTags, bool> func2 = (Func <RouteTags, bool>)(x =>
                            {
                                if (x.Key == "junction")
                                {
                                    return(x.Value == "roundabout");
                                }
                                return(false);
                            });
                            if (((IEnumerable <RouteTags>)tags2).Any <RouteTags>(func2))
                            {
                                if (r.Segments[index].SideStreets != null)
                                {
                                    ++num1;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (num1 == 1)
                    {
                        instruction = new Instruction()
                        {
                            Text    = string.Format(languageReference["Take the first exit at the next roundabout."], (object)num1),
                            Type    = "roundabout",
                            Segment = i
                        }
                    }
                    ;
                    else if (num1 == 2)
                    {
                        instruction = new Instruction()
                        {
                            Text    = string.Format(languageReference["Take the second exit at the next roundabout."], (object)num1),
                            Type    = "roundabout",
                            Segment = i
                        }
                    }
                    ;
                    else if (num1 == 3)
                    {
                        instruction = new Instruction()
                        {
                            Text    = string.Format(languageReference["Take the third exit at the next roundabout."], (object)num1),
                            Type    = "roundabout",
                            Segment = i
                        }
                    }
                    ;
                    else
                    {
                        instruction = new Instruction()
                        {
                            Text    = string.Format(languageReference["Take the {0}th exit at the next roundabout."], (object)num1),
                            Type    = "roundabout",
                            Segment = i
                        }
                    };
                    return(num2);
                }
            }
label_19:
            instruction = (Instruction)null;
            return(0);
        }
예제 #16
0
 public InstructionCarGenerator(Route route, ILanguageReference languageReference, InstructionCarGenerator.TryGetDelegateWithLanguageReference[] getInstructionDelegates)
     : base(route, InstructionCarGenerator.GetInstructionDelegatesFrom(getInstructionDelegates, languageReference))
 {
 }
예제 #17
0
        /// <summary>
        /// Gets the roundabout function.
        /// </summary>
        public static int GetRoundaboutInstruction(RoutePosition position, ILanguageReference languageReference,
                                                   out Instruction instruction)
        {
            if (position.ContainsMetaAttribute("junction", "roundabout") &&
                !position.IsLast() &&
                !position.Next().Value.ContainsMetaAttribute("junction", "roundabout"))
            { // ok, it's a roundabout, if the next segment is not on the roundabout, generate an exit roundabout instruction.
                var exit  = 1;
                var count = 1;
                while (position.MovePrevious() &&
                       position.ContainsMetaAttribute("junction", "roundabout"))
                {
                    if (position.HasBranches())
                    {
                        exit++;
                    }
                    count++;
                }

                if (exit == 1)
                {
                    instruction = new Instruction()
                    {
                        Text = string.Format(languageReference["Take the first exit at the next roundabout."],
                                             exit),
                        Type  = "roundabout",
                        Shape = position.Shape
                    };
                }
                else if (exit == 2)
                {
                    instruction = new Instruction()
                    {
                        Text = string.Format(languageReference["Take the second exit at the next roundabout."],
                                             exit),
                        Type  = "roundabout",
                        Shape = position.Shape
                    };
                }
                else if (exit == 3)
                {
                    instruction = new Instruction()
                    {
                        Text = string.Format(languageReference["Take the third exit at the next roundabout."],
                                             exit),
                        Type  = "roundabout",
                        Shape = position.Shape
                    };
                }
                else
                {
                    instruction = new Instruction()
                    {
                        Text = string.Format(languageReference["Take the {0}th exit at the next roundabout."],
                                             exit),
                        Type  = "roundabout",
                        Shape = position.Shape
                    };
                }
                return(count);
            }
            instruction = null;
            return(0);
        }
예제 #18
0
        /// <summary>
        /// Gets the turn instruction function.
        /// </summary>
        public static int GetTurnInstruction(RoutePosition position, ILanguageReference languageReference,
                                             out Instruction instruction)
        {
            var relative = position.RelativeDirection();

            var doInstruction = false;

            if (position.HasBranches())
            {
                var branches = new List <Route.Branch>(position.Branches());
                if (relative.Direction == RelativeDirectionEnum.StraightOn &&
                    branches.Count >= 2)
                { // straight-on at cross roads.
                    doInstruction = true;
                }
                else if (relative.Direction != RelativeDirectionEnum.StraightOn &&
                         relative.Direction != RelativeDirectionEnum.SlightlyLeft &&
                         relative.Direction != RelativeDirectionEnum.SlightlyRight &&
                         branches.Count > 0)
                { // an actual turn and there is at least on other street around.
                    doInstruction = true;
                }
            }
            if (doInstruction)
            {
                var name = position.GetMetaAttribute("name");
                if (!string.IsNullOrWhiteSpace(name))
                { // there is a name.
                    if (relative.Direction == RelativeDirectionEnum.StraightOn)
                    {
                        instruction = new Instruction()
                        {
                            Text = string.Format(languageReference["Go {0} on {1}."],
                                                 languageReference[relative.Direction.ToInvariantString()], name),
                            Type  = "turn",
                            Shape = position.Shape
                        };
                        return(1);
                    }
                    instruction = new Instruction()
                    {
                        Text = string.Format(languageReference["Turn {0} on {1}."],
                                             languageReference[relative.Direction.ToInvariantString()], name),
                        Type  = "turn",
                        Shape = position.Shape
                    };
                    return(1);
                }
                else
                { // there is no name.
                    if (relative.Direction == RelativeDirectionEnum.StraightOn)
                    {
                        instruction = new Instruction()
                        {
                            Text = string.Format(languageReference["Go {0}."],
                                                 languageReference[relative.Direction.ToInvariantString()]),
                            Type  = "turn",
                            Shape = position.Shape
                        };
                    }

                    instruction = new Instruction()
                    {
                        Text = string.Format(languageReference["Turn {0}."],
                                             languageReference[relative.Direction.ToInvariantString()]),
                        Type  = "turn",
                        Shape = position.Shape
                    };
                    return(1);
                }
            }
            instruction = null;
            return(0);
        }
예제 #19
0
 private static InstructionGenerator <Instruction> .TryGetDelegate GetInstructionDelegateFrom(InstructionCarGenerator.TryGetDelegateWithLanguageReference tryGetDelegate, ILanguageReference languageReference)
 {
     return((InstructionGenerator <Instruction> .TryGetDelegate)((Route r, int i, out Instruction instruction) => tryGetDelegate(r, i, languageReference, out instruction)));
 }
 /// <summary>
 /// Generates instructions for the given route assuming it's using the profile in this generator.
 /// </summary>
 public IList <Instruction> Generate(Route route, ILanguageReference languageReference)
 {
     return(this.Generate(route, languageReference, CancellationToken.None));
 }
예제 #21
0
 private static InstructionGenerator <Instruction> .TryGetDelegate[] GetInstructionDelegatesFrom(InstructionCarGenerator.TryGetDelegateWithLanguageReference[] tryGetDelegates, ILanguageReference languageReference)
 {
     InstructionGenerator <Instruction> .TryGetDelegate[] tryGetDelegateArray = new InstructionGenerator <Instruction> .TryGetDelegate[tryGetDelegates.Length];
     for (int index = 0; index < tryGetDelegateArray.Length; ++index)
     {
         tryGetDelegateArray[index] = InstructionCarGenerator.GetInstructionDelegateFrom(tryGetDelegates[index], languageReference);
     }
     return(tryGetDelegateArray);
 }
예제 #22
0
 /// <summary>
 /// Creates a new instruction generator.
 /// </summary>
 public InstructionGenerator(Route route, TryGetDelegate[] tryGetInstructions, ILanguageReference languageReference)
     : this(route, tryGetInstructions, null, languageReference)
 {
 }
예제 #23
0
        public static int GetTurnInstruction(Route r, int i, ILanguageReference languageReference, out Instruction instruction)
        {
            RelativeDirection relativeDirection = r.RelativeDirectionAt(i);

            if (relativeDirection != null)
            {
                bool flag = false;
                if (relativeDirection.Direction == RelativeDirectionEnum.StraightOn && r.Segments[i].SideStreets != null && r.Segments[i].SideStreets.Length >= 2)
                {
                    flag = true;
                }
                else if (relativeDirection.Direction != RelativeDirectionEnum.StraightOn && relativeDirection.Direction != RelativeDirectionEnum.SlightlyLeft && (relativeDirection.Direction != RelativeDirectionEnum.SlightlyRight && r.Segments[i].SideStreets != null) && r.Segments[i].SideStreets.Length != 0)
                {
                    flag = true;
                }
                if (flag)
                {
                    string str  = languageReference[relativeDirection.Direction.ToInvariantString()];
                    string name = string.Empty;
                    if (i + 1 < r.Segments.Count && r.Segments[i + 1].Tags != null && ((IEnumerable <RouteTags>)r.Segments[i + 1].Tags).Any <RouteTags>((Func <RouteTags, bool>)(x =>
                    {
                        if (!(x.Key == "name"))
                        {
                            return(false);
                        }
                        name = x.Value;
                        return(true);
                    })))
                    {
                        if (relativeDirection.Direction == RelativeDirectionEnum.StraightOn)
                        {
                            instruction = new Instruction()
                            {
                                Text = string.Format(languageReference["Go {0} on {1}."], new object[2]
                                {
                                    (object)str,
                                    (object)name
                                }),
                                Type    = "turn",
                                Segment = i
                            };
                            return(1);
                        }
                        instruction = new Instruction()
                        {
                            Text = string.Format(languageReference["Turn {0} on {1}."], new object[2]
                            {
                                (object)str,
                                (object)name
                            }),
                            Type    = "turn",
                            Segment = i
                        };
                        return(1);
                    }
                    if (relativeDirection.Direction == RelativeDirectionEnum.StraightOn)
                    {
                        instruction = new Instruction()
                        {
                            Text    = string.Format(languageReference["Go {0}."], (object)str),
                            Type    = "turn",
                            Segment = i
                        }
                    }
                    ;
                    instruction = new Instruction()
                    {
                        Text    = string.Format(languageReference["Turn {0}."], (object)str),
                        Type    = "turn",
                        Segment = i
                    };
                    return(1);
                }
            }
            instruction = (Instruction)null;
            return(0);
        }