From df1d9d770ceb42d5bb8c9243fe5dbfb65e56d97c Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 24 Sep 2020 04:01:12 +0000 Subject: [PATCH] Nominatim: Use OpenStreetMap to resolve coordinates into locations --- nominatim/nominatim.php | 87 +++++++++++++++++++++++++++++++++++ nominatim/templates/admin.tpl | 2 + 2 files changed, 89 insertions(+) create mode 100644 nominatim/nominatim.php create mode 100644 nominatim/templates/admin.tpl diff --git a/nominatim/nominatim.php b/nominatim/nominatim.php new file mode 100644 index 000000000..45f5042be --- /dev/null +++ b/nominatim/nominatim.php @@ -0,0 +1,87 @@ + + */ + +use Friendica\Core\Hook; +use Friendica\Core\Logger; +use Friendica\Core\Renderer; +use Friendica\DI; + +function nominatim_install() +{ + Hook::register('post_local', 'addon/nominatim/nominatim.php', 'nominatim_post_hook'); + Hook::register('post_remote', 'addon/nominatim/nominatim.php', 'nominatim_post_hook'); +} + +function nominatim_resolve_item(&$item) +{ + if(empty($item['coord']) || !empty($item['location'])) { + return; + } + + $language = DI::config()->get('nominatim', 'language'); + if (empty($language)) { + $language = 'en'; + } + + $coords = explode(' ',$item['coord']); + + if (count($coords) < 2) { + return; + } + + $coords[0] = round($coords[0], 5); + $coords[1] = round($coords[1], 5); + + $result = DI::cache()->get('nominatim:' . $language . ':' . $coords[0] . '-' . $coords[1]); + if (!is_null($result)) { + $item['location'] = $result; + return; + } + + $s = DI::httpRequest()->fetch('https://nominatim.openstreetmap.org/reverse?lat=' . $coords[0] . '&lon=' . $coords[1] . '&format=json&addressdetails=0&accept-language=' . $language); + if (empty($s)) { + Logger::info('API could not be queried'); + return; + } + + $data = json_decode($s, true); + if (empty($data['display_name'])) { + Logger::info('No results found for coordinates', ['coordinates' => $item['coord'], 'data' => $data]); + return; + } + + $item['location'] = $data['display_name']; + + Logger::info('Got location', ['lat' => $coords[0], 'long' => $coords[1], 'location' => $item['location']]); + + if (!empty($item['location'])) { + DI::cache()->set('nominatim:' . $language . ':' . $coords[0] . '-' . $coords[1], $item['location']); + } +} + +function nominatim_post_hook($a, &$item) +{ + nominatim_resolve_item($item); +} + +function nominatim_addon_admin(&$a, &$o) +{ + + $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/nominatim/'); + + $o = Renderer::replaceMacros($t, [ + '$submit' => DI::l10n()->t('Save Settings'), + '$language' => ['language', DI::l10n()->t('Language code (IETF format)'), DI::config()->get('nominatim', 'language'), ''], + ]); +} + +function nominatim_addon_admin_post(&$a) +{ + $language = !empty($_POST['language']) ? trim($_POST['language']) : ''; + DI::config()->set('nominatim', 'language', $language); +} diff --git a/nominatim/templates/admin.tpl b/nominatim/templates/admin.tpl new file mode 100644 index 000000000..12dbf27f7 --- /dev/null +++ b/nominatim/templates/admin.tpl @@ -0,0 +1,2 @@ +{{include file="field_input.tpl" field=$language}} +