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 CREATE OR REPLACE FUNCTION place_insert()
15 existingplacex RECORD;
20 RAISE WARNING 'place_insert: % % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type,st_area(NEW.geometry);
23 -- Filter tuples with bad geometries.
24 IF ST_IsEmpty(NEW.geometry) OR NOT ST_IsValid(NEW.geometry) THEN
25 INSERT INTO import_polygon_error (osm_type, osm_id, class, type, name,
26 country_code, updated, errormessage,
27 prevgeometry, newgeometry)
28 VALUES (NEW.osm_type, NEW.osm_id, NEW.class, NEW.type, NEW.name,
29 NEW.address->'country', now(), ST_IsValidReason(NEW.geometry),
32 RAISE WARNING 'Invalid Geometry: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
37 -- Have we already done this place?
38 SELECT * INTO existing
40 WHERE osm_type = NEW.osm_type and osm_id = NEW.osm_id
41 and class = NEW.class and type = NEW.type;
43 {% if debug %}RAISE WARNING 'Existing: %',existing.osm_id;{% endif %}
45 -- Handle a place changing type by removing the old data.
46 -- (This trigger is executed BEFORE INSERT of the NEW tuple.)
47 IF existing.osm_type IS NULL THEN
48 DELETE FROM place where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class;
51 -- Remove any old logged data.
52 DELETE from import_polygon_error where osm_type = NEW.osm_type and osm_id = NEW.osm_id;
53 DELETE from import_polygon_delete where osm_type = NEW.osm_type and osm_id = NEW.osm_id;
55 -- ---- Interpolation Lines
57 IF NEW.class='place' and NEW.type='houses'
58 and NEW.osm_type='W' and ST_GeometryType(NEW.geometry) = 'ST_LineString'
60 -- Get the existing entry from the interpolation table.
61 SELECT * INTO existingline
62 FROM location_property_osmline WHERE osm_id = NEW.osm_id;
64 -- Update the interpolation table:
65 -- delete all old interpolation lines with same osm_id
66 -- and insert the new one(s) (they can be split up, if they have > 2 nodes)
67 IF existingline.osm_id IS NOT NULL THEN
68 DELETE FROM location_property_osmline where osm_id = NEW.osm_id;
71 INSERT INTO location_property_osmline (osm_id, address, linegeo)
72 VALUES (NEW.osm_id, NEW.address, NEW.geometry);
74 -- Now invalidate all address nodes on the line.
75 -- They get their parent from the interpolation.
76 UPDATE placex p SET indexed_status = 2
77 FROM planet_osm_ways w
78 WHERE w.id = NEW.osm_id and p.osm_type = 'N' and p.osm_id = any(w.nodes);
80 -- If there is already an entry in place, just update that, if necessary.
81 IF existing.osm_type is not null THEN
82 IF coalesce(existing.address, ''::hstore) != coalesce(NEW.address, ''::hstore)
83 OR existing.geometry::text != NEW.geometry::text
87 address = NEW.address,
88 extratags = NEW.extratags,
89 admin_level = NEW.admin_level,
90 geometry = NEW.geometry
91 WHERE osm_type = NEW.osm_type and osm_id = NEW.osm_id
92 and class = NEW.class and type = NEW.type;
101 -- ---- Postcode points.
103 IF NEW.class = 'place' AND NEW.type = 'postcode' THEN
104 -- Pure postcodes are never queried from placex so we don't add them.
105 -- location_postcodes is filled from the place table directly.
107 -- Remove any old placex entry.
108 DELETE FROM placex WHERE osm_type = NEW.osm_type and osm_id = NEW.osm_id;
110 IF existing.osm_type IS NOT NULL THEN
111 IF coalesce(existing.address, ''::hstore) != coalesce(NEW.address, ''::hstore)
112 OR existing.geometry::text != NEW.geometry::text
116 address = NEW.address,
117 extratags = NEW.extratags,
118 admin_level = NEW.admin_level,
119 geometry = NEW.geometry
120 WHERE osm_type = NEW.osm_type and osm_id = NEW.osm_id
121 and class = NEW.class and type = NEW.type;
130 -- ---- All other place types.
132 -- Patch in additional country names
133 IF NEW.admin_level = 2 and NEW.type = 'administrative' and NEW.address ? 'country'
136 SELECT name FROM country_name WHERE country_code = lower(NEW.address->'country')
138 NEW.name = country.name || NEW.name;
142 -- When an area is changed from large to small: log and discard change
143 IF existing.geometry is not null AND ST_IsValid(existing.geometry)
144 AND ST_Area(existing.geometry) > 0.02
145 AND ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon')
146 AND ST_Area(NEW.geometry) < ST_Area(existing.geometry) * 0.5
148 INSERT INTO import_polygon_error (osm_type, osm_id, class, type, name,
149 country_code, updated, errormessage,
150 prevgeometry, newgeometry)
151 VALUES (NEW.osm_type, NEW.osm_id, NEW.class, NEW.type, NEW.name,
152 NEW.address->'country', now(),
153 'Area reduced from '||st_area(existing.geometry)||' to '||st_area(NEW.geometry),
154 existing.geometry, NEW.geometry);
159 -- Get the existing placex entry.
160 SELECT * INTO existingplacex
162 WHERE osm_type = NEW.osm_type and osm_id = NEW.osm_id
163 and class = NEW.class and type = NEW.type;
165 {% if debug %}RAISE WARNING 'Existing PlaceX: %',existingplacex.place_id;{% endif %}
167 -- To paraphrase: if there isn't an existing item, OR if the admin level has changed
168 IF existingplacex.osm_type IS NULL
169 or (existingplacex.class = 'boundary'
170 and ((coalesce(existingplacex.admin_level, 15) != coalesce(NEW.admin_level, 15)
171 and existingplacex.type = 'administrative')
172 or existingplacex.type != NEW.type))
174 {% if config.get_bool('LIMIT_REINDEXING') %}
175 -- sanity check: ignore admin_level changes on places with too many active children
176 -- or we end up reindexing entire countries because somebody accidentally deleted admin_level
177 IF existingplacex.osm_type IS NOT NULL THEN
178 SELECT count(*) INTO i FROM
179 (SELECT 'a' FROM placex, place_addressline
180 WHERE address_place_id = existingplacex.place_id
181 and placex.place_id = place_addressline.place_id
182 and indexed_status = 0 and place_addressline.isaddress LIMIT 100001) sub;
189 IF existing.osm_type IS NOT NULL THEN
190 -- Pathological case caused by the triggerless copy into place during initial import
191 -- force delete even for large areas, it will be reinserted later
192 UPDATE place SET geometry = ST_SetSRID(ST_Point(0,0), 4326)
193 WHERE osm_type = NEW.osm_type and osm_id = NEW.osm_id
194 and class = NEW.class and type = NEW.type;
196 WHERE osm_type = NEW.osm_type and osm_id = NEW.osm_id
197 and class = NEW.class and type = NEW.type;
200 -- Process it as a new insertion
201 INSERT INTO placex (osm_type, osm_id, class, type, name,
202 admin_level, address, extratags, geometry)
203 VALUES (NEW.osm_type, NEW.osm_id, NEW.class, NEW.type, NEW.name,
204 NEW.admin_level, NEW.address, NEW.extratags, NEW.geometry);
206 {% if debug %}RAISE WARNING 'insert done % % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type,NEW.name;{% endif %}
211 -- Special case for polygon shape changes because they tend to be large
212 -- and we can be a bit clever about how we handle them
213 IF existing.geometry::text != NEW.geometry::text
214 AND ST_GeometryType(existing.geometry) in ('ST_Polygon','ST_MultiPolygon')
215 AND ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon')
218 IF ST_Area(NEW.geometry) < 0.000000001 AND ST_Area(existingplacex.geometry) < 1
220 -- re-index points that have moved in / out of the polygon.
221 -- Could be done as a single query but postgres gets the index usage wrong.
222 update placex set indexed_status = 2 where indexed_status = 0
223 AND ST_Intersects(NEW.geometry, placex.geometry)
224 AND NOT ST_Intersects(existingplacex.geometry, placex.geometry)
225 AND rank_search > existingplacex.rank_search AND (rank_search < 28 or name is not null);
227 update placex set indexed_status = 2 where indexed_status = 0
228 AND ST_Intersects(existingplacex.geometry, placex.geometry)
229 AND NOT ST_Intersects(NEW.geometry, placex.geometry)
230 AND rank_search > existingplacex.rank_search AND (rank_search < 28 or name is not null);
235 -- Has something relevant changed?
236 IF coalesce(existing.name::text, '') != coalesce(NEW.name::text, '')
237 OR coalesce(existing.extratags::text, '') != coalesce(NEW.extratags::text, '')
238 OR coalesce(existing.address, ''::hstore) != coalesce(NEW.address, ''::hstore)
239 OR coalesce(existing.admin_level, 15) != coalesce(NEW.admin_level, 15)
240 OR existing.geometry::text != NEW.geometry::text
244 address = NEW.address,
245 extratags = NEW.extratags,
246 admin_level = NEW.admin_level,
247 geometry = NEW.geometry
248 WHERE osm_type = NEW.osm_type and osm_id = NEW.osm_id
249 and class = NEW.class and type = NEW.type;
251 -- Postcode areas are only kept, when there is an actual postcode assigned.
252 IF NEW.class = 'boundary' AND NEW.type = 'postal_code' THEN
253 IF NEW.address is NULL OR NOT NEW.address ? 'postcode' THEN
254 -- postcode was deleted, no longer retain in placex
255 DELETE FROM placex where place_id = existingplacex.place_id;
259 NEW.name := hstore('ref', NEW.address->'postcode');
262 -- Boundaries must be areas.
263 IF NEW.class in ('boundary')
264 AND ST_GeometryType(NEW.geometry) not in ('ST_Polygon','ST_MultiPolygon')
266 DELETE FROM placex where place_id = existingplacex.place_id;
270 -- Update the placex entry in-place.
273 address = NEW.address,
274 parent_place_id = null,
275 extratags = NEW.extratags,
276 admin_level = NEW.admin_level,
278 geometry = NEW.geometry
279 WHERE place_id = existingplacex.place_id;
281 -- If an address node which is part of a interpolation line changes
282 -- mark this line for reparenting.
283 -- (Already here, because interpolation lines are reindexed before nodes,
284 -- so in the second call it would be too late.)
286 and coalesce(existing.address, ''::hstore) != coalesce(NEW.address, ''::hstore)
288 result:= osmline_reinsert(NEW.osm_id, NEW.geometry);
291 -- Invalidate linked places: they potentially get a new name and addresses.
292 IF existingplacex.linked_place_id is not NULL THEN
295 extratags = p.extratags,
298 WHERE x.place_id = existingplacex.linked_place_id
299 and x.indexed_status = 0
300 and x.osm_type = p.osm_type
301 and x.osm_id = p.osm_id
302 and x.class = p.class;
305 -- Invalidate dependent objects effected by name changes
306 IF coalesce(existing.name::text, '') != coalesce(NEW.name::text, '')
308 IF existingplacex.rank_address between 26 and 27 THEN
309 -- When streets change their name, this may have an effect on POI objects
310 -- with addr:street tags.
311 UPDATE placex SET indexed_status = 2
312 WHERE indexed_status = 0 and address ? 'street'
313 and parent_place_id = existingplacex.place_id;
314 UPDATE placex SET indexed_status = 2
315 WHERE indexed_status = 0 and rank_search = 30 and address ? 'street'
316 and ST_DWithin(NEW.geometry, geometry, 0.002);
317 ELSEIF existingplacex.rank_address between 16 and 25 THEN
318 -- When places change their name, this may have an effect on POI objects
319 -- with addr:place tags.
320 UPDATE placex SET indexed_status = 2
321 WHERE indexed_status = 0 and address ? 'place' and rank_search = 30
322 and parent_place_id = existingplacex.place_id;
323 -- No update of surrounding objects, potentially too expensive.
328 -- Abort the insertion (we modified the existing place instead)
334 CREATE OR REPLACE FUNCTION place_delete()
341 {% if debug %}RAISE WARNING 'delete: % % % %',OLD.osm_type,OLD.osm_id,OLD.class,OLD.type;{% endif %}
343 -- deleting large polygons can have a massive effect on the system - require manual intervention to let them through
344 IF st_area(OLD.geometry) > 2 and st_isvalid(OLD.geometry) THEN
345 SELECT bool_or(not (rank_address = 0 or rank_address > 25)) as ranked FROM placex WHERE osm_type = OLD.osm_type and osm_id = OLD.osm_id and class = OLD.class and type = OLD.type INTO has_rank;
347 insert into import_polygon_delete (osm_type, osm_id, class, type) values (OLD.osm_type,OLD.osm_id,OLD.class,OLD.type);
353 UPDATE placex set indexed_status = 100 where osm_type = OLD.osm_type and osm_id = OLD.osm_id and class = OLD.class and type = OLD.type;
355 -- interpolations are special
356 IF OLD.osm_type='W' and OLD.class = 'place' and OLD.type = 'houses' THEN
357 UPDATE location_property_osmline set indexed_status = 100 where osm_id = OLD.osm_id; -- osm_id = wayid (=old.osm_id)