~ihabunek/triglav

0db0f4b1e6800fb29426404a044aeab0b4f8ea8b — Ivan Habunek 11 months ago fbfed6d
Start ZET Osmosis context
2 files changed, 36 insertions(+), 0 deletions(-)

A lib/triglav/zet/osmosis.ex
M lib/triglav/zet/validator.ex
A lib/triglav/zet/osmosis.ex => lib/triglav/zet/osmosis.ex +35 -0
@@ 0,0 1,35 @@
defmodule Triglav.Zet.Osmosis do
  @moduledoc """
  Loads public relation data from the osmosis schema.
  """
  alias Triglav.Repo
  alias Triglav.Schemas.Osmosis.Node
  alias Triglav.Schemas.Osmosis.Relation
  alias Triglav.Schemas.Osmosis.Way

  import Ecto.Query
  import Geo.PostGIS

  def list_public_transport_relations(opts \\ []) do
    pt_relations()
    |> maybe_filter_by_ref(opts)
    |> maybe_preload_members(opts)
    |> Repo.all()
  end

  defp pt_relations() do
    from(r in Relation,
      where:
        fragment("tags->?", "type") in ["route", "route_master"] and
          fragment("lower(tags->?)", "network") == "zet"
    )
  end

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

  defp maybe_filter_by_ref(query, _), do: query

  defp maybe_preload_members(query, members: true), do: preload(query, [r], :members)
  defp maybe_preload_members(query, _), do: query
end

M lib/triglav/zet/validator.ex => lib/triglav/zet/validator.ex +1 -0
@@ 8,6 8,7 @@ defmodule Triglav.Zet.Validator do
  alias Triglav.Schemas.Zet.Route
  alias Triglav.Zet.Gtfs
  alias Triglav.Zet.Osm
  alias Triglav.Zet.Osmosis

  @spec validate_all_routes() :: [Error.t()]
  def validate_all_routes() do