~ihabunek/triglav

ref: 72d1f411368cbf732c3e8286a4781c313234fa5f triglav/lib/triglav/zet/validator.ex -rw-r--r-- 5.8 KiB
72d1f411Ivan Habunek Add route master validation 1 year, 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
defmodule Triglav.Zet.Validator do
  @moduledoc """
  Validates ZET routes in OSM based on ZET GTFS data.
  """
  alias Triglav.Schemas.Osm.Relation
  alias Triglav.Schemas.Zet.Route
  alias Triglav.Zet.Gtfs
  alias Triglav.Zet.Osm

  @type error :: binary

  @spec validate_all() :: [error]
  def validate_all() do
    routes = Gtfs.list_routes()

    relations =
      Osm.list_public_transport_relations()
      |> Enum.group_by(& &1.tags["ref"])

    for route <- routes do
      validate_route(route, Map.get(relations, route.id, []))
    end
  end

  @spec validate_route(Route.t(), [Relation.t()]) :: [error]
  def validate_route(route, relations) do
    [
      validate_has_route_master(relations),
      validate_has_relations(relations),
      validate_routes_are_contained_in_route_master(relations),
      validate_routes_master_route_has_no_unknown_members(relations),
      Enum.map(relations, &validate_route_master(route, &1)),
      Enum.map(relations, &validate_route_naming/1)
    ]
    |> List.flatten()
    |> Enum.filter(& &1)
  end

  #
  # Validation fns
  #

  defp validate_has_route_master(relations) do
    count = Enum.count(relations, &is_route_master/1)
    all_roundtrips = Enum.all?(relations, &is_roundtrip/1)

    cond do
      count == 0 and !all_roundtrips -> "Missing route master"
      count > 1 -> "Multiple route masters"
      true -> nil
    end
  end

  defp validate_has_relations(relations) do
    if Enum.empty?(relations) do
      "No OSM relations found"
    end
  end

  defp validate_routes_are_contained_in_route_master(relations) do
    route_masters = Enum.filter(relations, &is_route_master/1)

    if length(route_masters) == 1 do
      [route_master] = route_masters
      routes = Enum.filter(relations, &is_route/1)
      contained_ids = MapSet.new(route_master.parts)
      route_ids = routes |> Enum.map(& &1.id) |> MapSet.new()
      extra_ids = MapSet.difference(route_ids, contained_ids)

      if !Enum.empty?(extra_ids) do
        "Routes not contained in route master: #{Enum.join(extra_ids, ", ")}"
      end
    end
  end

  defp validate_routes_master_route_has_no_unknown_members(relations) do
    route_masters = Enum.filter(relations, &is_route_master/1)

    if length(route_masters) == 1 do
      [route_master] = route_masters
      routes = Enum.filter(relations, &is_route/1)
      contained_ids = MapSet.new(route_master.parts)
      route_ids = routes |> Enum.map(& &1.id) |> MapSet.new()
      ids = MapSet.difference(contained_ids, route_ids)

      if !Enum.empty?(ids) do
        "Master route has unknown members: #{Enum.join(ids, ", ")}"
      end
    end
  end

  defp validate_route_master(%Route{} = route, %Relation{type: "route_master"} = rel) do
    [
      validate_route_master_naming(rel),
      validate_route_master_required_tags(rel),
      validate_route_master_allowed_tags(rel),
      validate_route_master_tag(route, rel)
    ]
  end

  defp validate_route_master(_, _), do: nil

  defp validate_route_master_naming(relation) do
    if has_tags(relation, ["name", "ref", "route_master"]) do
      name = get_tag(relation, "name")
      ref = get_tag(relation, "ref")
      type = get_tag(relation, "route_master")

      expected = "#{String.capitalize(type)} #{ref}"

      if name != expected do
        {relation, "Expected name: '#{expected}', actual name: '#{name}'"}
      end
    end
  end

  defp validate_route_master_required_tags(relation) do
    required_tags = MapSet.new(["type", "route_master", "ref", "name", "operator"])
    existing_tags = MapSet.new(Map.keys(relation.tags))
    missing_tags = MapSet.difference(required_tags, existing_tags)

    if MapSet.size(missing_tags) > 0 do
      {relation, "Missing required tags: #{Enum.join(missing_tags, ", ")}"}
    end
  end

  defp validate_route_master_allowed_tags(relation) do
    allowed_tags = MapSet.new(["type", "route_master", "ref", "name", "operator", "network"])
    existing_tags = MapSet.new(Map.keys(relation.tags))
    unknown_tags = MapSet.difference(existing_tags, allowed_tags)

    if MapSet.size(unknown_tags) > 0 do
      {relation, "Unknown tags: #{Enum.join(unknown_tags, ", ")}"}
    end
  end

  # Relations [type=route_master] must have [route_master=bus] or [route_master=tram].
  defp validate_route_master_tag(route, relation) do
    actual = get_tag(relation, "route_master")
    expected = case route.type do
      0 -> "tram"
      3 -> "bus"
    end

    if !is_nil(actual) and actual != expected do
      {relation, "Invalid tag: [route_master=#{actual}], expected: [route_master=#{expected}]"}
    end
  end

  defp validate_route_naming(%Relation{} = rel) do
    if is_route(rel) and has_tags(rel, ["name", "ref", "route", "from", "to"]) do
      name = get_tag(rel, "name")
      ref = get_tag(rel, "ref")
      type = get_tag(rel, "route")
      from = get_tag(rel, "from")
      to = get_tag(rel, "to")
      via = get_tag(rel, "via")

      expected =
        if via do
          "#{String.capitalize(type)} #{ref}: #{from} => #{via} => #{to}"
        else
          "#{String.capitalize(type)} #{ref}: #{from} => #{to}"
        end

      if name != expected do
        {rel, "Expected name based on tags: '#{expected}', actual name: '#{name}'"}
      end
    end
  end

  #
  # Util fns
  #

  defp get_tag(%Relation{} = relation, name), do: Map.get(relation.tags, name)
  defp has_tag(%Relation{} = relation, name), do: get_tag(relation, name) not in [nil, ""]
  defp has_tags(%Relation{} = relation, names), do: Enum.all?(names, &has_tag(relation, &1))
  defp type(%Relation{} = relation), do: get_tag(relation, "type")

  defp is_route(relation), do: type(relation) == "route"
  defp is_route_master(relation), do: type(relation) == "route_master"
  defp is_roundtrip(relation), do: get_tag(relation, "roundtrip") == "yes"
  defp is_ptv2(relation), do: get_tag(relation, "public_transport:version") == "2"
end