~ihabunek/triglav

ref: 5bee7c999e50283fe87056046ad9ab42e362e70c triglav/lib/triglav/zet/validator.ex -rw-r--r-- 4.3 KiB
5bee7c99Ivan Habunek Improve osm_link helper 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
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_naming/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_naming(%Relation{} = rel) do
    if is_route_master(rel) and has_tags(rel, ["name", "ref", "route_master"]) do
      name = get_tag(rel, "name")
      ref = get_tag(rel, "ref")
      type = get_tag(rel, "route_master")

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

      if name != expected do
        {rel, "Expected name: '#{expected}', actual name: '#{name}'"}
      end
    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