~ihabunek/triglav

9a0a35d35b52c191c488a70d84e67db049b923f0 — Ivan Habunek 9 months ago a80d305
Migrate routes pages to osmosis data
M lib/triglav/html.ex => lib/triglav/html.ex +2 -2
@@ 1,4 1,4 @@
defimpl Phoenix.HTML.Safe, for: Triglav.Schemas.Osm.Relation do
defimpl Phoenix.HTML.Safe, for: Triglav.Schemas.Osmosis.Relation do
  use Phoenix.HTML

  def to_iodata(nil), do: ""


@@ 10,7 10,7 @@ defimpl Phoenix.HTML.Safe, for: Triglav.Schemas.Osm.Relation do
  end
end

defimpl Phoenix.HTML.Safe, for: Triglav.Schemas.Osm.Point do
defimpl Phoenix.HTML.Safe, for: Triglav.Schemas.Osmosis.Point do
  use Phoenix.HTML

  def to_iodata(nil), do: ""

M lib/triglav/zet/osmosis.ex => lib/triglav/zet/osmosis.ex +15 -5
@@ 74,11 74,21 @@ defmodule Triglav.Zet.Osmosis do
    )
  end

  defp maybe_filter_by_ref(query, ref: ref),
    do: where(query, [r], fragment("tags->?", "ref") == ^ref)
  defp maybe_filter_by_ref(query, opts) do
    ref = Keyword.get(opts, :ref)

  defp maybe_filter_by_ref(query, _), do: query
    if ref do
      where(query, [r], fragment("tags->?", "ref") == ^ref)
    else
      query
    end
  end

  defp maybe_preload_members(query, members: true), do: preload(query, [r], :members)
  defp maybe_preload_members(query, _), do: query
  defp maybe_preload_members(query, opts) do
    if Keyword.get(opts, :members, false) do
      preload(query, [r], :members)
    else
      query
    end
  end
end

M lib/triglav_web/controllers/zet/routes_controller.ex => lib/triglav_web/controllers/zet/routes_controller.ex +12 -10
@@ 2,13 2,13 @@ defmodule TriglavWeb.Zet.RoutesController do
  use TriglavWeb, :controller

  alias Triglav.DataImport
  alias Triglav.Schemas.Osm.Relation
  alias Triglav.Schemas.Osmosis.Relation
  alias Triglav.Zet.Gtfs
  alias Triglav.Zet.Osm
  alias Triglav.Zet.Osmosis

  def index(conn, _params) do
    routes = Gtfs.list_routes(with_errors: true)
    relations = Osm.list_public_transport_relations()
    relations = Osmosis.list_public_transport_relations(members: true)
    osm_info = DataImport.load_state()
    gtfs_info = Gtfs.get_feed_info()



@@ 36,9 36,9 @@ defmodule TriglavWeb.Zet.RoutesController do

  def detail(conn, %{"id" => id}) do
    route = Gtfs.get_route(id)
    relations = Osm.list_public_transport_relations(ref: id)
    relations = Osmosis.list_public_transport_relations(ref: id, members: true)
    hierarchy = make_hierarchy(relations)
    platforms = Osm.list_platform_nodes(relations) |> Map.new(&{&1.id, &1})
    platforms = Osmosis.list_platform_nodes(relations) |> Map.new(&{&1.id, &1})
    {errors, rel_errors} = Gtfs.get_errors(id) |> group_errors_by_relation()

    trips =


@@ 71,8 71,10 @@ defmodule TriglavWeb.Zet.RoutesController do
    relations_stop_ids =
      for relation <- route_relations do
        relation.members
        |> Enum.filter(&(String.starts_with?(&1.role, "platform") and &1.type == :node))
        |> Enum.map(&Map.get(platforms, &1.id))
        |> Enum.filter(
          &(String.starts_with?(&1.member_role, "platform") and &1.member_type == :node)
        )
        |> Enum.map(&Map.get(platforms, &1.member_id))
        |> Enum.map(&Map.get(&1.tags, "gtfs:stop_id"))
      end



@@ 97,15 99,15 @@ defmodule TriglavWeb.Zet.RoutesController do
  end

  defp make_hierarchy(relations) do
    {route_masters, routes} = Enum.split_with(relations, &(&1.tags["type"] == "route_master"))
    {route_masters, routes} = Enum.split_with(relations, &(Relation.type(&1) == "route_master"))

    # Add `route` relations to `route_master` relations
    route_masters =
      route_masters
      |> Enum.map(fn route_master ->
        contained_routes =
          Enum.map(route_master.parts, fn route_id ->
            Enum.find(routes, &(&1.id == route_id))
          Enum.map(route_master.members, fn member ->
            Enum.find(routes, &(&1.id == member.member_id))
          end)

        Map.put(route_master, :contained_routes, contained_routes)