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   -- Get the existing entry from the interpolation table.
 
  88   SELECT array_agg(place_id) INTO existing
 
  89     FROM location_property_osmline WHERE osm_id = way_id;
 
  91   IF existing IS NULL or array_length(existing, 1) = 0 THEN
 
  92     INSERT INTO location_property_osmline (osm_id, address, linegeo)
 
  93       VALUES (way_id, addr, geom);
 
  95     -- Update the interpolation table:
 
  96     --   The first entry gets the original data, all other entries
 
  97     --   are removed and will be recreated on indexing.
 
  98     --   (An interpolation can be split up, if it has more than 2 address nodes)
 
  99     UPDATE location_property_osmline
 
 104       WHERE place_id = existing[1];
 
 105     IF array_length(existing, 1) > 1 THEN
 
 106       DELETE FROM location_property_osmline
 
 107         WHERE place_id = any(existing[2:]);
 
 117 CREATE OR REPLACE FUNCTION osmline_insert()
 
 121   NEW.place_id := nextval('seq_place');
 
 122   NEW.indexed_date := now();
 
 124   IF NEW.indexed_status IS NULL THEN
 
 125       IF NEW.address is NULL OR NOT NEW.address ? 'interpolation'
 
 126          OR NOT (NEW.address->'interpolation' in ('odd', 'even', 'all')
 
 127                  or NEW.address->'interpolation' similar to '[1-9]')
 
 129           -- alphabetic interpolation is not supported
 
 133       NEW.indexed_status := 1; --STATUS_NEW
 
 134       NEW.country_code := lower(get_country_code(NEW.linegeo));
 
 136       NEW.partition := get_partition(NEW.country_code);
 
 137       NEW.geometry_sector := geometry_sector(NEW.partition, NEW.linegeo);
 
 146 CREATE OR REPLACE FUNCTION osmline_update()
 
 165   IF OLD.indexed_status = 100 THEN
 
 166     delete from location_property_osmline where place_id = OLD.place_id;
 
 170   IF NEW.indexed_status != 0 OR OLD.indexed_status = 0 THEN
 
 174   NEW.parent_place_id := get_interpolation_parent(NEW.token_info, NEW.partition,
 
 175                                                  ST_PointOnSurface(NEW.linegeo),
 
 178   NEW.token_info := token_strip_info(NEW.token_info);
 
 179   IF NEW.address ? '_inherited' THEN
 
 180     NEW.address := hstore('interpolation', NEW.address->'interpolation');
 
 183   -- If the line was newly inserted, split the line as necessary.
 
 184   IF OLD.indexed_status = 1 THEN
 
 185     IF NEW.address->'interpolation' in ('odd', 'even') THEN
 
 187       stepmod := CASE WHEN NEW.address->'interpolation' = 'odd' THEN 1 ELSE 0 END;
 
 189       NEW.step := CASE WHEN NEW.address->'interpolation' = 'all'
 
 191                        ELSE (NEW.address->'interpolation')::SMALLINT END;
 
 195     SELECT nodes INTO waynodes
 
 196       FROM planet_osm_ways WHERE id = NEW.osm_id;
 
 198     IF array_upper(waynodes, 1) IS NULL THEN
 
 203     SELECT null::integer as hnr INTO prevnode;
 
 205     -- Go through all nodes on the interpolation line that have a housenumber.
 
 207       SELECT DISTINCT ON (nodeidpos)
 
 208           osm_id, address, geometry,
 
 209           -- Take the postcode from the node only if it has a housenumber itself.
 
 210           -- Note that there is a corner-case where the node has a wrongly
 
 211           -- formatted postcode and therefore 'postcode' contains a derived
 
 213           CASE WHEN address ? 'postcode' THEN placex.postcode ELSE NULL::text END as postcode,
 
 214           substring(address->'housenumber','[0-9]+')::integer as hnr
 
 215         FROM placex, generate_series(1, array_upper(waynodes, 1)) nodeidpos
 
 216         WHERE osm_type = 'N' and osm_id = waynodes[nodeidpos]::BIGINT
 
 217               and address is not NULL and address ? 'housenumber'
 
 220       {% if debug %}RAISE WARNING 'processing point % (%)', nextnode.hnr, ST_AsText(nextnode.geometry);{% endif %}
 
 221       IF linegeo is null THEN
 
 222         linegeo := NEW.linegeo;
 
 224         splitline := ST_Split(ST_Snap(linegeo, nextnode.geometry, 0.0005), nextnode.geometry);
 
 225         sectiongeo := ST_GeometryN(splitline, 1);
 
 226         linegeo := ST_GeometryN(splitline, 2);
 
 229       IF prevnode.hnr is not null
 
 230          -- Check if there are housenumbers to interpolate between the
 
 231          -- regularly mapped housenumbers.
 
 232          -- (Conveniently also fails if one of the house numbers is not a number.)
 
 233          and abs(prevnode.hnr - nextnode.hnr) > NEW.step
 
 235         IF prevnode.hnr < nextnode.hnr THEN
 
 236           startnumber := prevnode.hnr;
 
 237           endnumber := nextnode.hnr;
 
 239           startnumber := nextnode.hnr;
 
 240           endnumber := prevnode.hnr;
 
 241           sectiongeo := ST_Reverse(sectiongeo);
 
 244         -- Adjust the interpolation, so that only inner housenumbers
 
 245         -- are taken into account.
 
 246         IF stepmod is null THEN
 
 247           newstart := startnumber + NEW.step;
 
 249           newstart := startnumber + 1;
 
 250           moddiff := newstart % NEW.step - stepmod;
 
 252             newstart := newstart + (NEW.step + moddiff);
 
 254             newstart := newstart + moddiff;
 
 257         newend := newstart + ((endnumber - 1 - newstart) / NEW.step) * NEW.step;
 
 259         -- If newstart and newend are the same, then this returns a point.
 
 260         sectiongeo := ST_LineSubstring(sectiongeo,
 
 261                               (newstart - startnumber)::float / (endnumber - startnumber)::float,
 
 262                               (newend - startnumber)::float / (endnumber - startnumber)::float);
 
 263         startnumber := newstart;
 
 266         -- determine postcode
 
 267         postcode := coalesce(prevnode.postcode, nextnode.postcode, postcode);
 
 268         IF postcode is NULL and NEW.parent_place_id > 0 THEN
 
 269             SELECT placex.postcode FROM placex
 
 270               WHERE place_id = NEW.parent_place_id INTO postcode;
 
 272         IF postcode is NULL THEN
 
 273             postcode := get_nearest_postcode(NEW.country_code, nextnode.geometry);
 
 276         -- Add the interpolation. If this is the first segment, just modify
 
 277         -- the interpolation to be inserted, otherwise add an additional one
 
 278         -- (marking it indexed already).
 
 279         IF NEW.startnumber IS NULL THEN
 
 280             NEW.startnumber := startnumber;
 
 281             NEW.endnumber := endnumber;
 
 282             NEW.linegeo := sectiongeo;
 
 283             NEW.postcode := postcode;
 
 285           INSERT INTO location_property_osmline
 
 286                  (linegeo, partition, osm_id, parent_place_id,
 
 287                   startnumber, endnumber, step,
 
 288                   address, postcode, country_code,
 
 289                   geometry_sector, indexed_status)
 
 290           VALUES (sectiongeo, NEW.partition, NEW.osm_id, NEW.parent_place_id,
 
 291                   startnumber, endnumber, NEW.step,
 
 292                   NEW.address, postcode,
 
 293                   NEW.country_code, NEW.geometry_sector, 0);
 
 296         -- early break if we are out of line string,
 
 297         -- might happen when a line string loops back on itself
 
 298         IF ST_GeometryType(linegeo) != 'ST_LineString' THEN
 
 303       prevnode := nextnode;