1 -- SPDX-License-Identifier: GPL-2.0-only
3 -- This file is part of Nominatim. (https://nominatim.org)
5 -- Copyright (C) 2022 by the Nominatim developer community.
6 -- For a full list of authors see the git log.
8 -- Functions for returning address information for a place.
10 DROP TYPE IF EXISTS addressline CASCADE;
11 CREATE TYPE addressline as (
27 CREATE OR REPLACE FUNCTION get_name_by_language(name hstore, languagepref TEXT[])
37 FOR j IN 1..array_upper(languagepref,1) LOOP
38 IF name ? languagepref[j] THEN
39 result := trim(name->languagepref[j]);
46 -- change this code below
47 -- anything will do as a fallback - just take the first name type thing there is
48 RETURN trim((avals(name))[array_length(avals(name), 1)]);
51 LANGUAGE plpgsql IMMUTABLE;
54 --housenumber only needed for tiger data
55 CREATE OR REPLACE FUNCTION get_address_by_language(for_place_id BIGINT,
60 -- ARRAY['zh','default','brand','official_name:zh','short_name:zh','official_name','short_name','ref','type']
73 CASE WHEN place_id = for_place_id THEN 99 ELSE rank_address END as rank_address
74 FROM get_addressdata(for_place_id, housenumber)
75 WHERE isaddress order by rank_address desc
77 currresult := trim(get_name_by_language(location.name, languagepref));
78 IF currresult != prevresult AND currresult IS NOT NULL
79 AND result[(100 - location.rank_address)] IS NULL
81 result[(100 - location.rank_address)] := currresult;
82 prevresult := currresult;
86 RETURN array_to_string(result,', ');
89 LANGUAGE plpgsql STABLE;
91 DROP TYPE IF EXISTS addressdata_place;
92 CREATE TYPE addressdata_place AS (
94 country_code VARCHAR(2),
104 -- Compute the list of address parts for the given place.
106 -- If in_housenumber is greator or equal 0, look for an interpolation.
107 CREATE OR REPLACE FUNCTION get_addressdata(in_place_id BIGINT, in_housenumber INTEGER)
108 RETURNS setof addressline
111 place addressdata_place;
114 current_rank_address INTEGER;
115 location_isaddress BOOLEAN;
117 -- The place in question might not have a direct entry in place_addressline.
118 -- Look for the parent of such places then and save it in place.
120 -- first query osmline (interpolation lines)
121 IF in_housenumber >= 0 THEN
122 SELECT parent_place_id as place_id, country_code,
123 in_housenumber as housenumber, postcode,
124 'place' as class, 'house' as type,
125 null as name, null as address,
126 ST_Centroid(linegeo) as centroid
128 FROM location_property_osmline
129 WHERE place_id = in_place_id
130 AND in_housenumber between startnumber and endnumber;
133 --then query tiger data
134 {% if config.get_bool('USE_US_TIGER_DATA') %}
135 IF place IS NULL AND in_housenumber >= 0 THEN
136 SELECT parent_place_id as place_id, 'us' as country_code,
137 in_housenumber as housenumber, postcode,
138 'place' as class, 'house' as type,
139 null as name, null as address,
140 ST_Centroid(linegeo) as centroid
142 FROM location_property_tiger
143 WHERE place_id = in_place_id
144 AND in_housenumber between startnumber and endnumber;
149 IF place IS NULL THEN
150 SELECT parent_place_id as place_id, country_code,
151 null::text as housenumber, postcode,
152 'place' as class, 'postcode' as type,
153 null as name, null as address,
156 FROM location_postcode
157 WHERE place_id = in_place_id;
160 -- POI objects in the placex table
161 IF place IS NULL THEN
162 SELECT parent_place_id as place_id, country_code,
163 coalesce(address->'housenumber',
164 address->'streetnumber',
165 address->'conscriptionnumber')::text as housenumber,
172 WHERE place_id = in_place_id and rank_search > 27;
175 -- If place is still NULL at this point then the object has its own
176 -- entry in place_address line. However, still check if there is not linked
177 -- place we should be using instead.
178 IF place IS NULL THEN
179 select coalesce(linked_place_id, place_id) as place_id, country_code,
180 null::text as housenumber, postcode,
182 null as name, address,
185 FROM placex where place_id = in_place_id;
188 --RAISE WARNING '% % % %',searchcountrycode, searchhousenumber, searchpostcode;
190 -- --- Return the record for the base entry.
193 SELECT placex.place_id, osm_type, osm_id, name,
194 coalesce(extratags->'linked_place', extratags->'place') as place_type,
195 class, type, admin_level,
196 CASE WHEN rank_address = 0 THEN 100
197 WHEN rank_address = 11 THEN 5
198 ELSE rank_address END as rank_address,
201 WHERE place_id = place.place_id
203 --RAISE WARNING '%',location;
204 -- mix in default names for countries
205 IF location.rank_address = 4 and place.country_code is not NULL THEN
207 SELECT coalesce(name, ''::hstore) as name FROM country_name
208 WHERE country_code = place.country_code LIMIT 1
210 place.name := country.name || place.name;
214 IF location.rank_address < 4 THEN
215 -- no country locations for ranks higher than country
216 place.country_code := NULL::varchar(2);
217 ELSEIF place.country_code IS NULL AND location.country_code IS NOT NULL THEN
218 place.country_code := location.country_code;
221 RETURN NEXT ROW(location.place_id, location.osm_type, location.osm_id,
222 location.name, location.class, location.type,
224 location.admin_level, true,
225 location.type not in ('postcode', 'postal_code'),
226 location.rank_address, 0)::addressline;
228 current_rank_address := location.rank_address;
231 -- --- Return records for address parts.
234 SELECT placex.place_id, osm_type, osm_id, name, class, type,
235 coalesce(extratags->'linked_place', extratags->'place') as place_type,
236 admin_level, fromarea, isaddress,
237 CASE WHEN rank_address = 11 THEN 5 ELSE rank_address END as rank_address,
238 distance, country_code, postcode
239 FROM place_addressline join placex on (address_place_id = placex.place_id)
240 WHERE place_addressline.place_id IN (place.place_id, in_place_id)
241 AND linked_place_id is null
242 AND (placex.country_code IS NULL OR place.country_code IS NULL
243 OR placex.country_code = place.country_code)
244 ORDER BY rank_address desc,
245 (place_addressline.place_id = in_place_id) desc,
246 (CASE WHEN coalesce((avals(name) && avals(place.address)), False) THEN 2
247 WHEN isaddress THEN 0
249 and place.centroid is not null
250 and ST_Contains(geometry, place.centroid) THEN 1
252 fromarea desc, distance asc, rank_search desc
254 -- RAISE WARNING '%',location;
255 location_isaddress := location.rank_address != current_rank_address;
257 IF place.country_code IS NULL AND location.country_code IS NOT NULL THEN
258 place.country_code := location.country_code;
260 IF location.type in ('postcode', 'postal_code')
261 AND place.postcode is not null
263 -- If the place had a postcode assigned, take this one only
264 -- into consideration when it is an area and the place does not have
265 -- a postcode itself.
266 IF location.fromarea AND location.isaddress
267 AND (place.address is null or not place.address ? 'postcode')
269 place.postcode := null; -- remove the less exact postcode
271 location_isaddress := false;
274 RETURN NEXT ROW(location.place_id, location.osm_type, location.osm_id,
275 location.name, location.class, location.type,
277 location.admin_level, location.fromarea,
279 location.rank_address,
280 location.distance)::addressline;
282 current_rank_address := location.rank_address;
285 -- If no country was included yet, add the name information from country_name.
286 IF current_rank_address > 4 THEN
288 SELECT name || coalesce(derived_name, ''::hstore) as name FROM country_name
289 WHERE country_code = place.country_code LIMIT 1
291 --RAISE WARNING '% % %',current_rank_address,searchcountrycode,countryname;
292 RETURN NEXT ROW(null, null, null, location.name, 'place', 'country', NULL,
293 null, true, true, 4, 0)::addressline;
297 -- Finally add some artificial rows.
298 IF place.country_code IS NOT NULL THEN
299 location := ROW(null, null, null, hstore('ref', place.country_code),
300 'place', 'country_code', null, null, true, false, 4, 0)::addressline;
301 RETURN NEXT location;
304 IF place.name IS NOT NULL THEN
305 location := ROW(in_place_id, null, null, place.name, place.class,
306 place.type, null, null, true, true, 29, 0)::addressline;
307 RETURN NEXT location;
310 IF place.housenumber IS NOT NULL THEN
311 location := ROW(null, null, null, hstore('ref', place.housenumber),
312 'place', 'house_number', null, null, true, true, 28, 0)::addressline;
313 RETURN NEXT location;
316 IF place.address is not null and place.address ? '_unlisted_place' THEN
317 RETURN NEXT ROW(null, null, null, hstore('name', place.address->'_unlisted_place'),
318 'place', 'locality', null, null, true, true, 25, 0)::addressline;
321 IF place.postcode is not null THEN
322 location := ROW(null, null, null, hstore('ref', place.postcode), 'place',
323 'postcode', null, null, false, true, 5, 0)::addressline;
324 RETURN NEXT location;
330 LANGUAGE plpgsql STABLE;