]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/template/address-geocodejson.php
Merge pull request #2391 from lonvia/fix-sonar-issues
[nominatim.git] / lib-php / template / address-geocodejson.php
1 <?php
2
3 // https://github.com/geocoders/geocodejson-spec/
4
5 $aFilteredPlaces = array();
6
7 if (empty($aPlace)) {
8     if (isset($sError)) {
9         $aFilteredPlaces['error'] = $sError;
10     } else {
11         $aFilteredPlaces['error'] = 'Unable to geocode';
12     }
13     javascript_renderData($aFilteredPlaces);
14 } else {
15     $aFilteredPlaces = array(
16                         'type' => 'Feature',
17                         'properties' => array(
18                                          'geocoding' => array()
19                                         )
20                        );
21
22     if (isset($aPlace['place_id'])) {
23         $aFilteredPlaces['properties']['geocoding']['place_id'] = $aPlace['place_id'];
24     }
25     $sOSMType = formatOSMType($aPlace['osm_type']);
26     if ($sOSMType) {
27         $aFilteredPlaces['properties']['geocoding']['osm_type'] = $sOSMType;
28         $aFilteredPlaces['properties']['geocoding']['osm_id'] = $aPlace['osm_id'];
29     }
30
31     $aFilteredPlaces['properties']['geocoding']['type'] = $aPlace['type'];
32
33     $aFilteredPlaces['properties']['geocoding']['accuracy'] = (int) $fDistance;
34
35     $aFilteredPlaces['properties']['geocoding']['label'] = $aPlace['langaddress'];
36
37     if ($aPlace['placename'] !== null) {
38         $aFilteredPlaces['properties']['geocoding']['name'] = $aPlace['placename'];
39     }
40
41     if (isset($aPlace['address'])) {
42         $aPlace['address']->addGeocodeJsonAddressParts(
43             $aFilteredPlaces['properties']['geocoding']
44         );
45
46         $aFilteredPlaces['properties']['geocoding']['admin']
47             = $aPlace['address']->getAdminLevels();
48     }
49
50     if (isset($aPlace['asgeojson'])) {
51         $aFilteredPlaces['geometry'] = json_decode($aPlace['asgeojson']);
52     } else {
53         $aFilteredPlaces['geometry'] = array(
54                                         'type' => 'Point',
55                                         'coordinates' => array(
56                                                           (float) $aPlace['lon'],
57                                                           (float) $aPlace['lat']
58                                                          )
59                                        );
60     }
61
62     javascript_renderData(array(
63                            'type' => 'FeatureCollection',
64                            'geocoding' => array(
65                                            'version' => '0.1.0',
66                                            'attribution' => 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',
67                                            'licence' => 'ODbL',
68                                            'query' => $sQuery
69                                           ),
70                            'features' => array($aFilteredPlaces)
71                           ));
72 }