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)
17 -- For POI nodes, check if the address should be derived from a surrounding
19 IF p.rank_search < 30 OR p.osm_type != 'N' OR p.address is not null THEN
20 RAISE WARNING 'self address for % %', p.osm_type, p.osm_id;
23 -- The additional && condition works around the misguided query
24 -- planner of postgis 3.0.
25 SELECT placex.address || hstore('_inherited', '') INTO address
27 WHERE ST_Covers(geometry, p.centroid)
28 and geometry && p.centroid
29 and placex.address is not null
30 and (placex.address ? 'housenumber' or placex.address ? 'street' or placex.address ? 'place')
31 and rank_search = 30 AND ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon')
33 RAISE WARNING 'other address for % %: % (%)', p.osm_type, p.osm_id, address, p.centroid;
36 address := address - '_unlisted_place'::TEXT;
39 country_feature := CASE WHEN p.admin_level = 2
40 and p.class = 'boundary' and p.type = 'administrative'
47 LANGUAGE plpgsql STABLE;
50 CREATE OR REPLACE FUNCTION find_associated_street(poi_osm_type CHAR(1),
59 SELECT members FROM planet_osm_rels
60 WHERE parts @> ARRAY[poi_osm_id]
61 and members @> ARRAY[lower(poi_osm_type) || poi_osm_id]
62 and tags @> ARRAY['associatedStreet']
64 FOR i IN 1..array_upper(location.members, 1) BY 2 LOOP
65 IF location.members[i+1] = 'street' THEN
67 SELECT place_id from placex
68 WHERE osm_type = 'W' and osm_id = substring(location.members[i],2)::bigint
70 and rank_search between 26 and 27
72 RETURN parent.place_id;
81 LANGUAGE plpgsql STABLE;
84 -- Find the parent road of a POI.
86 -- \returns Place ID of parent object or NULL if none
88 -- Copy data from linked items (POIs on ways, addr:street links, relations).
90 CREATE OR REPLACE FUNCTION find_parent_for_poi(poi_osm_type CHAR(1),
92 poi_partition SMALLINT,
94 addr_street INTEGER[],
96 is_place_addr BOOLEAN)
100 parent_place_id BIGINT DEFAULT NULL;
103 {% if debug %}RAISE WARNING 'finding street for % %', poi_osm_type, poi_osm_id;{% endif %}
105 -- Is this object part of an associatedStreet relation?
106 parent_place_id := find_associated_street(poi_osm_type, poi_osm_id);
108 IF parent_place_id is null THEN
109 parent_place_id := find_parent_for_address(addr_street, addr_place,
110 poi_partition, bbox);
113 IF parent_place_id is null and poi_osm_type = 'N' THEN
114 -- Is this node part of an interpolation?
116 SELECT q.parent_place_id
117 FROM location_property_osmline q, planet_osm_ways x
118 WHERE q.linegeo && bbox and x.id = q.osm_id
119 and poi_osm_id = any(x.nodes)
122 {% if debug %}RAISE WARNING 'Get parent from interpolation: %', location.parent_place_id;{% endif %}
123 RETURN location.parent_place_id;
127 SELECT p.place_id, p.osm_id, p.rank_search, p.address,
128 coalesce(p.centroid, ST_Centroid(p.geometry)) as centroid
129 FROM placex p, planet_osm_ways w
130 WHERE p.osm_type = 'W' and p.rank_search >= 26
131 and p.geometry && bbox
132 and w.id = p.osm_id and poi_osm_id = any(w.nodes)
134 {% if debug %}RAISE WARNING 'Node is part of way % ', location.osm_id;{% endif %}
136 -- Way IS a road then we are on it - that must be our road
137 IF location.rank_search < 28 THEN
138 {% if debug %}RAISE WARNING 'node in way that is a street %',location;{% endif %}
139 RETURN location.place_id;
142 parent_place_id := find_associated_street('W', location.osm_id);
146 IF parent_place_id is NULL THEN
147 IF is_place_addr THEN
148 -- The address is attached to a place we don't know.
149 -- Instead simply use the containing area with the largest rank.
151 SELECT place_id FROM placex
152 WHERE bbox && geometry AND _ST_Covers(geometry, ST_Centroid(bbox))
153 AND rank_address between 5 and 25
154 ORDER BY rank_address desc
156 RETURN location.place_id;
158 ELSEIF ST_Area(bbox) < 0.005 THEN
159 -- for smaller features get the nearest road
160 SELECT getNearestRoadPlaceId(poi_partition, bbox) INTO parent_place_id;
161 {% if debug %}RAISE WARNING 'Checked for nearest way (%)', parent_place_id;{% endif %}
163 -- for larger features simply find the area with the largest rank that
164 -- contains the bbox, only use addressable features
166 SELECT place_id FROM placex
167 WHERE bbox && geometry AND _ST_Covers(geometry, ST_Centroid(bbox))
168 AND rank_address between 5 and 25
169 ORDER BY rank_address desc
171 RETURN location.place_id;
176 RETURN parent_place_id;
179 LANGUAGE plpgsql STABLE;
181 -- Try to find a linked place for the given object.
182 CREATE OR REPLACE FUNCTION find_linked_place(bnd placex)
186 relation_members TEXT[];
188 linked_placex placex%ROWTYPE;
191 IF bnd.rank_search >= 26 or bnd.rank_address = 0
192 or ST_GeometryType(bnd.geometry) NOT IN ('ST_Polygon','ST_MultiPolygon')
193 or bnd.type IN ('postcode', 'postal_code')
198 IF bnd.osm_type = 'R' THEN
199 -- see if we have any special relation members
200 SELECT members FROM planet_osm_rels WHERE id = bnd.osm_id INTO relation_members;
201 {% if debug %}RAISE WARNING 'Got relation members';{% endif %}
203 -- Search for relation members with role 'lable'.
204 IF relation_members IS NOT NULL THEN
206 SELECT get_rel_node_members(relation_members, ARRAY['label']) as member
208 {% if debug %}RAISE WARNING 'Found label member %', rel_member.member;{% endif %}
212 WHERE osm_type = 'N' and osm_id = rel_member.member
215 {% if debug %}RAISE WARNING 'Linked label member';{% endif %}
216 RETURN linked_placex;
223 IF bnd.name ? 'name' THEN
224 bnd_name := lower(bnd.name->'name');
225 IF bnd_name = '' THEN
230 -- If extratags has a place tag, look for linked nodes by their place type.
231 -- Area and node still have to have the same name.
232 IF bnd.extratags ? 'place' and bnd_name is not null THEN
235 WHERE (position(lower(name->'name') in bnd_name) > 0
236 OR position(bnd_name in lower(name->'name')) > 0)
237 AND placex.class = 'place' AND placex.type = bnd.extratags->'place'
238 AND placex.osm_type = 'N'
239 AND placex.linked_place_id is null
240 AND placex.rank_search < 26 -- needed to select the right index
241 AND placex.type != 'postcode'
242 AND ST_Covers(bnd.geometry, placex.geometry)
244 {% if debug %}RAISE WARNING 'Found type-matching place node %', linked_placex.osm_id;{% endif %}
245 RETURN linked_placex;
249 IF bnd.extratags ? 'wikidata' THEN
252 WHERE placex.class = 'place' AND placex.osm_type = 'N'
253 AND placex.extratags ? 'wikidata' -- needed to select right index
254 AND placex.extratags->'wikidata' = bnd.extratags->'wikidata'
255 AND placex.linked_place_id is null
256 AND placex.rank_search < 26
257 AND _st_covers(bnd.geometry, placex.geometry)
258 ORDER BY lower(name->'name') = bnd_name desc
260 {% if debug %}RAISE WARNING 'Found wikidata-matching place node %', linked_placex.osm_id;{% endif %}
261 RETURN linked_placex;
265 -- Name searches can be done for ways as well as relations
266 IF bnd_name is not null THEN
267 {% if debug %}RAISE WARNING 'Looking for nodes with matching names';{% endif %}
269 SELECT placex.* from placex
270 WHERE lower(name->'name') = bnd_name
271 AND ((bnd.rank_address > 0
272 and bnd.rank_address = (compute_place_rank(placex.country_code,
274 placex.type, 15::SMALLINT,
275 false, placex.postcode)).address_rank)
276 OR (bnd.rank_address = 0 and placex.rank_search = bnd.rank_search))
277 AND placex.osm_type = 'N'
278 AND placex.class = 'place'
279 AND placex.linked_place_id is null
280 AND placex.rank_search < 26 -- needed to select the right index
281 AND placex.type != 'postcode'
282 AND ST_Covers(bnd.geometry, placex.geometry)
284 {% if debug %}RAISE WARNING 'Found matching place node %', linked_placex.osm_id;{% endif %}
285 RETURN linked_placex;
292 LANGUAGE plpgsql STABLE;
295 CREATE OR REPLACE FUNCTION create_poi_search_terms(obj_place_id BIGINT,
296 in_partition SMALLINT,
297 parent_place_id BIGINT,
298 is_place_addr BOOLEAN,
302 OUT name_vector INTEGER[],
303 OUT nameaddress_vector INTEGER[])
306 parent_name_vector INTEGER[];
307 parent_address_vector INTEGER[];
308 addr_place_ids INTEGER[];
309 hnr_vector INTEGER[];
313 parent_address_place_ids BIGINT[];
315 nameaddress_vector := '{}'::INTEGER[];
317 SELECT s.name_vector, s.nameaddress_vector
318 INTO parent_name_vector, parent_address_vector
320 WHERE s.place_id = parent_place_id;
323 SELECT (get_addr_tag_rank(key, country)).*, match_tokens, search_tokens
324 FROM token_get_address_tokens(token_info)
325 WHERE not search_tokens <@ parent_address_vector
327 addr_place := get_address_place(in_partition, geometry,
328 addr_item.from_rank, addr_item.to_rank,
329 addr_item.extent, addr_item.match_tokens);
331 IF addr_place is null THEN
332 -- No place found in OSM that matches. Make it at least searchable.
333 nameaddress_vector := array_merge(nameaddress_vector, addr_item.search_tokens);
335 IF parent_address_place_ids is null THEN
336 SELECT array_agg(parent_place_id) INTO parent_address_place_ids
337 FROM place_addressline
338 WHERE place_id = parent_place_id;
341 -- If the parent already lists the place in place_address line, then we
342 -- are done. Otherwise, add its own place_address line.
343 IF not parent_address_place_ids @> ARRAY[addr_place.place_id] THEN
344 nameaddress_vector := array_merge(nameaddress_vector, addr_place.keywords);
346 INSERT INTO place_addressline (place_id, address_place_id, fromarea,
347 isaddress, distance, cached_rank_address)
348 VALUES (obj_place_id, addr_place.place_id, not addr_place.isguess,
349 true, addr_place.distance, addr_place.rank_address);
354 name_vector := token_get_name_search_tokens(token_info);
356 -- Check if the parent covers all address terms.
357 -- If not, create a search name entry with the house number as the name.
358 -- This is unusual for the search_name table but prevents that the place
359 -- is returned when we only search for the street/place.
361 hnr_vector := token_get_housenumber_search_tokens(token_info);
363 IF hnr_vector is not null and not nameaddress_vector <@ parent_address_vector THEN
364 name_vector := array_merge(name_vector, hnr_vector);
367 IF is_place_addr THEN
368 addr_place_ids := token_addr_place_search_tokens(token_info);
369 IF not addr_place_ids <@ parent_name_vector THEN
370 -- make sure addr:place terms are always searchable
371 nameaddress_vector := array_merge(nameaddress_vector, addr_place_ids);
372 -- If there is a housenumber, also add the place name as a name,
373 -- so we can search it by the usual housenumber+place algorithms.
374 IF hnr_vector is not null THEN
375 name_vector := array_merge(name_vector, addr_place_ids);
380 -- Cheating here by not recomputing all terms but simply using the ones
381 -- from the parent object.
382 nameaddress_vector := array_merge(nameaddress_vector, parent_name_vector);
383 nameaddress_vector := array_merge(nameaddress_vector, parent_address_vector);
390 -- Insert address of a place into the place_addressline table.
392 -- \param obj_place_id Place_id of the place to compute the address for.
393 -- \param partition Partition number where the place is in.
394 -- \param maxrank Rank of the place. All address features must have
395 -- a search rank lower than the given rank.
396 -- \param address Address terms for the place.
397 -- \param geometry Geometry to which the address objects should be close.
399 -- \retval parent_place_id Place_id of the address object that is the direct
401 -- \retval postcode Postcode computed from the address. This is the
402 -- addr:postcode of one of the address objects. If
403 -- more than one of has a postcode, the highest ranking
404 -- one is used. May be NULL.
405 -- \retval nameaddress_vector Search terms for the address. This is the sum
406 -- of name terms of all address objects.
407 CREATE OR REPLACE FUNCTION insert_addresslines(obj_place_id BIGINT,
413 OUT parent_place_id BIGINT,
415 OUT nameaddress_vector INT[])
418 address_havelevel BOOLEAN[];
420 location_isaddress BOOLEAN;
421 current_boundary GEOMETRY := NULL;
422 current_node_area GEOMETRY := NULL;
424 parent_place_rank INT := 0;
425 addr_place_ids BIGINT[] := '{}'::int[];
426 new_address_vector INT[];
430 parent_place_id := 0;
431 nameaddress_vector := '{}'::int[];
433 address_havelevel := array_fill(false, ARRAY[maxrank]);
436 SELECT (get_address_place(partition, geometry, from_rank, to_rank,
437 extent, match_tokens)).*, search_tokens
438 FROM (SELECT (get_addr_tag_rank(key, country)).*, match_tokens, search_tokens
439 FROM token_get_address_tokens(token_info)) x
440 ORDER BY rank_address, distance, isguess desc
442 IF location.place_id is null THEN
443 {% if not db.reverse_only %}
444 nameaddress_vector := array_merge(nameaddress_vector, location.search_tokens);
447 {% if not db.reverse_only %}
448 nameaddress_vector := array_merge(nameaddress_vector, location.keywords::INTEGER[]);
451 location_isaddress := not address_havelevel[location.rank_address];
452 IF not address_havelevel[location.rank_address] THEN
453 address_havelevel[location.rank_address] := true;
454 IF parent_place_rank < location.rank_address THEN
455 parent_place_id := location.place_id;
456 parent_place_rank := location.rank_address;
460 INSERT INTO place_addressline (place_id, address_place_id, fromarea,
461 isaddress, distance, cached_rank_address)
462 VALUES (obj_place_id, location.place_id, not location.isguess,
463 true, location.distance, location.rank_address);
465 addr_place_ids := addr_place_ids || location.place_id;
470 SELECT * FROM getNearFeatures(partition, geometry, maxrank)
471 WHERE not addr_place_ids @> ARRAY[place_id]
472 ORDER BY rank_address, isguess asc,
474 CASE WHEN rank_address = 16 AND rank_search = 15 THEN 0.2
475 WHEN rank_address = 16 AND rank_search = 16 THEN 0.25
476 WHEN rank_address = 16 AND rank_search = 18 THEN 0.5
479 -- Ignore all place nodes that do not fit in a lower level boundary.
480 CONTINUE WHEN location.isguess
481 and current_boundary is not NULL
482 and not ST_Contains(current_boundary, location.centroid);
484 -- If this is the first item in the rank, then assume it is the address.
485 location_isaddress := not address_havelevel[location.rank_address];
487 -- Further sanity checks to ensure that the address forms a sane hierarchy.
488 IF location_isaddress THEN
489 IF location.isguess and current_node_area is not NULL THEN
490 location_isaddress := ST_Contains(current_node_area, location.centroid);
492 IF not location.isguess and current_boundary is not NULL
493 and location.rank_address != 11 AND location.rank_address != 5 THEN
494 location_isaddress := ST_Contains(current_boundary, location.centroid);
498 IF location_isaddress THEN
499 address_havelevel[location.rank_address] := true;
500 parent_place_id := location.place_id;
502 -- Set postcode if we have one.
503 -- (Returned will be the highest ranking one.)
504 IF location.postcode is not NULL THEN
505 postcode = location.postcode;
508 -- Recompute the areas we need for hierarchy sanity checks.
509 IF location.rank_address != 11 AND location.rank_address != 5 THEN
510 IF location.isguess THEN
511 current_node_area := place_node_fuzzy_area(location.centroid,
512 location.rank_search);
514 current_node_area := NULL;
515 SELECT p.geometry FROM placex p
516 WHERE p.place_id = location.place_id INTO current_boundary;
521 -- Add it to the list of search terms
522 {% if not db.reverse_only %}
523 nameaddress_vector := array_merge(nameaddress_vector,
524 location.keywords::integer[]);
527 INSERT INTO place_addressline (place_id, address_place_id, fromarea,
528 isaddress, distance, cached_rank_address)
529 VALUES (obj_place_id, location.place_id, not location.isguess,
530 location_isaddress, location.distance, location.rank_address);
537 CREATE OR REPLACE FUNCTION placex_insert()
544 country_code VARCHAR(2);
548 {% if debug %}RAISE WARNING '% % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;{% endif %}
550 NEW.place_id := nextval('seq_place');
551 NEW.indexed_status := 1; --STATUS_NEW
553 NEW.centroid := ST_PointOnSurface(NEW.geometry);
554 NEW.country_code := lower(get_country_code(NEW.centroid));
556 NEW.partition := get_partition(NEW.country_code);
557 NEW.geometry_sector := geometry_sector(NEW.partition, NEW.centroid);
559 IF NEW.osm_type = 'X' THEN
560 -- E'X'ternal records should already be in the right format so do nothing
562 is_area := ST_GeometryType(NEW.geometry) IN ('ST_Polygon','ST_MultiPolygon');
564 IF NEW.class in ('place','boundary')
565 AND NEW.type in ('postcode','postal_code')
567 IF NEW.address IS NULL OR NOT NEW.address ? 'postcode' THEN
568 -- most likely just a part of a multipolygon postcode boundary, throw it away
572 NEW.name := hstore('ref', NEW.address->'postcode');
574 ELSEIF NEW.class = 'highway' AND is_area AND NEW.name is null
575 AND NEW.extratags ? 'area' AND NEW.extratags->'area' = 'yes'
578 ELSEIF NEW.class = 'boundary' AND NOT is_area
581 ELSEIF NEW.class = 'boundary' AND NEW.type = 'administrative'
582 AND NEW.admin_level <= 4 AND NEW.osm_type = 'W'
587 SELECT * INTO NEW.rank_search, NEW.rank_address
588 FROM compute_place_rank(NEW.country_code,
589 CASE WHEN is_area THEN 'A' ELSE NEW.osm_type END,
590 NEW.class, NEW.type, NEW.admin_level,
591 (NEW.extratags->'capital') = 'yes',
592 NEW.address->'postcode');
594 -- a country code make no sense below rank 4 (country)
595 IF NEW.rank_search < 4 THEN
596 NEW.country_code := NULL;
601 {% if debug %}RAISE WARNING 'placex_insert:END: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;{% endif %}
603 {% if not disable_diff_updates %}
604 -- The following is not needed until doing diff updates, and slows the main index process down
606 IF NEW.osm_type = 'N' and NEW.rank_search > 28 THEN
607 -- might be part of an interpolation
608 result := osmline_reinsert(NEW.osm_id, NEW.geometry);
609 ELSEIF NEW.rank_address > 0 THEN
610 IF (ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon') AND ST_IsValid(NEW.geometry)) THEN
611 -- Performance: We just can't handle re-indexing for country level changes
612 IF st_area(NEW.geometry) < 1 THEN
613 -- mark items within the geometry for re-indexing
614 -- RAISE WARNING 'placex poly insert: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
616 UPDATE placex SET indexed_status = 2
617 WHERE ST_Intersects(NEW.geometry, placex.geometry)
618 and indexed_status = 0
619 and ((rank_address = 0 and rank_search > NEW.rank_address)
620 or rank_address > NEW.rank_address
621 or (class = 'place' and osm_type = 'N')
623 and (rank_search < 28
625 or (NEW.rank_address >= 16 and address ? 'place'));
628 -- mark nearby items for re-indexing, where 'nearby' depends on the features rank_search and is a complete guess :(
629 diameter := update_place_diameter(NEW.rank_search);
631 -- RAISE WARNING 'placex point insert: % % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type,diameter;
632 IF NEW.rank_search >= 26 THEN
633 -- roads may cause reparenting for >27 rank places
634 update placex set indexed_status = 2 where indexed_status = 0 and rank_search > NEW.rank_search and ST_DWithin(placex.geometry, NEW.geometry, diameter);
635 -- reparenting also for OSM Interpolation Lines (and for Tiger?)
636 update location_property_osmline set indexed_status = 2 where indexed_status = 0 and ST_DWithin(location_property_osmline.linegeo, NEW.geometry, diameter);
637 ELSEIF NEW.rank_search >= 16 THEN
638 -- up to rank 16, street-less addresses may need reparenting
639 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');
641 -- for all other places the search terms may change as well
642 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);
649 -- add to tables for special search
650 -- Note: won't work on initial import because the classtype tables
651 -- do not yet exist. It won't hurt either.
652 classtable := 'place_classtype_' || NEW.class || '_' || NEW.type;
653 SELECT count(*)>0 FROM pg_tables WHERE tablename = classtable and schemaname = current_schema() INTO result;
655 EXECUTE 'INSERT INTO ' || classtable::regclass || ' (place_id, centroid) VALUES ($1,$2)'
656 USING NEW.place_id, ST_Centroid(NEW.geometry);
659 {% endif %} -- not disable_diff_updates
667 CREATE OR REPLACE FUNCTION placex_update()
673 relation_members TEXT[];
676 parent_address_level SMALLINT;
677 place_address_level SMALLINT;
679 addr_street INTEGER[];
680 addr_place INTEGER[];
684 name_vector INTEGER[];
685 nameaddress_vector INTEGER[];
686 addr_nameaddress_vector INTEGER[];
688 linked_node_id BIGINT;
689 linked_importance FLOAT;
690 linked_wikipedia TEXT;
692 is_place_address BOOLEAN;
696 IF OLD.indexed_status = 100 THEN
697 {% if debug %}RAISE WARNING 'placex_update delete % %',NEW.osm_type,NEW.osm_id;{% endif %}
698 delete from placex where place_id = OLD.place_id;
702 IF NEW.indexed_status != 0 OR OLD.indexed_status = 0 THEN
706 {% if debug %}RAISE WARNING 'placex_update % % (%)',NEW.osm_type,NEW.osm_id,NEW.place_id;{% endif %}
708 NEW.indexed_date = now();
710 {% if 'search_name' in db.tables %}
711 DELETE from search_name WHERE place_id = NEW.place_id;
713 result := deleteSearchName(NEW.partition, NEW.place_id);
714 DELETE FROM place_addressline WHERE place_id = NEW.place_id;
715 result := deleteRoad(NEW.partition, NEW.place_id);
716 result := deleteLocationArea(NEW.partition, NEW.place_id, NEW.rank_search);
717 UPDATE placex set linked_place_id = null, indexed_status = 2
718 where linked_place_id = NEW.place_id;
719 -- update not necessary for osmline, cause linked_place_id does not exist
721 NEW.extratags := NEW.extratags - 'linked_place'::TEXT;
723 IF NEW.linked_place_id is not null THEN
724 NEW.token_info := null;
725 {% if debug %}RAISE WARNING 'place already linked to %', NEW.linked_place_id;{% endif %}
729 -- Postcodes are just here to compute the centroids. They are not searchable
730 -- unless they are a boundary=postal_code.
731 -- There was an error in the style so that boundary=postal_code used to be
732 -- imported as place=postcode. That's why relations are allowed to pass here.
733 -- This can go away in a couple of versions.
734 IF NEW.class = 'place' and NEW.type = 'postcode' and NEW.osm_type != 'R' THEN
735 NEW.token_info := null;
739 -- Compute a preliminary centroid.
740 NEW.centroid := ST_PointOnSurface(NEW.geometry);
742 -- recalculate country and partition
743 IF NEW.rank_search = 4 AND NEW.address is not NULL AND NEW.address ? 'country' THEN
744 -- for countries, believe the mapped country code,
745 -- so that we remain in the right partition if the boundaries
747 NEW.country_code := lower(NEW.address->'country');
748 NEW.partition := get_partition(lower(NEW.country_code));
749 IF NEW.partition = 0 THEN
750 NEW.country_code := lower(get_country_code(NEW.centroid));
751 NEW.partition := get_partition(NEW.country_code);
754 IF NEW.rank_search >= 4 THEN
755 NEW.country_code := lower(get_country_code(NEW.centroid));
757 NEW.country_code := NULL;
759 NEW.partition := get_partition(NEW.country_code);
761 {% if debug %}RAISE WARNING 'Country updated: "%"', NEW.country_code;{% endif %}
764 -- recompute the ranks, they might change when linking changes
765 SELECT * INTO NEW.rank_search, NEW.rank_address
766 FROM compute_place_rank(NEW.country_code,
767 CASE WHEN ST_GeometryType(NEW.geometry)
768 IN ('ST_Polygon','ST_MultiPolygon')
769 THEN 'A' ELSE NEW.osm_type END,
770 NEW.class, NEW.type, NEW.admin_level,
771 (NEW.extratags->'capital') = 'yes',
772 NEW.address->'postcode');
773 -- We must always increase the address level relative to the admin boundary.
774 IF NEW.class = 'boundary' and NEW.type = 'administrative'
775 and NEW.osm_type = 'R' and NEW.rank_address > 0
777 -- First, check that admin boundaries do not overtake each other rank-wise.
778 parent_address_level := 3;
781 (CASE WHEN extratags ? 'wikidata' and NEW.extratags ? 'wikidata'
782 and extratags->'wikidata' = NEW.extratags->'wikidata'
783 THEN ST_Equals(geometry, NEW.geometry)
784 ELSE false END) as is_same
786 WHERE osm_type = 'R' and class = 'boundary' and type = 'administrative'
787 and admin_level < NEW.admin_level and admin_level > 3
789 and geometry && NEW.centroid and _ST_Covers(geometry, NEW.centroid)
790 ORDER BY admin_level desc LIMIT 1
792 IF location.is_same THEN
793 -- Looks like the same boundary is replicated on multiple admin_levels.
794 -- Usual tagging in Poland. Remove our boundary from addresses.
795 NEW.rank_address := 0;
797 parent_address_level := location.rank_address;
798 IF location.rank_address >= NEW.rank_address THEN
799 IF location.rank_address >= 24 THEN
800 NEW.rank_address := 25;
802 NEW.rank_address := location.rank_address + 2;
808 IF NEW.rank_address > 9 THEN
809 -- Second check that the boundary is not completely contained in a
810 -- place area with a higher address rank
812 SELECT rank_address FROM placex
813 WHERE class = 'place' and rank_address < 24
814 and rank_address > NEW.rank_address
815 and geometry && NEW.geometry
816 and geometry ~ NEW.geometry -- needed because ST_Relate does not do bbox cover test
817 and ST_Relate(geometry, NEW.geometry, 'T*T***FF*') -- contains but not equal
818 ORDER BY rank_address desc LIMIT 1
820 NEW.rank_address := location.rank_address + 2;
823 ELSEIF NEW.class = 'place' and NEW.osm_type = 'N'
824 and NEW.rank_address between 16 and 23
826 -- If a place node is contained in a admin boundary with the same address level
827 -- and has not been linked, then make the node a subpart by increasing the
828 -- address rank (city level and above).
830 SELECT rank_address FROM placex
831 WHERE osm_type = 'R' and class = 'boundary' and type = 'administrative'
832 and rank_address = NEW.rank_address
833 and geometry && NEW.centroid and _ST_Covers(geometry, NEW.centroid)
836 NEW.rank_address = NEW.rank_address + 2;
839 parent_address_level := 3;
842 NEW.housenumber := token_normalized_housenumber(NEW.token_info);
843 addr_street := token_addr_street_match_tokens(NEW.token_info);
844 addr_place := token_addr_place_match_tokens(NEW.token_info);
846 NEW.postcode := null;
848 -- waterway ways are linked when they are part of a relation and have the same class/type
849 IF NEW.osm_type = 'R' and NEW.class = 'waterway' THEN
850 FOR relation_members IN select members from planet_osm_rels r where r.id = NEW.osm_id and r.parts != array[]::bigint[]
852 FOR i IN 1..array_upper(relation_members, 1) BY 2 LOOP
853 IF relation_members[i+1] in ('', 'main_stream', 'side_stream') AND substring(relation_members[i],1,1) = 'w' THEN
854 {% if debug %}RAISE WARNING 'waterway parent %, child %/%', NEW.osm_id, i, relation_members[i];{% endif %}
855 FOR linked_node_id IN SELECT place_id FROM placex
856 WHERE osm_type = 'W' and osm_id = substring(relation_members[i],2,200)::bigint
857 and class = NEW.class and type in ('river', 'stream', 'canal', 'drain', 'ditch')
858 and ( relation_members[i+1] != 'side_stream' or NEW.name->'name' = name->'name')
860 UPDATE placex SET linked_place_id = NEW.place_id WHERE place_id = linked_node_id;
861 {% if 'search_name' in db.tables %}
862 DELETE FROM search_name WHERE place_id = linked_node_id;
868 {% if debug %}RAISE WARNING 'Waterway processed';{% endif %}
871 NEW.importance := null;
872 SELECT wikipedia, importance
873 FROM compute_importance(NEW.extratags, NEW.country_code, NEW.osm_type, NEW.osm_id)
874 INTO NEW.wikipedia,NEW.importance;
876 {% if debug %}RAISE WARNING 'Importance computed from wikipedia: %', NEW.importance;{% endif %}
878 -- ---------------------------------------------------------------------------
879 -- For low level elements we inherit from our parent road
880 IF NEW.rank_search > 27 THEN
882 {% if debug %}RAISE WARNING 'finding street for % %', NEW.osm_type, NEW.osm_id;{% endif %}
883 NEW.parent_place_id := null;
884 is_place_address := coalesce(not NEW.address ? 'street' and NEW.address ? 'place', FALSE);
886 -- We have to find our parent road.
887 NEW.parent_place_id := find_parent_for_poi(NEW.osm_type, NEW.osm_id,
889 ST_Envelope(NEW.geometry),
890 addr_street, addr_place,
893 -- If we found the road take a shortcut here.
894 -- Otherwise fall back to the full address getting method below.
895 IF NEW.parent_place_id is not null THEN
897 -- Get the details of the parent road
898 SELECT p.country_code, p.postcode, p.name FROM placex p
899 WHERE p.place_id = NEW.parent_place_id INTO location;
901 IF is_place_address THEN
902 -- Check if the addr:place tag is part of the parent name
903 SELECT count(*) INTO i
904 FROM svals(location.name) AS pname WHERE pname = NEW.address->'place';
906 NEW.address = NEW.address || hstore('_unlisted_place', NEW.address->'place');
910 NEW.country_code := location.country_code;
911 {% if debug %}RAISE WARNING 'Got parent details from search name';{% endif %}
913 -- determine postcode
914 NEW.postcode := coalesce(token_normalized_postcode(NEW.address->'postcode'),
916 get_nearest_postcode(NEW.country_code, NEW.geometry));
918 IF NEW.name is not NULL THEN
919 NEW.name := add_default_place_name(NEW.country_code, NEW.name);
922 {% if not db.reverse_only %}
923 IF NEW.name is not NULL OR NEW.address is not NULL THEN
924 SELECT * INTO name_vector, nameaddress_vector
925 FROM create_poi_search_terms(NEW.place_id,
926 NEW.partition, NEW.parent_place_id,
927 is_place_address, NEW.country_code,
928 NEW.token_info, NEW.centroid);
930 IF array_length(name_vector, 1) is not NULL THEN
931 INSERT INTO search_name (place_id, search_rank, address_rank,
932 importance, country_code, name_vector,
933 nameaddress_vector, centroid)
934 VALUES (NEW.place_id, NEW.rank_search, NEW.rank_address,
935 NEW.importance, NEW.country_code, name_vector,
936 nameaddress_vector, NEW.centroid);
937 {% if debug %}RAISE WARNING 'Place added to search table';{% endif %}
942 NEW.token_info := token_strip_info(NEW.token_info);
943 -- If the address was inherited from a surrounding building,
944 -- do not add it permanently to the table.
945 IF NEW.address ? '_inherited' THEN
946 IF NEW.address ? '_unlisted_place' THEN
947 NEW.address := hstore('_unlisted_place', NEW.address->'_unlisted_place');
958 -- ---------------------------------------------------------------------------
960 {% if debug %}RAISE WARNING 'Using full index mode for % %', NEW.osm_type, NEW.osm_id;{% endif %}
961 SELECT * INTO location FROM find_linked_place(NEW);
962 IF location.place_id is not null THEN
963 {% if debug %}RAISE WARNING 'Linked %', location;{% endif %}
965 -- Use the linked point as the centre point of the geometry,
966 -- but only if it is within the area of the boundary.
967 geom := coalesce(location.centroid, ST_Centroid(location.geometry));
968 IF geom is not NULL AND ST_Within(geom, NEW.geometry) THEN
969 NEW.centroid := geom;
972 {% if debug %}RAISE WARNING 'parent address: % rank address: %', parent_address_level, location.rank_address;{% endif %}
973 IF location.rank_address > parent_address_level
974 and location.rank_address < 26
976 NEW.rank_address := location.rank_address;
979 -- merge in the label name
980 IF NOT location.name IS NULL THEN
981 NEW.name := location.name || NEW.name;
984 -- merge in extra tags
985 NEW.extratags := hstore('linked_' || location.class, location.type)
986 || coalesce(location.extratags, ''::hstore)
987 || coalesce(NEW.extratags, ''::hstore);
989 -- mark the linked place (excludes from search results)
990 UPDATE placex set linked_place_id = NEW.place_id
991 WHERE place_id = location.place_id;
992 -- ensure that those places are not found anymore
993 {% if 'search_name' in db.tables %}
994 DELETE FROM search_name WHERE place_id = location.place_id;
996 PERFORM deleteLocationArea(NEW.partition, location.place_id, NEW.rank_search);
998 SELECT wikipedia, importance
999 FROM compute_importance(location.extratags, NEW.country_code,
1000 'N', location.osm_id)
1001 INTO linked_wikipedia,linked_importance;
1003 -- Use the maximum importance if one could be computed from the linked object.
1004 IF linked_importance is not null AND
1005 (NEW.importance is null or NEW.importance < linked_importance)
1007 NEW.importance = linked_importance;
1010 -- No linked place? As a last resort check if the boundary is tagged with
1011 -- a place type and adapt the rank address.
1012 IF NEW.rank_address > 0 and NEW.extratags ? 'place' THEN
1013 SELECT address_rank INTO place_address_level
1014 FROM compute_place_rank(NEW.country_code, 'A', 'place',
1015 NEW.extratags->'place', 0::SMALLINT, False, null);
1016 IF place_address_level > parent_address_level and
1017 place_address_level < 26 THEN
1018 NEW.rank_address := place_address_level;
1023 IF NEW.admin_level = 2
1024 AND NEW.class = 'boundary' AND NEW.type = 'administrative'
1025 AND NEW.country_code IS NOT NULL AND NEW.osm_type = 'R'
1027 -- Update the list of country names. Adding an additional sanity
1028 -- check here: make sure the country does overlap with the area where
1029 -- we expect it to be as per static country grid.
1031 SELECT country_code FROM country_osm_grid
1032 WHERE ST_Covers(geometry, NEW.centroid) and country_code = NEW.country_code
1035 {% if debug %}RAISE WARNING 'Updating names for country '%' with: %', NEW.country_code, NEW.name;{% endif %}
1036 UPDATE country_name SET name = name || NEW.name WHERE country_code = NEW.country_code;
1040 -- For linear features we need the full geometry for determining the address
1041 -- because they may go through several administrative entities. Otherwise use
1042 -- the centroid for performance reasons.
1043 IF ST_GeometryType(NEW.geometry) in ('ST_LineString', 'ST_MultiLineString') THEN
1044 geom := NEW.geometry;
1046 geom := NEW.centroid;
1049 IF NEW.rank_address = 0 THEN
1050 max_rank := geometry_to_rank(NEW.rank_search, NEW.geometry, NEW.country_code);
1051 -- Rank 0 features may also span multiple administrative areas (e.g. lakes)
1052 -- so use the geometry here too. Just make sure the areas don't become too
1054 IF NEW.class = 'natural' or max_rank > 10 THEN
1055 geom := NEW.geometry;
1057 ELSEIF NEW.rank_address > 25 THEN
1060 max_rank := NEW.rank_address;
1063 SELECT * FROM insert_addresslines(NEW.place_id, NEW.partition, max_rank,
1064 NEW.token_info, geom, NEW.country_code)
1065 INTO NEW.parent_place_id, NEW.postcode, nameaddress_vector;
1067 {% if debug %}RAISE WARNING 'RETURN insert_addresslines: %, %, %', NEW.parent_place_id, NEW.postcode, nameaddress_vector;{% endif %}
1069 NEW.postcode := coalesce(token_normalized_postcode(NEW.address->'postcode'),
1072 -- if we have a name add this to the name search table
1073 IF NEW.name IS NOT NULL THEN
1074 -- Initialise the name vector using our name
1075 NEW.name := add_default_place_name(NEW.country_code, NEW.name);
1076 name_vector := token_get_name_search_tokens(NEW.token_info);
1078 IF NEW.rank_search <= 25 and NEW.rank_address > 0 THEN
1079 result := add_location(NEW.place_id, NEW.country_code, NEW.partition,
1080 name_vector, NEW.rank_search, NEW.rank_address,
1081 NEW.postcode, NEW.geometry, NEW.centroid);
1082 {% if debug %}RAISE WARNING 'added to location (full)';{% endif %}
1085 IF NEW.rank_search between 26 and 27 and NEW.class = 'highway' THEN
1086 result := insertLocationRoad(NEW.partition, NEW.place_id, NEW.country_code, NEW.geometry);
1087 {% if debug %}RAISE WARNING 'insert into road location table (full)';{% endif %}
1090 IF NEW.rank_address between 16 and 27 THEN
1091 result := insertSearchName(NEW.partition, NEW.place_id,
1092 token_get_name_match_tokens(NEW.token_info),
1093 NEW.rank_search, NEW.rank_address, NEW.geometry);
1095 {% if debug %}RAISE WARNING 'added to search name (full)';{% endif %}
1097 {% if not db.reverse_only %}
1098 INSERT INTO search_name (place_id, search_rank, address_rank,
1099 importance, country_code, name_vector,
1100 nameaddress_vector, centroid)
1101 VALUES (NEW.place_id, NEW.rank_search, NEW.rank_address,
1102 NEW.importance, NEW.country_code, name_vector,
1103 nameaddress_vector, NEW.centroid);
1107 IF NEW.postcode is null AND NEW.rank_search > 8 THEN
1108 NEW.postcode := get_nearest_postcode(NEW.country_code, NEW.geometry);
1111 {% if debug %}RAISE WARNING 'place update % % finsihed.', NEW.osm_type, NEW.osm_id;{% endif %}
1113 NEW.token_info := token_strip_info(NEW.token_info);
1120 CREATE OR REPLACE FUNCTION placex_delete()
1127 -- RAISE WARNING 'placex_delete % %',OLD.osm_type,OLD.osm_id;
1129 IF OLD.linked_place_id is null THEN
1130 update placex set linked_place_id = null, indexed_status = 2 where linked_place_id = OLD.place_id and indexed_status = 0;
1131 {% if debug %}RAISE WARNING 'placex_delete:01 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1132 update placex set linked_place_id = null where linked_place_id = OLD.place_id;
1133 {% if debug %}RAISE WARNING 'placex_delete:02 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1135 update placex set indexed_status = 2 where place_id = OLD.linked_place_id and indexed_status = 0;
1138 IF OLD.rank_address < 30 THEN
1140 -- mark everything linked to this place for re-indexing
1141 {% if debug %}RAISE WARNING 'placex_delete:03 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1142 UPDATE placex set indexed_status = 2 from place_addressline where address_place_id = OLD.place_id
1143 and placex.place_id = place_addressline.place_id and indexed_status = 0 and place_addressline.isaddress;
1145 {% if debug %}RAISE WARNING 'placex_delete:04 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1146 DELETE FROM place_addressline where address_place_id = OLD.place_id;
1148 {% if debug %}RAISE WARNING 'placex_delete:05 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1149 b := deleteRoad(OLD.partition, OLD.place_id);
1151 {% if debug %}RAISE WARNING 'placex_delete:06 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1152 update placex set indexed_status = 2 where parent_place_id = OLD.place_id and indexed_status = 0;
1153 {% if debug %}RAISE WARNING 'placex_delete:07 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1154 -- reparenting also for OSM Interpolation Lines (and for Tiger?)
1155 update location_property_osmline set indexed_status = 2 where indexed_status = 0 and parent_place_id = OLD.place_id;
1159 {% if debug %}RAISE WARNING 'placex_delete:08 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1161 IF OLD.rank_address < 26 THEN
1162 b := deleteLocationArea(OLD.partition, OLD.place_id, OLD.rank_search);
1165 {% if debug %}RAISE WARNING 'placex_delete:09 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1167 IF OLD.name is not null THEN
1168 {% if 'search_name' in db.tables %}
1169 DELETE from search_name WHERE place_id = OLD.place_id;
1171 b := deleteSearchName(OLD.partition, OLD.place_id);
1174 {% if debug %}RAISE WARNING 'placex_delete:10 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1176 DELETE FROM place_addressline where place_id = OLD.place_id;
1178 {% if debug %}RAISE WARNING 'placex_delete:11 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1180 -- remove from tables for special search
1181 classtable := 'place_classtype_' || OLD.class || '_' || OLD.type;
1182 SELECT count(*)>0 FROM pg_tables WHERE tablename = classtable and schemaname = current_schema() INTO b;
1184 EXECUTE 'DELETE FROM ' || classtable::regclass || ' WHERE place_id = $1' USING OLD.place_id;
1187 {% if debug %}RAISE WARNING 'placex_delete:12 % %',OLD.osm_type,OLD.osm_id;{% endif %}