]> git.openstreetmap.org Git - nominatim.git/blob - lib/template/address-geocodejson.php
Merge pull request #1638 from mtmail/check-for-invalid-indices
[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     if (isset($aPlace['address'])) {
36         $aFieldMappings = array(
37                            'house_number' => 'housenumber',
38                            'road' => 'street',
39                            'locality' => 'locality',
40                            'postcode' => 'postcode',
41                            'city' => 'city',
42                            'district' => 'district',
43                            'county' => 'county',
44                            'state' => 'state',
45                            'country' => 'country'
46                           );
47
48         $aAddressNames = $aPlace['address']->getAddressNames();
49         foreach ($aFieldMappings as $sFrom => $sTo) {
50             if (isset($aAddressNames[$sFrom])) {
51                 $aFilteredPlaces['properties']['geocoding'][$sTo] = $aAddressNames[$sFrom];
52             }
53         }
54
55         $aFilteredPlaces['properties']['geocoding']['admin']
56             = $aPlace['address']->getAdminLevels();
57     }
58
59     if (isset($aPlace['asgeojson'])) {
60         $aFilteredPlaces['geometry'] = json_decode($aPlace['asgeojson']);
61     } else {
62         $aFilteredPlaces['geometry'] = array(
63                                         'type' => 'Point',
64                                         'coordinates' => array(
65                                                           (float) $aPlace['lon'],
66                                                           (float) $aPlace['lat']
67                                                          )
68                                        );
69     }
70
71     javascript_renderData(array(
72                            'type' => 'FeatureCollection',
73                            'geocoding' => array(
74                                            'version' => '0.1.0',
75                                            'attribution' => 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',
76                                            'licence' => 'ODbL',
77                                            'query' => $sQuery
78                                           ),
79                            'features' => array($aFilteredPlaces)
80                           ));
81 }