]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/template/address-geocodejson.php
avoid fallback country lookup when places are excluded
[nominatim.git] / lib-php / template / address-geocodejson.php
1 <?php
2 /**
3  * SPDX-License-Identifier: GPL-2.0-only
4  *
5  * This file is part of Nominatim. (https://nominatim.org)
6  *
7  * Copyright (C) 2022 by the Nominatim developer community.
8  * For a full list of authors see the git log.
9  */
10
11 // https://github.com/geocoders/geocodejson-spec/
12
13 $aFilteredPlaces = array();
14
15 if (empty($aPlace)) {
16     if (isset($sError)) {
17         $aFilteredPlaces['error'] = $sError;
18     } else {
19         $aFilteredPlaces['error'] = 'Unable to geocode';
20     }
21     javascript_renderData($aFilteredPlaces);
22 } else {
23     $aFilteredPlaces = array(
24                         'type' => 'Feature',
25                         'properties' => array(
26                                          'geocoding' => array()
27                                         )
28                        );
29
30     if (isset($aPlace['place_id'])) {
31         $aFilteredPlaces['properties']['geocoding']['place_id'] = $aPlace['place_id'];
32     }
33     $sOSMType = formatOSMType($aPlace['osm_type']);
34     if ($sOSMType) {
35         $aFilteredPlaces['properties']['geocoding']['osm_type'] = $sOSMType;
36         $aFilteredPlaces['properties']['geocoding']['osm_id'] = $aPlace['osm_id'];
37     }
38
39     $aFilteredPlaces['properties']['geocoding']['osm_key'] = $aPlace['class'];
40     $aFilteredPlaces['properties']['geocoding']['osm_value'] = $aPlace['type'];
41
42     $aFilteredPlaces['properties']['geocoding']['type'] = addressRankToGeocodeJsonType($aPlace['rank_address']);
43
44     $aFilteredPlaces['properties']['geocoding']['accuracy'] = (int) $fDistance;
45
46     $aFilteredPlaces['properties']['geocoding']['label'] = $aPlace['langaddress'];
47
48     if ($aPlace['placename'] !== null) {
49         $aFilteredPlaces['properties']['geocoding']['name'] = $aPlace['placename'];
50     }
51
52     if (isset($aPlace['address'])) {
53         $aPlace['address']->addGeocodeJsonAddressParts(
54             $aFilteredPlaces['properties']['geocoding']
55         );
56
57         $aFilteredPlaces['properties']['geocoding']['admin']
58             = $aPlace['address']->getAdminLevels();
59     }
60
61     if (isset($aPlace['asgeojson'])) {
62         $aFilteredPlaces['geometry'] = json_decode($aPlace['asgeojson'], true);
63     } else {
64         $aFilteredPlaces['geometry'] = array(
65                                         'type' => 'Point',
66                                         'coordinates' => array(
67                                                           (float) $aPlace['lon'],
68                                                           (float) $aPlace['lat']
69                                                          )
70                                        );
71     }
72
73     javascript_renderData(array(
74                            'type' => 'FeatureCollection',
75                            'geocoding' => array(
76                                            'version' => '0.1.0',
77                                            'attribution' => 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',
78                                            'licence' => 'ODbL',
79                                            'query' => $sQuery
80                                           ),
81                            'features' => array($aFilteredPlaces)
82                           ));
83 }