1 -- SPDX-License-Identifier: GPL-2.0-only
3 -- This file is part of Nominatim. (https://nominatim.org)
5 -- Copyright (C) 2025 by the Nominatim developer community.
6 -- For a full list of authors see the git log.
8 -- Assorted helper functions for the triggers.
10 CREATE OR REPLACE FUNCTION get_center_point(place GEOMETRY)
16 geom_type := ST_GeometryType(place);
17 IF geom_type = 'ST_Point' THEN
20 IF geom_type = 'ST_LineString' THEN
21 RETURN ST_ReducePrecision(ST_LineInterpolatePoint(place, 0.5), 0.0000001);
24 RETURN ST_ReducePrecision(ST_PointOnSurface(place), 0.0000001);
27 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
30 CREATE OR REPLACE FUNCTION geometry_sector(partition INTEGER, place GEOMETRY)
34 RETURN (partition*1000000) + (500-ST_X(place)::INTEGER)*1000 + (500-ST_Y(place)::INTEGER);
37 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
41 CREATE OR REPLACE FUNCTION array_merge(a INTEGER[], b INTEGER[])
48 IF array_upper(a, 1) IS NULL THEN
51 IF array_upper(b, 1) IS NULL THEN
55 FOR i IN 1..array_upper(b, 1) LOOP
56 IF NOT (ARRAY[b[i]] <@ r) THEN
63 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
65 -- Return the node members with a given label from a relation member list
68 -- \param members Member list in osm2pgsql middle format.
69 -- \param memberLabels Array of labels to accept.
71 -- \returns Set of OSM ids of nodes that are found.
73 CREATE OR REPLACE FUNCTION get_rel_node_members(members TEXT[],
80 FOR i IN 1..ARRAY_UPPER(members,1) BY 2 LOOP
81 IF members[i+1] = ANY(memberLabels)
82 AND upper(substring(members[i], 1, 1))::char(1) = 'N'
84 RETURN NEXT substring(members[i], 2)::bigint;
91 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
94 CREATE OR REPLACE FUNCTION get_rel_node_members(members JSONB, memberLabels TEXT[])
100 FOR member IN SELECT * FROM jsonb_array_elements(members)
102 IF member->>'type' = 'N' and member->>'role' = ANY(memberLabels) THEN
103 RETURN NEXT (member->>'ref')::bigint;
110 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
113 -- Copy 'name' to or from the default language.
115 -- \param country_code Country code of the object being named.
116 -- \param[inout] name List of names of the object.
118 -- If the country named by country_code has a single default language,
119 -- then a `name` tag is copied to `name:<country_code>` if this tag does
120 -- not yet exist and vice versa.
121 CREATE OR REPLACE FUNCTION add_default_place_name(country_code VARCHAR(2),
125 default_language VARCHAR(10);
127 IF name is not null AND array_upper(akeys(name),1) > 1 THEN
128 default_language := get_country_language_code(country_code);
129 IF default_language IS NOT NULL THEN
130 IF name ? 'name' AND NOT name ? ('name:'||default_language) THEN
131 name := name || hstore(('name:'||default_language), (name -> 'name'));
132 ELSEIF name ? ('name:'||default_language) AND NOT name ? 'name' THEN
133 name := name || hstore('name', (name -> ('name:'||default_language)));
139 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
142 -- Find the best-matching postcode for the given geometry
143 CREATE OR REPLACE FUNCTION get_nearest_postcode(country VARCHAR(2), geom GEOMETRY)
151 -- If the geometry is an area then only one postcode must be within
152 -- that area, otherwise consider the area as not having a postcode.
153 IF ST_GeometryType(geom) in ('ST_Polygon','ST_MultiPolygon') THEN
154 SELECT min(postcode), count(*) FROM
155 (SELECT postcode FROM location_postcodes
156 WHERE geom && location_postcodes.geometry -- want to use the index
157 AND ST_Contains(geom, location_postcodes.centroid)
158 AND country_code = country
169 -- Otherwise: be fully within the coverage area of a postcode
172 FROM location_postcodes p
173 WHERE ST_Covers(p.geometry, geom)
174 AND p.country_code = country
175 ORDER BY osm_id is null, ST_Distance(p.centroid, geom)
178 RETURN location.postcode;
184 LANGUAGE plpgsql STABLE PARALLEL SAFE;
187 CREATE OR REPLACE FUNCTION get_country_code(place geometry)
194 -- RAISE WARNING 'get_country_code, start: %', ST_AsText(place);
196 -- Try for a OSM polygon
197 SELECT array_agg(country_code) FROM location_area_country
198 WHERE country_code is not null and st_covers(geometry, place)
201 IF array_length(countries, 1) = 1 THEN
205 IF array_length(countries, 1) > 1 THEN
206 -- more than one country found, confirm against the fallback data what to choose
208 SELECT country_code FROM country_osm_grid
209 WHERE ST_Covers(geometry, place) AND country_code = ANY(countries)
212 RETURN nearcountry.country_code;
214 -- Still nothing? Choose the country code with the smallest partition number.
215 -- And failing that, just go by the alphabet.
218 (SELECT partition FROM country_name WHERE country_code = cc) as partition
219 FROM unnest(countries) cc
220 ORDER BY partition, cc
222 RETURN nearcountry.cc;
225 -- Should never be reached.
229 -- RAISE WARNING 'osm fallback: %', ST_AsText(place);
231 -- Try for OSM fallback data
232 -- The order is to deal with places like HongKong that are 'states' within another polygon
234 SELECT country_code from country_osm_grid
235 WHERE st_covers(geometry, place) order by area asc limit 1
237 RETURN nearcountry.country_code;
240 -- RAISE WARNING 'near osm fallback: %', ST_AsText(place);
245 LANGUAGE plpgsql STABLE PARALLEL SAFE;
248 CREATE OR REPLACE FUNCTION get_country_language_code(search_country_code VARCHAR(2))
255 SELECT distinct country_default_language_code from country_name
256 WHERE country_code = search_country_code limit 1
258 RETURN lower(nearcountry.country_default_language_code);
263 LANGUAGE plpgsql STABLE PARALLEL SAFE;
266 CREATE OR REPLACE FUNCTION get_partition(in_country_code VARCHAR(10))
273 SELECT partition from country_name where country_code = in_country_code
275 RETURN nearcountry.partition;
280 LANGUAGE plpgsql STABLE PARALLEL SAFE;
283 -- Find the parent of an address with addr:street/addr:place tag.
285 -- \param token_info Naming info with the address information.
286 -- \param partition Partition where to search the parent.
287 -- \param centroid Location of the address.
289 -- \return Place ID of the parent if one was found, NULL otherwise.
290 CREATE OR REPLACE FUNCTION find_parent_for_address(token_info JSONB,
296 parent_place_id BIGINT;
298 -- Check for addr:street attributes
299 parent_place_id := getNearestNamedRoadPlaceId(partition, centroid, token_info);
300 IF parent_place_id is not null THEN
301 {% if debug %}RAISE WARNING 'Get parent from addr:street: %', parent_place_id;{% endif %}
302 RETURN parent_place_id;
305 -- Check for addr:place attributes.
306 parent_place_id := getNearestNamedPlacePlaceId(partition, centroid, token_info);
307 {% if debug %}RAISE WARNING 'Get parent from addr:place: %', parent_place_id;{% endif %}
308 RETURN parent_place_id;
311 LANGUAGE plpgsql STABLE PARALLEL SAFE;
314 CREATE OR REPLACE FUNCTION delete_location(OLD_place_id BIGINT)
319 DELETE FROM location_area where place_id = OLD_place_id;
320 -- TODO:location_area
326 -- Create a bounding box with an extent computed from the radius (in meters)
327 -- which in turn is derived from the given search rank.
328 CREATE OR REPLACE FUNCTION place_node_fuzzy_area(geom GEOMETRY, rank_search INTEGER)
334 IF rank_search <= 16 THEN -- city
336 ELSIF rank_search <= 18 THEN -- town
338 ELSIF rank_search <= 19 THEN -- village
340 ELSIF rank_search <= 20 THEN -- hamlet
344 RETURN ST_Envelope(ST_Collect(
345 ST_Project(geom::geography, radius, 0.785398)::geometry,
346 ST_Project(geom::geography, radius, 3.9269908)::geometry));
349 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
352 CREATE OR REPLACE FUNCTION add_location(place_id BIGINT, country_code varchar(2),
353 partition INTEGER, keywords INTEGER[],
354 rank_search INTEGER, rank_address INTEGER,
355 in_postcode TEXT, geometry GEOMETRY,
362 PERFORM deleteLocationArea(partition, place_id, rank_search);
364 -- add postcode only if it contains a single entry, i.e. ignore postcode lists
366 IF in_postcode is not null AND in_postcode not similar to '%(,|;)%' THEN
367 postcode := upper(trim (in_postcode));
370 IF ST_Dimension(geometry) = 2 THEN
371 RETURN insertLocationAreaLarge(partition, place_id, country_code, keywords,
372 rank_search, rank_address, false, postcode,
376 IF ST_Dimension(geometry) = 0 THEN
377 RETURN insertLocationAreaLarge(partition, place_id, country_code, keywords,
378 rank_search, rank_address, true, postcode,
379 centroid, place_node_fuzzy_area(geometry, rank_search));
388 CREATE OR REPLACE FUNCTION quad_split_geometry(geometry GEOMETRY, maxarea FLOAT,
390 RETURNS SETOF GEOMETRY
404 remainingdepth INTEGER;
406 -- RAISE WARNING 'quad_split_geometry: maxarea=%, depth=%',maxarea,maxdepth;
408 IF not ST_IsValid(geometry) THEN
412 IF ST_Dimension(geometry) != 2 OR maxdepth <= 1 THEN
413 RETURN NEXT geometry;
417 remainingdepth := maxdepth - 1;
418 area := ST_AREA(geometry);
419 IF area < maxarea THEN
420 RETURN NEXT geometry;
424 xmin := st_xmin(geometry);
425 xmax := st_xmax(geometry);
426 ymin := st_ymin(geometry);
427 ymax := st_ymax(geometry);
428 secbox := ST_SetSRID(ST_MakeBox2D(ST_Point(ymin,xmin),ST_Point(ymax,xmax)),4326);
430 -- if the geometry completely covers the box don't bother to slice any more
431 IF ST_AREA(secbox) = area THEN
432 RETURN NEXT geometry;
436 xmid := (xmin+xmax)/2;
437 ymid := (ymin+ymax)/2;
442 secbox := ST_SetSRID(ST_MakeBox2D(ST_Point(xmin,ymin),ST_Point(xmid,ymid)),4326);
445 secbox := ST_SetSRID(ST_MakeBox2D(ST_Point(xmin,ymid),ST_Point(xmid,ymax)),4326);
448 secbox := ST_SetSRID(ST_MakeBox2D(ST_Point(xmid,ymin),ST_Point(xmax,ymid)),4326);
451 secbox := ST_SetSRID(ST_MakeBox2D(ST_Point(xmid,ymid),ST_Point(xmax,ymax)),4326);
454 secgeo := st_intersection(geometry, secbox);
455 IF NOT ST_IsEmpty(secgeo) AND ST_Dimension(secgeo) = 2 THEN
456 FOR geo IN SELECT quad_split_geometry(secgeo, maxarea, remainingdepth) as geom LOOP
457 IF NOT ST_IsEmpty(geo.geom) AND ST_Dimension(geo.geom) = 2 THEN
458 RETURN NEXT geo.geom;
467 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
470 CREATE OR REPLACE FUNCTION split_geometry(geometry GEOMETRY)
471 RETURNS SETOF GEOMETRY
476 IF ST_GeometryType(geometry) = 'ST_MultiPolygon'
477 and ST_Area(geometry) * 10 > ST_Area(Box2D(geometry))
480 SELECT quad_split_geometry(g, 0.25, 20) as geom
481 FROM (SELECT (ST_Dump(geometry)).geom::geometry(Polygon, 4326) AS g) xx
483 RETURN NEXT geo.geom;
487 SELECT quad_split_geometry(geometry, 0.25, 20) as geom
489 RETURN NEXT geo.geom;
495 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
497 CREATE OR REPLACE FUNCTION simplify_large_polygons(geometry GEOMETRY)
501 IF ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon')
502 and ST_MemSize(geometry) > 3000000
504 geometry := ST_SimplifyPreserveTopology(geometry, 0.0001);
509 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
512 CREATE OR REPLACE FUNCTION place_force_delete(placeid BIGINT)
517 osmtype character(1);
521 SELECT osm_type, osm_id, class, type FROM placex WHERE place_id = placeid INTO osmtype, osmid, pclass, ptype;
522 DELETE FROM import_polygon_delete where osm_type = osmtype and osm_id = osmid and class = pclass and type = ptype;
523 DELETE FROM import_polygon_error where osm_type = osmtype and osm_id = osmid and class = pclass and type = ptype;
524 -- force delete by directly entering it into the to-be-deleted table
525 INSERT INTO place_to_be_deleted (osm_type, osm_id, class, type, deferred)
526 VALUES(osmtype, osmid, pclass, ptype, false);
527 PERFORM flush_deleted_places();
535 CREATE OR REPLACE FUNCTION place_force_update(placeid BIGINT)
544 UPDATE placex SET indexed_status = 2 WHERE place_id = placeid;
546 SELECT geometry, rank_address INTO placegeom, rank
547 FROM placex WHERE place_id = placeid;
549 IF placegeom IS NOT NULL AND ST_IsValid(placegeom) THEN
550 IF ST_GeometryType(placegeom) in ('ST_Polygon','ST_MultiPolygon')
553 FOR geom IN SELECT split_geometry(placegeom) LOOP
554 UPDATE placex SET indexed_status = 2
555 WHERE ST_Intersects(geom, placex.geometry)
556 and indexed_status = 0
557 and ((rank_address = 0 and rank_search > rank) or rank_address > rank)
558 and (rank_search < 28 or name is not null or (rank >= 16 and address ? 'place'));
561 diameter := update_place_diameter(rank);
564 -- roads may cause reparenting for >27 rank places
565 update placex set indexed_status = 2 where indexed_status = 0 and rank_search > rank and ST_DWithin(placex.geometry, placegeom, diameter);
566 ELSEIF rank >= 16 THEN
567 -- up to rank 16, street-less addresses may need reparenting
568 update placex set indexed_status = 2 where indexed_status = 0 and rank_search > rank and ST_DWithin(placex.geometry, placegeom, diameter) and (rank_search < 28 or name is not null or address ? 'place');
570 -- for all other places the search terms may change as well
571 update placex set indexed_status = 2 where indexed_status = 0 and rank_search > rank and ST_DWithin(placex.geometry, placegeom, diameter) and (rank_search < 28 or name is not null);
583 CREATE OR REPLACE FUNCTION flush_deleted_places()
587 -- deleting large polygons can have a massive effect on the system - require manual intervention to let them through
588 INSERT INTO import_polygon_delete (osm_type, osm_id, class, type)
589 SELECT osm_type, osm_id, class, type FROM place_to_be_deleted WHERE deferred;
591 -- delete from place table
592 ALTER TABLE place DISABLE TRIGGER place_before_delete;
593 DELETE FROM place USING place_to_be_deleted
594 WHERE place.osm_type = place_to_be_deleted.osm_type
595 and place.osm_id = place_to_be_deleted.osm_id
596 and place.class = place_to_be_deleted.class
597 and place.type = place_to_be_deleted.type
599 ALTER TABLE place ENABLE TRIGGER place_before_delete;
601 -- Mark for delete in the placex table
602 UPDATE placex SET indexed_status = 100 FROM place_to_be_deleted
603 WHERE placex.osm_type = 'N' and place_to_be_deleted.osm_type = 'N'
604 and placex.osm_id = place_to_be_deleted.osm_id
605 and placex.class = place_to_be_deleted.class
606 and placex.type = place_to_be_deleted.type
608 UPDATE placex SET indexed_status = 100 FROM place_to_be_deleted
609 WHERE placex.osm_type = 'W' and place_to_be_deleted.osm_type = 'W'
610 and placex.osm_id = place_to_be_deleted.osm_id
611 and placex.class = place_to_be_deleted.class
612 and placex.type = place_to_be_deleted.type
614 UPDATE placex SET indexed_status = 100 FROM place_to_be_deleted
615 WHERE placex.osm_type = 'R' and place_to_be_deleted.osm_type = 'R'
616 and placex.osm_id = place_to_be_deleted.osm_id
617 and placex.class = place_to_be_deleted.class
618 and placex.type = place_to_be_deleted.type
621 -- Mark for delete in interpolations
622 UPDATE location_property_osmline SET indexed_status = 100 FROM place_to_be_deleted
623 WHERE place_to_be_deleted.osm_type = 'W'
624 and place_to_be_deleted.class = 'place'
625 and place_to_be_deleted.type = 'houses'
626 and location_property_osmline.osm_id = place_to_be_deleted.osm_id
630 TRUNCATE TABLE place_to_be_deleted;
637 CREATE OR REPLACE FUNCTION place_update_entrances(placeid BIGINT, osmid BIGINT)
645 FOR entrance in SELECT osm_id, type, geometry, extratags
647 WHERE osm_id IN (SELECT unnest(nodes) FROM planet_osm_ways WHERE id=osmid)
649 osm_ids := array_append(osm_ids, entrance.osm_id);
650 INSERT INTO placex_entrance (place_id, osm_id, type, location, extratags)
651 VALUES (placeid, entrance.osm_id, entrance.type, entrance.geometry, entrance.extratags)
652 ON CONFLICT (place_id, osm_id) DO UPDATE
653 SET type = excluded.type, location = excluded.location, extratags = excluded.extratags;
656 IF array_length(osm_ids, 1) > 0 THEN
657 DELETE FROM placex_entrance WHERE place_id=placeid AND NOT osm_id=ANY(osm_ids);
659 DELETE FROM placex_entrance WHERE place_id=placeid;