~ihabunek/triglav

ref: 248d445efaf6ff9a19f9fc8c8ed4359a87aeb85b triglav/lib/triglav/zet/osmosis.ex -rw-r--r-- 1.2 KiB
248d445eIvan Habunek Data status page 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
36
37
38
39
40
41
42
43
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.ReplicationChanges
  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

  def list_replication_changes(opts \\ []) do
    limit = Keyword.get(opts, :limit, 100)

    from(rc in ReplicationChanges, order_by: [desc: :tstamp], limit: ^limit)
    |> 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