~ihabunek/triglav

ref: 3a62b8feb3be418cf9f3fb176ac6c84a36e66a2f triglav/lib/triglav_web/controllers/zet/stops_controller.ex -rw-r--r-- 1.1 KiB
3a62b8feIvan Habunek Validate tags on platforms 10 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
defmodule TriglavWeb.Zet.StopsController do
  use TriglavWeb, :controller

  alias Triglav.Schemas.Osmosis.Node
  alias Triglav.Schemas.Osmosis.Relation
  alias Triglav.Schemas.Osmosis.Way
  alias Triglav.Zet.Gtfs
  alias Triglav.Zet.Osmosis

  def index(conn, _params) do
    relations = Osmosis.list_public_transport_relations()
    relation_ids = Enum.map(relations, & &1.id)

    unmatched_platforms =
      Osmosis.list_platform_members(relation_ids)
      |> Enum.map(& &1.member)
      # TODO: move sort/filter to database
      |> Enum.uniq()
      |> Enum.reject(&Relation.has_tag?(&1, "gtfs:stop_id"))
      |> Enum.sort_by(& &1.id)

    annotated_unmatched_platforms =
      Enum.map(unmatched_platforms, fn member ->
        {stop, distance} =
          case member do
            %Node{} = node -> Gtfs.find_closest_stop(node.geom)
            %Way{} = way -> Gtfs.find_closest_stop(way.linestring)
          end

        {member, stop, distance}
      end)

    render(conn, "index.html",
      unmatched_platforms: unmatched_platforms,
      annotated_unmatched_platforms: annotated_unmatched_platforms
    )
  end
end