2019-03-12 10:52:28 +00:00
|
|
|
defmodule Mobilizon.Service.Geospatial.MapQuest do
|
|
|
|
@moduledoc """
|
|
|
|
[MapQuest](https://developer.mapquest.com/documentation) backend.
|
|
|
|
|
|
|
|
## Options
|
|
|
|
In addition to the [the shared options](Mobilizon.Service.Geospatial.Provider.html#module-shared-options),
|
|
|
|
MapQuest methods support the following options:
|
2020-06-04 08:58:27 +00:00
|
|
|
* `:open_data` Whether to use [Open Data or Licenced Data](https://developer.mapquest.com/documentation/open/).
|
|
|
|
Defaults to `true`
|
2019-03-12 10:52:28 +00:00
|
|
|
"""
|
2019-09-22 14:26:23 +00:00
|
|
|
|
2019-03-12 10:52:28 +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
|
2019-09-22 14:26:23 +00:00
|
|
|
|
2019-03-12 10:52:28 +00:00
|
|
|
require Logger
|
|
|
|
|
|
|
|
@behaviour Provider
|
|
|
|
|
|
|
|
@api_key_missing_message "API Key required to use MapQuest"
|
|
|
|
|
|
|
|
@impl Provider
|
|
|
|
@doc """
|
|
|
|
MapQuest implementation for `c:Mobilizon.Service.Geospatial.Provider.geocode/3`.
|
|
|
|
"""
|
|
|
|
@spec geocode(String.t(), keyword()) :: list(Address.t())
|
|
|
|
def geocode(lon, lat, options \\ []) do
|
2021-03-16 14:33:44 +00:00
|
|
|
api_key = Keyword.get(options, :api_key, api_key())
|
2019-03-12 10:52:28 +00:00
|
|
|
limit = Keyword.get(options, :limit, 10)
|
|
|
|
open_data = Keyword.get(options, :open_data, true)
|
|
|
|
|
|
|
|
prefix = if open_data, do: "open", else: "www"
|
|
|
|
|
|
|
|
if is_nil(api_key), do: raise(ArgumentError, message: @api_key_missing_message)
|
|
|
|
|
2020-07-09 15:24:28 +00:00
|
|
|
with {:ok, %{status: 200, body: body}} <-
|
2020-08-30 21:29:56 +00:00
|
|
|
GeospatialClient.get(
|
2021-06-07 14:39:44 +00:00
|
|
|
"https://#{prefix}.mapquestapi.com/geocoding/v1/reverse?key=#{api_key}&location=#{lat},#{lon}&maxResults=#{limit}"
|
2019-03-12 10:52:28 +00:00
|
|
|
),
|
2020-07-09 15:24:28 +00:00
|
|
|
%{"results" => results, "info" => %{"statuscode" => 0}} <- body do
|
2019-07-23 16:06:22 +00:00
|
|
|
results |> Enum.map(&process_data/1)
|
2019-03-12 10:52:28 +00:00
|
|
|
else
|
2020-07-09 15:24:28 +00:00
|
|
|
{:ok, %{status: 403, body: err}} ->
|
2019-03-12 10:52:28 +00:00
|
|
|
raise(ArgumentError, message: err)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl Provider
|
|
|
|
@doc """
|
|
|
|
MapQuest implementation for `c:Mobilizon.Service.Geospatial.Provider.search/2`.
|
|
|
|
"""
|
|
|
|
@spec search(String.t(), keyword()) :: list(Address.t())
|
|
|
|
def search(q, options \\ []) do
|
|
|
|
limit = Keyword.get(options, :limit, 10)
|
2021-03-16 14:33:44 +00:00
|
|
|
api_key = Keyword.get(options, :api_key, api_key())
|
2019-03-12 10:52:28 +00:00
|
|
|
|
|
|
|
open_data = Keyword.get(options, :open_data, true)
|
|
|
|
|
|
|
|
prefix = if open_data, do: "open", else: "www"
|
|
|
|
|
|
|
|
if is_nil(api_key), do: raise(ArgumentError, message: @api_key_missing_message)
|
|
|
|
|
|
|
|
url =
|
2021-06-07 14:39:44 +00:00
|
|
|
"https://#{prefix}.mapquestapi.com/geocoding/v1/address?key=#{api_key}&location=#{URI.encode(q)}&maxResults=#{limit}"
|
2019-03-12 10:52:28 +00:00
|
|
|
|
|
|
|
Logger.debug("Asking MapQuest for addresses with #{url}")
|
|
|
|
|
2020-08-30 21:29:56 +00:00
|
|
|
with {:ok, %{status: 200, body: body}} <- GeospatialClient.get(url),
|
2020-07-09 15:24:28 +00:00
|
|
|
%{"results" => results, "info" => %{"statuscode" => 0}} <- body do
|
2019-07-23 16:06:22 +00:00
|
|
|
results |> Enum.map(&process_data/1)
|
2019-03-12 10:52:28 +00:00
|
|
|
else
|
2020-07-09 15:24:28 +00:00
|
|
|
{:ok, %{status: 403, body: err}} ->
|
2019-03-12 10:52:28 +00:00
|
|
|
raise(ArgumentError, message: err)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-23 16:06:22 +00:00
|
|
|
defp process_data(
|
2019-03-12 10:52:28 +00:00
|
|
|
%{
|
|
|
|
"locations" => addresses,
|
|
|
|
"providedLocation" => %{"latLng" => %{"lat" => lat, "lng" => lng}}
|
|
|
|
} = _body
|
|
|
|
) do
|
|
|
|
case addresses do
|
|
|
|
[] -> nil
|
2019-07-23 16:06:22 +00:00
|
|
|
addresses -> addresses |> hd |> produce_address(lat, lng)
|
2019-03-12 10:52:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-23 16:06:22 +00:00
|
|
|
defp process_data(%{"locations" => addresses}) do
|
2019-03-12 10:52:28 +00:00
|
|
|
case addresses do
|
|
|
|
[] -> nil
|
2019-07-23 16:06:22 +00:00
|
|
|
addresses -> addresses |> hd |> produce_address()
|
2019-03-12 10:52:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-23 16:06:22 +00:00
|
|
|
defp produce_address(%{"latLng" => %{"lat" => lat, "lng" => lng}} = address) do
|
|
|
|
produce_address(address, lat, lng)
|
2019-03-12 10:52:28 +00:00
|
|
|
end
|
|
|
|
|
2019-07-23 16:06:22 +00:00
|
|
|
defp produce_address(address, lat, lng) do
|
2021-10-10 14:25:50 +00:00
|
|
|
coordinates = Provider.coordinates([lng, lat])
|
|
|
|
|
2019-03-12 10:52:28 +00:00
|
|
|
%Address{
|
2019-03-22 14:51:23 +00:00
|
|
|
country: Map.get(address, "adminArea1"),
|
|
|
|
locality: Map.get(address, "adminArea5"),
|
|
|
|
region: Map.get(address, "adminArea3"),
|
2019-03-12 10:52:28 +00:00
|
|
|
description: Map.get(address, "street"),
|
2021-10-10 14:25:50 +00:00
|
|
|
geom: coordinates,
|
|
|
|
timezone: Provider.timezone(coordinates),
|
2019-03-22 14:51:23 +00:00
|
|
|
postal_code: Map.get(address, "postalCode"),
|
|
|
|
street: Map.get(address, "street")
|
2019-03-12 10:52:28 +00:00
|
|
|
}
|
|
|
|
end
|
2021-03-16 14:33:44 +00:00
|
|
|
|
|
|
|
defp api_key do
|
|
|
|
Application.get_env(:mobilizon, __MODULE__) |> get_in([:api_key])
|
|
|
|
end
|
2019-03-12 10:52:28 +00:00
|
|
|
end
|