~ihabunek/triglav

3db2b18347671890476a5fbfad1c7b9f23605d42 — Ivan Habunek 1 year, 8 months ago a91f30b wip
wip
M lib/triglav/public_transport/mappings.ex => lib/triglav/public_transport/mappings.ex +43 -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)

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



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

  # For each platorm mapped to a
  def add_distance_from_route(feed_id) do
    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]
    )
    |> IO.inspect()

    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]
    )
    |> IO.inspect()

    :ok
  end

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


@@ 167,6 209,7 @@ defmodule Triglav.PublicTransport.Mappings do
    Logger.debug("#{feed.operator.ref} #{feed.ref}: Created #{c2} route master mappings")
  end

  # For each RouteVariant, set the `mapped_relation` field
  defp generate_route_variant_mappings(feed) do
    Repo.update_all(where(RouteVariant, feed_id: ^feed.id), set: [mapped_relation_id: nil])


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 +3 -0
@@ 13,5 13,8 @@ defmodule Triglav.PublicTransport.Schemas.RouteVariantPlatform do
    belongs_to :platform, Platform

    field :sequence_no, :integer

    # Derived data
    field :distance_from_route, :integer
  end
end

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