2019-03-12 11:52:28 +01:00
|
|
|
defmodule Mobilizon.Service.Geospatial.Addok do
|
|
|
|
@moduledoc """
|
|
|
|
[Addok](https://github.com/addok/addok) backend.
|
|
|
|
"""
|
2019-09-22 16:26:23 +02:00
|
|
|
|
|
|
|
alias Mobilizon.Addresses.Address
|
2019-10-11 17:20:03 +02:00
|
|
|
alias Mobilizon.Config
|
2020-01-28 20:15:59 +01:00
|
|
|
alias Mobilizon.Service.Geospatial.Provider
|
2019-09-22 16:26:23 +02:00
|
|
|
|
2019-03-12 11:52:28 +01:00
|
|
|
require Logger
|
|
|
|
|
|
|
|
@behaviour Provider
|
|
|
|
|
|
|
|
@endpoint Application.get_env(:mobilizon, __MODULE__) |> get_in([:endpoint])
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
@http_options [
|
|
|
|
follow_redirect: true,
|
|
|
|
ssl: [{:versions, [:"tlsv1.2"]}]
|
|
|
|
]
|
|
|
|
|
2019-03-12 11:52:28 +01:00
|
|
|
@impl Provider
|
|
|
|
@doc """
|
|
|
|
Addok implementation for `c:Mobilizon.Service.Geospatial.Provider.geocode/3`.
|
|
|
|
"""
|
|
|
|
@spec geocode(String.t(), keyword()) :: list(Address.t())
|
|
|
|
def geocode(lon, lat, options \\ []) do
|
2019-10-11 17:20:03 +02:00
|
|
|
user_agent = Keyword.get(options, :user_agent, Config.instance_user_agent())
|
|
|
|
headers = [{"User-Agent", user_agent}]
|
2019-03-12 11:52:28 +01:00
|
|
|
url = build_url(:geocode, %{lon: lon, lat: lat}, options)
|
|
|
|
|
|
|
|
Logger.debug("Asking addok for addresses with #{url}")
|
|
|
|
|
|
|
|
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
2020-02-18 08:57:00 +01:00
|
|
|
HTTPoison.get(url, headers, @http_options),
|
2019-03-12 11:52:28 +01:00
|
|
|
{:ok, %{"features" => features}} <- Poison.decode(body) do
|
2019-07-23 18:06:22 +02:00
|
|
|
process_data(features)
|
2019-11-20 13:49:57 +01:00
|
|
|
else
|
|
|
|
_ -> []
|
2019-03-12 11:52:28 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl Provider
|
|
|
|
@doc """
|
|
|
|
Addok implementation for `c:Mobilizon.Service.Geospatial.Provider.search/2`.
|
|
|
|
"""
|
|
|
|
@spec search(String.t(), keyword()) :: list(Address.t())
|
|
|
|
def search(q, options \\ []) do
|
2019-10-11 17:20:03 +02:00
|
|
|
user_agent = Keyword.get(options, :user_agent, Config.instance_user_agent())
|
|
|
|
headers = [{"User-Agent", user_agent}]
|
2019-03-12 11:52:28 +01:00
|
|
|
url = build_url(:search, %{q: q}, options)
|
|
|
|
Logger.debug("Asking addok for addresses with #{url}")
|
|
|
|
|
|
|
|
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
2020-02-18 08:57:00 +01:00
|
|
|
HTTPoison.get(url, headers, @http_options),
|
2019-03-12 11:52:28 +01:00
|
|
|
{:ok, %{"features" => features}} <- Poison.decode(body) do
|
2019-07-23 18:06:22 +02:00
|
|
|
process_data(features)
|
2019-11-20 13:49:57 +01:00
|
|
|
else
|
|
|
|
_ -> []
|
2019-03-12 11:52:28 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec build_url(atom(), map(), list()) :: String.t()
|
|
|
|
defp build_url(method, args, options) do
|
|
|
|
limit = Keyword.get(options, :limit, 10)
|
|
|
|
coords = Keyword.get(options, :coords, nil)
|
|
|
|
endpoint = Keyword.get(options, :endpoint, @endpoint)
|
|
|
|
|
|
|
|
case method do
|
|
|
|
:geocode ->
|
|
|
|
"#{endpoint}/reverse/?lon=#{args.lon}&lat=#{args.lat}&limit=#{limit}"
|
|
|
|
|
|
|
|
:search ->
|
|
|
|
url = "#{endpoint}/search/?q=#{URI.encode(args.q)}&limit=#{limit}"
|
|
|
|
if is_nil(coords), do: url, else: url <> "&lat=#{coords.lat}&lon=#{coords.lon}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-23 18:06:22 +02:00
|
|
|
defp process_data(features) do
|
2019-03-12 11:52:28 +01:00
|
|
|
features
|
|
|
|
|> Enum.map(fn %{"geometry" => geometry, "properties" => properties} ->
|
|
|
|
%Address{
|
2019-03-22 15:51:23 +01:00
|
|
|
country: Map.get(properties, "country"),
|
|
|
|
locality: Map.get(properties, "city"),
|
|
|
|
region: Map.get(properties, "state"),
|
2019-07-23 18:06:22 +02:00
|
|
|
description: Map.get(properties, "name") || street_address(properties),
|
2019-09-22 13:41:24 +02:00
|
|
|
geom: geometry |> Map.get("coordinates") |> Provider.coordinates(),
|
2019-03-22 15:51:23 +01:00
|
|
|
postal_code: Map.get(properties, "postcode"),
|
2019-07-23 18:06:22 +02:00
|
|
|
street: properties |> street_address()
|
2019-03-12 11:52:28 +01:00
|
|
|
}
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2019-07-23 18:06:22 +02:00
|
|
|
defp street_address(properties) do
|
2019-03-12 11:52:28 +01:00
|
|
|
if Map.has_key?(properties, "housenumber") do
|
|
|
|
Map.get(properties, "housenumber") <> " " <> Map.get(properties, "street")
|
|
|
|
else
|
|
|
|
Map.get(properties, "street")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|