]> git.openstreetmap.org Git - nominatim.git/blob - sql/functions/address_lookup.sql
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / sql / functions / address_lookup.sql
1 -- Functions for returning address information for a place.
2
3 DROP TYPE IF EXISTS addressline CASCADE;
4 CREATE TYPE addressline as (
5   place_id BIGINT,
6   osm_type CHAR(1),
7   osm_id BIGINT,
8   name HSTORE,
9   class TEXT,
10   type TEXT,
11   place_type TEXT,
12   admin_level INTEGER,
13   fromarea BOOLEAN,
14   isaddress BOOLEAN,
15   rank_address INTEGER,
16   distance FLOAT
17 );
18
19
20 CREATE OR REPLACE FUNCTION get_name_by_language(name hstore, languagepref TEXT[])
21   RETURNS TEXT
22   AS $$
23 DECLARE
24   result TEXT;
25 BEGIN
26   IF name is null THEN
27     RETURN null;
28   END IF;
29
30   FOR j IN 1..array_upper(languagepref,1) LOOP
31     IF name ? languagepref[j] THEN
32       result := trim(name->languagepref[j]);
33       IF result != '' THEN
34         return result;
35       END IF;
36     END IF;
37   END LOOP;
38
39   -- anything will do as a fallback - just take the first name type thing there is
40   RETURN trim((avals(name))[1]);
41 END;
42 $$
43 LANGUAGE plpgsql IMMUTABLE;
44
45
46 --housenumber only needed for tiger data
47 CREATE OR REPLACE FUNCTION get_address_by_language(for_place_id BIGINT,
48                                                    housenumber INTEGER,
49                                                    languagepref TEXT[])
50   RETURNS TEXT
51   AS $$
52 DECLARE
53   result TEXT[];
54   currresult TEXT;
55   prevresult TEXT;
56   location RECORD;
57 BEGIN
58
59   result := '{}';
60   prevresult := '';
61
62   FOR location IN
63     SELECT name,
64        CASE WHEN place_id = for_place_id THEN 99 ELSE rank_address END as rank_address
65     FROM get_addressdata(for_place_id, housenumber)
66     WHERE isaddress order by rank_address desc
67   LOOP
68     currresult := trim(get_name_by_language(location.name, languagepref));
69     IF currresult != prevresult AND currresult IS NOT NULL
70        AND result[(100 - location.rank_address)] IS NULL
71     THEN
72       result[(100 - location.rank_address)] := currresult;
73       prevresult := currresult;
74     END IF;
75   END LOOP;
76
77   RETURN array_to_string(result,', ');
78 END;
79 $$
80 LANGUAGE plpgsql STABLE;
81
82
83 -- Compute the list of address parts for the given place.
84 --
85 -- If in_housenumber is greator or equal 0, look for an interpolation.
86 CREATE OR REPLACE FUNCTION get_addressdata(in_place_id BIGINT, in_housenumber INTEGER)
87   RETURNS setof addressline
88   AS $$
89 DECLARE
90   for_place_id BIGINT;
91   result TEXT[];
92   search TEXT[];
93   found INTEGER;
94   location RECORD;
95   countrylocation RECORD;
96   searchcountrycode varchar(2);
97   searchhousenumber TEXT;
98   searchhousename HSTORE;
99   searchrankaddress INTEGER;
100   searchpostcode TEXT;
101   postcode_isexact BOOL;
102   searchclass TEXT;
103   searchtype TEXT;
104   search_unlisted_place TEXT;
105   countryname HSTORE;
106 BEGIN
107   -- The place ein question might not have a direct entry in place_addressline.
108   -- Look for the parent of such places then and save if in for_place_id.
109
110   postcode_isexact := false;
111
112   -- first query osmline (interpolation lines)
113   IF in_housenumber >= 0 THEN
114     SELECT parent_place_id, country_code, in_housenumber::text, 30, postcode,
115            null, 'place', 'house'
116       FROM location_property_osmline
117       WHERE place_id = in_place_id AND in_housenumber>=startnumber
118             AND in_housenumber <= endnumber
119       INTO for_place_id, searchcountrycode, searchhousenumber, searchrankaddress,
120            searchpostcode, searchhousename, searchclass, searchtype;
121   END IF;
122
123   --then query tiger data
124   -- %NOTIGERDATA% IF 0 THEN
125   IF for_place_id IS NULL AND in_housenumber >= 0 THEN
126     SELECT parent_place_id, 'us', in_housenumber::text, 30, postcode, null,
127            'place', 'house'
128       FROM location_property_tiger
129       WHERE place_id = in_place_id AND in_housenumber >= startnumber
130             AND in_housenumber <= endnumber
131       INTO for_place_id, searchcountrycode, searchhousenumber, searchrankaddress,
132            searchpostcode, searchhousename, searchclass, searchtype;
133   END IF;
134   -- %NOTIGERDATA% END IF;
135
136   -- %NOAUXDATA% IF 0 THEN
137   IF for_place_id IS NULL THEN
138     SELECT parent_place_id, 'us', housenumber, 30, postcode, null, 'place', 'house'
139       FROM location_property_aux
140       WHERE place_id = in_place_id
141       INTO for_place_id,searchcountrycode, searchhousenumber, searchrankaddress,
142            searchpostcode, searchhousename, searchclass, searchtype;
143   END IF;
144   -- %NOAUXDATA% END IF;
145
146   -- postcode table
147   IF for_place_id IS NULL THEN
148     SELECT parent_place_id, country_code, rank_search, postcode, 'place', 'postcode'
149       FROM location_postcode
150       WHERE place_id = in_place_id
151       INTO for_place_id, searchcountrycode, searchrankaddress, searchpostcode,
152            searchclass, searchtype;
153   END IF;
154
155   -- POI objects in the placex table
156   IF for_place_id IS NULL THEN
157     SELECT parent_place_id, country_code, housenumber, rank_search,
158            postcode, address is not null and address ? 'postcode',
159            name, class, type,
160            address -> '_unlisted_place' as unlisted_place
161       FROM placex
162       WHERE place_id = in_place_id and rank_search > 27
163       INTO for_place_id, searchcountrycode, searchhousenumber, searchrankaddress,
164            searchpostcode, postcode_isexact, searchhousename, searchclass,
165            searchtype, search_unlisted_place;
166   END IF;
167
168   -- If for_place_id is still NULL at this point then the object has its own
169   -- entry in place_address line. However, still check if there is not linked
170   -- place we should be using instead.
171   IF for_place_id IS NULL THEN
172     select coalesce(linked_place_id, place_id),  country_code,
173            housenumber, rank_search, postcode,
174            address is not null and address ? 'postcode', null
175       from placex where place_id = in_place_id
176       INTO for_place_id, searchcountrycode, searchhousenumber, searchrankaddress, searchpostcode, postcode_isexact, searchhousename;
177   END IF;
178
179 --RAISE WARNING '% % % %',searchcountrycode, searchhousenumber, searchrankaddress, searchpostcode;
180
181   found := 1000; -- the lowest rank_address included
182
183   -- Return the record for the base entry.
184   FOR location IN
185     SELECT placex.place_id, osm_type, osm_id, name,
186            coalesce(extratags->'linked_place', extratags->'place') as place_type,
187            class, type, admin_level,
188            type not in ('postcode', 'postal_code') as isaddress,
189            CASE WHEN rank_address = 0 THEN 100
190                 WHEN rank_address = 11 THEN 5
191                 ELSE rank_address END as rank_address,
192            0 as distance, country_code, postcode
193       FROM placex
194       WHERE place_id = for_place_id
195   LOOP
196 --RAISE WARNING '%',location;
197     IF searchcountrycode IS NULL AND location.country_code IS NOT NULL THEN
198       searchcountrycode := location.country_code;
199     END IF;
200     IF location.rank_address < 4 THEN
201       -- no country locations for ranks higher than country
202       searchcountrycode := NULL;
203     END IF;
204     countrylocation := ROW(location.place_id, location.osm_type, location.osm_id,
205                            location.name, location.class, location.type,
206                            location.place_type,
207                            location.admin_level, true, location.isaddress,
208                            location.rank_address, location.distance)::addressline;
209     RETURN NEXT countrylocation;
210     found := location.rank_address;
211   END LOOP;
212
213   FOR location IN
214     SELECT placex.place_id, osm_type, osm_id, name, class, type,
215            coalesce(extratags->'linked_place', extratags->'place') as place_type,
216            admin_level, fromarea, isaddress and linked_place_id is NULL as isaddress,
217            CASE WHEN rank_address = 11 THEN 5 ELSE rank_address END as rank_address,
218            distance, country_code, postcode
219       FROM place_addressline join placex on (address_place_id = placex.place_id)
220       WHERE place_addressline.place_id = for_place_id
221             AND (cached_rank_address >= 4 AND cached_rank_address < searchrankaddress)
222             AND linked_place_id is null
223             AND (placex.country_code IS NULL OR searchcountrycode IS NULL
224                  OR placex.country_code = searchcountrycode)
225       ORDER BY rank_address desc, isaddress desc, fromarea desc,
226                distance asc, rank_search desc
227   LOOP
228 --RAISE WARNING '%',location;
229     IF searchcountrycode IS NULL AND location.country_code IS NOT NULL THEN
230       searchcountrycode := location.country_code;
231     END IF;
232     IF location.type in ('postcode', 'postal_code')
233        AND searchpostcode is not null
234     THEN
235       -- If the place had a postcode assigned, take this one only
236       -- into consideration when it is an area and the place does not have
237       -- a postcode itself.
238       IF location.fromarea AND not postcode_isexact AND location.isaddress THEN
239         searchpostcode := null; -- remove the less exact postcode
240       ELSE
241         location.isaddress := false;
242       END IF;
243     END IF;
244     countrylocation := ROW(location.place_id, location.osm_type, location.osm_id,
245                            location.name, location.class, location.type,
246                            location.place_type,
247                            location.admin_level, location.fromarea,
248                            location.isaddress, location.rank_address,
249                            location.distance)::addressline;
250     RETURN NEXT countrylocation;
251     found := location.rank_address;
252   END LOOP;
253
254   -- If no country was included yet, add the name information from country_name.
255   IF found > 4 THEN
256     SELECT name FROM country_name
257       WHERE country_code = searchcountrycode LIMIT 1 INTO countryname;
258 --RAISE WARNING '% % %',found,searchcountrycode,countryname;
259     IF countryname IS NOT NULL THEN
260       location := ROW(null, null, null, countryname, 'place', 'country', NULL,
261                       null, true, true, 4, 0)::addressline;
262       RETURN NEXT location;
263     END IF;
264   END IF;
265
266   -- Finally add some artificial rows.
267   IF searchcountrycode IS NOT NULL THEN
268     location := ROW(null, null, null, hstore('ref', searchcountrycode),
269                     'place', 'country_code', null, null, true, false, 4, 0)::addressline;
270     RETURN NEXT location;
271   END IF;
272
273   IF searchhousename IS NOT NULL THEN
274     location := ROW(in_place_id, null, null, searchhousename, searchclass,
275                     searchtype, null, null, true, true, 29, 0)::addressline;
276     RETURN NEXT location;
277   END IF;
278
279   IF searchhousenumber IS NOT NULL THEN
280     location := ROW(null, null, null, hstore('ref', searchhousenumber),
281                     'place', 'house_number', null, null, true, true, 28, 0)::addressline;
282     RETURN NEXT location;
283   END IF;
284
285   IF search_unlisted_place is not null THEN
286     RETURN NEXT ROW(null, null, null, hstore('name', search_unlisted_place),
287                     'place', 'locality', null, null, true, true, 26, 0)::addressline;
288   END IF;
289
290   IF searchpostcode IS NOT NULL THEN
291     location := ROW(null, null, null, hstore('ref', searchpostcode), 'place',
292                     'postcode', null, null, false, true, 5, 0)::addressline;
293     RETURN NEXT location;
294   END IF;
295
296   RETURN;
297 END;
298 $$
299 LANGUAGE plpgsql STABLE;