2019-03-12 11:52:28 +01:00
|
|
|
defmodule Mobilizon.Service.Geospatial.Nominatim do
|
|
|
|
@moduledoc """
|
|
|
|
[Nominatim](https://wiki.openstreetmap.org/wiki/Nominatim) backend.
|
|
|
|
"""
|
2019-09-22 16:26:23 +02:00
|
|
|
|
2019-03-12 11:52:28 +01:00
|
|
|
alias Mobilizon.Addresses.Address
|
2020-01-28 20:15:59 +01:00
|
|
|
alias Mobilizon.Service.Geospatial.Provider
|
2020-08-30 23:29:56 +02:00
|
|
|
alias Mobilizon.Service.HTTP.GeospatialClient
|
2021-03-16 15:33:44 +01:00
|
|
|
import Mobilizon.Service.Geospatial.Provider, only: [endpoint: 1]
|
2019-03-12 11:52:28 +01:00
|
|
|
require Logger
|
|
|
|
|
|
|
|
@behaviour Provider
|
|
|
|
|
|
|
|
@impl Provider
|
|
|
|
@doc """
|
|
|
|
Nominatim implementation for `c:Mobilizon.Service.Geospatial.Provider.geocode/3`.
|
|
|
|
"""
|
|
|
|
@spec geocode(String.t(), keyword()) :: list(Address.t())
|
|
|
|
def geocode(lon, lat, options \\ []) do
|
2020-08-30 23:29:56 +02:00
|
|
|
:geocode
|
|
|
|
|> build_url(%{lon: lon, lat: lat}, options)
|
|
|
|
|> fetch_features
|
2019-03-12 11:52:28 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl Provider
|
|
|
|
@doc """
|
|
|
|
Nominatim implementation for `c:Mobilizon.Service.Geospatial.Provider.search/2`.
|
|
|
|
"""
|
|
|
|
@spec search(String.t(), keyword()) :: list(Address.t())
|
|
|
|
def search(q, options \\ []) do
|
2020-08-30 23:29:56 +02:00
|
|
|
:search
|
|
|
|
|> build_url(%{q: q}, options)
|
|
|
|
|> fetch_features
|
2019-03-12 11:52:28 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec build_url(atom(), map(), list()) :: String.t()
|
|
|
|
defp build_url(method, args, options) do
|
|
|
|
limit = Keyword.get(options, :limit, 10)
|
|
|
|
lang = Keyword.get(options, :lang, "en")
|
2021-03-16 15:33:44 +01:00
|
|
|
endpoint = Keyword.get(options, :endpoint, endpoint(__MODULE__))
|
2019-03-12 11:52:28 +01:00
|
|
|
|
|
|
|
url =
|
|
|
|
case method do
|
|
|
|
:search ->
|
2019-11-08 19:37:14 +01:00
|
|
|
"#{endpoint}/search?format=geocodejson&q=#{URI.encode(args.q)}&limit=#{limit}&accept-language=#{
|
2019-03-12 11:52:28 +01:00
|
|
|
lang
|
2019-11-08 19:37:14 +01:00
|
|
|
}&addressdetails=1&namedetails=1"
|
2019-03-12 11:52:28 +01:00
|
|
|
|
|
|
|
:geocode ->
|
2021-02-12 18:19:49 +01:00
|
|
|
"#{endpoint}/reverse?format=geocodejson&lat=#{args.lat}&lon=#{args.lon}&accept-language=#{
|
|
|
|
lang
|
|
|
|
}&addressdetails=1&namedetails=1"
|
|
|
|
|> add_parameter(options, :zoom)
|
2019-03-12 11:52:28 +01:00
|
|
|
end
|
|
|
|
|
2021-02-12 18:19:49 +01:00
|
|
|
url
|
|
|
|
|> add_parameter(options, :country_code)
|
2021-03-16 15:33:44 +01:00
|
|
|
|> add_parameter(options, :api_key, api_key())
|
2019-03-12 11:52:28 +01:00
|
|
|
end
|
|
|
|
|
2020-08-30 23:29:56 +02:00
|
|
|
@spec fetch_features(String.t()) :: list(Address.t())
|
|
|
|
defp fetch_features(url) do
|
|
|
|
Logger.debug("Asking Nominatim with #{url}")
|
|
|
|
|
|
|
|
with {:ok, %{status: 200, body: body}} <- GeospatialClient.get(url),
|
|
|
|
%{"features" => features} <- body do
|
|
|
|
features |> process_data |> Enum.filter(& &1)
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
Logger.error("Asking Nominatim with #{url}")
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-08 19:37:14 +01:00
|
|
|
defp process_data(features) do
|
|
|
|
features
|
|
|
|
|> Enum.map(fn %{
|
|
|
|
"geometry" => %{"coordinates" => coordinates},
|
|
|
|
"properties" => %{"geocoding" => geocoding}
|
|
|
|
} ->
|
|
|
|
address = process_address(geocoding)
|
|
|
|
%Address{address | geom: Provider.coordinates(coordinates)}
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp process_address(geocoding) do
|
2019-09-22 16:26:23 +02:00
|
|
|
%Address{
|
2019-11-08 19:37:14 +01:00
|
|
|
country: Map.get(geocoding, "country"),
|
|
|
|
locality:
|
|
|
|
Map.get(geocoding, "city") || Map.get(geocoding, "town") || Map.get(geocoding, "county"),
|
|
|
|
region: Map.get(geocoding, "state"),
|
|
|
|
description: description(geocoding),
|
|
|
|
postal_code: Map.get(geocoding, "postcode"),
|
|
|
|
type: Map.get(geocoding, "type"),
|
|
|
|
street: street_address(geocoding),
|
|
|
|
origin_id: "nominatim:" <> to_string(Map.get(geocoding, "osm_id"))
|
2019-09-22 16:26:23 +02:00
|
|
|
}
|
2019-03-12 11:52:28 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec street_address(map()) :: String.t()
|
|
|
|
defp street_address(body) do
|
2019-08-22 15:57:44 +02:00
|
|
|
road =
|
|
|
|
cond do
|
|
|
|
Map.has_key?(body, "road") ->
|
|
|
|
Map.get(body, "road")
|
|
|
|
|
2019-11-08 19:37:14 +01:00
|
|
|
Map.has_key?(body, "street") ->
|
|
|
|
Map.get(body, "street")
|
2019-08-22 15:57:44 +02:00
|
|
|
|
|
|
|
Map.has_key?(body, "pedestrian") ->
|
|
|
|
Map.get(body, "pedestrian")
|
|
|
|
|
|
|
|
true ->
|
|
|
|
""
|
|
|
|
end
|
|
|
|
|
2019-11-08 19:37:14 +01:00
|
|
|
Map.get(body, "housenumber", "") <> " " <> road
|
2019-08-22 15:57:44 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@address29_classes ["amenity", "shop", "tourism", "leisure"]
|
|
|
|
@address29_categories ["office"]
|
|
|
|
|
|
|
|
@spec description(map()) :: String.t()
|
|
|
|
defp description(body) do
|
2019-11-08 19:37:14 +01:00
|
|
|
description = Map.get(body, "name")
|
2019-08-22 15:57:44 +02:00
|
|
|
address = Map.get(body, "address")
|
|
|
|
|
2019-11-08 19:37:14 +01:00
|
|
|
description =
|
|
|
|
if Map.has_key?(body, "namedetails"),
|
|
|
|
do: body |> Map.get("namedetails") |> Map.get("name", description),
|
|
|
|
else: description
|
|
|
|
|
|
|
|
description = if is_nil(description), do: street_address(body), else: description
|
|
|
|
|
2019-08-22 15:57:44 +02:00
|
|
|
if (Map.get(body, "category") in @address29_categories or
|
|
|
|
Map.get(body, "class") in @address29_classes) and Map.has_key?(address, "address29") do
|
|
|
|
Map.get(address, "address29")
|
2019-03-12 11:52:28 +01:00
|
|
|
else
|
2019-08-22 15:57:44 +02:00
|
|
|
description
|
2019-03-12 11:52:28 +01:00
|
|
|
end
|
|
|
|
end
|
2021-02-12 18:19:49 +01:00
|
|
|
|
|
|
|
@spec add_parameter(String.t(), Keyword.t(), atom()) :: String.t()
|
|
|
|
defp add_parameter(url, options, key, default \\ nil) do
|
|
|
|
value = Keyword.get(options, key, default)
|
|
|
|
|
|
|
|
if is_nil(value), do: url, else: do_add_parameter(url, key, value)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec do_add_parameter(String.t(), atom(), any()) :: String.t()
|
|
|
|
defp do_add_parameter(url, :zoom, zoom),
|
|
|
|
do: "#{url}&zoom=#{zoom}"
|
|
|
|
|
|
|
|
@spec do_add_parameter(String.t(), atom(), any()) :: String.t()
|
|
|
|
defp do_add_parameter(url, :country_code, country_code),
|
|
|
|
do: "#{url}&countrycodes=#{country_code}"
|
|
|
|
|
|
|
|
@spec do_add_parameter(String.t(), atom(), any()) :: String.t()
|
|
|
|
defp do_add_parameter(url, :api_key, api_key),
|
|
|
|
do: "#{url}&key=#{api_key}"
|
2021-03-16 15:33:44 +01:00
|
|
|
|
|
|
|
defp api_key do
|
|
|
|
Application.get_env(:mobilizon, __MODULE__) |> get_in([:api_key])
|
|
|
|
end
|
2019-03-12 11:52:28 +01:00
|
|
|
end
|