~ihabunek/triglav

ref: d1b5915f3e023d04850eabf702bda7207a337302 triglav/lib/triglav/osm.ex -rw-r--r-- 1.1 KiB
d1b5915fIvan Habunek Validate platform distance from route 7 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
44
45
46
47
defmodule Triglav.Osm do
  alias Triglav.Repo
  alias Triglav.Schemas.Osmosis.Node
  alias Triglav.Schemas.Osmosis.Relation
  alias Triglav.Schemas.Osmosis.Way

  import Ecto.Query

  def get_relation(relation_id) do
    Repo.get(Relation, relation_id)
  end

  @doc """
  Returns a list of nodes with given ids, ordered the same as the given ids.
  """
  @spec list_ordered_nodes([integer()]) :: [Node.t()]
  def list_ordered_nodes(node_ids) do
    node_map =
      from(n in Node, where: n.id in ^node_ids)
      |> Repo.all()
      |> Map.new(&{&1.id, &1})

    Enum.map(node_ids, &Map.get(node_map, &1))
  end

  def list_ordered_ways(relation_id) when is_integer(relation_id) do
    relation_id
    |> get_relation()
    |> list_ordered_ways()
  end

  def list_ordered_ways(%Relation{} = relation) do
    way_ids =
      relation
      |> Repo.preload(:members)
      |> Map.get(:members)
      |> Enum.filter(&(&1.member_type == "W" and &1.member_role == ""))
      |> Enum.map(& &1.member_id)

    ways =
      from(w in Way, where: w.id in ^way_ids)
      |> Repo.all()
      |> Map.new(&{&1.id, &1})

    Enum.map(way_ids, &Map.get(ways, &1))
  end
end