]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/template/address-geocodejson.php
add event handler for default connection settings
[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']['type'] = addressRankToGeocodeJsonType($aPlace['rank_address']);
40
41     $aFilteredPlaces['properties']['geocoding']['accuracy'] = (int) $fDistance;
42
43     $aFilteredPlaces['properties']['geocoding']['label'] = $aPlace['langaddress'];
44
45     if ($aPlace['placename'] !== null) {
46         $aFilteredPlaces['properties']['geocoding']['name'] = $aPlace['placename'];
47     }
48
49     if (isset($aPlace['address'])) {
50         $aPlace['address']->addGeocodeJsonAddressParts(
51             $aFilteredPlaces['properties']['geocoding']
52         );
53
54         $aFilteredPlaces['properties']['geocoding']['admin']
55             = $aPlace['address']->getAdminLevels();
56     }
57
58     if (isset($aPlace['asgeojson'])) {
59         $aFilteredPlaces['geometry'] = json_decode($aPlace['asgeojson'], true);
60     } else {
61         $aFilteredPlaces['geometry'] = array(
62                                         'type' => 'Point',
63                                         'coordinates' => array(
64                                                           (float) $aPlace['lon'],
65                                                           (float) $aPlace['lat']
66                                                          )
67                                        );
68     }
69
70     javascript_renderData(array(
71                            'type' => 'FeatureCollection',
72                            'geocoding' => array(
73                                            'version' => '0.1.0',
74                                            'attribution' => 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',
75                                            'licence' => 'ODbL',
76                                            'query' => $sQuery
77                                           ),
78                            'features' => array($aFilteredPlaces)
79                           ));
80 }