Introduce Mimirsbrunn geocoder and improve addresses & maps

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2019-11-08 19:37:14 +01:00
parent 0e7cf89492
commit c599a47d58
36 changed files with 940 additions and 267 deletions

View File

@@ -1,6 +1,6 @@
defmodule Mobilizon.Service.Geospatial.GoogleMaps do
@moduledoc """
Google Maps [Geocoding service](https://developers.google.com/maps/documentation/geocoding/intro).
Google Maps [Geocoding service](https://developers.google.com/maps/documentation/geocoding/intro). Only works with addresses.
Note: Endpoint is hardcoded to Google Maps API.
"""
@@ -89,7 +89,11 @@ defmodule Mobilizon.Service.Geospatial.GoogleMaps do
url <> "&address=#{args.q}"
:geocode ->
url <> "&latlng=#{args.lat},#{args.lon}&result_type=street_address"
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}"
:place_details ->
"https://maps.googleapis.com/maps/api/place/details/json?key=#{api_key}&placeid=#{

View File

@@ -0,0 +1,146 @@
defmodule Mobilizon.Service.Geospatial.Mimirsbrunn do
@moduledoc """
[Mimirsbrunn](https://github.com/CanalTP/mimirsbrunn) backend.
## Issues
* Has trouble finding POIs.
* Doesn't support zoom level for reverse geocoding
"""
alias Mobilizon.Addresses.Address
alias Mobilizon.Service.Geospatial.Provider
alias Mobilizon.Config
require Logger
@behaviour Provider
@endpoint Application.get_env(:mobilizon, __MODULE__) |> get_in([:endpoint])
@impl Provider
@doc """
Mimirsbrunn implementation for `c:Mobilizon.Service.Geospatial.Provider.geocode/3`.
"""
@spec geocode(number(), number(), keyword()) :: list(Address.t())
def geocode(lon, lat, options \\ []) do
user_agent = Keyword.get(options, :user_agent, Config.instance_user_agent())
headers = [{"User-Agent", user_agent}]
url = build_url(:geocode, %{lon: lon, lat: lat}, options)
Logger.debug("Asking Mimirsbrunn for reverse geocoding with #{url}")
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
HTTPoison.get(url, headers),
{:ok, %{"features" => features}} <- Poison.decode(body) do
process_data(features)
end
end
@impl Provider
@doc """
Mimirsbrunn implementation for `c:Mobilizon.Service.Geospatial.Provider.search/2`.
"""
@spec search(String.t(), keyword()) :: list(Address.t())
def search(q, options \\ []) do
user_agent = Keyword.get(options, :user_agent, Config.instance_user_agent())
headers = [{"User-Agent", user_agent}]
url = build_url(:search, %{q: q}, options)
Logger.debug("Asking Mimirsbrunn for addresses with #{url}")
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
HTTPoison.get(url, headers),
{:ok, %{"features" => features}} <- Poison.decode(body) do
process_data(features)
end
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")
coords = Keyword.get(options, :coords, nil)
endpoint = Keyword.get(options, :endpoint, @endpoint)
case method do
:search ->
url = "#{endpoint}/autocomplete?q=#{URI.encode(args.q)}&lang=#{lang}&limit=#{limit}"
if is_nil(coords), do: url, else: url <> "&lat=#{coords.lat}&lon=#{coords.lon}"
:geocode ->
"#{endpoint}/reverse?lon=#{args.lon}&lat=#{args.lat}"
end
end
defp process_data(features) do
features
|> Enum.map(fn %{
"geometry" => %{"coordinates" => coordinates},
"properties" => %{"geocoding" => geocoding}
} ->
address = process_address(geocoding)
%Address{address | geom: Provider.coordinates(coordinates)}
end)
end
defp process_address(%{"type" => "poi", "address" => address} = geocoding) do
address = process_address(address)
%Address{
address
| type: get_type(geocoding),
origin_id: Map.get(geocoding, "id"),
description: Map.get(geocoding, "name")
}
end
defp process_address(geocoding) do
%Address{
country: get_administrative_region(geocoding, "country"),
locality: Map.get(geocoding, "city"),
region: get_administrative_region(geocoding, "region"),
description: Map.get(geocoding, "name"),
postal_code: get_postal_code(geocoding),
street: street_address(geocoding),
origin_id: "mimirsbrunn:" <> Map.get(geocoding, "id"),
type: get_type(geocoding)
}
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
defp get_type(%{"type" => type}) when type in ["house", "street", "zone", "address"], do: type
defp get_type(%{"type" => "poi", "poi_types" => types})
when is_list(types) and length(types) > 0,
do: hd(types)["id"]
defp get_type(_), do: nil
defp get_administrative_region(
%{"administrative_regions" => administrative_regions},
administrative_level
) do
Enum.find_value(
administrative_regions,
&process_administrative_region(&1, administrative_level)
)
end
defp get_administrative_region(_, _), do: nil
defp process_administrative_region(%{"zone_type" => "country", "name" => name}, "country"),
do: name
defp process_administrative_region(%{"zone_type" => "state", "name" => name}, "region"),
do: name
defp process_administrative_region(_, _), do: nil
defp get_postal_code(%{"postcode" => nil}), do: nil
defp get_postal_code(%{"postcode" => postcode}), do: postcode |> String.split(";") |> hd()
end

