4 * Description: Use Nominatim from OpenStreetMap to resolve the location for the given latitude and longitude. Derived from "geocoordinates"
6 * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
10 use Friendica\Core\Hook;
11 use Friendica\Core\Logger;
12 use Friendica\Core\Renderer;
15 function nominatim_install()
17 Hook::register('post_local', 'addon/nominatim/nominatim.php', 'nominatim_post_hook');
18 Hook::register('post_remote', 'addon/nominatim/nominatim.php', 'nominatim_post_hook');
21 function nominatim_resolve_item(array &$item)
23 if(empty($item['coord']) || !empty($item['location'])) {
27 $language = DI::config()->get('nominatim', 'language');
28 if (empty($language)) {
32 $coords = explode(' ',$item['coord']);
34 if (count($coords) < 2) {
38 $coords[0] = round($coords[0], 5);
39 $coords[1] = round($coords[1], 5);
41 $result = DI::cache()->get('nominatim:' . $language . ':' . $coords[0] . '-' . $coords[1]);
42 if (!is_null($result)) {
43 $item['location'] = $result;
47 $s = DI::httpClient()->fetch('https://nominatim.openstreetmap.org/reverse?lat=' . $coords[0] . '&lon=' . $coords[1] . '&format=json&addressdetails=0&accept-language=' . $language);
49 Logger::info('API could not be queried');
53 $data = json_decode($s, true);
54 if (empty($data['display_name'])) {
55 Logger::info('No results found for coordinates', ['coordinates' => $item['coord'], 'data' => $data]);
59 $item['location'] = $data['display_name'];
61 Logger::info('Got location', ['lat' => $coords[0], 'long' => $coords[1], 'location' => $item['location']]);
63 if (!empty($item['location'])) {
64 DI::cache()->set('nominatim:' . $language . ':' . $coords[0] . '-' . $coords[1], $item['location']);
68 function nominatim_post_hook(App $a, array &$item)
70 nominatim_resolve_item($item);
73 function nominatim_addon_admin(App $a, string &$o)
76 $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/nominatim/');
78 $o = Renderer::replaceMacros($t, [
79 '$submit' => DI::l10n()->t('Save Settings'),
80 '$language' => ['language', DI::l10n()->t('Language code (IETF format)'), DI::config()->get('nominatim', 'language'), ''],
84 function nominatim_addon_admin_post(App $a)
86 DI::config()->set('nominatim', 'language', (!empty($_POST['language']) ? trim($_POST['language']) : ''));