~ihabunek/triglav

ref: 447e9869e2e0c07f789d2c1dbe68c0b1891e72ba triglav/lib/triglav/zet/osmosis.ex -rw-r--r-- 951 bytes
447e9869Ivan Habunek Ask confirmation before nuking osmosis schema 1 year, 3 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
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