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>
9 use Friendica\Core\Hook;
10 use Friendica\Core\Logger;
11 use Friendica\Core\Renderer;
14 function nominatim_install()
16 Hook::register('post_local', 'addon/nominatim/nominatim.php', 'nominatim_post_hook');
17 Hook::register('post_remote', 'addon/nominatim/nominatim.php', 'nominatim_post_hook');
20 function nominatim_resolve_item(&$item)
22 if(empty($item['coord']) || !empty($item['location'])) {
26 $language = DI::config()->get('nominatim', 'language');
27 if (empty($language)) {
31 $coords = explode(' ',$item['coord']);
33 if (count($coords) < 2) {
37 $coords[0] = round($coords[0], 5);
38 $coords[1] = round($coords[1], 5);
40 $result = DI::cache()->get('nominatim:' . $language . ':' . $coords[0] . '-' . $coords[1]);
41 if (!is_null($result)) {
42 $item['location'] = $result;
46 $s = DI::httpRequest()->fetch('https://nominatim.openstreetmap.org/reverse?lat=' . $coords[0] . '&lon=' . $coords[1] . '&format=json&addressdetails=0&accept-language=' . $language);
48 Logger::info('API could not be queried');
52 $data = json_decode($s, true);
53 if (empty($data['display_name'])) {
54 Logger::info('No results found for coordinates', ['coordinates' => $item['coord'], 'data' => $data]);
58 $item['location'] = $data['display_name'];
60 Logger::info('Got location', ['lat' => $coords[0], 'long' => $coords[1], 'location' => $item['location']]);
62 if (!empty($item['location'])) {
63 DI::cache()->set('nominatim:' . $language . ':' . $coords[0] . '-' . $coords[1], $item['location']);
67 function nominatim_post_hook($a, &$item)
69 nominatim_resolve_item($item);
72 function nominatim_addon_admin(&$a, &$o)
75 $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/nominatim/');
77 $o = Renderer::replaceMacros($t, [
78 '$submit' => DI::l10n()->t('Save Settings'),
79 '$language' => ['language', DI::l10n()->t('Language code (IETF format)'), DI::config()->get('nominatim', 'language'), ''],
83 function nominatim_addon_admin_post(&$a)
85 $language = !empty($_POST['language']) ? trim($_POST['language']) : '';
86 DI::config()->set('nominatim', 'language', $language);