RealWorldTerrainOSMBase.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* INFINITY CODE 2013-2019 */
  2. /* http://www.infinity-code.com */
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace InfinityCode.RealWorldTerrain.OSM
  6. {
  7. /// <summary>
  8. /// The base class for the Open Street Map objects.
  9. /// </summary>
  10. public class RealWorldTerrainOSMBase
  11. {
  12. /// <summary>
  13. /// ID.
  14. /// </summary>
  15. public string id;
  16. /// <summary>
  17. /// List of tags.
  18. /// </summary>
  19. public List<RealWorldTerrainOSMTag> tags;
  20. public bool Equals(RealWorldTerrainOSMBase other)
  21. {
  22. if (ReferenceEquals(other, null)) return false;
  23. if (ReferenceEquals(this, other)) return true;
  24. return id == other.id;
  25. }
  26. public override int GetHashCode()
  27. {
  28. return id.GetHashCode();
  29. }
  30. /// <summary>
  31. /// Gets tag value by key.
  32. /// </summary>
  33. /// <param name="key">Tag key.</param>
  34. /// <returns>Tag value or string.Empty.</returns>
  35. public string GetTagValue(string key)
  36. {
  37. List<RealWorldTerrainOSMTag> curTags = tags.Where(tag => tag.key == key).ToList();
  38. if (curTags.Count > 0) return curTags[0].value;
  39. return string.Empty;
  40. }
  41. /// <summary>
  42. /// Checks tag with the specified pair (key, value).
  43. /// </summary>
  44. /// <param name="key">Tag key.</param>
  45. /// <param name="value">Tag value.</param>
  46. /// <returns>True - success, False - otherwise.</returns>
  47. public bool HasTag(string key, string value)
  48. {
  49. return tags.Any(t => t.key == key && t.value == value);
  50. }
  51. /// <summary>
  52. /// Checks whether there is a tag with at least one of the keys.
  53. /// </summary>
  54. /// <param name="keys">Keys</param>
  55. /// <returns>True - success, False - otherwise.</returns>
  56. public bool HasTagKey(params string[] keys)
  57. {
  58. return keys.Any(key => tags.Any(t => t.key == key));
  59. }
  60. /// <summary>
  61. /// Checks whether there is a tag with at least one of the values.
  62. /// </summary>
  63. /// <param name="values">Values</param>
  64. /// <returns>True - success, False - otherwise.</returns>
  65. public bool HasTagValue(params string[] values)
  66. {
  67. return values.Any(val => tags.Any(t => t.value == val));
  68. }
  69. /// <summary>
  70. /// Checks whether there is a tag with key and at least one of the values.
  71. /// </summary>
  72. /// <param name="key">Key</param>
  73. /// <param name="values">Values</param>
  74. /// <returns>True - success, False - otherwise.</returns>
  75. public bool HasTags(string key, params string[] values)
  76. {
  77. return tags.Any(tag => tag.key == key && values.Any(v => v == tag.value));
  78. }
  79. }
  80. }