2019-03-12 10:52:28 +00:00
|
|
|
defmodule Mobilizon.Service.Geospatial.Provider do
|
|
|
|
@moduledoc """
|
|
|
|
Provider Behaviour for Geospatial stuff.
|
|
|
|
|
|
|
|
## Supported backends
|
|
|
|
|
2020-06-04 08:58:27 +00:00
|
|
|
* `Mobilizon.Service.Geospatial.Nominatim` [🔗](https://wiki.openstreetmap.org/wiki/Nominatim)
|
|
|
|
* `Mobilizon.Service.Geospatial.Photon` [🔗](https://photon.komoot.de)
|
|
|
|
* `Mobilizon.Service.Geospatial.Addok` [🔗](https://github.com/addok/addok)
|
|
|
|
* `Mobilizon.Service.Geospatial.MapQuest` [🔗](https://developer.mapquest.com/documentation/open/)
|
|
|
|
* `Mobilizon.Service.Geospatial.GoogleMaps` [🔗](https://developers.google.com/maps/documentation/geocoding/intro)
|
|
|
|
* `Mobilizon.Service.Geospatial.Mimirsbrunn` [🔗](https://github.com/CanalTP/mimirsbrunn)
|
|
|
|
* `Mobilizon.Service.Geospatial.Pelias` [🔗](https://pelias.io)
|
2019-03-12 10:52:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
## Shared options
|
|
|
|
|
2020-06-04 08:58:27 +00:00
|
|
|
* `:lang` Lang in which to prefer results. Used as a request parameter or
|
|
|
|
through an `Accept-Language` HTTP header. Defaults to `"en"`.
|
|
|
|
* `:country_code` An ISO 3166 country code. String or `nil`
|
|
|
|
* `:limit` Maximum limit for the number of results returned by the backend.
|
|
|
|
Defaults to `10`
|
|
|
|
* `:api_key` Allows to override the API key (if the backend requires one) set
|
|
|
|
inside the configuration.
|
|
|
|
* `:endpoint` Allows to override the endpoint set inside the configuration.
|
2019-03-12 10:52:28 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
alias Mobilizon.Addresses.Address
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Get an address from longitude and latitude coordinates.
|
|
|
|
|
|
|
|
## Options
|
|
|
|
|
2019-11-19 19:01:31 +00:00
|
|
|
In addition to [the shared options](#module-shared-options), `c:geocode/3` also
|
|
|
|
accepts the following options:
|
|
|
|
|
|
|
|
* `zoom` Level of detail required for the address. Default: 15
|
2019-03-12 10:52:28 +00:00
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> geocode(48.11, -1.77)
|
|
|
|
%Address{}
|
|
|
|
"""
|
2021-09-24 14:46:42 +00:00
|
|
|
@callback geocode(longitude :: number, latitude :: number, options :: keyword) ::
|
2021-10-05 15:43:45 +00:00
|
|
|
[Address.t()]
|
2019-03-12 10:52:28 +00:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Search for an address
|
|
|
|
|
|
|
|
## Options
|
|
|
|
|
2019-09-22 14:26:23 +00:00
|
|
|
In addition to [the shared options](#module-shared-options), `c:search/2` also
|
|
|
|
accepts the following options:
|
2019-03-12 10:52:28 +00:00
|
|
|
|
2019-09-22 14:26:23 +00:00
|
|
|
* `coords` Map of coordinates (ex: `%{lon: 48.11, lat: -1.77}`) allowing to
|
|
|
|
give a geographic priority to the search. Defaults to `nil`.
|
2021-02-12 17:19:49 +00:00
|
|
|
* `type` Filter by type of results. Allowed values:
|
|
|
|
* `:administrative` (cities, regions)
|
2019-03-12 10:52:28 +00:00
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> search("10 rue Jangot")
|
|
|
|
%Address{}
|
|
|
|
"""
|
2021-10-05 15:43:45 +00:00
|
|
|
@callback search(address :: String.t(), options :: keyword) :: [Address.t()]
|
2019-03-12 10:52:28 +00:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns a `Geo.Point` for given coordinates
|
|
|
|
"""
|
2021-10-05 15:43:45 +00:00
|
|
|
@spec coordinates([number | String.t()]) :: Geo.Point.t() | nil
|
|
|
|
def coordinates([x, y]) when is_number(x) and is_number(y) do
|
|
|
|
%Geo.Point{coordinates: {x, y}, srid: 4326}
|
2019-09-22 14:26:23 +00:00
|
|
|
end
|
2019-03-12 10:52:28 +00:00
|
|
|
|
2021-10-05 15:43:45 +00:00
|
|
|
def coordinates([x, y]) when is_binary(x) and is_binary(y) do
|
|
|
|
%Geo.Point{coordinates: {String.to_float(x), String.to_float(y)}, srid: 4326}
|
2019-09-22 14:26:23 +00:00
|
|
|
end
|
2019-03-12 10:52:28 +00:00
|
|
|
|
2021-10-05 15:43:45 +00:00
|
|
|
def coordinates(_), do: nil
|
2021-03-16 14:33:44 +00:00
|
|
|
|
2021-10-10 14:25:50 +00:00
|
|
|
@doc """
|
|
|
|
Returns the timezone for a Geo.Point
|
|
|
|
"""
|
|
|
|
@spec timezone(nil | Geo.Point.t()) :: nil | String.t()
|
|
|
|
def timezone(nil), do: nil
|
|
|
|
|
|
|
|
def timezone(%Geo.Point{} = point) do
|
|
|
|
case TzWorld.timezone_at(point) do
|
|
|
|
{:ok, tz} -> tz
|
|
|
|
{:error, _err} -> nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-16 14:33:44 +00:00
|
|
|
@spec endpoint(atom()) :: String.t()
|
|
|
|
def endpoint(provider) do
|
|
|
|
Application.get_env(:mobilizon, provider) |> get_in([:endpoint])
|
|
|
|
end
|
2019-03-12 10:52:28 +00:00
|
|
|
end
|