From f131c4c66c241f39e463b4526d0f2a9646b080e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Corentin=20No=C3=ABl?= Date: Thu, 25 Sep 2025 11:16:34 +0200 Subject: [PATCH] AddressService: Use the structured query when possible MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allows nominatim to make sure that we are requesting the right place and enables more heuristics on their side. Signed-off-by: Corentin Noël --- lib/Service/AddressService.php | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/Service/AddressService.php b/lib/Service/AddressService.php index 23627196b..9e3853e61 100644 --- a/lib/Service/AddressService.php +++ b/lib/Service/AddressService.php @@ -181,16 +181,28 @@ private function lookupAddressExternal($adr): array { // we get rid of "post office box" field $splitted_adr = explode(';', $adr); - if (count($splitted_adr) > 2) { - array_shift($splitted_adr); - } - // remove blank lines (#706) $splitted_adr = array_filter(array_map('trim', $splitted_adr)); - $query_adr = implode(', ', $splitted_adr); + // ADR in VCard is mandated to 7 fields + if (sizeof($splitted_adr) == 7) { + $query_adr_parts = []; + // This matches the nominatim query with the fields of 'ADR' in VCard + $query_key_part = ['','','street', 'city','state', 'postalcode', 'country']; + foreach ($query_key_part as $index => $query_key) { + if ($query_key !== '' && $splitted_adr[$index] !== '') { + $query_adr_parts += $query_key . '=' . urlencode($splitted_adr[$index]); + } + } + + $query_adr = implode(';', $query_adr_parts); + } else { + // Try to do our best with a naive query + $query_adr = 'q=' . urlencode(implode(', ', $splitted_adr)); + } + $result_json = @file_get_contents( - 'https://nominatim.openstreetmap.org/search.php?q=' . urlencode($query_adr) . '&format=jsonv2', + 'https://nominatim.openstreetmap.org/search?format=jsonv2&' . $query_adr, false, $context );