~ihabunek/triglav

7691d4645c00a9d4b8a0bb69ed0b0096eb8f0417 — Ivan Habunek 1 year, 8 months ago a91f30b distance_from_route
WIP
M lib/triglav/public_transport/mappings.ex => lib/triglav/public_transport/mappings.ex +45 -0
@@ 27,6 27,7 @@ defmodule Triglav.PublicTransport.Mappings do
    generate_platform_mappings(feed)
    generate_route_mappings(feed)
    generate_route_variant_mappings(feed)
    add_distance_from_route(feed.id)

    Logger.info("#{feed.operator.ref} #{feed.ref}: Mappings done")



@@ 124,6 125,50 @@ defmodule Triglav.PublicTransport.Mappings do
    )
  end

  def add_distance_from_route(feed_id) do
    # For nodes
    Repo.query!(
      """
      UPDATE pt_route_variant_platforms AS rvp
      SET distance_from_route = st_distance(p.geometry::geography, n.geom::geography)
      FROM pt_platforms p,
           pt_route_variants rv,
           pt_platform_mappings pm,
           osmosis.nodes n
      WHERE rvp.feed_id = $1
        AND p.id = rvp.platform_id
        AND rv.id = rvp.route_variant_id
        AND pm.platform_id = p.id
        AND n.id = pm.node_id
      """,
      [feed_id]
    )

    # For ways
    #
    # TODO: currently there aren't any because tram platforms in zagreb which
    # use ways for platforms don't have the `source` tag set. To fix this we
    # should add set source tag to "Zagrebački električni tramvaj".
    Repo.query!(
      """
      UPDATE pt_route_variant_platforms AS rvp
      SET distance_from_route = st_distance(p.geometry::geography, w.linestring::geography)
      FROM pt_platforms p,
           pt_route_variants rv,
           pt_platform_mappings pm,
           osmosis.ways w
      WHERE rvp.feed_id = $1
        AND p.id = rvp.platform_id
        AND rv.id = rvp.route_variant_id
        AND pm.platform_id = p.id
        AND w.id = pm.way_id
      """,
      [feed_id]
    )

    :ok
  end

  defp generate_route_mappings(feed) do
    mappings =
      from(route in Route,

M lib/triglav/public_transport/schemas/platform_mapping.ex => lib/triglav/public_transport/schemas/platform_mapping.ex +3 -0
@@ 1,4 1,7 @@
defmodule Triglav.PublicTransport.Schemas.PlatformMapping do
  @moduledoc """
  Maps GTFS platforms to an OSM way or node.
  """
  use Ecto.Schema

  @type t() :: %__MODULE__{}

M lib/triglav/public_transport/schemas/route_variant_platform.ex => lib/triglav/public_transport/schemas/route_variant_platform.ex +4 -0
@@ 12,6 12,10 @@ defmodule Triglav.PublicTransport.Schemas.RouteVariantPlatform do
    belongs_to :route_variant, RouteVariant
    belongs_to :platform, Platform

    # Derived from OSM data
    field :distance_from_route, :float

    # Order of platform in the route variant
    field :sequence_no, :integer
  end
end

M lib/triglav_web/templates/public_transport/route.html.heex => lib/triglav_web/templates/public_transport/route.html.heex +2 -0
@@ 48,6 48,7 @@
              <th>Platform</th>
              <th>Name</th>
              <th>Stop ID</th>
              <th>Distance</th>
              <th>Errors</th>
            </tr>
          </thead>


@@ 67,6 68,7 @@
                    <span class="text-red-900">Missing</span>
                  <% end %>
                </td>
                <td></td>
                <% errors = @platform_errors[relation.id][member.sequence_id] %>
                <td class={(if errors, do: "error")}>
                  <%= if errors do %>

A priv/repo/migrations/20220927091004_alter_pt_route_variant_platforms_add_distance_from_route.exs => priv/repo/migrations/20220927091004_alter_pt_route_variant_platforms_add_distance_from_route.exs +9 -0
@@ 0,0 1,9 @@
defmodule Triglav.Repo.Migrations.AlterPtRouteVariantPlatformsAddDistanceFromRoute do
  use Ecto.Migration

  def change do
    alter table(:pt_route_variant_platforms) do
      add :distance_from_route, :float
    end
  end
end