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 address interpolation objects in location_property_osmline.
11 CREATE OR REPLACE FUNCTION get_interpolation_address(in_address HSTORE, wayid BIGINT)
18 IF in_address ? 'street' or in_address ? 'place' THEN
22 SELECT nodes INTO waynodes FROM planet_osm_ways WHERE id = wayid;
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
30 -- mark it as a derived address
31 RETURN location.address || in_address || hstore('_inherited', '');
37 LANGUAGE plpgsql STABLE;
41 -- find the parent road of the cut road parts
42 CREATE OR REPLACE FUNCTION get_interpolation_parent(token_info JSONB,
44 centroid GEOMETRY, geom GEOMETRY)
48 parent_place_id BIGINT;
51 parent_place_id := find_parent_for_address(token_info, partition, centroid);
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
66 parent_place_id := location.place_id;
70 IF parent_place_id is null THEN
74 RETURN parent_place_id;
77 LANGUAGE plpgsql STABLE;
80 CREATE OR REPLACE FUNCTION reinsert_interpolation(way_id BIGINT, addr HSTORE,
87 IF addr is NULL OR NOT addr ? 'interpolation'
88 OR NOT (addr->'interpolation' in ('odd', 'even', 'all')
89 or addr->'interpolation' similar to '[1-9]')
91 -- the new interpolation is illegal, simply remove existing entries
92 DELETE FROM location_property_osmline WHERE osm_id = way_id;
94 -- Get the existing entry from the interpolation table.
95 SELECT array_agg(place_id) INTO existing
96 FROM location_property_osmline WHERE osm_id = way_id;
98 IF existing IS NULL or array_length(existing, 1) = 0 THEN
99 INSERT INTO location_property_osmline (osm_id, address, linegeo)
100 VALUES (way_id, addr, geom);
102 -- Update the interpolation table:
103 -- The first entry gets the original data, all other entries
104 -- are removed and will be recreated on indexing.
105 -- (An interpolation can be split up, if it has more than 2 address nodes)
106 UPDATE location_property_osmline
111 WHERE place_id = existing[1];
112 IF array_length(existing, 1) > 1 THEN
113 DELETE FROM location_property_osmline
114 WHERE place_id = any(existing[2:]);
125 CREATE OR REPLACE FUNCTION osmline_insert()
129 NEW.place_id := nextval('seq_place');
130 NEW.indexed_date := now();
132 IF NEW.indexed_status IS NULL THEN
133 IF NEW.address is NULL OR NOT NEW.address ? 'interpolation'
134 OR NOT (NEW.address->'interpolation' in ('odd', 'even', 'all')
135 or NEW.address->'interpolation' similar to '[1-9]')
137 -- alphabetic interpolation is not supported
141 NEW.indexed_status := 1; --STATUS_NEW
142 NEW.country_code := lower(get_country_code(NEW.linegeo));
144 NEW.partition := get_partition(NEW.country_code);
145 NEW.geometry_sector := geometry_sector(NEW.partition, NEW.linegeo);
154 CREATE OR REPLACE FUNCTION osmline_update()
173 IF OLD.indexed_status = 100 THEN
174 delete from location_property_osmline where place_id = OLD.place_id;
178 IF NEW.indexed_status != 0 OR OLD.indexed_status = 0 THEN
182 NEW.parent_place_id := get_interpolation_parent(NEW.token_info, NEW.partition,
183 ST_PointOnSurface(NEW.linegeo),
186 NEW.token_info := token_strip_info(NEW.token_info);
187 IF NEW.address ? '_inherited' THEN
188 NEW.address := hstore('interpolation', NEW.address->'interpolation');
191 -- If the line was newly inserted, split the line as necessary.
192 IF OLD.indexed_status = 1 THEN
193 IF NEW.address->'interpolation' in ('odd', 'even') THEN
195 stepmod := CASE WHEN NEW.address->'interpolation' = 'odd' THEN 1 ELSE 0 END;
197 NEW.step := CASE WHEN NEW.address->'interpolation' = 'all'
199 ELSE (NEW.address->'interpolation')::SMALLINT END;
203 SELECT nodes INTO waynodes
204 FROM planet_osm_ways WHERE id = NEW.osm_id;
206 IF array_upper(waynodes, 1) IS NULL THEN
211 SELECT null::integer as hnr INTO prevnode;
213 -- Go through all nodes on the interpolation line that have a housenumber.
215 SELECT DISTINCT ON (nodeidpos)
216 osm_id, address, geometry,
217 -- Take the postcode from the node only if it has a housenumber itself.
218 -- Note that there is a corner-case where the node has a wrongly
219 -- formatted postcode and therefore 'postcode' contains a derived
221 CASE WHEN address ? 'postcode' THEN placex.postcode ELSE NULL::text END as postcode,
222 substring(address->'housenumber','[0-9]+')::integer as hnr
223 FROM placex, generate_series(1, array_upper(waynodes, 1)) nodeidpos
224 WHERE osm_type = 'N' and osm_id = waynodes[nodeidpos]::BIGINT
225 and address is not NULL and address ? 'housenumber'
226 and ST_Distance(NEW.linegeo, geometry) < 0.0005
229 {% if debug %}RAISE WARNING 'processing point % (%)', nextnode.hnr, ST_AsText(nextnode.geometry);{% endif %}
230 IF linegeo is null THEN
231 linegeo := NEW.linegeo;
233 splitpoint := ST_LineLocatePoint(linegeo, nextnode.geometry);
234 IF splitpoint = 0 THEN
235 -- Corner case where the splitpoint falls on the first point
236 -- and thus would not return a geometry. Skip that section.
238 ELSEIF splitpoint = 1 THEN
239 -- Point is at the end of the line.
240 sectiongeo := linegeo;
244 sectiongeo := ST_LineSubstring(linegeo, 0, splitpoint);
245 linegeo := ST_LineSubstring(linegeo, splitpoint, 1);
249 IF prevnode.hnr is not null
250 -- Check if there are housenumbers to interpolate between the
251 -- regularly mapped housenumbers.
252 -- (Conveniently also fails if one of the house numbers is not a number.)
253 and abs(prevnode.hnr - nextnode.hnr) > NEW.step
254 -- If the interpolation geometry is broken or two nodes are at the
255 -- same place, then splitting might produce a point. Ignore that.
256 and ST_GeometryType(sectiongeo) = 'ST_LineString'
258 IF prevnode.hnr < nextnode.hnr THEN
259 startnumber := prevnode.hnr;
260 endnumber := nextnode.hnr;
262 startnumber := nextnode.hnr;
263 endnumber := prevnode.hnr;
264 sectiongeo := ST_Reverse(sectiongeo);
267 -- Adjust the interpolation, so that only inner housenumbers
268 -- are taken into account.
269 IF stepmod is null THEN
270 newstart := startnumber + NEW.step;
272 newstart := startnumber + 1;
273 moddiff := newstart % NEW.step - stepmod;
275 newstart := newstart + (NEW.step + moddiff);
277 newstart := newstart + moddiff;
280 newend := newstart + ((endnumber - 1 - newstart) / NEW.step) * NEW.step;
282 -- If newstart and newend are the same, then this returns a point.
283 sectiongeo := ST_LineSubstring(sectiongeo,
284 (newstart - startnumber)::float / (endnumber - startnumber)::float,
285 (newend - startnumber)::float / (endnumber - startnumber)::float);
286 startnumber := newstart;
289 -- determine postcode
290 postcode := coalesce(prevnode.postcode, nextnode.postcode, postcode);
291 IF postcode is NULL and NEW.parent_place_id > 0 THEN
292 SELECT placex.postcode FROM placex
293 WHERE place_id = NEW.parent_place_id INTO postcode;
295 IF postcode is NULL THEN
296 postcode := get_nearest_postcode(NEW.country_code, nextnode.geometry);
299 -- Add the interpolation. If this is the first segment, just modify
300 -- the interpolation to be inserted, otherwise add an additional one
301 -- (marking it indexed already).
302 IF NEW.startnumber IS NULL THEN
303 NEW.startnumber := startnumber;
304 NEW.endnumber := endnumber;
305 NEW.linegeo := sectiongeo;
306 NEW.postcode := postcode;
308 INSERT INTO location_property_osmline
309 (linegeo, partition, osm_id, parent_place_id,
310 startnumber, endnumber, step,
311 address, postcode, country_code,
312 geometry_sector, indexed_status)
313 VALUES (sectiongeo, NEW.partition, NEW.osm_id, NEW.parent_place_id,
314 startnumber, endnumber, NEW.step,
315 NEW.address, postcode,
316 NEW.country_code, NEW.geometry_sector, 0);
320 -- early break if we are out of line string,
321 -- might happen when a line string loops back on itself
322 IF linegeo is null or ST_GeometryType(linegeo) != 'ST_LineString' THEN
326 prevnode := nextnode;