~ihabunek/triglav

ref: 3a62b8feb3be418cf9f3fb176ac6c84a36e66a2f triglav/lib/triglav_web/views/osm_helpers.ex -rw-r--r-- 4.5 KiB
3a62b8feIvan Habunek Validate tags on platforms 8 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
defmodule TriglavWeb.OsmHelpers do
  @moduledoc """
  Helpers for displaying OSM data.
  """
  use Phoenix.HTML

  alias Triglav.Schemas.Osmosis.Node
  alias Triglav.Schemas.Osmosis.Relation
  alias Triglav.Schemas.Osmosis.Way
  alias Triglav.Zet.Errors
  alias TriglavWeb.Router.Helpers

  @doc """
  Renders a link to the relation on OpenStreetMap website.
  """
  def osm_link(element, opts \\ [])

  def osm_link(%Relation{} = relation, opts) do
    image_url = Helpers.static_path(TriglavWeb.Endpoint, "/images/osm/relation.svg")
    target_url = "https://www.openstreetmap.org/relation/#{relation.id}"
    name? = Keyword.get(opts, :name)
    josm? = Keyword.get(opts, :josm)

    josm_link = Triglav.Josm.load_object(relation)

    tags =
      opts
      |> Keyword.get(:tags, [])
      |> Enum.filter(&Relation.has_tag?(relation, &1))
      |> Enum.map(&{&1, Relation.get_tag(relation, &1)})

    ~E"""
    <span class="osm-link">
      <img src="<%= image_url %>" alt="Relation" />
      <a href="<%= target_url %>"><%= relation.id %></a>
      <%= if name? do %>
        <%= relation.tags["name"] %>
      <% end %>
      <%= if !Enum.empty?(tags) do %>
        <span class="tag text-smaller">
          <%= for {k, v} <- tags do %>[<%= k %>=<%= v %>]<% end %>
        </span>
      <% end %>
      <%= if josm? do %>
        <a class="josm-remote" href="<%= josm_link %>" title="Open relation in JOSM" target="_blank">
          🜨 Load
        </a>
      <% end %>
    </span>
    """
  end

  def osm_link(%Node{} = node, opts) do
    image_url = Helpers.static_path(TriglavWeb.Endpoint, "/images/osm/node.svg")
    target_url = "https://www.openstreetmap.org/node/#{node.id}"
    name? = Keyword.get(opts, :name)
    josm? = Keyword.get(opts, :josm)

    josm_link = Triglav.Josm.load_object(node)

    tags =
      opts
      |> Keyword.get(:tags, [])
      |> Enum.filter(&Node.has_tag?(node, &1))
      |> Enum.map(&{&1, Node.get_tag(node, &1)})

    ~E"""
    <span class="osm-link">
      <img src="<%= image_url %>" alt="node" />
      <a href="<%= target_url %>"><%= node.id %></a>
      <%= if name? do %>
        <%= node.tags["name"] %>
      <% end %>
      <%= if !Enum.empty?(tags) do %>
        <span class="tag text-smaller">
          <%= for {k, v} <- tags do %>[<%= k %>=<%= v %>]<% end %>
        </span>
      <% end %>
      <%= if josm? do %>
        <a class="josm-remote" href="<%= josm_link %>" title="Open node in JOSM" target="_blank">
          🜨
        </a>
      <% end %>
    </span>
    """
  end

  def osm_link(%Way{} = way, opts) do
    image_url = Helpers.static_path(TriglavWeb.Endpoint, "/images/osm/way.svg")
    target_url = "https://www.openstreetmap.org/way/#{way.id}"
    name? = Keyword.get(opts, :name)
    josm? = Keyword.get(opts, :josm)

    josm_link = Triglav.Josm.load_object(way)

    tags =
      opts
      |> Keyword.get(:tags, [])
      |> Enum.filter(&Way.has_tag?(way, &1))
      |> Enum.map(&{&1, Way.get_tag(way, &1)})

    ~E"""
    <span class="osm-link">
      <img src="<%= image_url %>" alt="way" />
      <a href="<%= target_url %>"><%= way.id %></a>
      <%= if name? do %>
        <%= way.tags["name"] %>
      <% end %>
      <%= if !Enum.empty?(tags) do %>
        <span class="tag text-smaller">
          <%= for {k, v} <- tags do %>[<%= k %>=<%= v %>]<% end %>
        </span>
      <% end %>
      <%= if josm? do %>
        <a class="josm-remote" href="<%= josm_link %>" title="Open way in JOSM" target="_blank">
          🜨
        </a>
      <% end %>
    </span>
    """
  end

  def osm_link(_, _) do
    ~E"<p>FIXME: osm_link failed</p>"
  end

  def josm_load_objects(objects, opts \\ []) do
    {title, opts} = Keyword.pop(opts, :title, "Load")
    link = Triglav.Josm.load_objects(objects, opts)

    ~E"""
    <a class="josm-remote" href="<%= link %>" title="<%= title %>" target="josm">
      🜨
      <%= title %>
    </a>
    """
  end

  def josm_zoom(lat, lon, opts \\ []) do
    title = Keyword.get(opts, :title, "Zoom")
    link = Triglav.Josm.zoom(lat, lon)

    ~E"""
    <a href="<%= link %>" class="josm-remote" title="<%= title %>" target="josm">
      🜨
      <%= title %>
    </a>
    """
  end

  def josm_add_tags(object, tags) do
    link = Triglav.Josm.add_tags(object, tags)
    str_tags = Enum.map(tags, fn {k, v} -> "#{k}=#{v}" end) |> Enum.join(", ")

    ~E"""
    <a href="<%= link %>" class="josm-remote" title="Add tags" target="josm">
      🜨 Add <%= if map_size(tags) > 1, do: "tags", else: "tag" %> [<%= str_tags %>]
    </a>
    """
  end

  def render_error(error) do
    Errors.render(error)
  end
end