2019-03-12 10:52:28 +00:00
|
|
|
defmodule Mobilizon.Service.Geospatial.Addok do
|
|
|
|
@moduledoc """
|
|
|
|
[Addok](https://github.com/addok/addok) backend.
|
|
|
|
"""
|
2019-09-22 14:26:23 +00:00
|
|
|
|
|
|
|
alias Mobilizon.Addresses.Address
|
2020-01-28 19:15:59 +00:00
|
|
|
alias Mobilizon.Service.Geospatial.Provider
|
2020-08-30 21:29:56 +00:00
|
|
|
alias Mobilizon.Service.HTTP.GeospatialClient
|
2021-03-16 14:33:44 +00:00
|
|
|
import Mobilizon.Service.Geospatial.Provider, only: [endpoint: 1]
|
2019-03-12 10:52:28 +00:00
|
|
|
require Logger
|
|
|
|
|
|
|
|
@behaviour Provider
|
|
|
|
|
|
|
|
@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
|
2020-08-30 21:29:56 +00:00
|
|
|
:geocode
|
|
|
|
|> build_url(%{lon: lon, lat: lat}, options)
|
|
|
|
|> fetch_features
|
2019-03-12 10:52:28 +00:00
|
|
|
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
|
2020-08-30 21:29:56 +00:00
|
|
|
:search
|
|
|
|
|> build_url(%{q: q}, options)
|
|
|
|
|> fetch_features
|
2019-03-12 10:52:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec build_url(atom(), map(), list()) :: String.t()
|
|
|
|
defp build_url(method, args, options) do
|
|
|
|
limit = Keyword.get(options, :limit, 10)
|
2021-03-16 14:33:44 +00:00
|
|
|
endpoint = Keyword.get(options, :endpoint, endpoint(__MODULE__))
|
2019-03-12 10:52:28 +00:00
|
|
|
|
|
|
|
case method do
|
|
|
|
:geocode ->
|
|
|
|
"#{endpoint}/reverse/?lon=#{args.lon}&lat=#{args.lat}&limit=#{limit}"
|
|
|
|
|
|
|
|
:search ->
|
2021-02-12 17:19:49 +00:00
|
|
|
"#{endpoint}/search/?q=#{URI.encode(args.q)}&limit=#{limit}"
|
|
|
|
|> add_parameter(options, :country_code)
|
|
|
|
|> add_parameter(options, :type)
|
2019-03-12 10:52:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-30 21:29:56 +00:00
|
|
|
@spec fetch_features(String.t()) :: list(Address.t())
|
|
|
|
defp fetch_features(url) do
|
|
|
|
Logger.debug("Asking addok with #{url}")
|
|
|
|
|
|
|
|
with {:ok, %{status: 200, body: body}} <- GeospatialClient.get(url),
|
|
|
|
%{"features" => features} <- body do
|
|
|
|
process_data(features)
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
Logger.error("Asking addok with #{url}")
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-23 16:06:22 +00:00
|
|
|
defp process_data(features) do
|
2019-03-12 10:52:28 +00:00
|
|
|
features
|
|
|
|
|> Enum.map(fn %{"geometry" => geometry, "properties" => properties} ->
|
|
|
|
%Address{
|
2021-03-16 14:33:44 +00:00
|
|
|
country: Map.get(properties, "country", default_country()),
|
2019-03-22 14:51:23 +00:00
|
|
|
locality: Map.get(properties, "city"),
|
2020-06-26 09:16:48 +00:00
|
|
|
region: Map.get(properties, "context"),
|
2019-07-23 16:06:22 +00:00
|
|
|
description: Map.get(properties, "name") || street_address(properties),
|
2019-09-22 11:41:24 +00:00
|
|
|
geom: geometry |> Map.get("coordinates") |> Provider.coordinates(),
|
2019-03-22 14:51:23 +00:00
|
|
|
postal_code: Map.get(properties, "postcode"),
|
2019-07-23 16:06:22 +00:00
|
|
|
street: properties |> street_address()
|
2019-03-12 10:52:28 +00:00
|
|
|
}
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2019-07-23 16:06:22 +00:00
|
|
|
defp street_address(properties) do
|
2019-03-12 10:52:28 +00:00
|
|
|
if Map.has_key?(properties, "housenumber") do
|
|
|
|
Map.get(properties, "housenumber") <> " " <> Map.get(properties, "street")
|
|
|
|
else
|
|
|
|
Map.get(properties, "street")
|
|
|
|
end
|
|
|
|
end
|
2021-02-12 17:19:49 +00:00
|
|
|
|
|
|
|
@spec add_parameter(String.t(), Keyword.t(), atom()) :: String.t()
|
|
|
|
defp add_parameter(url, options, key) do
|
|
|
|
value = Keyword.get(options, key)
|
|
|
|
|
|
|
|
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, :coords, coords),
|
|
|
|
do: "#{url}&lat=#{coords.lat}&lon=#{coords.lon}"
|
|
|
|
|
|
|
|
defp do_add_parameter(url, :type, :administrative),
|
|
|
|
do: "#{url}&type=municipality"
|
|
|
|
|
|
|
|
defp do_add_parameter(url, :type, _type), do: url
|
2021-03-16 14:33:44 +00:00
|
|
|
|
|
|
|
defp default_country do
|
|
|
|
Application.get_env(:mobilizon, __MODULE__) |> get_in([:default_country]) ||
|
|
|
|
"France"
|
|
|
|
end
|
2019-03-12 10:52:28 +00:00
|
|
|
end
|