2019-11-19 16:59:04 +00:00
|
|
|
defmodule Mobilizon.Service.Geospatial.Pelias do
|
|
|
|
@moduledoc """
|
|
|
|
[Pelias](https://pelias.io) backend.
|
2019-11-19 19:01:31 +00:00
|
|
|
|
|
|
|
Doesn't provide type of POI.
|
2019-11-19 16:59:04 +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-11-19 16:59:04 +00:00
|
|
|
require Logger
|
|
|
|
|
|
|
|
@behaviour Provider
|
|
|
|
|
|
|
|
@impl Provider
|
|
|
|
@doc """
|
|
|
|
Pelias implementation for `c:Mobilizon.Service.Geospatial.Provider.geocode/3`.
|
|
|
|
"""
|
|
|
|
@spec geocode(number(), number(), 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-11-19 16:59:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl Provider
|
|
|
|
@doc """
|
|
|
|
Pelias 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-11-19 16:59:04 +00: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 14:33:44 +00:00
|
|
|
endpoint = Keyword.get(options, :endpoint, endpoint(__MODULE__))
|
2019-11-19 16:59:04 +00:00
|
|
|
|
|
|
|
url =
|
|
|
|
case method do
|
|
|
|
:search ->
|
2021-02-12 17:19:49 +00:00
|
|
|
"#{endpoint}/v1/autocomplete?text=#{URI.encode(args.q)}&lang=#{lang}&size=#{limit}"
|
|
|
|
|> add_parameter(options, :coords)
|
|
|
|
|> add_parameter(options, :type)
|
2019-11-19 16:59:04 +00:00
|
|
|
|
|
|
|
:geocode ->
|
|
|
|
"#{endpoint}/v1/reverse?point.lon=#{args.lon}&point.lat=#{args.lat}"
|
|
|
|
end
|
|
|
|
|
2021-02-12 17:19:49 +00:00
|
|
|
add_parameter(url, options, :country_code)
|
2019-11-19 16:59:04 +00:00
|
|
|
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 pelias with #{url}")
|
|
|
|
|
|
|
|
with {:ok, %{status: 200, body: body}} <- GeospatialClient.get(url),
|
|
|
|
%{"features" => features} <- body do
|
|
|
|
process_data(features)
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
Logger.error("Asking pelias with #{url}")
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-19 16:59:04 +00:00
|
|
|
defp process_data(features) do
|
|
|
|
features
|
|
|
|
|> Enum.map(fn %{
|
|
|
|
"geometry" => %{"coordinates" => coordinates},
|
|
|
|
"properties" => properties
|
|
|
|
} ->
|
|
|
|
address = process_address(properties)
|
2021-10-10 14:25:50 +00:00
|
|
|
coordinates = Provider.coordinates(coordinates)
|
|
|
|
%Address{address | geom: coordinates, timezone: Provider.timezone(coordinates)}
|
2019-11-19 16:59:04 +00:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp process_address(properties) do
|
|
|
|
%Address{
|
|
|
|
country: Map.get(properties, "country"),
|
|
|
|
locality: Map.get(properties, "locality"),
|
|
|
|
region: Map.get(properties, "region"),
|
|
|
|
description: Map.get(properties, "name"),
|
|
|
|
postal_code: Map.get(properties, "postalcode"),
|
|
|
|
street: street_address(properties),
|
|
|
|
origin_id: "pelias:#{Map.get(properties, "id")}",
|
|
|
|
type: get_type(properties)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp street_address(properties) do
|
|
|
|
if Map.has_key?(properties, "housenumber") do
|
|
|
|
"#{Map.get(properties, "housenumber")} #{Map.get(properties, "street")}"
|
|
|
|
else
|
|
|
|
Map.get(properties, "street")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@administrative_layers [
|
|
|
|
"neighbourhood",
|
|
|
|
"borough",
|
|
|
|
"localadmin",
|
|
|
|
"locality",
|
|
|
|
"county",
|
|
|
|
"macrocounty",
|
|
|
|
"region",
|
|
|
|
"macroregion",
|
|
|
|
"dependency"
|
|
|
|
]
|
|
|
|
|
2021-02-12 17:19:49 +00:00
|
|
|
@spec get_type(map()) :: String.t() | nil
|
2019-11-19 16:59:04 +00:00
|
|
|
defp get_type(%{"layer" => layer}) when layer in @administrative_layers, do: "administrative"
|
|
|
|
defp get_type(%{"layer" => "address"}), do: "house"
|
|
|
|
defp get_type(%{"layer" => "street"}), do: "street"
|
|
|
|
defp get_type(%{"layer" => "venue"}), do: "venue"
|
|
|
|
defp get_type(%{"layer" => _}), do: nil
|
2021-02-12 17:19:49 +00:00
|
|
|
|
|
|
|
@spec add_parameter(String.t(), Keyword.t(), atom()) :: String.t()
|
|
|
|
def 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, value),
|
|
|
|
do: "#{url}&focus.point.lat=#{value.lat}&focus.point.lon=#{value.lon}"
|
|
|
|
|
|
|
|
defp do_add_parameter(url, :type, :administrative),
|
|
|
|
do: "#{url}&layers=coarse"
|
|
|
|
|
|
|
|
defp do_add_parameter(url, :type, _type), do: url
|
|
|
|
|
|
|
|
defp do_add_parameter(url, :country_code, nil), do: url
|
|
|
|
|
|
|
|
defp do_add_parameter(url, :country_code, country_code),
|
|
|
|
do: "#{url}&boundary.country=#{country_code}"
|
2019-11-19 16:59:04 +00:00
|
|
|
end
|