]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/api/v1/format_json.py
make sure PHP and Python reverse code does the same
[nominatim.git] / nominatim / api / v1 / format_json.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2023 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Helper functions for output of results in json formats.
9 """
10 from typing import Mapping, Any, Optional, Tuple
11
12 import nominatim.api as napi
13 import nominatim.api.v1.classtypes as cl
14 from nominatim.utils.json_writer import JsonWriter
15
16 def _write_osm_id(out: JsonWriter, osm_object: Optional[Tuple[str, int]]) -> None:
17     if osm_object is not None:
18         out.keyval_not_none('osm_type', cl.OSM_TYPE_NAME.get(osm_object[0], None))\
19            .keyval('osm_id', osm_object[1])
20
21
22 def _write_typed_address(out: JsonWriter, address: Optional[napi.AddressLines],
23                                country_code: Optional[str]) -> None:
24     parts = {}
25     for line in (address or []):
26         if line.isaddress:
27             if line.local_name:
28                 label = cl.get_label_tag(line.category, line.extratags,
29                                          line.rank_address, country_code)
30                 if label not in parts:
31                     print(label)
32                     parts[label] = line.local_name
33             if line.names and 'ISO3166-2' in line.names and line.admin_level:
34                 parts[f"ISO3166-2-lvl{line.admin_level}"] = line.names['ISO3166-2']
35
36     for k, v in parts.items():
37         out.keyval(k, v)
38
39     if country_code:
40         out.keyval('country_code', country_code)
41
42
43 def _write_geocodejson_address(out: JsonWriter,
44                                address: Optional[napi.AddressLines],
45                                obj_place_id: Optional[int],
46                                country_code: Optional[str]) -> None:
47     extra = {}
48     for line in (address or []):
49         if line.isaddress and line.local_name:
50             if line.category[1] in ('postcode', 'postal_code'):
51                 out.keyval('postcode', line.local_name)
52             elif line.category[1] == 'house_number':
53                 out.keyval('housenumber', line.local_name)
54             elif (obj_place_id is None or obj_place_id != line.place_id) \
55                  and line.rank_address >= 4 and line.rank_address < 28:
56                 extra[GEOCODEJSON_RANKS[line.rank_address]] = line.local_name
57
58     for k, v in extra.items():
59         out.keyval(k, v)
60
61     if country_code:
62         out.keyval('country_code', country_code)
63
64
65 def format_base_json(results: napi.ReverseResults, #pylint: disable=too-many-branches
66                      options: Mapping[str, Any], simple: bool,
67                      class_label: str) -> str:
68     """ Return the result list as a simple json string in custom Nominatim format.
69     """
70     locales = options.get('locales', napi.Locales())
71
72     out = JsonWriter()
73
74     if simple:
75         if not results:
76             return '{"error":"Unable to geocode"}'
77     else:
78         out.start_array()
79
80     for result in results:
81         label_parts = result.address_rows.localize(locales) if result.address_rows else []
82
83         out.start_object()\
84              .keyval_not_none('place_id', result.place_id)\
85              .keyval('licence', cl.OSM_ATTRIBUTION)\
86
87         _write_osm_id(out, result.osm_object)
88
89         out.keyval('lat', result.centroid.lat)\
90              .keyval('lon', result.centroid.lon)\
91              .keyval(class_label, result.category[0])\
92              .keyval('type', result.category[1])\
93              .keyval('place_rank', result.rank_search)\
94              .keyval('importance', result.calculated_importance())\
95              .keyval('addresstype', cl.get_label_tag(result.category, result.extratags,
96                                                      result.rank_address,
97                                                      result.country_code))\
98              .keyval('name', locales.display_name(result.names))\
99              .keyval('display_name', ', '.join(label_parts))
100
101
102         if options.get('icon_base_url', None):
103             icon = cl.ICONS.get(result.category)
104             if icon:
105                 out.keyval('icon', f"{options['icon_base_url']}/{icon}.p.20.png")
106
107         if options.get('addressdetails', False):
108             out.key('address').start_object()
109             _write_typed_address(out, result.address_rows, result.country_code)
110             out.end_object().next()
111
112         if options.get('extratags', False):
113             out.keyval('extratags', result.extratags)
114
115         if options.get('namedetails', False):
116             out.keyval('namedetails', result.names)
117
118         bbox = cl.bbox_from_result(result)
119         out.key('boundingbox').start_array()\
120              .value(f"{bbox.minlat:0.7f}").next()\
121              .value(f"{bbox.maxlat:0.7f}").next()\
122              .value(f"{bbox.minlon:0.7f}").next()\
123              .value(f"{bbox.maxlon:0.7f}").next()\
124            .end_array().next()
125
126         if result.geometry:
127             for key in ('text', 'kml'):
128                 out.keyval_not_none('geo' + key, result.geometry.get(key))
129             if 'geojson' in result.geometry:
130                 out.key('geojson').raw(result.geometry['geojson']).next()
131             out.keyval_not_none('svg', result.geometry.get('svg'))
132
133         out.end_object()
134
135         if simple:
136             return out()
137
138         out.next()
139
140     out.end_array()
141
142     return out()
143
144
145 def format_base_geojson(results: napi.ReverseResults,
146                         options: Mapping[str, Any],
147                         simple: bool) -> str:
148     """ Return the result list as a geojson string.
149     """
150     if not results and simple:
151         return '{"error":"Unable to geocode"}'
152
153     locales = options.get('locales', napi.Locales())
154
155     out = JsonWriter()
156
157     out.start_object()\
158          .keyval('type', 'FeatureCollection')\
159          .keyval('licence', cl.OSM_ATTRIBUTION)\
160          .key('features').start_array()
161
162     for result in results:
163         if result.address_rows:
164             label_parts = result.address_rows.localize(locales)
165         else:
166             label_parts = []
167
168         out.start_object()\
169              .keyval('type', 'Feature')\
170              .key('properties').start_object()
171
172         out.keyval_not_none('place_id', result.place_id)
173
174         _write_osm_id(out, result.osm_object)
175
176         out.keyval('place_rank', result.rank_search)\
177            .keyval('category', result.category[0])\
178            .keyval('type', result.category[1])\
179            .keyval('importance', result.calculated_importance())\
180            .keyval('addresstype', cl.get_label_tag(result.category, result.extratags,
181                                                    result.rank_address,
182                                                    result.country_code))\
183            .keyval('name', locales.display_name(result.names))\
184            .keyval('display_name', ', '.join(label_parts))
185
186         if options.get('addressdetails', False):
187             out.key('address').start_object()
188             _write_typed_address(out, result.address_rows, result.country_code)
189             out.end_object().next()
190
191         if options.get('extratags', False):
192             out.keyval('extratags', result.extratags)
193
194         if options.get('namedetails', False):
195             out.keyval('namedetails', result.names)
196
197         out.end_object().next() # properties
198
199         out.key('bbox').start_array()
200         for coord in cl.bbox_from_result(result).coords:
201             out.float(coord, 7).next()
202         out.end_array().next()
203
204         out.key('geometry').raw(result.geometry.get('geojson')
205                                 or result.centroid.to_geojson()).next()
206
207         out.end_object().next()
208
209     out.end_array().next().end_object()
210
211     return out()
212
213
214 def format_base_geocodejson(results: napi.ReverseResults,
215                             options: Mapping[str, Any], simple: bool) -> str:
216     """ Return the result list as a geocodejson string.
217     """
218     if not results and simple:
219         return '{"error":"Unable to geocode"}'
220
221     locales = options.get('locales', napi.Locales())
222
223     out = JsonWriter()
224
225     out.start_object()\
226          .keyval('type', 'FeatureCollection')\
227          .key('geocoding').start_object()\
228            .keyval('version', '0.1.0')\
229            .keyval('attribution', cl.OSM_ATTRIBUTION)\
230            .keyval('licence', 'ODbL')\
231            .keyval_not_none('query', options.get('query'))\
232            .end_object().next()\
233          .key('features').start_array()
234
235     for result in results:
236         if result.address_rows:
237             label_parts = result.address_rows.localize(locales)
238         else:
239             label_parts = []
240
241         out.start_object()\
242              .keyval('type', 'Feature')\
243              .key('properties').start_object()\
244                .key('geocoding').start_object()
245
246         out.keyval_not_none('place_id', result.place_id)
247
248         _write_osm_id(out, result.osm_object)
249
250         out.keyval('osm_key', result.category[0])\
251            .keyval('osm_value', result.category[1])\
252            .keyval('type', GEOCODEJSON_RANKS[max(3, min(28, result.rank_address))])\
253            .keyval_not_none('accuracy', result.distance, transform=int)\
254            .keyval('label', ', '.join(label_parts))\
255            .keyval_not_none('name', result.names, transform=locales.display_name)\
256
257         if options.get('addressdetails', False):
258             _write_geocodejson_address(out, result.address_rows, result.place_id,
259                                        result.country_code)
260
261             out.key('admin').start_object()
262             if result.address_rows:
263                 for line in result.address_rows:
264                     if line.isaddress and (line.admin_level or 15) < 15 and line.local_name:
265                         out.keyval(f"level{line.admin_level}", line.local_name)
266             out.end_object().next()
267
268         out.end_object().next().end_object().next()
269
270         out.key('geometry').raw(result.geometry.get('geojson')
271                                 or result.centroid.to_geojson()).next()
272
273         out.end_object().next()
274
275     out.end_array().next().end_object()
276
277     return out()
278
279
280 GEOCODEJSON_RANKS = {
281     3: 'locality',
282     4: 'country',
283     5: 'state', 6: 'state', 7: 'state', 8: 'state', 9: 'state',
284     10: 'county', 11: 'county', 12: 'county',
285     13: 'city', 14: 'city', 15: 'city', 16: 'city',
286     17: 'district', 18: 'district', 19: 'district', 20: 'district', 21: 'district',
287     22: 'locality', 23: 'locality', 24: 'locality',
288     25: 'street', 26: 'street', 27: 'street', 28: 'house'}