2019-03-12 11:52:28 +01:00
|
|
|
defmodule Mobilizon.Service.Geospatial.GoogleMaps do
|
|
|
|
@moduledoc """
|
2019-11-08 19:37:14 +01:00
|
|
|
Google Maps [Geocoding service](https://developers.google.com/maps/documentation/geocoding/intro). Only works with addresses.
|
2019-03-12 11:52:28 +01:00
|
|
|
|
2019-09-22 16:26:23 +02:00
|
|
|
Note: Endpoint is hardcoded to Google Maps API.
|
2019-03-12 11:52:28 +01:00
|
|
|
"""
|
2019-09-22 16:26:23 +02:00
|
|
|
|
2019-03-12 11:52:28 +01:00
|
|
|
alias Mobilizon.Addresses.Address
|
2019-09-22 16:26:23 +02:00
|
|
|
alias Mobilizon.Service.Geospatial.Provider
|
2020-08-30 23:29:56 +02:00
|
|
|
alias Mobilizon.Service.HTTP.GeospatialClient
|
2019-09-22 16:26:23 +02:00
|
|
|
|
2019-03-12 11:52:28 +01:00
|
|
|
require Logger
|
|
|
|
|
|
|
|
@behaviour Provider
|
|
|
|
|
|
|
|
@components [
|
|
|
|
"street_number",
|
|
|
|
"route",
|
|
|
|
"locality",
|
|
|
|
"administrative_area_level_1",
|
|
|
|
"country",
|
|
|
|
"postal_code"
|
|
|
|
]
|
|
|
|
|
|
|
|
@api_key_missing_message "API Key required to use Google Maps"
|
|
|
|
|
2021-02-12 18:19:49 +01:00
|
|
|
@geocode_endpoint "https://maps.googleapis.com/maps/api/geocode/json"
|
|
|
|
@details_endpoint "https://maps.googleapis.com/maps/api/place/details/json"
|
|
|
|
|
2019-03-12 11:52:28 +01:00
|
|
|
@impl Provider
|
|
|
|
@doc """
|
|
|
|
Google Maps implementation for `c:Mobilizon.Service.Geospatial.Provider.geocode/3`.
|
|
|
|
"""
|
2021-10-05 17:43:45 +02:00
|
|
|
@spec geocode(float(), float(), keyword()) :: list(Address.t())
|
2019-03-12 11:52:28 +01:00
|
|
|
def geocode(lon, lat, options \\ []) do
|
|
|
|
url = build_url(:geocode, %{lon: lon, lat: lat}, options)
|
|
|
|
|
|
|
|
Logger.debug("Asking Google Maps for reverse geocode with #{url}")
|
|
|
|
|
2021-10-05 17:43:45 +02:00
|
|
|
%Tesla.Env{status: 200, body: body} = GeospatialClient.get!(url)
|
|
|
|
|
|
|
|
case body do
|
|
|
|
%{"results" => results, "status" => "OK"} ->
|
|
|
|
Enum.map(results, &process_data(&1, options))
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
%{"status" => "REQUEST_DENIED", "error_message" => error_message} ->
|
2019-03-12 11:52:28 +01:00
|
|
|
raise ArgumentError, message: to_string(error_message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl Provider
|
|
|
|
@doc """
|
|
|
|
Google Maps implementation for `c:Mobilizon.Service.Geospatial.Provider.search/2`.
|
|
|
|
"""
|
2021-10-05 17:43:45 +02:00
|
|
|
@spec search(String.t(), keyword()) :: list(Address.t())
|
2019-03-12 11:52:28 +01:00
|
|
|
def search(q, options \\ []) do
|
|
|
|
url = build_url(:search, %{q: q}, options)
|
|
|
|
|
|
|
|
Logger.debug("Asking Google Maps for addresses with #{url}")
|
|
|
|
|
2021-10-05 17:43:45 +02:00
|
|
|
%Tesla.Env{status: 200, body: body} = GeospatialClient.get!(url)
|
|
|
|
|
|
|
|
case body do
|
|
|
|
%{"results" => results, "status" => "OK"} ->
|
|
|
|
results |> Enum.map(fn entry -> process_data(entry, options) end)
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
%{"status" => "REQUEST_DENIED", "error_message" => error_message} ->
|
2019-03-12 11:52:28 +01:00
|
|
|
raise ArgumentError, message: to_string(error_message)
|
2019-08-22 15:57:44 +02:00
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
%{"results" => [], "status" => "ZERO_RESULTS"} ->
|
2019-08-22 15:57:44 +02:00
|
|
|
[]
|
2019-03-12 11:52:28 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-05 17:43:45 +02:00
|
|
|
@spec build_url(:search | :geocode, map(), list()) :: String.t() | no_return
|
2019-03-12 11:52:28 +01:00
|
|
|
defp build_url(method, args, options) do
|
|
|
|
limit = Keyword.get(options, :limit, 10)
|
|
|
|
lang = Keyword.get(options, :lang, "en")
|
2021-03-16 15:33:44 +01:00
|
|
|
api_key = Keyword.get(options, :api_key, api_key())
|
2019-03-12 11:52:28 +01:00
|
|
|
if is_nil(api_key), do: raise(ArgumentError, message: @api_key_missing_message)
|
|
|
|
|
2021-02-12 18:19:49 +01:00
|
|
|
url = "#{@geocode_endpoint}?limit=#{limit}&key=#{api_key}&language=#{lang}"
|
2019-03-12 11:52:28 +01:00
|
|
|
|
2019-08-22 15:57:44 +02:00
|
|
|
uri =
|
|
|
|
case method do
|
|
|
|
:search ->
|
2021-02-12 18:19:49 +01:00
|
|
|
"#{url}&address=#{args.q}"
|
|
|
|
|> add_parameter(options, :type)
|
2019-03-12 11:52:28 +01:00
|
|
|
|
2019-08-22 15:57:44 +02:00
|
|
|
:geocode ->
|
2019-11-08 19:37:14 +01:00
|
|
|
zoom = Keyword.get(options, :zoom, 15)
|
|
|
|
|
|
|
|
result_type = if zoom >= 15, do: "street_address", else: "locality"
|
|
|
|
|
|
|
|
url <> "&latlng=#{args.lat},#{args.lon}&result_type=#{result_type}"
|
2019-08-22 15:57:44 +02:00
|
|
|
|
|
|
|
:place_details ->
|
2021-02-12 18:19:49 +01:00
|
|
|
"#{@details_endpoint}?key=#{api_key}&placeid=#{args.place_id}"
|
2019-08-22 15:57:44 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
URI.encode(uri)
|
2019-03-12 11:52:28 +01:00
|
|
|
end
|
|
|
|
|
2021-10-05 17:43:45 +02:00
|
|
|
@spec process_data(map(), Keyword.t()) :: Address.t()
|
2019-08-22 15:57:44 +02:00
|
|
|
defp process_data(
|
|
|
|
%{
|
|
|
|
"formatted_address" => description,
|
|
|
|
"geometry" => %{"location" => %{"lat" => lat, "lng" => lon}},
|
|
|
|
"address_components" => components,
|
|
|
|
"place_id" => place_id
|
|
|
|
},
|
|
|
|
options
|
|
|
|
) do
|
2019-03-12 11:52:28 +01:00
|
|
|
components =
|
|
|
|
@components
|
|
|
|
|> Enum.reduce(%{}, fn component, acc ->
|
|
|
|
Map.put(acc, component, extract_component(components, component))
|
|
|
|
end)
|
|
|
|
|
2019-08-22 15:57:44 +02:00
|
|
|
description =
|
2021-03-16 15:33:44 +01:00
|
|
|
if Keyword.get(options, :fetch_place_details, fetch_place_details()) == true do
|
2019-08-22 15:57:44 +02:00
|
|
|
do_fetch_place_details(place_id, options) || description
|
|
|
|
else
|
|
|
|
description
|
|
|
|
end
|
|
|
|
|
2021-10-10 16:25:50 +02:00
|
|
|
coordinates = Provider.coordinates([lon, lat])
|
|
|
|
|
2019-03-12 11:52:28 +01:00
|
|
|
%Address{
|
2019-03-22 15:51:23 +01:00
|
|
|
country: Map.get(components, "country"),
|
|
|
|
locality: Map.get(components, "locality"),
|
|
|
|
region: Map.get(components, "administrative_area_level_1"),
|
2019-03-12 11:52:28 +01:00
|
|
|
description: description,
|
2021-10-10 16:25:50 +02:00
|
|
|
geom: coordinates,
|
|
|
|
timezone: Provider.timezone(coordinates),
|
2019-03-22 15:51:23 +01:00
|
|
|
postal_code: Map.get(components, "postal_code"),
|
2019-08-22 15:57:44 +02:00
|
|
|
street: street_address(components),
|
|
|
|
origin_id: "gm:" <> to_string(place_id)
|
2019-03-12 11:52:28 +01:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp extract_component(components, key) do
|
|
|
|
case components
|
|
|
|
|> Enum.filter(fn component -> key in component["types"] end)
|
|
|
|
|> Enum.map(& &1["long_name"]) do
|
|
|
|
[] -> nil
|
|
|
|
component -> hd(component)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp street_address(body) do
|
|
|
|
if Map.has_key?(body, "street_number") && !is_nil(Map.get(body, "street_number")) do
|
|
|
|
Map.get(body, "street_number") <> " " <> Map.get(body, "route")
|
|
|
|
else
|
|
|
|
Map.get(body, "route")
|
|
|
|
end
|
|
|
|
end
|
2019-08-22 15:57:44 +02:00
|
|
|
|
2021-10-05 17:43:45 +02:00
|
|
|
@spec do_fetch_place_details(String.t() | nil, Keyword.t()) :: String.t() | nil
|
2019-08-22 15:57:44 +02:00
|
|
|
defp do_fetch_place_details(place_id, options) do
|
|
|
|
url = build_url(:place_details, %{place_id: place_id}, options)
|
|
|
|
|
|
|
|
Logger.debug("Asking Google Maps for details with #{url}")
|
|
|
|
|
2021-10-05 17:43:45 +02:00
|
|
|
%Tesla.Env{status: 200, body: body} = GeospatialClient.get!(url)
|
|
|
|
|
|
|
|
case body do
|
|
|
|
%{"result" => %{"name" => name}, "status" => "OK"} ->
|
|
|
|
name
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
%{"status" => "REQUEST_DENIED", "error_message" => error_message} ->
|
2019-08-22 15:57:44 +02:00
|
|
|
raise ArgumentError, message: to_string(error_message)
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
%{"status" => "INVALID_REQUEST"} ->
|
2019-08-22 15:57:44 +02:00
|
|
|
raise ArgumentError, message: "Invalid Request"
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
%{"results" => [], "status" => "ZERO_RESULTS"} ->
|
2019-08-22 15:57:44 +02:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
2021-02-12 18:19:49 +01:00
|
|
|
|
|
|
|
@spec add_parameter(String.t(), Keyword.t(), atom()) :: String.t()
|
|
|
|
defp add_parameter(url, options, key, default \\ nil) do
|
|
|
|
value = Keyword.get(options, key, default)
|
|
|
|
|
|
|
|
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, :type, :administrative),
|
|
|
|
do: "#{url}&components=administrative_area"
|
|
|
|
|
|
|
|
defp do_add_parameter(url, :type, _), do: url
|
2021-03-16 15:33:44 +01:00
|
|
|
|
|
|
|
defp api_key do
|
|
|
|
Application.get_env(:mobilizon, __MODULE__) |> get_in([:api_key])
|
|
|
|
end
|
|
|
|
|
|
|
|
defp fetch_place_details do
|
|
|
|
(Application.get_env(:mobilizon, __MODULE__)
|
|
|
|
|> get_in([:fetch_place_details])) in [true, "true", "True"]
|
|
|
|
end
|
2019-03-12 11:52:28 +01:00
|
|
|
end
|