View File

@@ -27,8 +27,8 @@ defmodule Mobilizon.Service.Geospatial.Nominatim do
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
HTTPoison.get(url, headers),
{:ok, body} <- Poison.decode(body) do
[process_data(body)]
{:ok, %{"features" => features}} <- Poison.decode(body) do
features |> process_data() |> Enum.filter(& &1)
end
end
@@ -45,8 +45,8 @@ defmodule Mobilizon.Service.Geospatial.Nominatim do
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
HTTPoison.get(url, headers),
{:ok, body} <- Poison.decode(body) do
body |> Enum.map(fn entry -> process_data(entry) end) |> Enum.filter(& &1)
{:ok, %{"features" => features}} <- Poison.decode(body) do
features |> process_data() |> Enum.filter(& &1)
end
end
@@ -55,39 +55,53 @@ defmodule Mobilizon.Service.Geospatial.Nominatim do
limit = Keyword.get(options, :limit, 10)
lang = Keyword.get(options, :lang, "en")
endpoint = Keyword.get(options, :endpoint, @endpoint)
country_code = Keyword.get(options, :country_code)
zoom = Keyword.get(options, :zoom)
api_key = Keyword.get(options, :api_key, @api_key)
url =
case method do
:search ->
"#{endpoint}/search?format=jsonv2&q=#{URI.encode(args.q)}&limit=#{limit}&accept-language=#{
"#{endpoint}/search?format=geocodejson&q=#{URI.encode(args.q)}&limit=#{limit}&accept-language=#{
lang
}&addressdetails=1"
}&addressdetails=1&namedetails=1"
:geocode ->
"#{endpoint}/reverse?format=jsonv2&lat=#{args.lat}&lon=#{args.lon}&addressdetails=1"
url =
"#{endpoint}/reverse?format=geocodejson&lat=#{args.lat}&lon=#{args.lon}&accept-language=#{
lang
}&addressdetails=1&namedetails=1"
if is_nil(zoom), do: url, else: url <> "&zoom=#{zoom}"
end
url = if is_nil(country_code), do: url, else: "#{url}&countrycodes=#{country_code}"
if is_nil(api_key), do: url, else: url <> "&key=#{api_key}"
end
@spec process_data(map()) :: Address.t()
defp process_data(%{"address" => address} = body) do
%Address{
country: Map.get(address, "country"),
locality: Map.get(address, "city"),
region: Map.get(address, "state"),
description: description(body),
geom: [Map.get(body, "lon"), Map.get(body, "lat")] |> Provider.coordinates(),
postal_code: Map.get(address, "postcode"),
street: street_address(address),
origin_id: "osm:" <> to_string(Map.get(body, "osm_id"))
}
rescue
error in ArgumentError ->
Logger.warn(inspect(error))
defp process_data(features) do
features
|> Enum.map(fn %{
"geometry" => %{"coordinates" => coordinates},
"properties" => %{"geocoding" => geocoding}
} ->
address = process_address(geocoding)
%Address{address | geom: Provider.coordinates(coordinates)}
end)
end
nil
defp process_address(geocoding) do
%Address{
country: Map.get(geocoding, "country"),
locality:
Map.get(geocoding, "city") || Map.get(geocoding, "town") || Map.get(geocoding, "county"),
region: Map.get(geocoding, "state"),
description: description(geocoding),
postal_code: Map.get(geocoding, "postcode"),
type: Map.get(geocoding, "type"),
street: street_address(geocoding),
origin_id: "nominatim:" <> to_string(Map.get(geocoding, "osm_id"))
}
end
@spec street_address(map()) :: String.t()
@@ -97,8 +111,8 @@ defmodule Mobilizon.Service.Geospatial.Nominatim do
Map.has_key?(body, "road") ->
Map.get(body, "road")
Map.has_key?(body, "road") ->
Map.get(body, "road")
Map.has_key?(body, "street") ->
Map.get(body, "street")
Map.has_key?(body, "pedestrian") ->
Map.get(body, "pedestrian")
@@ -107,7 +121,7 @@ defmodule Mobilizon.Service.Geospatial.Nominatim do
""
end
Map.get(body, "house_number", "") <> " " <> road
Map.get(body, "housenumber", "") <> " " <> road
end
@address29_classes ["amenity", "shop", "tourism", "leisure"]
@@ -115,14 +129,16 @@ defmodule Mobilizon.Service.Geospatial.Nominatim do
@spec description(map()) :: String.t()
defp description(body) do
if !Map.has_key?(body, "display_name") do
Logger.warn("Address has no display name")
raise ArgumentError, message: "Address has no display_name"
end
description = Map.get(body, "display_name")
description = Map.get(body, "name")
address = Map.get(body, "address")
description =
if Map.has_key?(body, "namedetails"),
do: body |> Map.get("namedetails") |> Map.get("name", description),
else: description
description = if is_nil(description), do: street_address(body), else: description
if (Map.get(body, "category") in @address29_categories or
Map.get(body, "class") in @address29_classes) and Map.has_key?(address, "address29") do
Map.get(address, "address29")

View File

@@ -16,6 +16,7 @@ defmodule Mobilizon.Service.Geospatial.Provider do
* `:user_agent` User-Agent string to send to the backend. Defaults to `"Mobilizon"`
* `: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