]> 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   place RECORD;
91   location RECORD;
92   current_rank_address INTEGER;
93   location_isaddress BOOLEAN;
94 BEGIN
95   -- The place in question might not have a direct entry in place_addressline.
96   -- Look for the parent of such places then and save it in place.
97
98   -- first query osmline (interpolation lines)
99   IF in_housenumber >= 0 THEN
100     SELECT parent_place_id as place_id, country_code,
101            in_housenumber::text as housenumber, postcode,
102            'place' as class, 'house' as type,
103            null::hstore as name, null::hstore as address,
104            ST_Centroid(linegeo) as centroid
105       INTO place
106       FROM location_property_osmline
107       WHERE place_id = in_place_id
108             AND in_housenumber between startnumber and endnumber;
109   END IF;
110
111   --then query tiger data
112   -- %NOTIGERDATA% IF 0 THEN
113   IF place IS NULL AND in_housenumber >= 0 THEN
114     SELECT parent_place_id as place_id, 'us' as country_code,
115            in_housenumber::text as housenumber, postcode,
116            'place' as class, 'house' as type,
117            null::hstore as name, null::hstore as address,
118            ST_Centroid(linegeo) as centroid
119       INTO place
120       FROM location_property_tiger
121       WHERE place_id = in_place_id
122             AND in_housenumber between startnumber and endnumber;
123   END IF;
124   -- %NOTIGERDATA% END IF;
125
126   -- %NOAUXDATA% IF 0 THEN
127   IF place IS NULL THEN
128     SELECT parent_place_id as place_id, 'us' as country_code,
129            housenumber, postcode,
130            'place' as class, 'house' as type,
131            null::hstore as name, null::hstore as address,
132            centroid
133       INTO place
134       FROM location_property_aux
135       WHERE place_id = in_place_id;
136   END IF;
137   -- %NOAUXDATA% END IF;
138
139   -- postcode table
140   IF place IS NULL THEN
141     SELECT parent_place_id as place_id, country_code,
142            null::text as housenumber, postcode,
143            'place' as class, 'postcode' as type,
144            null::hstore as name, null::hstore as address,
145            null::geometry as centroid
146       INTO place
147       FROM location_postcode
148       WHERE place_id = in_place_id;
149   END IF;
150
151   -- POI objects in the placex table
152   IF place IS NULL THEN
153     SELECT parent_place_id as place_id, country_code,
154            housenumber, postcode,
155            class, type,
156            name, address,
157            centroid
158       INTO place
159       FROM placex
160       WHERE place_id = in_place_id and rank_search > 27;
161   END IF;
162
163   -- If place is still NULL at this point then the object has its own
164   -- entry in place_address line. However, still check if there is not linked
165   -- place we should be using instead.
166   IF place IS NULL THEN
167     select coalesce(linked_place_id, place_id) as place_id,  country_code,
168            housenumber, postcode,
169            class, type,
170            null::hstore as name, address,
171            null::geometry as centroid
172       INTO place
173       FROM placex where place_id = in_place_id;
174   END IF;
175
176 --RAISE WARNING '% % % %',searchcountrycode, searchhousenumber, searchpostcode;
177
178   -- --- Return the record for the base entry.
179
180   FOR location IN
181     SELECT placex.place_id, osm_type, osm_id, name,
182            coalesce(extratags->'linked_place', extratags->'place') as place_type,
183            class, type, admin_level,
184            CASE WHEN rank_address = 0 THEN 100
185                 WHEN rank_address = 11 THEN 5
186                 ELSE rank_address END as rank_address,
187            country_code
188       FROM placex
189       WHERE place_id = place.place_id
190   LOOP
191 --RAISE WARNING '%',location;
192     IF location.rank_address < 4 THEN
193       -- no country locations for ranks higher than country
194       place.country_code := NULL;
195     ELSEIF place.country_code IS NULL AND location.country_code IS NOT NULL THEN
196       place.country_code := location.country_code;
197     END IF;
198
199     RETURN NEXT ROW(location.place_id, location.osm_type, location.osm_id,
200                     location.name, location.class, location.type,
201                     location.place_type,
202                     location.admin_level, true,
203                     location.type not in ('postcode', 'postal_code'),
204                     location.rank_address, 0)::addressline;
205
206     current_rank_address := location.rank_address;
207   END LOOP;
208
209   -- --- Return records for address parts.
210
211   FOR location IN
212     SELECT placex.place_id, osm_type, osm_id, name, class, type,
213            coalesce(extratags->'linked_place', extratags->'place') as place_type,
214            admin_level, fromarea, isaddress and linked_place_id is NULL as isaddress,
215            CASE WHEN rank_address = 11 THEN 5 ELSE rank_address END as rank_address,
216            distance, country_code, postcode
217       FROM place_addressline join placex on (address_place_id = placex.place_id)
218       WHERE place_addressline.place_id IN (place.place_id, in_place_id)
219             AND linked_place_id is null
220             AND (placex.country_code IS NULL OR place.country_code IS NULL
221                  OR placex.country_code = place.country_code)
222       ORDER BY rank_address desc,
223                (place_addressline.place_id = in_place_id) desc,
224                (fromarea and place.centroid is not null and not isaddress
225                 and (place.address is null or avals(name) && avals(place.address))
226                 and ST_Contains(geometry, place.centroid)) desc,
227                isaddress desc, fromarea desc,
228                distance asc, rank_search desc
229   LOOP
230     -- RAISE WARNING '%',location;
231     location_isaddress := location.rank_address != current_rank_address;
232
233     IF place.country_code IS NULL AND location.country_code IS NOT NULL THEN
234       place.country_code := location.country_code;
235     END IF;
236     IF location.type in ('postcode', 'postal_code')
237        AND place.postcode is not null
238     THEN
239       -- If the place had a postcode assigned, take this one only
240       -- into consideration when it is an area and the place does not have
241       -- a postcode itself.
242       IF location.fromarea AND location.isaddress
243          AND (place.address is null or not place.address ? 'postcode')
244       THEN
245         place.postcode := null; -- remove the less exact postcode
246       ELSE
247         location_isaddress := false;
248       END IF;
249     END IF;
250     RETURN NEXT ROW(location.place_id, location.osm_type, location.osm_id,
251                     location.name, location.class, location.type,
252                     location.place_type,
253                     location.admin_level, location.fromarea,
254                     location_isaddress,
255                     location.rank_address,
256                     location.distance)::addressline;
257
258     current_rank_address := location.rank_address;
259   END LOOP;
260
261   -- If no country was included yet, add the name information from country_name.
262   IF current_rank_address > 4 THEN
263     FOR location IN
264       SELECT name FROM country_name WHERE country_code = place.country_code LIMIT 1
265     LOOP
266 --RAISE WARNING '% % %',current_rank_address,searchcountrycode,countryname;
267       RETURN NEXT ROW(null, null, null, location.name, 'place', 'country', NULL,
268                       null, true, true, 4, 0)::addressline;
269     END LOOP;
270   END IF;
271
272   -- Finally add some artificial rows.
273   IF place.country_code IS NOT NULL THEN
274     location := ROW(null, null, null, hstore('ref', place.country_code),
275                     'place', 'country_code', null, null, true, false, 4, 0)::addressline;
276     RETURN NEXT location;
277   END IF;
278
279   IF place.name IS NOT NULL THEN
280     location := ROW(in_place_id, null, null, place.name, place.class,
281                     place.type, null, null, true, true, 29, 0)::addressline;
282     RETURN NEXT location;
283   END IF;
284
285   IF place.housenumber IS NOT NULL THEN
286     location := ROW(null, null, null, hstore('ref', place.housenumber),
287                     'place', 'house_number', null, null, true, true, 28, 0)::addressline;
288     RETURN NEXT location;
289   END IF;
290
291   IF place.address is not null and place.address ? '_unlisted_place' THEN
292     RETURN NEXT ROW(null, null, null, hstore('name', place.address->'_unlisted_place'),
293                     'place', 'locality', null, null, true, true, 25, 0)::addressline;
294   END IF;
295
296   IF place.postcode is not null THEN
297     location := ROW(null, null, null, hstore('ref', place.postcode), 'place',
298                     'postcode', null, null, false, true, 5, 0)::addressline;
299     RETURN NEXT location;
300   END IF;
301
302   RETURN;
303 END;
304 $$
305 LANGUAGE plpgsql STABLE;