1 -- Trigger functions for the placex table.
3 -- Retrieve the data needed by the indexer for updating the place.
7 -- address list of address tags, either from the object or a surrounding
9 -- country_feature If the place is a country feature, this contains the
10 -- country code, otherwise it is null.
11 CREATE OR REPLACE FUNCTION placex_prepare_update(p placex,
14 OUT country_feature VARCHAR,
15 OUT linked_place_id BIGINT)
20 -- For POI nodes, check if the address should be derived from a surrounding
22 IF p.rank_search < 30 OR p.osm_type != 'N' OR p.address is not null THEN
25 -- The additional && condition works around the misguided query
26 -- planner of postgis 3.0.
27 SELECT placex.address || hstore('_inherited', '') INTO address
29 WHERE ST_Covers(geometry, p.centroid)
30 and geometry && p.centroid
31 and placex.address is not null
32 and (placex.address ? 'housenumber' or placex.address ? 'street' or placex.address ? 'place')
33 and rank_search = 30 AND ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon')
37 address := address - '_unlisted_place'::TEXT;
40 -- Names of linked places need to be merged in, so search for a linkable
41 -- place already here.
42 SELECT * INTO location FROM find_linked_place(p);
44 IF location.place_id is not NULL THEN
45 linked_place_id := location.place_id;
47 IF NOT location.name IS NULL THEN
48 name := location.name || name;
52 country_feature := CASE WHEN p.admin_level = 2
53 and p.class = 'boundary' and p.type = 'administrative'
60 LANGUAGE plpgsql STABLE;
63 CREATE OR REPLACE FUNCTION find_associated_street(poi_osm_type CHAR(1),
72 SELECT members FROM planet_osm_rels
73 WHERE parts @> ARRAY[poi_osm_id]
74 and members @> ARRAY[lower(poi_osm_type) || poi_osm_id]
75 and tags @> ARRAY['associatedStreet']
77 FOR i IN 1..array_upper(location.members, 1) BY 2 LOOP
78 IF location.members[i+1] = 'street' THEN
80 SELECT place_id from placex
81 WHERE osm_type = 'W' and osm_id = substring(location.members[i],2)::bigint
83 and rank_search between 26 and 27
85 RETURN parent.place_id;
94 LANGUAGE plpgsql STABLE;
97 -- Find the parent road of a POI.
99 -- \returns Place ID of parent object or NULL if none
101 -- Copy data from linked items (POIs on ways, addr:street links, relations).
103 CREATE OR REPLACE FUNCTION find_parent_for_poi(poi_osm_type CHAR(1),
105 poi_partition SMALLINT,
107 addr_street INTEGER[],
108 addr_place INTEGER[],
109 is_place_addr BOOLEAN)
113 parent_place_id BIGINT DEFAULT NULL;
116 {% if debug %}RAISE WARNING 'finding street for % %', poi_osm_type, poi_osm_id;{% endif %}
118 -- Is this object part of an associatedStreet relation?
119 parent_place_id := find_associated_street(poi_osm_type, poi_osm_id);
121 IF parent_place_id is null THEN
122 parent_place_id := find_parent_for_address(addr_street, addr_place,
123 poi_partition, bbox);
126 IF parent_place_id is null and poi_osm_type = 'N' THEN
127 -- Is this node part of an interpolation?
129 SELECT q.parent_place_id
130 FROM location_property_osmline q, planet_osm_ways x
131 WHERE q.linegeo && bbox and x.id = q.osm_id
132 and poi_osm_id = any(x.nodes)
135 {% if debug %}RAISE WARNING 'Get parent from interpolation: %', location.parent_place_id;{% endif %}
136 RETURN location.parent_place_id;
140 SELECT p.place_id, p.osm_id, p.rank_search, p.address,
141 coalesce(p.centroid, ST_Centroid(p.geometry)) as centroid
142 FROM placex p, planet_osm_ways w
143 WHERE p.osm_type = 'W' and p.rank_search >= 26
144 and p.geometry && bbox
145 and w.id = p.osm_id and poi_osm_id = any(w.nodes)
147 {% if debug %}RAISE WARNING 'Node is part of way % ', location.osm_id;{% endif %}
149 -- Way IS a road then we are on it - that must be our road
150 IF location.rank_search < 28 THEN
151 {% if debug %}RAISE WARNING 'node in way that is a street %',location;{% endif %}
152 RETURN location.place_id;
155 parent_place_id := find_associated_street('W', location.osm_id);
159 IF parent_place_id is NULL THEN
160 IF is_place_addr THEN
161 -- The address is attached to a place we don't know.
162 -- Instead simply use the containing area with the largest rank.
164 SELECT place_id FROM placex
165 WHERE bbox && geometry AND _ST_Covers(geometry, ST_Centroid(bbox))
166 AND rank_address between 5 and 25
167 ORDER BY rank_address desc
169 RETURN location.place_id;
171 ELSEIF ST_Area(bbox) < 0.005 THEN
172 -- for smaller features get the nearest road
173 SELECT getNearestRoadPlaceId(poi_partition, bbox) INTO parent_place_id;
174 {% if debug %}RAISE WARNING 'Checked for nearest way (%)', parent_place_id;{% endif %}
176 -- for larger features simply find the area with the largest rank that
177 -- contains the bbox, only use addressable features
179 SELECT place_id FROM placex
180 WHERE bbox && geometry AND _ST_Covers(geometry, ST_Centroid(bbox))
181 AND rank_address between 5 and 25
182 ORDER BY rank_address desc
184 RETURN location.place_id;
189 RETURN parent_place_id;
192 LANGUAGE plpgsql STABLE;
194 -- Try to find a linked place for the given object.
195 CREATE OR REPLACE FUNCTION find_linked_place(bnd placex)
199 relation_members TEXT[];
201 linked_placex placex%ROWTYPE;
204 IF bnd.rank_search >= 26 or bnd.rank_address = 0
205 or ST_GeometryType(bnd.geometry) NOT IN ('ST_Polygon','ST_MultiPolygon')
206 or bnd.type IN ('postcode', 'postal_code')
211 IF bnd.osm_type = 'R' THEN
212 -- see if we have any special relation members
213 SELECT members FROM planet_osm_rels WHERE id = bnd.osm_id INTO relation_members;
214 {% if debug %}RAISE WARNING 'Got relation members';{% endif %}
216 -- Search for relation members with role 'lable'.
217 IF relation_members IS NOT NULL THEN
219 SELECT get_rel_node_members(relation_members, ARRAY['label']) as member
221 {% if debug %}RAISE WARNING 'Found label member %', rel_member.member;{% endif %}
225 WHERE osm_type = 'N' and osm_id = rel_member.member
228 {% if debug %}RAISE WARNING 'Linked label member';{% endif %}
229 RETURN linked_placex;
236 IF bnd.name ? 'name' THEN
237 bnd_name := lower(bnd.name->'name');
238 IF bnd_name = '' THEN
243 -- If extratags has a place tag, look for linked nodes by their place type.
244 -- Area and node still have to have the same name.
245 IF bnd.extratags ? 'place' and bnd_name is not null THEN
248 WHERE (position(lower(name->'name') in bnd_name) > 0
249 OR position(bnd_name in lower(name->'name')) > 0)
250 AND placex.class = 'place' AND placex.type = bnd.extratags->'place'
251 AND placex.osm_type = 'N'
252 AND placex.linked_place_id is null
253 AND placex.rank_search < 26 -- needed to select the right index
254 AND placex.type != 'postcode'
255 AND ST_Covers(bnd.geometry, placex.geometry)
257 {% if debug %}RAISE WARNING 'Found type-matching place node %', linked_placex.osm_id;{% endif %}
258 RETURN linked_placex;
262 IF bnd.extratags ? 'wikidata' THEN
265 WHERE placex.class = 'place' AND placex.osm_type = 'N'
266 AND placex.extratags ? 'wikidata' -- needed to select right index
267 AND placex.extratags->'wikidata' = bnd.extratags->'wikidata'
268 AND placex.linked_place_id is null
269 AND placex.rank_search < 26
270 AND _st_covers(bnd.geometry, placex.geometry)
271 ORDER BY lower(name->'name') = bnd_name desc
273 {% if debug %}RAISE WARNING 'Found wikidata-matching place node %', linked_placex.osm_id;{% endif %}
274 RETURN linked_placex;
278 -- Name searches can be done for ways as well as relations
279 IF bnd_name is not null THEN
280 {% if debug %}RAISE WARNING 'Looking for nodes with matching names';{% endif %}
282 SELECT placex.* from placex
283 WHERE lower(name->'name') = bnd_name
284 AND ((bnd.rank_address > 0
285 and bnd.rank_address = (compute_place_rank(placex.country_code,
287 placex.type, 15::SMALLINT,
288 false, placex.postcode)).address_rank)
289 OR (bnd.rank_address = 0 and placex.rank_search = bnd.rank_search))
290 AND placex.osm_type = 'N'
291 AND placex.class = 'place'
292 AND placex.linked_place_id is null
293 AND placex.rank_search < 26 -- needed to select the right index
294 AND placex.type != 'postcode'
295 AND ST_Covers(bnd.geometry, placex.geometry)
297 {% if debug %}RAISE WARNING 'Found matching place node %', linked_placex.osm_id;{% endif %}
298 RETURN linked_placex;
305 LANGUAGE plpgsql STABLE;
308 CREATE OR REPLACE FUNCTION create_poi_search_terms(obj_place_id BIGINT,
309 in_partition SMALLINT,
310 parent_place_id BIGINT,
311 is_place_addr BOOLEAN,
315 OUT name_vector INTEGER[],
316 OUT nameaddress_vector INTEGER[])
319 parent_name_vector INTEGER[];
320 parent_address_vector INTEGER[];
321 addr_place_ids INTEGER[];
322 hnr_vector INTEGER[];
326 parent_address_place_ids BIGINT[];
328 nameaddress_vector := '{}'::INTEGER[];
330 SELECT s.name_vector, s.nameaddress_vector
331 INTO parent_name_vector, parent_address_vector
333 WHERE s.place_id = parent_place_id;
336 SELECT (get_addr_tag_rank(key, country)).*, match_tokens, search_tokens
337 FROM token_get_address_tokens(token_info)
338 WHERE not search_tokens <@ parent_address_vector
340 addr_place := get_address_place(in_partition, geometry,
341 addr_item.from_rank, addr_item.to_rank,
342 addr_item.extent, addr_item.match_tokens);
344 IF addr_place is null THEN
345 -- No place found in OSM that matches. Make it at least searchable.
346 nameaddress_vector := array_merge(nameaddress_vector, addr_item.search_tokens);
348 IF parent_address_place_ids is null THEN
349 SELECT array_agg(parent_place_id) INTO parent_address_place_ids
350 FROM place_addressline
351 WHERE place_id = parent_place_id;
354 -- If the parent already lists the place in place_address line, then we
355 -- are done. Otherwise, add its own place_address line.
356 IF not parent_address_place_ids @> ARRAY[addr_place.place_id] THEN
357 nameaddress_vector := array_merge(nameaddress_vector, addr_place.keywords);
359 INSERT INTO place_addressline (place_id, address_place_id, fromarea,
360 isaddress, distance, cached_rank_address)
361 VALUES (obj_place_id, addr_place.place_id, not addr_place.isguess,
362 true, addr_place.distance, addr_place.rank_address);
367 name_vector := token_get_name_search_tokens(token_info);
369 -- Check if the parent covers all address terms.
370 -- If not, create a search name entry with the house number as the name.
371 -- This is unusual for the search_name table but prevents that the place
372 -- is returned when we only search for the street/place.
374 hnr_vector := token_get_housenumber_search_tokens(token_info);
376 IF hnr_vector is not null and not nameaddress_vector <@ parent_address_vector THEN
377 name_vector := array_merge(name_vector, hnr_vector);
380 IF is_place_addr THEN
381 addr_place_ids := token_addr_place_search_tokens(token_info);
382 IF not addr_place_ids <@ parent_name_vector THEN
383 -- make sure addr:place terms are always searchable
384 nameaddress_vector := array_merge(nameaddress_vector, addr_place_ids);
385 -- If there is a housenumber, also add the place name as a name,
386 -- so we can search it by the usual housenumber+place algorithms.
387 IF hnr_vector is not null THEN
388 name_vector := array_merge(name_vector, addr_place_ids);
393 -- Cheating here by not recomputing all terms but simply using the ones
394 -- from the parent object.
395 nameaddress_vector := array_merge(nameaddress_vector, parent_name_vector);
396 nameaddress_vector := array_merge(nameaddress_vector, parent_address_vector);
403 -- Insert address of a place into the place_addressline table.
405 -- \param obj_place_id Place_id of the place to compute the address for.
406 -- \param partition Partition number where the place is in.
407 -- \param maxrank Rank of the place. All address features must have
408 -- a search rank lower than the given rank.
409 -- \param address Address terms for the place.
410 -- \param geometry Geometry to which the address objects should be close.
412 -- \retval parent_place_id Place_id of the address object that is the direct
414 -- \retval postcode Postcode computed from the address. This is the
415 -- addr:postcode of one of the address objects. If
416 -- more than one of has a postcode, the highest ranking
417 -- one is used. May be NULL.
418 -- \retval nameaddress_vector Search terms for the address. This is the sum
419 -- of name terms of all address objects.
420 CREATE OR REPLACE FUNCTION insert_addresslines(obj_place_id BIGINT,
426 OUT parent_place_id BIGINT,
428 OUT nameaddress_vector INT[])
431 address_havelevel BOOLEAN[];
433 location_isaddress BOOLEAN;
434 current_boundary GEOMETRY := NULL;
435 current_node_area GEOMETRY := NULL;
437 parent_place_rank INT := 0;
438 addr_place_ids BIGINT[] := '{}'::int[];
439 new_address_vector INT[];
443 parent_place_id := 0;
444 nameaddress_vector := '{}'::int[];
446 address_havelevel := array_fill(false, ARRAY[maxrank]);
449 SELECT (get_address_place(partition, geometry, from_rank, to_rank,
450 extent, match_tokens)).*, search_tokens
451 FROM (SELECT (get_addr_tag_rank(key, country)).*, match_tokens, search_tokens
452 FROM token_get_address_tokens(token_info)) x
453 ORDER BY rank_address, distance, isguess desc
455 IF location.place_id is null THEN
456 {% if not db.reverse_only %}
457 nameaddress_vector := array_merge(nameaddress_vector, location.search_tokens);
460 {% if not db.reverse_only %}
461 nameaddress_vector := array_merge(nameaddress_vector, location.keywords::INTEGER[]);
464 location_isaddress := not address_havelevel[location.rank_address];
465 IF not address_havelevel[location.rank_address] THEN
466 address_havelevel[location.rank_address] := true;
467 IF parent_place_rank < location.rank_address THEN
468 parent_place_id := location.place_id;
469 parent_place_rank := location.rank_address;
473 INSERT INTO place_addressline (place_id, address_place_id, fromarea,
474 isaddress, distance, cached_rank_address)
475 VALUES (obj_place_id, location.place_id, not location.isguess,
476 true, location.distance, location.rank_address);
478 addr_place_ids := addr_place_ids || location.place_id;
483 SELECT * FROM getNearFeatures(partition, geometry, maxrank)
484 WHERE not addr_place_ids @> ARRAY[place_id]
485 ORDER BY rank_address, isguess asc,
487 CASE WHEN rank_address = 16 AND rank_search = 15 THEN 0.2
488 WHEN rank_address = 16 AND rank_search = 16 THEN 0.25
489 WHEN rank_address = 16 AND rank_search = 18 THEN 0.5
492 -- Ignore all place nodes that do not fit in a lower level boundary.
493 CONTINUE WHEN location.isguess
494 and current_boundary is not NULL
495 and not ST_Contains(current_boundary, location.centroid);
497 -- If this is the first item in the rank, then assume it is the address.
498 location_isaddress := not address_havelevel[location.rank_address];
500 -- Further sanity checks to ensure that the address forms a sane hierarchy.
501 IF location_isaddress THEN
502 IF location.isguess and current_node_area is not NULL THEN
503 location_isaddress := ST_Contains(current_node_area, location.centroid);
505 IF not location.isguess and current_boundary is not NULL
506 and location.rank_address != 11 AND location.rank_address != 5 THEN
507 location_isaddress := ST_Contains(current_boundary, location.centroid);
511 IF location_isaddress THEN
512 address_havelevel[location.rank_address] := true;
513 parent_place_id := location.place_id;
515 -- Set postcode if we have one.
516 -- (Returned will be the highest ranking one.)
517 IF location.postcode is not NULL THEN
518 postcode = location.postcode;
521 -- Recompute the areas we need for hierarchy sanity checks.
522 IF location.rank_address != 11 AND location.rank_address != 5 THEN
523 IF location.isguess THEN
524 current_node_area := place_node_fuzzy_area(location.centroid,
525 location.rank_search);
527 current_node_area := NULL;
528 SELECT p.geometry FROM placex p
529 WHERE p.place_id = location.place_id INTO current_boundary;
534 -- Add it to the list of search terms
535 {% if not db.reverse_only %}
536 nameaddress_vector := array_merge(nameaddress_vector,
537 location.keywords::integer[]);
540 INSERT INTO place_addressline (place_id, address_place_id, fromarea,
541 isaddress, distance, cached_rank_address)
542 VALUES (obj_place_id, location.place_id, not location.isguess,
543 location_isaddress, location.distance, location.rank_address);
550 CREATE OR REPLACE FUNCTION placex_insert()
557 country_code VARCHAR(2);
561 {% if debug %}RAISE WARNING '% % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;{% endif %}
563 NEW.place_id := nextval('seq_place');
564 NEW.indexed_status := 1; --STATUS_NEW
566 NEW.centroid := ST_PointOnSurface(NEW.geometry);
567 NEW.country_code := lower(get_country_code(NEW.centroid));
569 NEW.partition := get_partition(NEW.country_code);
570 NEW.geometry_sector := geometry_sector(NEW.partition, NEW.centroid);
572 IF NEW.osm_type = 'X' THEN
573 -- E'X'ternal records should already be in the right format so do nothing
575 is_area := ST_GeometryType(NEW.geometry) IN ('ST_Polygon','ST_MultiPolygon');
577 IF NEW.class in ('place','boundary')
578 AND NEW.type in ('postcode','postal_code')
580 IF NEW.address IS NULL OR NOT NEW.address ? 'postcode' THEN
581 -- most likely just a part of a multipolygon postcode boundary, throw it away
585 NEW.name := hstore('ref', NEW.address->'postcode');
587 ELSEIF NEW.class = 'highway' AND is_area AND NEW.name is null
588 AND NEW.extratags ? 'area' AND NEW.extratags->'area' = 'yes'
591 ELSEIF NEW.class = 'boundary' AND NOT is_area
594 ELSEIF NEW.class = 'boundary' AND NEW.type = 'administrative'
595 AND NEW.admin_level <= 4 AND NEW.osm_type = 'W'
600 SELECT * INTO NEW.rank_search, NEW.rank_address
601 FROM compute_place_rank(NEW.country_code,
602 CASE WHEN is_area THEN 'A' ELSE NEW.osm_type END,
603 NEW.class, NEW.type, NEW.admin_level,
604 (NEW.extratags->'capital') = 'yes',
605 NEW.address->'postcode');
607 -- a country code make no sense below rank 4 (country)
608 IF NEW.rank_search < 4 THEN
609 NEW.country_code := NULL;
614 {% if debug %}RAISE WARNING 'placex_insert:END: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;{% endif %}
616 {% if not disable_diff_updates %}
617 -- The following is not needed until doing diff updates, and slows the main index process down
619 IF NEW.osm_type = 'N' and NEW.rank_search > 28 THEN
620 -- might be part of an interpolation
621 result := osmline_reinsert(NEW.osm_id, NEW.geometry);
622 ELSEIF NEW.rank_address > 0 THEN
623 IF (ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon') AND ST_IsValid(NEW.geometry)) THEN
624 -- Performance: We just can't handle re-indexing for country level changes
625 IF st_area(NEW.geometry) < 1 THEN
626 -- mark items within the geometry for re-indexing
627 -- RAISE WARNING 'placex poly insert: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
629 UPDATE placex SET indexed_status = 2
630 WHERE ST_Intersects(NEW.geometry, placex.geometry)
631 and indexed_status = 0
632 and ((rank_address = 0 and rank_search > NEW.rank_address)
633 or rank_address > NEW.rank_address
634 or (class = 'place' and osm_type = 'N')
636 and (rank_search < 28
638 or (NEW.rank_address >= 16 and address ? 'place'));
641 -- mark nearby items for re-indexing, where 'nearby' depends on the features rank_search and is a complete guess :(
642 diameter := update_place_diameter(NEW.rank_search);
644 -- RAISE WARNING 'placex point insert: % % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type,diameter;
645 IF NEW.rank_search >= 26 THEN
646 -- roads may cause reparenting for >27 rank places
647 update placex set indexed_status = 2 where indexed_status = 0 and rank_search > NEW.rank_search and ST_DWithin(placex.geometry, NEW.geometry, diameter);
648 -- reparenting also for OSM Interpolation Lines (and for Tiger?)
649 update location_property_osmline set indexed_status = 2 where indexed_status = 0 and ST_DWithin(location_property_osmline.linegeo, NEW.geometry, diameter);
650 ELSEIF NEW.rank_search >= 16 THEN
651 -- up to rank 16, street-less addresses may need reparenting
652 update placex set indexed_status = 2 where indexed_status = 0 and rank_search > NEW.rank_search and ST_DWithin(placex.geometry, NEW.geometry, diameter) and (rank_search < 28 or name is not null or address ? 'place');
654 -- for all other places the search terms may change as well
655 update placex set indexed_status = 2 where indexed_status = 0 and rank_search > NEW.rank_search and ST_DWithin(placex.geometry, NEW.geometry, diameter) and (rank_search < 28 or name is not null);
662 -- add to tables for special search
663 -- Note: won't work on initial import because the classtype tables
664 -- do not yet exist. It won't hurt either.
665 classtable := 'place_classtype_' || NEW.class || '_' || NEW.type;
666 SELECT count(*)>0 FROM pg_tables WHERE tablename = classtable and schemaname = current_schema() INTO result;
668 EXECUTE 'INSERT INTO ' || classtable::regclass || ' (place_id, centroid) VALUES ($1,$2)'
669 USING NEW.place_id, ST_Centroid(NEW.geometry);
672 {% endif %} -- not disable_diff_updates
680 CREATE OR REPLACE FUNCTION placex_update()
686 relation_members TEXT[];
689 parent_address_level SMALLINT;
690 place_address_level SMALLINT;
692 addr_street INTEGER[];
693 addr_place INTEGER[];
697 name_vector INTEGER[];
698 nameaddress_vector INTEGER[];
699 addr_nameaddress_vector INTEGER[];
703 linked_node_id BIGINT;
704 linked_importance FLOAT;
705 linked_wikipedia TEXT;
707 is_place_address BOOLEAN;
711 IF OLD.indexed_status = 100 THEN
712 {% if debug %}RAISE WARNING 'placex_update delete % %',NEW.osm_type,NEW.osm_id;{% endif %}
713 delete from placex where place_id = OLD.place_id;
717 IF NEW.indexed_status != 0 OR OLD.indexed_status = 0 THEN
721 {% if debug %}RAISE WARNING 'placex_update % % (%)',NEW.osm_type,NEW.osm_id,NEW.place_id;{% endif %}
723 NEW.indexed_date = now();
725 {% if 'search_name' in db.tables %}
726 DELETE from search_name WHERE place_id = NEW.place_id;
728 result := deleteSearchName(NEW.partition, NEW.place_id);
729 DELETE FROM place_addressline WHERE place_id = NEW.place_id;
730 result := deleteRoad(NEW.partition, NEW.place_id);
731 result := deleteLocationArea(NEW.partition, NEW.place_id, NEW.rank_search);
732 UPDATE placex set linked_place_id = null, indexed_status = 2
733 where linked_place_id = NEW.place_id;
734 -- update not necessary for osmline, cause linked_place_id does not exist
736 NEW.extratags := NEW.extratags - 'linked_place'::TEXT;
738 -- NEW.linked_place_id contains the precomputed linkee. Save this and restore
739 -- the previous link status.
740 linked_place := NEW.linked_place_id;
741 NEW.linked_place_id := OLD.linked_place_id;
743 IF NEW.linked_place_id is not null THEN
744 NEW.token_info := null;
745 {% if debug %}RAISE WARNING 'place already linked to %', OLD.linked_place_id;{% endif %}
749 -- Postcodes are just here to compute the centroids. They are not searchable
750 -- unless they are a boundary=postal_code.
751 -- There was an error in the style so that boundary=postal_code used to be
752 -- imported as place=postcode. That's why relations are allowed to pass here.
753 -- This can go away in a couple of versions.
754 IF NEW.class = 'place' and NEW.type = 'postcode' and NEW.osm_type != 'R' THEN
755 NEW.token_info := null;
759 -- Compute a preliminary centroid.
760 NEW.centroid := ST_PointOnSurface(NEW.geometry);
762 -- recalculate country and partition
763 IF NEW.rank_search = 4 AND NEW.address is not NULL AND NEW.address ? 'country' THEN
764 -- for countries, believe the mapped country code,
765 -- so that we remain in the right partition if the boundaries
767 NEW.country_code := lower(NEW.address->'country');
768 NEW.partition := get_partition(lower(NEW.country_code));
769 IF NEW.partition = 0 THEN
770 NEW.country_code := lower(get_country_code(NEW.centroid));
771 NEW.partition := get_partition(NEW.country_code);
774 IF NEW.rank_search >= 4 THEN
775 NEW.country_code := lower(get_country_code(NEW.centroid));
777 NEW.country_code := NULL;
779 NEW.partition := get_partition(NEW.country_code);
781 {% if debug %}RAISE WARNING 'Country updated: "%"', NEW.country_code;{% endif %}
784 -- recompute the ranks, they might change when linking changes
785 SELECT * INTO NEW.rank_search, NEW.rank_address
786 FROM compute_place_rank(NEW.country_code,
787 CASE WHEN ST_GeometryType(NEW.geometry)
788 IN ('ST_Polygon','ST_MultiPolygon')
789 THEN 'A' ELSE NEW.osm_type END,
790 NEW.class, NEW.type, NEW.admin_level,
791 (NEW.extratags->'capital') = 'yes',
792 NEW.address->'postcode');
793 -- We must always increase the address level relative to the admin boundary.
794 IF NEW.class = 'boundary' and NEW.type = 'administrative'
795 and NEW.osm_type = 'R' and NEW.rank_address > 0
797 -- First, check that admin boundaries do not overtake each other rank-wise.
798 parent_address_level := 3;
801 (CASE WHEN extratags ? 'wikidata' and NEW.extratags ? 'wikidata'
802 and extratags->'wikidata' = NEW.extratags->'wikidata'
803 THEN ST_Equals(geometry, NEW.geometry)
804 ELSE false END) as is_same
806 WHERE osm_type = 'R' and class = 'boundary' and type = 'administrative'
807 and admin_level < NEW.admin_level and admin_level > 3
809 and geometry && NEW.centroid and _ST_Covers(geometry, NEW.centroid)
810 ORDER BY admin_level desc LIMIT 1
812 IF location.is_same THEN
813 -- Looks like the same boundary is replicated on multiple admin_levels.
814 -- Usual tagging in Poland. Remove our boundary from addresses.
815 NEW.rank_address := 0;
817 parent_address_level := location.rank_address;
818 IF location.rank_address >= NEW.rank_address THEN
819 IF location.rank_address >= 24 THEN
820 NEW.rank_address := 25;
822 NEW.rank_address := location.rank_address + 2;
828 IF NEW.rank_address > 9 THEN
829 -- Second check that the boundary is not completely contained in a
830 -- place area with a higher address rank
832 SELECT rank_address FROM placex
833 WHERE class = 'place' and rank_address < 24
834 and rank_address > NEW.rank_address
835 and geometry && NEW.geometry
836 and geometry ~ NEW.geometry -- needed because ST_Relate does not do bbox cover test
837 and ST_Relate(geometry, NEW.geometry, 'T*T***FF*') -- contains but not equal
838 ORDER BY rank_address desc LIMIT 1
840 NEW.rank_address := location.rank_address + 2;
843 ELSEIF NEW.class = 'place' and NEW.osm_type = 'N'
844 and NEW.rank_address between 16 and 23
846 -- If a place node is contained in a admin boundary with the same address level
847 -- and has not been linked, then make the node a subpart by increasing the
848 -- address rank (city level and above).
850 SELECT rank_address FROM placex
851 WHERE osm_type = 'R' and class = 'boundary' and type = 'administrative'
852 and rank_address = NEW.rank_address
853 and geometry && NEW.centroid and _ST_Covers(geometry, NEW.centroid)
856 NEW.rank_address = NEW.rank_address + 2;
859 parent_address_level := 3;
862 NEW.housenumber := token_normalized_housenumber(NEW.token_info);
863 addr_street := token_addr_street_match_tokens(NEW.token_info);
864 addr_place := token_addr_place_match_tokens(NEW.token_info);
866 NEW.postcode := null;
868 -- waterway ways are linked when they are part of a relation and have the same class/type
869 IF NEW.osm_type = 'R' and NEW.class = 'waterway' THEN
870 FOR relation_members IN select members from planet_osm_rels r where r.id = NEW.osm_id and r.parts != array[]::bigint[]
872 FOR i IN 1..array_upper(relation_members, 1) BY 2 LOOP
873 IF relation_members[i+1] in ('', 'main_stream', 'side_stream') AND substring(relation_members[i],1,1) = 'w' THEN
874 {% if debug %}RAISE WARNING 'waterway parent %, child %/%', NEW.osm_id, i, relation_members[i];{% endif %}
875 FOR linked_node_id IN SELECT place_id FROM placex
876 WHERE osm_type = 'W' and osm_id = substring(relation_members[i],2,200)::bigint
877 and class = NEW.class and type in ('river', 'stream', 'canal', 'drain', 'ditch')
878 and ( relation_members[i+1] != 'side_stream' or NEW.name->'name' = name->'name')
880 UPDATE placex SET linked_place_id = NEW.place_id WHERE place_id = linked_node_id;
881 {% if 'search_name' in db.tables %}
882 DELETE FROM search_name WHERE place_id = linked_node_id;
888 {% if debug %}RAISE WARNING 'Waterway processed';{% endif %}
891 NEW.importance := null;
892 SELECT wikipedia, importance
893 FROM compute_importance(NEW.extratags, NEW.country_code, NEW.osm_type, NEW.osm_id)
894 INTO NEW.wikipedia,NEW.importance;
896 {% if debug %}RAISE WARNING 'Importance computed from wikipedia: %', NEW.importance;{% endif %}
898 -- ---------------------------------------------------------------------------
899 -- For low level elements we inherit from our parent road
900 IF NEW.rank_search > 27 THEN
902 {% if debug %}RAISE WARNING 'finding street for % %', NEW.osm_type, NEW.osm_id;{% endif %}
903 NEW.parent_place_id := null;
904 is_place_address := coalesce(not NEW.address ? 'street' and NEW.address ? 'place', FALSE);
906 -- We have to find our parent road.
907 NEW.parent_place_id := find_parent_for_poi(NEW.osm_type, NEW.osm_id,
909 ST_Envelope(NEW.geometry),
910 addr_street, addr_place,
913 -- If we found the road take a shortcut here.
914 -- Otherwise fall back to the full address getting method below.
915 IF NEW.parent_place_id is not null THEN
917 -- Get the details of the parent road
918 SELECT p.country_code, p.postcode, p.name FROM placex p
919 WHERE p.place_id = NEW.parent_place_id INTO location;
921 IF is_place_address THEN
922 -- Check if the addr:place tag is part of the parent name
923 SELECT count(*) INTO i
924 FROM svals(location.name) AS pname WHERE pname = NEW.address->'place';
926 NEW.address = NEW.address || hstore('_unlisted_place', NEW.address->'place');
930 NEW.country_code := location.country_code;
931 {% if debug %}RAISE WARNING 'Got parent details from search name';{% endif %}
933 -- determine postcode
934 NEW.postcode := coalesce(token_normalized_postcode(NEW.address->'postcode'),
936 get_nearest_postcode(NEW.country_code, NEW.centroid));
938 IF NEW.name is not NULL THEN
939 NEW.name := add_default_place_name(NEW.country_code, NEW.name);
942 {% if not db.reverse_only %}
943 IF NEW.name is not NULL OR NEW.address is not NULL THEN
944 SELECT * INTO name_vector, nameaddress_vector
945 FROM create_poi_search_terms(NEW.place_id,
946 NEW.partition, NEW.parent_place_id,
947 is_place_address, NEW.country_code,
948 NEW.token_info, NEW.centroid);
950 IF array_length(name_vector, 1) is not NULL THEN
951 INSERT INTO search_name (place_id, search_rank, address_rank,
952 importance, country_code, name_vector,
953 nameaddress_vector, centroid)
954 VALUES (NEW.place_id, NEW.rank_search, NEW.rank_address,
955 NEW.importance, NEW.country_code, name_vector,
956 nameaddress_vector, NEW.centroid);
957 {% if debug %}RAISE WARNING 'Place added to search table';{% endif %}
962 NEW.token_info := token_strip_info(NEW.token_info);
963 -- If the address was inherited from a surrounding building,
964 -- do not add it permanently to the table.
965 IF NEW.address ? '_inherited' THEN
966 IF NEW.address ? '_unlisted_place' THEN
967 NEW.address := hstore('_unlisted_place', NEW.address->'_unlisted_place');
978 -- ---------------------------------------------------------------------------
980 {% if debug %}RAISE WARNING 'Using full index mode for % %', NEW.osm_type, NEW.osm_id;{% endif %}
981 IF linked_place is not null THEN
982 SELECT * INTO location FROM placex WHERE place_id = linked_place;
984 {% if debug %}RAISE WARNING 'Linked %', location;{% endif %}
986 -- Use the linked point as the centre point of the geometry,
987 -- but only if it is within the area of the boundary.
988 geom := coalesce(location.centroid, ST_Centroid(location.geometry));
989 IF geom is not NULL AND ST_Within(geom, NEW.geometry) THEN
990 NEW.centroid := geom;
993 {% if debug %}RAISE WARNING 'parent address: % rank address: %', parent_address_level, location.rank_address;{% endif %}
994 IF location.rank_address > parent_address_level
995 and location.rank_address < 26
997 NEW.rank_address := location.rank_address;
1000 -- merge in extra tags
1001 NEW.extratags := hstore('linked_' || location.class, location.type)
1002 || coalesce(location.extratags, ''::hstore)
1003 || coalesce(NEW.extratags, ''::hstore);
1005 -- mark the linked place (excludes from search results)
1006 UPDATE placex set linked_place_id = NEW.place_id
1007 WHERE place_id = location.place_id;
1008 -- ensure that those places are not found anymore
1009 {% if 'search_name' in db.tables %}
1010 DELETE FROM search_name WHERE place_id = location.place_id;
1012 PERFORM deleteLocationArea(NEW.partition, location.place_id, NEW.rank_search);
1014 SELECT wikipedia, importance
1015 FROM compute_importance(location.extratags, NEW.country_code,
1016 'N', location.osm_id)
1017 INTO linked_wikipedia,linked_importance;
1019 -- Use the maximum importance if one could be computed from the linked object.
1020 IF linked_importance is not null AND
1021 (NEW.importance is null or NEW.importance < linked_importance)
1023 NEW.importance = linked_importance;
1026 -- No linked place? As a last resort check if the boundary is tagged with
1027 -- a place type and adapt the rank address.
1028 IF NEW.rank_address > 0 and NEW.extratags ? 'place' THEN
1029 SELECT address_rank INTO place_address_level
1030 FROM compute_place_rank(NEW.country_code, 'A', 'place',
1031 NEW.extratags->'place', 0::SMALLINT, False, null);
1032 IF place_address_level > parent_address_level and
1033 place_address_level < 26 THEN
1034 NEW.rank_address := place_address_level;
1039 IF NEW.admin_level = 2
1040 AND NEW.class = 'boundary' AND NEW.type = 'administrative'
1041 AND NEW.country_code IS NOT NULL AND NEW.osm_type = 'R'
1043 -- Update the list of country names. Adding an additional sanity
1044 -- check here: make sure the country does overlap with the area where
1045 -- we expect it to be as per static country grid.
1047 SELECT country_code FROM country_osm_grid
1048 WHERE ST_Covers(geometry, NEW.centroid) and country_code = NEW.country_code
1051 {% if debug %}RAISE WARNING 'Updating names for country '%' with: %', NEW.country_code, NEW.name;{% endif %}
1052 UPDATE country_name SET name = name || NEW.name WHERE country_code = NEW.country_code;
1056 -- For linear features we need the full geometry for determining the address
1057 -- because they may go through several administrative entities. Otherwise use
1058 -- the centroid for performance reasons.
1059 IF ST_GeometryType(NEW.geometry) in ('ST_LineString', 'ST_MultiLineString') THEN
1060 geom := NEW.geometry;
1062 geom := NEW.centroid;
1065 IF NEW.rank_address = 0 THEN
1066 max_rank := geometry_to_rank(NEW.rank_search, NEW.geometry, NEW.country_code);
1067 -- Rank 0 features may also span multiple administrative areas (e.g. lakes)
1068 -- so use the geometry here too. Just make sure the areas don't become too
1070 IF NEW.class = 'natural' or max_rank > 10 THEN
1071 geom := NEW.geometry;
1073 ELSEIF NEW.rank_address > 25 THEN
1076 max_rank := NEW.rank_address;
1079 SELECT * FROM insert_addresslines(NEW.place_id, NEW.partition, max_rank,
1080 NEW.token_info, geom, NEW.country_code)
1081 INTO NEW.parent_place_id, NEW.postcode, nameaddress_vector;
1083 {% if debug %}RAISE WARNING 'RETURN insert_addresslines: %, %, %', NEW.parent_place_id, NEW.postcode, nameaddress_vector;{% endif %}
1085 NEW.postcode := coalesce(token_normalized_postcode(NEW.address->'postcode'),
1088 -- if we have a name add this to the name search table
1089 IF NEW.name IS NOT NULL THEN
1090 -- Initialise the name vector using our name
1091 NEW.name := add_default_place_name(NEW.country_code, NEW.name);
1092 name_vector := token_get_name_search_tokens(NEW.token_info);
1094 IF NEW.rank_search <= 25 and NEW.rank_address > 0 THEN
1095 result := add_location(NEW.place_id, NEW.country_code, NEW.partition,
1096 name_vector, NEW.rank_search, NEW.rank_address,
1097 NEW.postcode, NEW.geometry, NEW.centroid);
1098 {% if debug %}RAISE WARNING 'added to location (full)';{% endif %}
1101 IF NEW.rank_search between 26 and 27 and NEW.class = 'highway' THEN
1102 result := insertLocationRoad(NEW.partition, NEW.place_id, NEW.country_code, NEW.geometry);
1103 {% if debug %}RAISE WARNING 'insert into road location table (full)';{% endif %}
1106 IF NEW.rank_address between 16 and 27 THEN
1107 result := insertSearchName(NEW.partition, NEW.place_id,
1108 token_get_name_match_tokens(NEW.token_info),
1109 NEW.rank_search, NEW.rank_address, NEW.geometry);
1111 {% if debug %}RAISE WARNING 'added to search name (full)';{% endif %}
1113 {% if not db.reverse_only %}
1114 INSERT INTO search_name (place_id, search_rank, address_rank,
1115 importance, country_code, name_vector,
1116 nameaddress_vector, centroid)
1117 VALUES (NEW.place_id, NEW.rank_search, NEW.rank_address,
1118 NEW.importance, NEW.country_code, name_vector,
1119 nameaddress_vector, NEW.centroid);
1123 IF NEW.postcode is null AND NEW.rank_search > 8 THEN
1124 NEW.postcode := get_nearest_postcode(NEW.country_code, NEW.geometry);
1127 {% if debug %}RAISE WARNING 'place update % % finsihed.', NEW.osm_type, NEW.osm_id;{% endif %}
1129 NEW.token_info := token_strip_info(NEW.token_info);
1136 CREATE OR REPLACE FUNCTION placex_delete()
1143 -- RAISE WARNING 'placex_delete % %',OLD.osm_type,OLD.osm_id;
1145 IF OLD.linked_place_id is null THEN
1146 update placex set linked_place_id = null, indexed_status = 2 where linked_place_id = OLD.place_id and indexed_status = 0;
1147 {% if debug %}RAISE WARNING 'placex_delete:01 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1148 update placex set linked_place_id = null where linked_place_id = OLD.place_id;
1149 {% if debug %}RAISE WARNING 'placex_delete:02 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1151 update placex set indexed_status = 2 where place_id = OLD.linked_place_id and indexed_status = 0;
1154 IF OLD.rank_address < 30 THEN
1156 -- mark everything linked to this place for re-indexing
1157 {% if debug %}RAISE WARNING 'placex_delete:03 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1158 UPDATE placex set indexed_status = 2 from place_addressline where address_place_id = OLD.place_id
1159 and placex.place_id = place_addressline.place_id and indexed_status = 0 and place_addressline.isaddress;
1161 {% if debug %}RAISE WARNING 'placex_delete:04 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1162 DELETE FROM place_addressline where address_place_id = OLD.place_id;
1164 {% if debug %}RAISE WARNING 'placex_delete:05 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1165 b := deleteRoad(OLD.partition, OLD.place_id);
1167 {% if debug %}RAISE WARNING 'placex_delete:06 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1168 update placex set indexed_status = 2 where parent_place_id = OLD.place_id and indexed_status = 0;
1169 {% if debug %}RAISE WARNING 'placex_delete:07 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1170 -- reparenting also for OSM Interpolation Lines (and for Tiger?)
1171 update location_property_osmline set indexed_status = 2 where indexed_status = 0 and parent_place_id = OLD.place_id;
1175 {% if debug %}RAISE WARNING 'placex_delete:08 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1177 IF OLD.rank_address < 26 THEN
1178 b := deleteLocationArea(OLD.partition, OLD.place_id, OLD.rank_search);
1181 {% if debug %}RAISE WARNING 'placex_delete:09 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1183 IF OLD.name is not null THEN
1184 {% if 'search_name' in db.tables %}
1185 DELETE from search_name WHERE place_id = OLD.place_id;
1187 b := deleteSearchName(OLD.partition, OLD.place_id);
1190 {% if debug %}RAISE WARNING 'placex_delete:10 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1192 DELETE FROM place_addressline where place_id = OLD.place_id;
1194 {% if debug %}RAISE WARNING 'placex_delete:11 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1196 -- remove from tables for special search
1197 classtable := 'place_classtype_' || OLD.class || '_' || OLD.type;
1198 SELECT count(*)>0 FROM pg_tables WHERE tablename = classtable and schemaname = current_schema() INTO b;
1200 EXECUTE 'DELETE FROM ' || classtable::regclass || ' WHERE place_id = $1' USING OLD.place_id;
1203 {% if debug %}RAISE WARNING 'placex_delete:12 % %',OLD.osm_type,OLD.osm_id;{% endif %}