/Core/OsmSharp.Routing/Instructions/LanguageGeneration/Defaults/SimpleEnglishLanguageGenerator.cs

https://bitbucket.org/tscheinpflug/osmsharp · C# · 274 lines · 157 code · 22 blank · 95 comment · 19 complexity · 91eb2fc40caa22b3ea2c6e557e9f4220 MD5 · raw file

  1. // OsmSharp - OpenStreetMap tools & library.
  2. // Copyright (C) 2012 Abelshausen Ben
  3. //
  4. // This file is part of OsmSharp.
  5. //
  6. // OsmSharp is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 2 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // OsmSharp is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with OsmSharp. If not, see <http://www.gnu.org/licenses/>.
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Linq;
  21. using System.Text;
  22. using OsmSharp.Tools.Math.Geo.Meta;
  23. using OsmSharp.Routing.ArcAggregation.Output;
  24. namespace OsmSharp.Routing.Instructions.LanguageGeneration.Defaults
  25. {
  26. /// <summary>
  27. /// A simple instruction generator, translating instructions into the english language.
  28. /// </summary>
  29. public class SimpleEnglishLanguageGenerator : ILanguageGenerator
  30. {
  31. private string TurnDirection(RelativeDirectionEnum direction)
  32. {
  33. switch (direction)
  34. {
  35. case RelativeDirectionEnum.Right:
  36. case RelativeDirectionEnum.SharpRight:
  37. case RelativeDirectionEnum.SlightlyRight:
  38. return "right";
  39. case RelativeDirectionEnum.Left:
  40. case RelativeDirectionEnum.SharpLeft:
  41. case RelativeDirectionEnum.SlightlyLeft:
  42. return "left";
  43. case RelativeDirectionEnum.TurnBack:
  44. return "back";
  45. }
  46. return string.Empty;
  47. }
  48. #region ILanguageGenerator Members
  49. /// <summary>
  50. /// Generates an instruction for a direct turn.
  51. /// </summary>
  52. /// <param name="instruction"></param>
  53. /// <param name="streetCountBeforeTurn"></param>
  54. /// <param name="streetTo"></param>
  55. /// <param name="direction"></param>
  56. /// <param name="list"></param>
  57. /// <returns></returns>
  58. public Instruction GenerateDirectTurn(Instruction instruction, int streetCountBeforeTurn,
  59. List<KeyValuePair<string, string>> streetTo, RelativeDirectionEnum direction, List<PointPoi> list)
  60. {
  61. if (streetCountBeforeTurn == 1)
  62. {
  63. instruction.Text = string.Format("Take the first turn {0}, on {1}.",
  64. TurnDirection(direction),
  65. this.GetName("en",streetTo));
  66. }
  67. else
  68. {
  69. instruction.Text = string.Format("Take the {0}th turn {1}, on {2}.",
  70. streetCountBeforeTurn,
  71. TurnDirection(direction),
  72. this.GetName("en",streetTo));
  73. }
  74. // returns the instruction with text.
  75. return instruction;
  76. }
  77. /// <summary>
  78. /// Generates an instruction for an indirect turn.
  79. /// </summary>
  80. /// <param name="instruction"></param>
  81. /// <param name="streetCountTurn"></param>
  82. /// <param name="streetCountBeforeTurn"></param>
  83. /// <param name="streetTo"></param>
  84. /// <param name="direction"></param>
  85. /// <param name="list"></param>
  86. /// <returns></returns>
  87. public Instruction GenerateIndirectTurn(Instruction instruction, int streetCountTurn, int streetCountBeforeTurn,
  88. List<KeyValuePair<string, string>> streetTo, RelativeDirectionEnum direction, List<PointPoi> list)
  89. {
  90. instruction.Text = string.Format("Take the {0}d turn {1}, on {2}.",
  91. streetCountBeforeTurn,
  92. TurnDirection(direction),
  93. this.GetName("en",streetTo));
  94. // returns the instruction with text.
  95. return instruction;
  96. }
  97. /// <summary>
  98. /// Generates an instruction for a POI.
  99. /// </summary>
  100. /// <param name="instruction"></param>
  101. /// <param name="list"></param>
  102. /// <param name="direction"></param>
  103. /// <returns></returns>
  104. public Instruction GeneratePoi(Instruction instruction, List<PointPoi> list, RelativeDirectionEnum? direction)
  105. {
  106. if (direction == null)
  107. {
  108. instruction.Text = string.Format("Poi");
  109. }
  110. else
  111. {
  112. instruction.Text = string.Format("Poi:{0}", direction);
  113. }
  114. // returns the instruction with text.
  115. return instruction;
  116. }
  117. /// <summary>
  118. /// Generates an instruction for a turn followed by another turn.
  119. /// </summary>
  120. /// <param name="instruction"></param>
  121. /// <param name="streetCountBeforeTurn"></param>
  122. /// <param name="streetTo"></param>
  123. /// <param name="direction"></param>
  124. /// <param name="list"></param>
  125. /// <returns></returns>
  126. public Instruction GenerateDirectFollowTurn(Instruction instruction, int streetCountBeforeTurn, List<KeyValuePair<string, string>> streetTo,
  127. RelativeDirectionEnum direction, List<PointPoi> list)
  128. {
  129. if (streetCountBeforeTurn == 1)
  130. {
  131. instruction.Text = string.Format("Turn {1} to stay on {0}.",
  132. this.GetName("en",streetTo),
  133. TurnDirection(direction));
  134. }
  135. else
  136. {
  137. instruction.Text = string.Format("Turn {1}d street {2} to stay on {0}.",
  138. this.GetName("en",streetTo),
  139. streetCountBeforeTurn,
  140. TurnDirection(direction));
  141. }
  142. // returns the instruction with text.
  143. return instruction;
  144. }
  145. /// <summary>
  146. /// Generates an instruction for an indirect turn.
  147. /// </summary>
  148. /// <param name="instruction"></param>
  149. /// <param name="streetCountTurn"></param>
  150. /// <param name="streetCountBeforeTurn"></param>
  151. /// <param name="streetTo"></param>
  152. /// <param name="direction"></param>
  153. /// <param name="list"></param>
  154. /// <returns></returns>
  155. public Instruction GenerateIndirectFollowTurn(Instruction instruction, int streetCountTurn, int streetCountBeforeTurn, List<KeyValuePair<string, string>> streetTo,
  156. RelativeDirectionEnum direction, List<PointPoi> list)
  157. {
  158. if (streetCountBeforeTurn == 1)
  159. {
  160. instruction.Text = string.Format("Turn {1} to stay on {0}.",
  161. this.GetName("en",streetTo),
  162. TurnDirection(direction));
  163. }
  164. else
  165. {
  166. instruction.Text = string.Format("Take the {1}d street {2} to stay on {0}.",
  167. this.GetName("en",streetTo),
  168. streetCountBeforeTurn,
  169. TurnDirection(direction));
  170. }
  171. // returns the instruction with text.
  172. return instruction;
  173. }
  174. /// <summary>
  175. /// Generates an instruction for an immidiate turn.
  176. /// </summary>
  177. /// <param name="instruction"></param>
  178. /// <param name="firstStreetCountTo"></param>
  179. /// <param name="firstStreetTo"></param>
  180. /// <param name="firstDirection"></param>
  181. /// <param name="secondStreetTo"></param>
  182. /// <param name="secondDirection"></param>
  183. /// <returns></returns>
  184. public Instruction GenerateImmidiateTurn(Instruction instruction, int firstStreetCountTo, List<KeyValuePair<string, string>> firstStreetTo,
  185. RelativeDirection firstDirection, List<KeyValuePair<string, string>> secondStreetTo, RelativeDirection secondDirection)
  186. {
  187. if (firstStreetCountTo == 1)
  188. {
  189. instruction.Text = string.Format("Take the first turn {0}, on the {1}, and turn immidiately {2} on the {3}.",
  190. TurnDirection(firstDirection.Direction),
  191. this.GetName("en",firstStreetTo),
  192. TurnDirection(secondDirection.Direction),
  193. this.GetName("en",secondStreetTo));
  194. }
  195. else
  196. {
  197. instruction.Text = string.Format("Take the {4}d turn {0}, on the {1}, and turn immidiately {2} on the {3}.",
  198. TurnDirection(firstDirection.Direction),
  199. this.GetName("en",firstStreetTo),
  200. TurnDirection(secondDirection.Direction),
  201. this.GetName("en",secondStreetTo),
  202. firstStreetCountTo);
  203. }
  204. // returns the instruction with text.
  205. return instruction;
  206. }
  207. /// <summary>
  208. /// Generates an instruction for a roundabout.
  209. /// </summary>
  210. /// <param name="instruction"></param>
  211. /// <param name="count"></param>
  212. /// <param name="nextStreet"></param>
  213. /// <returns></returns>
  214. public Instruction GenerateRoundabout(Instruction instruction, int count, List<KeyValuePair<string, string>> nextStreet)
  215. {
  216. instruction.Text = string.Format("Take the {0}d at the next roundabout on the {1}.",
  217. count,
  218. this.GetName("en",nextStreet));
  219. // returns the instruction with text.
  220. return instruction;
  221. }
  222. /// <summary>
  223. /// Generates an instruction for a simple turn.
  224. /// </summary>
  225. /// <param name="instruction"></param>
  226. /// <param name="direction"></param>
  227. /// <returns></returns>
  228. public Instruction GenerateSimpleTurn(Instruction instruction, RelativeDirectionEnum direction)
  229. {
  230. instruction.Text = string.Format("Turn {0}", this.TurnDirection(direction));
  231. return instruction;
  232. }
  233. #endregion
  234. private string GetName(string language_key, List<KeyValuePair<string, string>> tags)
  235. {
  236. language_key = language_key.ToLower();
  237. string name = string.Empty;
  238. foreach (KeyValuePair<string, string> tag in tags)
  239. {
  240. if (tag.Key != null && tag.Key.ToLower() == string.Format("name:{0}", language_key))
  241. {
  242. return tag.Value;
  243. }
  244. if (tag.Key != null && tag.Key.ToLower() == "name")
  245. {
  246. name = tag.Key;
  247. }
  248. }
  249. return name;
  250. }
  251. }
  252. }