]> git.openstreetmap.org Git - nominatim.git/blob - lib/template/address-geocodejson.php
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / lib / 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 $aFilteredPlaces['error'] = 'Unable to geocode';
11     javascript_renderData($aFilteredPlaces);
12 } else {
13     $aFilteredPlaces = array(
14                         'type' => 'Feature',
15                         'properties' => array(
16                                          'geocoding' => array()
17                                         )
18                        );
19
20     if (isset($aPlace['place_id'])) $aFilteredPlaces['properties']['geocoding']['place_id'] = $aPlace['place_id'];
21     $sOSMType = formatOSMType($aPlace['osm_type']);
22     if ($sOSMType) {
23         $aFilteredPlaces['properties']['geocoding']['osm_type'] = $sOSMType;
24         $aFilteredPlaces['properties']['geocoding']['osm_id'] = $aPlace['osm_id'];
25     }
26
27     $aFilteredPlaces['properties']['geocoding']['type'] = $aPlace['type'];
28
29     $aFilteredPlaces['properties']['geocoding']['accuracy'] = (int) $fDistance;
30
31     $aFilteredPlaces['properties']['geocoding']['label'] = $aPlace['langaddress'];
32
33     $aFilteredPlaces['properties']['geocoding']['name'] = $aPlace['placename'];
34
35     $aFieldMappings = array(
36                        'house_number' => 'housenumber',
37                        'road' => 'street',
38                        'locality' => 'locality',
39                        'postcode' => 'postcode',
40                        'city' => 'city',
41                        'district' => 'district',
42                        'county' => 'county',
43                        'state' => 'state',
44                        'country' => 'country'
45                       );
46
47     foreach ($aFieldMappings as $sFrom => $sTo) {
48         if (isset($aPlace['aAddress'][$sFrom])) {
49             $aFilteredPlaces['properties']['geocoding'][$sTo] = $aPlace['aAddress'][$sFrom];
50         }
51     }
52
53     $aFilteredPlaces['properties']['geocoding']['admin'] = $aPlace['aAddressAdminLevels'];
54
55     if (isset($aPlace['asgeojson'])) {
56         $aFilteredPlaces['geometry'] = json_decode($aPlace['asgeojson']);
57     } else {
58         $aFilteredPlaces['geometry'] = array(
59                                         'type' => 'Point',
60                                         'coordinates' => array(
61                                                           (float) $aPlace['lon'],
62                                                           (float) $aPlace['lat']
63                                                          )
64                                        );
65     }
66
67     javascript_renderData(array(
68                            'type' => 'FeatureCollection',
69                            'geocoding' => array(
70                                            'version' => '0.1.0',
71                                            'attribution' => 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',
72                                            'licence' => 'ODbL',
73                                            'query' => $sQuery
74                                           ),
75                            'features' => $aFilteredPlaces
76                           ));
77 }