]> git.openstreetmap.org Git - nominatim.git/blob - lib-sql/functions/interpolation.sql
ignore very long ways
[nominatim.git] / lib-sql / functions / interpolation.sql
1 -- SPDX-License-Identifier: GPL-2.0-only
2 --
3 -- This file is part of Nominatim. (https://nominatim.org)
4 --
5 -- Copyright (C) 2022 by the Nominatim developer community.
6 -- For a full list of authors see the git log.
7
8 -- Functions for address interpolation objects in location_property_osmline.
9
10
11 CREATE OR REPLACE FUNCTION get_interpolation_address(in_address HSTORE, wayid BIGINT)
12 RETURNS HSTORE
13   AS $$
14 DECLARE
15   location RECORD;
16   waynodes BIGINT[];
17 BEGIN
18   IF in_address ? 'street' or in_address ? 'place' THEN
19     RETURN in_address;
20   END IF;
21
22   SELECT nodes INTO waynodes FROM planet_osm_ways WHERE id = wayid;
23   FOR location IN
24     SELECT placex.address, placex.osm_id FROM placex
25      WHERE osm_type = 'N' and osm_id = ANY(waynodes)
26            and placex.address is not null
27            and (placex.address ? 'street' or placex.address ? 'place')
28            and indexed_status < 100
29   LOOP
30     -- mark it as a derived address
31     RETURN location.address || in_address || hstore('_inherited', '');
32   END LOOP;
33
34   RETURN in_address;
35 END;
36 $$
37 LANGUAGE plpgsql STABLE;
38
39
40
41 -- find the parent road of the cut road parts
42 CREATE OR REPLACE FUNCTION get_interpolation_parent(token_info JSONB,
43                                                     partition SMALLINT,
44                                                     centroid GEOMETRY, geom GEOMETRY)
45   RETURNS BIGINT
46   AS $$
47 DECLARE
48   parent_place_id BIGINT;
49   location RECORD;
50 BEGIN
51   parent_place_id := find_parent_for_address(token_info, partition, centroid);
52
53   IF parent_place_id is null THEN
54     FOR location IN SELECT place_id FROM placex
55         WHERE ST_DWithin(geom, placex.geometry, 0.001)
56               and placex.rank_search = 26
57               and placex.osm_type = 'W' -- needed for index selection
58         ORDER BY CASE WHEN ST_GeometryType(geom) = 'ST_Line' THEN
59                   (ST_distance(placex.geometry, ST_LineInterpolatePoint(geom,0))+
60                   ST_distance(placex.geometry, ST_LineInterpolatePoint(geom,0.5))+
61                   ST_distance(placex.geometry, ST_LineInterpolatePoint(geom,1)))
62                  ELSE ST_distance(placex.geometry, geom) END
63               ASC
64         LIMIT 1
65     LOOP
66       parent_place_id := location.place_id;
67     END LOOP;
68   END IF;
69
70   RETURN parent_place_id;
71 END;
72 $$
73 LANGUAGE plpgsql STABLE;
74
75
76 CREATE OR REPLACE FUNCTION reinsert_interpolation(way_id BIGINT, addr HSTORE,
77                                                   geom GEOMETRY)
78   RETURNS INT
79   AS $$
80 DECLARE
81   existing BIGINT[];
82 BEGIN
83   IF addr is NULL OR NOT addr ? 'interpolation'
84          OR NOT (addr->'interpolation' in ('odd', 'even', 'all')
85                  or addr->'interpolation' similar to '[1-9]')
86   THEN
87     -- the new interpolation is illegal, simply remove existing entries
88     DELETE FROM location_property_osmline WHERE osm_id = way_id;
89   ELSE
90     -- Get the existing entry from the interpolation table.
91     SELECT array_agg(place_id) INTO existing
92       FROM location_property_osmline WHERE osm_id = way_id;
93
94     IF existing IS NULL or array_length(existing, 1) = 0 THEN
95       INSERT INTO location_property_osmline (osm_id, address, linegeo)
96         VALUES (way_id, addr, geom);
97     ELSE
98       -- Update the interpolation table:
99       --   The first entry gets the original data, all other entries
100       --   are removed and will be recreated on indexing.
101       --   (An interpolation can be split up, if it has more than 2 address nodes)
102       UPDATE location_property_osmline
103         SET address = addr,
104             linegeo = geom,
105             startnumber = null,
106             indexed_status = 1
107         WHERE place_id = existing[1];
108       IF array_length(existing, 1) > 1 THEN
109         DELETE FROM location_property_osmline
110           WHERE place_id = any(existing[2:]);
111       END IF;
112     END IF;
113   END IF;
114
115   RETURN 1;
116 END;
117 $$
118 LANGUAGE plpgsql;
119
120
121 CREATE OR REPLACE FUNCTION osmline_insert()
122   RETURNS TRIGGER
123   AS $$
124 BEGIN
125   NEW.place_id := nextval('seq_place');
126   NEW.indexed_date := now();
127
128   IF NEW.indexed_status IS NULL THEN
129       IF NEW.address is NULL OR NOT NEW.address ? 'interpolation'
130          OR NOT (NEW.address->'interpolation' in ('odd', 'even', 'all')
131                  or NEW.address->'interpolation' similar to '[1-9]')
132       THEN
133           -- alphabetic interpolation is not supported
134           RETURN NULL;
135       END IF;
136
137       NEW.indexed_status := 1; --STATUS_NEW
138       NEW.country_code := lower(get_country_code(NEW.linegeo));
139
140       NEW.partition := get_partition(NEW.country_code);
141       NEW.geometry_sector := geometry_sector(NEW.partition, NEW.linegeo);
142   END IF;
143
144   RETURN NEW;
145 END;
146 $$
147 LANGUAGE plpgsql;
148
149
150 CREATE OR REPLACE FUNCTION osmline_update()
151   RETURNS TRIGGER
152   AS $$
153 DECLARE
154   waynodes BIGINT[];
155   prevnode RECORD;
156   nextnode RECORD;
157   startnumber INTEGER;
158   endnumber INTEGER;
159   newstart INTEGER;
160   newend INTEGER;
161   moddiff SMALLINT;
162   linegeo GEOMETRY;
163   splitpoint FLOAT;
164   sectiongeo GEOMETRY;
165   postcode TEXT;
166   stepmod SMALLINT;
167 BEGIN
168   -- deferred delete
169   IF OLD.indexed_status = 100 THEN
170     delete from location_property_osmline where place_id = OLD.place_id;
171     RETURN NULL;
172   END IF;
173
174   IF NEW.indexed_status != 0 OR OLD.indexed_status = 0 THEN
175     RETURN NEW;
176   END IF;
177
178   NEW.parent_place_id := get_interpolation_parent(NEW.token_info, NEW.partition,
179                                                  ST_PointOnSurface(NEW.linegeo),
180                                                  NEW.linegeo);
181
182   -- Cannot find a parent street. We will not be able to display a reliable
183   -- address, so drop entire interpolation.
184   IF NEW.parent_place_id is NULL THEN
185     DELETE FROM location_property_osmline where place_id = OLD.place_id;
186     RETURN NULL;
187   END IF;
188
189   NEW.token_info := token_strip_info(NEW.token_info);
190   IF NEW.address ? '_inherited' THEN
191     NEW.address := hstore('interpolation', NEW.address->'interpolation');
192   END IF;
193
194   -- If the line was newly inserted, split the line as necessary.
195   IF OLD.indexed_status = 1 THEN
196     IF NEW.address->'interpolation' in ('odd', 'even') THEN
197       NEW.step := 2;
198       stepmod := CASE WHEN NEW.address->'interpolation' = 'odd' THEN 1 ELSE 0 END;
199     ELSE
200       NEW.step := CASE WHEN NEW.address->'interpolation' = 'all'
201                        THEN 1
202                        ELSE (NEW.address->'interpolation')::SMALLINT END;
203       stepmod := NULL;
204     END IF;
205
206     SELECT nodes INTO waynodes
207       FROM planet_osm_ways WHERE id = NEW.osm_id;
208
209     IF array_upper(waynodes, 1) IS NULL THEN
210       RETURN NEW;
211     END IF;
212
213     linegeo := null;
214     SELECT null::integer as hnr INTO prevnode;
215
216     -- Go through all nodes on the interpolation line that have a housenumber.
217     FOR nextnode IN
218       SELECT DISTINCT ON (nodeidpos)
219           osm_id, address, geometry,
220           -- Take the postcode from the node only if it has a housenumber itself.
221           -- Note that there is a corner-case where the node has a wrongly
222           -- formatted postcode and therefore 'postcode' contains a derived
223           -- variant.
224           CASE WHEN address ? 'postcode' THEN placex.postcode ELSE NULL::text END as postcode,
225           substring(address->'housenumber','[0-9]+')::integer as hnr
226         FROM placex, generate_series(1, array_upper(waynodes, 1)) nodeidpos
227         WHERE osm_type = 'N' and osm_id = waynodes[nodeidpos]::BIGINT
228               and address is not NULL and address ? 'housenumber'
229               and ST_Distance(NEW.linegeo, geometry) < 0.0005
230         ORDER BY nodeidpos
231     LOOP
232       {% if debug %}RAISE WARNING 'processing point % (%)', nextnode.hnr, ST_AsText(nextnode.geometry);{% endif %}
233       IF linegeo is null THEN
234         linegeo := NEW.linegeo;
235       ELSE
236         splitpoint := ST_LineLocatePoint(linegeo, nextnode.geometry);
237         IF splitpoint = 0 THEN
238           -- Corner case where the splitpoint falls on the first point
239           -- and thus would not return a geometry. Skip that section.
240           sectiongeo := NULL;
241         ELSEIF splitpoint = 1 THEN
242           -- Point is at the end of the line.
243           sectiongeo := linegeo;
244           linegeo := NULL;
245         ELSE
246           -- Split the line.
247           sectiongeo := ST_LineSubstring(linegeo, 0, splitpoint);
248           linegeo := ST_LineSubstring(linegeo, splitpoint, 1);
249         END IF;
250       END IF;
251
252       IF prevnode.hnr is not null
253          -- Check if there are housenumbers to interpolate between the
254          -- regularly mapped housenumbers.
255          -- (Conveniently also fails if one of the house numbers is not a number.)
256          and abs(prevnode.hnr - nextnode.hnr) > NEW.step
257          -- If the interpolation geometry is broken or two nodes are at the
258          -- same place, then splitting might produce a point. Ignore that.
259          and ST_GeometryType(sectiongeo) = 'ST_LineString'
260       THEN
261         IF prevnode.hnr < nextnode.hnr THEN
262           startnumber := prevnode.hnr;
263           endnumber := nextnode.hnr;
264         ELSE
265           startnumber := nextnode.hnr;
266           endnumber := prevnode.hnr;
267           sectiongeo := ST_Reverse(sectiongeo);
268         END IF;
269
270         -- Adjust the interpolation, so that only inner housenumbers
271         -- are taken into account.
272         IF stepmod is null THEN
273           newstart := startnumber + NEW.step;
274         ELSE
275           newstart := startnumber + 1;
276           moddiff := newstart % NEW.step - stepmod;
277           IF moddiff < 0 THEN
278             newstart := newstart + (NEW.step + moddiff);
279           ELSE
280             newstart := newstart + moddiff;
281           END IF;
282         END IF;
283         newend := newstart + ((endnumber - 1 - newstart) / NEW.step) * NEW.step;
284
285         -- If newstart and newend are the same, then this returns a point.
286         sectiongeo := ST_LineSubstring(sectiongeo,
287                               (newstart - startnumber)::float / (endnumber - startnumber)::float,
288                               (newend - startnumber)::float / (endnumber - startnumber)::float);
289         startnumber := newstart;
290         endnumber := newend;
291
292         -- determine postcode
293         postcode := coalesce(prevnode.postcode, nextnode.postcode, postcode);
294         IF postcode is NULL and NEW.parent_place_id > 0 THEN
295             SELECT placex.postcode FROM placex
296               WHERE place_id = NEW.parent_place_id INTO postcode;
297         END IF;
298         IF postcode is NULL THEN
299             postcode := get_nearest_postcode(NEW.country_code, nextnode.geometry);
300         END IF;
301
302         -- Add the interpolation. If this is the first segment, just modify
303         -- the interpolation to be inserted, otherwise add an additional one
304         -- (marking it indexed already).
305         IF NEW.startnumber IS NULL THEN
306             NEW.startnumber := startnumber;
307             NEW.endnumber := endnumber;
308             NEW.linegeo := sectiongeo;
309             NEW.postcode := postcode;
310         ELSE
311           INSERT INTO location_property_osmline
312                  (linegeo, partition, osm_id, parent_place_id,
313                   startnumber, endnumber, step,
314                   address, postcode, country_code,
315                   geometry_sector, indexed_status)
316           VALUES (sectiongeo, NEW.partition, NEW.osm_id, NEW.parent_place_id,
317                   startnumber, endnumber, NEW.step,
318                   NEW.address, postcode,
319                   NEW.country_code, NEW.geometry_sector, 0);
320         END IF;
321       END IF;
322
323       -- early break if we are out of line string,
324       -- might happen when a line string loops back on itself
325       IF linegeo is null or ST_GeometryType(linegeo) != 'ST_LineString' THEN
326           RETURN NEW;
327       END IF;
328
329       prevnode := nextnode;
330     END LOOP;
331   END IF;
332
333   RETURN NEW;
334 END;
335 $$
336 LANGUAGE plpgsql;