Skip to content

Commit c1336b6

Browse files
committed
AddressService: Use the structured query when possible
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 <corentin.noel@collabora.com>
1 parent d7d9f26 commit c1336b6

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

lib/Service/AddressService.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,16 +181,27 @@ private function lookupAddressExternal($adr): array {
181181

182182
// we get rid of "post office box" field
183183
$splitted_adr = explode(';', $adr);
184-
if (count($splitted_adr) > 2) {
185-
array_shift($splitted_adr);
186-
}
187-
188184
// remove blank lines (#706)
189185
$splitted_adr = array_filter(array_map('trim', $splitted_adr));
190-
$query_adr = implode(', ', $splitted_adr);
186+
// ADR in VCard is mandated to 7 fields
187+
if (sizeof($splitted_adr) == 7) {
188+
$query_adr_parts = [];
189+
// This matches the nominatim query with the fields of 'ADR' in VCard
190+
$query_key_part = ['','','street', 'city','state', 'postalcode', 'country'];
191+
foreach ($query_key_part as $index => $query_key) {
192+
if ($query_key !== '' && $splitted_adr[$index] !== '')
193+
$query_adr_parts += $query_key . '=' . urlencode($splitted_adr[$index]);
194+
}
195+
196+
$query_adr = implode(';', $query_adr_parts);
197+
} else {
198+
// Try to do our best with a naive query
199+
$query_adr = 'q=' . urlencode(implode(', ', $splitted_adr));
200+
}
201+
191202

192203
$result_json = @file_get_contents(
193-
'https://nominatim.openstreetmap.org/search.php?q=' . urlencode($query_adr) . '&format=jsonv2',
204+
'https://nominatim.openstreetmap.org/search?format=jsonv2&' . $query_adr,
194205
false,
195206
$context
196207
);

0 commit comments

Comments
 (0)