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 -- Find the parent road of a POI.
52 -- \returns Place ID of parent object or NULL if none
54 -- Copy data from linked items (POIs on ways, addr:street links, relations).
56 CREATE OR REPLACE FUNCTION find_parent_for_poi(poi_osm_type CHAR(1),
58 poi_partition SMALLINT,
66 parent_place_id BIGINT DEFAULT NULL;
70 {% if debug %}RAISE WARNING 'finding street for % %', poi_osm_type, poi_osm_id;{% endif %}
72 -- Is this object part of an associatedStreet relation?
74 SELECT members FROM planet_osm_rels
75 WHERE parts @> ARRAY[poi_osm_id]
76 and members @> ARRAY[lower(poi_osm_type) || poi_osm_id]
77 and tags @> ARRAY['associatedStreet']
79 FOR i IN 1..array_upper(location.members, 1) BY 2 LOOP
80 IF location.members[i+1] = 'street' THEN
82 SELECT place_id from placex
83 WHERE osm_type = 'W' and osm_id = substring(location.members[i],2)::bigint
85 and rank_search between 26 and 27
87 RETURN parent.place_id;
93 parent_place_id := find_parent_for_address(addr_street, addr_place,
95 IF parent_place_id is not null THEN
96 RETURN parent_place_id;
99 IF poi_osm_type = 'N' THEN
100 -- Is this node part of an interpolation?
102 SELECT q.parent_place_id
103 FROM location_property_osmline q, planet_osm_ways x
104 WHERE q.linegeo && bbox and x.id = q.osm_id
105 and poi_osm_id = any(x.nodes)
108 {% if debug %}RAISE WARNING 'Get parent from interpolation: %', parent.parent_place_id;{% endif %}
109 RETURN parent.parent_place_id;
112 -- Is this node part of any other way?
114 SELECT p.place_id, p.osm_id, p.rank_search, p.address,
115 coalesce(p.centroid, ST_Centroid(p.geometry)) as centroid
116 FROM placex p, planet_osm_ways w
117 WHERE p.osm_type = 'W' and p.rank_search >= 26
118 and p.geometry && bbox
119 and w.id = p.osm_id and poi_osm_id = any(w.nodes)
121 {% if debug %}RAISE WARNING 'Node is part of way % ', location.osm_id;{% endif %}
123 -- Way IS a road then we are on it - that must be our road
124 IF location.rank_search < 28 THEN
125 {% if debug %}RAISE WARNING 'node in way that is a street %',location;{% endif %}
126 return location.place_id;
129 SELECT find_parent_for_poi('W', location.osm_id, poi_partition,
131 location.address->'street',
132 location.address->'place',
134 INTO parent_place_id;
135 IF parent_place_id is not null THEN
136 RETURN parent_place_id;
142 IF addr_street is null and addr_place is not null THEN
143 -- The address is attached to a place we don't know.
144 -- Instead simply use the containing area with the largest rank.
146 SELECT place_id FROM placex
147 WHERE bbox && geometry AND _ST_Covers(geometry, ST_Centroid(bbox))
148 AND rank_address between 5 and 25
149 ORDER BY rank_address desc
151 RETURN location.place_id;
153 ELSEIF ST_Area(bbox) < 0.005 THEN
154 -- for smaller features get the nearest road
155 SELECT getNearestRoadPlaceId(poi_partition, bbox) INTO parent_place_id;
156 {% if debug %}RAISE WARNING 'Checked for nearest way (%)', parent_place_id;{% endif %}
158 -- for larger features simply find the area with the largest rank that
159 -- contains the bbox, only use addressable features
161 SELECT place_id FROM placex
162 WHERE bbox && geometry AND _ST_Covers(geometry, ST_Centroid(bbox))
163 AND rank_address between 5 and 25
164 ORDER BY rank_address desc
166 RETURN location.place_id;
171 RETURN parent_place_id;
174 LANGUAGE plpgsql STABLE;
176 -- Try to find a linked place for the given object.
177 CREATE OR REPLACE FUNCTION find_linked_place(bnd placex)
181 relation_members TEXT[];
183 linked_placex placex%ROWTYPE;
186 IF bnd.rank_search >= 26 or bnd.rank_address = 0
187 or ST_GeometryType(bnd.geometry) NOT IN ('ST_Polygon','ST_MultiPolygon')
188 or bnd.type IN ('postcode', 'postal_code')
193 IF bnd.osm_type = 'R' THEN
194 -- see if we have any special relation members
195 SELECT members FROM planet_osm_rels WHERE id = bnd.osm_id INTO relation_members;
196 {% if debug %}RAISE WARNING 'Got relation members';{% endif %}
198 -- Search for relation members with role 'lable'.
199 IF relation_members IS NOT NULL THEN
201 SELECT get_rel_node_members(relation_members, ARRAY['label']) as member
203 {% if debug %}RAISE WARNING 'Found label member %', rel_member.member;{% endif %}
207 WHERE osm_type = 'N' and osm_id = rel_member.member
210 {% if debug %}RAISE WARNING 'Linked label member';{% endif %}
211 RETURN linked_placex;
218 IF bnd.name ? 'name' THEN
219 bnd_name := lower(bnd.name->'name');
220 IF bnd_name = '' THEN
225 -- If extratags has a place tag, look for linked nodes by their place type.
226 -- Area and node still have to have the same name.
227 IF bnd.extratags ? 'place' and bnd_name is not null THEN
230 WHERE (position(lower(name->'name') in bnd_name) > 0
231 OR position(bnd_name in lower(name->'name')) > 0)
232 AND placex.class = 'place' AND placex.type = bnd.extratags->'place'
233 AND placex.osm_type = 'N'
234 AND placex.linked_place_id is null
235 AND placex.rank_search < 26 -- needed to select the right index
236 AND placex.type != 'postcode'
237 AND ST_Covers(bnd.geometry, placex.geometry)
239 {% if debug %}RAISE WARNING 'Found type-matching place node %', linked_placex.osm_id;{% endif %}
240 RETURN linked_placex;
244 IF bnd.extratags ? 'wikidata' THEN
247 WHERE placex.class = 'place' AND placex.osm_type = 'N'
248 AND placex.extratags ? 'wikidata' -- needed to select right index
249 AND placex.extratags->'wikidata' = bnd.extratags->'wikidata'
250 AND placex.linked_place_id is null
251 AND placex.rank_search < 26
252 AND _st_covers(bnd.geometry, placex.geometry)
253 ORDER BY lower(name->'name') = bnd_name desc
255 {% if debug %}RAISE WARNING 'Found wikidata-matching place node %', linked_placex.osm_id;{% endif %}
256 RETURN linked_placex;
260 -- Name searches can be done for ways as well as relations
261 IF bnd_name is not null THEN
262 {% if debug %}RAISE WARNING 'Looking for nodes with matching names';{% endif %}
264 SELECT placex.* from placex
265 WHERE lower(name->'name') = bnd_name
266 AND ((bnd.rank_address > 0
267 and bnd.rank_address = (compute_place_rank(placex.country_code,
269 placex.type, 15::SMALLINT,
270 false, placex.postcode)).address_rank)
271 OR (bnd.rank_address = 0 and placex.rank_search = bnd.rank_search))
272 AND placex.osm_type = 'N'
273 AND placex.class = 'place'
274 AND placex.linked_place_id is null
275 AND placex.rank_search < 26 -- needed to select the right index
276 AND placex.type != 'postcode'
277 AND ST_Covers(bnd.geometry, placex.geometry)
279 {% if debug %}RAISE WARNING 'Found matching place node %', linked_placex.osm_id;{% endif %}
280 RETURN linked_placex;
287 LANGUAGE plpgsql STABLE;
290 CREATE OR REPLACE FUNCTION create_poi_search_terms(obj_place_id BIGINT,
291 in_partition SMALLINT,
292 parent_place_id BIGINT,
297 OUT name_vector INTEGER[],
298 OUT nameaddress_vector INTEGER[])
301 parent_name_vector INTEGER[];
302 parent_address_vector INTEGER[];
303 addr_place_ids INTEGER[];
304 hnr_vector INTEGER[];
307 parent_address_place_ids BIGINT[];
308 filtered_address HSTORE;
310 nameaddress_vector := '{}'::INTEGER[];
312 SELECT s.name_vector, s.nameaddress_vector
313 INTO parent_name_vector, parent_address_vector
315 WHERE s.place_id = parent_place_id;
317 -- Find all address tags that don't appear in the parent search names.
318 SELECT hstore(array_agg(ARRAY[k, v])) INTO filtered_address
319 FROM (SELECT skeys(address) as k, svals(address) as v) a
320 WHERE not addr_ids_from_name(v) && parent_address_vector
321 AND k not in ('country', 'street', 'place', 'postcode',
322 'housenumber', 'streetnumber', 'conscriptionnumber');
324 -- Compute all search terms from the addr: tags.
325 IF filtered_address IS NOT NULL THEN
328 get_places_for_addr_tags(in_partition, geometry, filtered_address, country)
330 IF addr_item.place_id is null THEN
331 nameaddress_vector := array_merge(nameaddress_vector,
336 IF parent_address_place_ids is null THEN
337 SELECT array_agg(parent_place_id) INTO parent_address_place_ids
338 FROM place_addressline
339 WHERE place_id = parent_place_id;
342 IF not parent_address_place_ids @> ARRAY[addr_item.place_id] THEN
343 nameaddress_vector := array_merge(nameaddress_vector,
346 INSERT INTO place_addressline (place_id, address_place_id, fromarea,
347 isaddress, distance, cached_rank_address)
348 VALUES (obj_place_id, addr_item.place_id, not addr_item.isguess,
349 true, addr_item.distance, addr_item.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 not address ? 'street' and address ? 'place' THEN
368 addr_place_ids := addr_ids_from_name(address->'place');
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,
376 ARRAY[getorcreate_name_id(make_standard_name(address->'place'))]);
381 -- Cheating here by not recomputing all terms but simply using the ones
382 -- from the parent object.
383 nameaddress_vector := array_merge(nameaddress_vector, parent_name_vector);
384 nameaddress_vector := array_merge(nameaddress_vector, parent_address_vector);
391 -- Insert address of a place into the place_addressline table.
393 -- \param obj_place_id Place_id of the place to compute the address for.
394 -- \param partition Partition number where the place is in.
395 -- \param maxrank Rank of the place. All address features must have
396 -- a search rank lower than the given rank.
397 -- \param address Address terms for the place.
398 -- \param geometry Geometry to which the address objects should be close.
400 -- \retval parent_place_id Place_id of the address object that is the direct
402 -- \retval postcode Postcode computed from the address. This is the
403 -- addr:postcode of one of the address objects. If
404 -- more than one of has a postcode, the highest ranking
405 -- one is used. May be NULL.
406 -- \retval nameaddress_vector Search terms for the address. This is the sum
407 -- of name terms of all address objects.
408 CREATE OR REPLACE FUNCTION insert_addresslines(obj_place_id BIGINT,
414 OUT parent_place_id BIGINT,
416 OUT nameaddress_vector INT[])
419 address_havelevel BOOLEAN[];
421 location_isaddress BOOLEAN;
422 current_boundary GEOMETRY := NULL;
423 current_node_area GEOMETRY := NULL;
425 parent_place_rank INT := 0;
426 addr_place_ids BIGINT[];
430 parent_place_id := 0;
431 nameaddress_vector := '{}'::int[];
433 address_havelevel := array_fill(false, ARRAY[maxrank]);
436 SELECT * FROM get_places_for_addr_tags(partition, geometry,
438 ORDER BY rank_address, distance, isguess desc
440 {% if not db.reverse_only %}
441 nameaddress_vector := array_merge(nameaddress_vector,
442 location.keywords::int[]);
445 IF location.place_id is not null THEN
446 location_isaddress := not address_havelevel[location.rank_address];
447 IF not address_havelevel[location.rank_address] THEN
448 address_havelevel[location.rank_address] := true;
449 IF parent_place_rank < location.rank_address THEN
450 parent_place_id := location.place_id;
451 parent_place_rank := location.rank_address;
455 INSERT INTO place_addressline (place_id, address_place_id, fromarea,
456 isaddress, distance, cached_rank_address)
457 VALUES (obj_place_id, location.place_id, not location.isguess,
458 true, location.distance, location.rank_address);
460 addr_place_ids := array_append(addr_place_ids, location.place_id);
465 SELECT * FROM getNearFeatures(partition, geometry, maxrank)
466 WHERE addr_place_ids is null or not addr_place_ids @> ARRAY[place_id]
467 ORDER BY rank_address, isguess asc,
469 CASE WHEN rank_address = 16 AND rank_search = 15 THEN 0.2
470 WHEN rank_address = 16 AND rank_search = 16 THEN 0.25
471 WHEN rank_address = 16 AND rank_search = 18 THEN 0.5
474 -- Ignore all place nodes that do not fit in a lower level boundary.
475 CONTINUE WHEN location.isguess
476 and current_boundary is not NULL
477 and not ST_Contains(current_boundary, location.centroid);
479 -- If this is the first item in the rank, then assume it is the address.
480 location_isaddress := not address_havelevel[location.rank_address];
482 -- Further sanity checks to ensure that the address forms a sane hierarchy.
483 IF location_isaddress THEN
484 IF location.isguess and current_node_area is not NULL THEN
485 location_isaddress := ST_Contains(current_node_area, location.centroid);
487 IF not location.isguess and current_boundary is not NULL
488 and location.rank_address != 11 AND location.rank_address != 5 THEN
489 location_isaddress := ST_Contains(current_boundary, location.centroid);
493 IF location_isaddress THEN
494 address_havelevel[location.rank_address] := true;
495 parent_place_id := location.place_id;
497 -- Set postcode if we have one.
498 -- (Returned will be the highest ranking one.)
499 IF location.postcode is not NULL THEN
500 postcode = location.postcode;
503 -- Recompute the areas we need for hierarchy sanity checks.
504 IF location.rank_address != 11 AND location.rank_address != 5 THEN
505 IF location.isguess THEN
506 current_node_area := place_node_fuzzy_area(location.centroid,
507 location.rank_search);
509 current_node_area := NULL;
510 SELECT p.geometry FROM placex p
511 WHERE p.place_id = location.place_id INTO current_boundary;
516 -- Add it to the list of search terms
517 {% if not db.reverse_only %}
518 nameaddress_vector := array_merge(nameaddress_vector,
519 location.keywords::integer[]);
522 INSERT INTO place_addressline (place_id, address_place_id, fromarea,
523 isaddress, distance, cached_rank_address)
524 VALUES (obj_place_id, location.place_id, not location.isguess,
525 location_isaddress, location.distance, location.rank_address);
532 CREATE OR REPLACE FUNCTION placex_insert()
539 country_code VARCHAR(2);
543 {% if debug %}RAISE WARNING '% % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;{% endif %}
545 NEW.place_id := nextval('seq_place');
546 NEW.indexed_status := 1; --STATUS_NEW
548 NEW.centroid := ST_PointOnSurface(NEW.geometry);
549 NEW.country_code := lower(get_country_code(NEW.centroid));
551 NEW.partition := get_partition(NEW.country_code);
552 NEW.geometry_sector := geometry_sector(NEW.partition, NEW.centroid);
554 IF NEW.osm_type = 'X' THEN
555 -- E'X'ternal records should already be in the right format so do nothing
557 is_area := ST_GeometryType(NEW.geometry) IN ('ST_Polygon','ST_MultiPolygon');
559 IF NEW.class in ('place','boundary')
560 AND NEW.type in ('postcode','postal_code')
562 IF NEW.address IS NULL OR NOT NEW.address ? 'postcode' THEN
563 -- most likely just a part of a multipolygon postcode boundary, throw it away
567 NEW.name := hstore('ref', NEW.address->'postcode');
569 ELSEIF NEW.class = 'highway' AND is_area AND NEW.name is null
570 AND NEW.extratags ? 'area' AND NEW.extratags->'area' = 'yes'
573 ELSEIF NEW.class = 'boundary' AND NOT is_area
576 ELSEIF NEW.class = 'boundary' AND NEW.type = 'administrative'
577 AND NEW.admin_level <= 4 AND NEW.osm_type = 'W'
582 SELECT * INTO NEW.rank_search, NEW.rank_address
583 FROM compute_place_rank(NEW.country_code,
584 CASE WHEN is_area THEN 'A' ELSE NEW.osm_type END,
585 NEW.class, NEW.type, NEW.admin_level,
586 (NEW.extratags->'capital') = 'yes',
587 NEW.address->'postcode');
589 -- a country code make no sense below rank 4 (country)
590 IF NEW.rank_search < 4 THEN
591 NEW.country_code := NULL;
596 {% if debug %}RAISE WARNING 'placex_insert:END: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;{% endif %}
598 {% if not disable_diff_updates %}
599 -- The following is not needed until doing diff updates, and slows the main index process down
601 IF NEW.osm_type = 'N' and NEW.rank_search > 28 THEN
602 -- might be part of an interpolation
603 result := osmline_reinsert(NEW.osm_id, NEW.geometry);
604 ELSEIF NEW.rank_address > 0 THEN
605 IF (ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon') AND ST_IsValid(NEW.geometry)) THEN
606 -- Performance: We just can't handle re-indexing for country level changes
607 IF st_area(NEW.geometry) < 1 THEN
608 -- mark items within the geometry for re-indexing
609 -- RAISE WARNING 'placex poly insert: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
611 UPDATE placex SET indexed_status = 2
612 WHERE ST_Intersects(NEW.geometry, placex.geometry)
613 and indexed_status = 0
614 and ((rank_address = 0 and rank_search > NEW.rank_address)
615 or rank_address > NEW.rank_address
616 or (class = 'place' and osm_type = 'N')
618 and (rank_search < 28
620 or (NEW.rank_address >= 16 and address ? 'place'));
623 -- mark nearby items for re-indexing, where 'nearby' depends on the features rank_search and is a complete guess :(
624 diameter := update_place_diameter(NEW.rank_search);
626 -- RAISE WARNING 'placex point insert: % % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type,diameter;
627 IF NEW.rank_search >= 26 THEN
628 -- roads may cause reparenting for >27 rank places
629 update placex set indexed_status = 2 where indexed_status = 0 and rank_search > NEW.rank_search and ST_DWithin(placex.geometry, NEW.geometry, diameter);
630 -- reparenting also for OSM Interpolation Lines (and for Tiger?)
631 update location_property_osmline set indexed_status = 2 where indexed_status = 0 and ST_DWithin(location_property_osmline.linegeo, NEW.geometry, diameter);
632 ELSEIF NEW.rank_search >= 16 THEN
633 -- up to rank 16, street-less addresses may need reparenting
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) and (rank_search < 28 or name is not null or address ? 'place');
636 -- for all other places the search terms may change as well
637 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);
644 -- add to tables for special search
645 -- Note: won't work on initial import because the classtype tables
646 -- do not yet exist. It won't hurt either.
647 classtable := 'place_classtype_' || NEW.class || '_' || NEW.type;
648 SELECT count(*)>0 FROM pg_tables WHERE tablename = classtable and schemaname = current_schema() INTO result;
650 EXECUTE 'INSERT INTO ' || classtable::regclass || ' (place_id, centroid) VALUES ($1,$2)'
651 USING NEW.place_id, ST_Centroid(NEW.geometry);
654 {% endif %} -- not disable_diff_updates
662 CREATE OR REPLACE FUNCTION placex_update()
668 relation_members TEXT[];
671 parent_address_level SMALLINT;
672 place_address_level SMALLINT;
679 name_vector INTEGER[];
680 nameaddress_vector INTEGER[];
681 addr_nameaddress_vector INTEGER[];
683 linked_node_id BIGINT;
684 linked_importance FLOAT;
685 linked_wikipedia TEXT;
690 IF OLD.indexed_status = 100 THEN
691 {% if debug %}RAISE WARNING 'placex_update delete % %',NEW.osm_type,NEW.osm_id;{% endif %}
692 delete from placex where place_id = OLD.place_id;
696 IF NEW.indexed_status != 0 OR OLD.indexed_status = 0 THEN
700 {% if debug %}RAISE WARNING 'placex_update % % (%)',NEW.osm_type,NEW.osm_id,NEW.place_id;{% endif %}
702 NEW.indexed_date = now();
704 {% if 'search_name' in db.tables %}
705 DELETE from search_name WHERE place_id = NEW.place_id;
707 result := deleteSearchName(NEW.partition, NEW.place_id);
708 DELETE FROM place_addressline WHERE place_id = NEW.place_id;
709 result := deleteRoad(NEW.partition, NEW.place_id);
710 result := deleteLocationArea(NEW.partition, NEW.place_id, NEW.rank_search);
711 UPDATE placex set linked_place_id = null, indexed_status = 2
712 where linked_place_id = NEW.place_id;
713 -- update not necessary for osmline, cause linked_place_id does not exist
715 NEW.extratags := NEW.extratags - 'linked_place'::TEXT;
717 IF NEW.linked_place_id is not null THEN
718 {% if debug %}RAISE WARNING 'place already linked to %', NEW.linked_place_id;{% endif %}
722 -- Postcodes are just here to compute the centroids. They are not searchable
723 -- unless they are a boundary=postal_code.
724 -- There was an error in the style so that boundary=postal_code used to be
725 -- imported as place=postcode. That's why relations are allowed to pass here.
726 -- This can go away in a couple of versions.
727 IF NEW.class = 'place' and NEW.type = 'postcode' and NEW.osm_type != 'R' THEN
731 -- Speed up searches - just use the centroid of the feature
732 -- cheaper but less acurate
733 NEW.centroid := ST_PointOnSurface(NEW.geometry);
734 {% if debug %}RAISE WARNING 'Computing preliminary centroid at %',ST_AsText(NEW.centroid);{% endif %}
736 -- recompute the ranks, they might change when linking changes
737 SELECT * INTO NEW.rank_search, NEW.rank_address
738 FROM compute_place_rank(NEW.country_code,
739 CASE WHEN ST_GeometryType(NEW.geometry)
740 IN ('ST_Polygon','ST_MultiPolygon')
741 THEN 'A' ELSE NEW.osm_type END,
742 NEW.class, NEW.type, NEW.admin_level,
743 (NEW.extratags->'capital') = 'yes',
744 NEW.address->'postcode');
745 -- We must always increase the address level relative to the admin boundary.
746 IF NEW.class = 'boundary' and NEW.type = 'administrative'
747 and NEW.osm_type = 'R' and NEW.rank_address > 0
749 -- First, check that admin boundaries do not overtake each other rank-wise.
750 parent_address_level := 3;
753 (CASE WHEN extratags ? 'wikidata' and NEW.extratags ? 'wikidata'
754 and extratags->'wikidata' = NEW.extratags->'wikidata'
755 THEN ST_Equals(geometry, NEW.geometry)
756 ELSE false END) as is_same
758 WHERE osm_type = 'R' and class = 'boundary' and type = 'administrative'
759 and admin_level < NEW.admin_level and admin_level > 3
761 and geometry && NEW.centroid and _ST_Covers(geometry, NEW.centroid)
762 ORDER BY admin_level desc LIMIT 1
764 IF location.is_same THEN
765 -- Looks like the same boundary is replicated on multiple admin_levels.
766 -- Usual tagging in Poland. Remove our boundary from addresses.
767 NEW.rank_address := 0;
769 parent_address_level := location.rank_address;
770 IF location.rank_address >= NEW.rank_address THEN
771 IF location.rank_address >= 24 THEN
772 NEW.rank_address := 25;
774 NEW.rank_address := location.rank_address + 2;
780 IF NEW.rank_address > 9 THEN
781 -- Second check that the boundary is not completely contained in a
782 -- place area with a higher address rank
784 SELECT rank_address FROM placex
785 WHERE class = 'place' and rank_address < 24
786 and rank_address > NEW.rank_address
787 and geometry && NEW.geometry
788 and geometry ~ NEW.geometry -- needed because ST_Relate does not do bbox cover test
789 and ST_Relate(geometry, NEW.geometry, 'T*T***FF*') -- contains but not equal
790 ORDER BY rank_address desc LIMIT 1
792 NEW.rank_address := location.rank_address + 2;
795 ELSEIF NEW.class = 'place' and NEW.osm_type = 'N'
796 and NEW.rank_address between 16 and 23
798 -- If a place node is contained in a admin boundary with the same address level
799 -- and has not been linked, then make the node a subpart by increasing the
800 -- address rank (city level and above).
802 SELECT rank_address FROM placex
803 WHERE osm_type = 'R' and class = 'boundary' and type = 'administrative'
804 and rank_address = NEW.rank_address
805 and geometry && NEW.centroid and _ST_Covers(geometry, NEW.centroid)
808 NEW.rank_address = NEW.rank_address + 2;
811 parent_address_level := 3;
814 {% if debug %}RAISE WARNING 'Copy over address tags';{% endif %}
815 -- housenumber is a computed field, so start with an empty value
816 NEW.housenumber := token_normalized_housenumber(NEW.token_info);
817 IF NEW.address is not NULL THEN
818 addr_street := NEW.address->'street';
819 addr_place := NEW.address->'place';
822 NEW.postcode := null;
824 -- recalculate country and partition
825 IF NEW.rank_search = 4 AND NEW.address is not NULL AND NEW.address ? 'country' THEN
826 -- for countries, believe the mapped country code,
827 -- so that we remain in the right partition if the boundaries
829 NEW.country_code := lower(NEW.address->'country');
830 NEW.partition := get_partition(lower(NEW.country_code));
831 IF NEW.partition = 0 THEN
832 NEW.country_code := lower(get_country_code(NEW.centroid));
833 NEW.partition := get_partition(NEW.country_code);
836 IF NEW.rank_search >= 4 THEN
837 NEW.country_code := lower(get_country_code(NEW.centroid));
839 NEW.country_code := NULL;
841 NEW.partition := get_partition(NEW.country_code);
843 {% if debug %}RAISE WARNING 'Country updated: "%"', NEW.country_code;{% endif %}
845 -- waterway ways are linked when they are part of a relation and have the same class/type
846 IF NEW.osm_type = 'R' and NEW.class = 'waterway' THEN
847 FOR relation_members IN select members from planet_osm_rels r where r.id = NEW.osm_id and r.parts != array[]::bigint[]
849 FOR i IN 1..array_upper(relation_members, 1) BY 2 LOOP
850 IF relation_members[i+1] in ('', 'main_stream', 'side_stream') AND substring(relation_members[i],1,1) = 'w' THEN
851 {% if debug %}RAISE WARNING 'waterway parent %, child %/%', NEW.osm_id, i, relation_members[i];{% endif %}
852 FOR linked_node_id IN SELECT place_id FROM placex
853 WHERE osm_type = 'W' and osm_id = substring(relation_members[i],2,200)::bigint
854 and class = NEW.class and type in ('river', 'stream', 'canal', 'drain', 'ditch')
855 and ( relation_members[i+1] != 'side_stream' or NEW.name->'name' = name->'name')
857 UPDATE placex SET linked_place_id = NEW.place_id WHERE place_id = linked_node_id;
858 {% if 'search_name' in db.tables %}
859 DELETE FROM search_name WHERE place_id = linked_node_id;
865 {% if debug %}RAISE WARNING 'Waterway processed';{% endif %}
868 NEW.importance := null;
869 SELECT wikipedia, importance
870 FROM compute_importance(NEW.extratags, NEW.country_code, NEW.osm_type, NEW.osm_id)
871 INTO NEW.wikipedia,NEW.importance;
873 {% if debug %}RAISE WARNING 'Importance computed from wikipedia: %', NEW.importance;{% endif %}
875 -- ---------------------------------------------------------------------------
876 -- For low level elements we inherit from our parent road
877 IF NEW.rank_search > 27 THEN
879 {% if debug %}RAISE WARNING 'finding street for % %', NEW.osm_type, NEW.osm_id;{% endif %}
880 NEW.parent_place_id := null;
882 -- We have to find our parent road.
883 NEW.parent_place_id := find_parent_for_poi(NEW.osm_type, NEW.osm_id,
885 ST_Envelope(NEW.geometry),
886 addr_street, addr_place);
888 -- If we found the road take a shortcut here.
889 -- Otherwise fall back to the full address getting method below.
890 IF NEW.parent_place_id is not null THEN
892 -- Get the details of the parent road
893 SELECT p.country_code, p.postcode, p.name FROM placex p
894 WHERE p.place_id = NEW.parent_place_id INTO location;
896 IF addr_street is null and addr_place is not null THEN
897 -- Check if the addr:place tag is part of the parent name
898 SELECT count(*) INTO i
899 FROM svals(location.name) AS pname WHERE pname = addr_place;
901 NEW.address = NEW.address || hstore('_unlisted_place', addr_place);
905 NEW.country_code := location.country_code;
906 {% if debug %}RAISE WARNING 'Got parent details from search name';{% endif %}
908 -- determine postcode
909 IF NEW.address is not null AND NEW.address ? 'postcode' THEN
910 NEW.postcode = upper(trim(NEW.address->'postcode'));
912 NEW.postcode := location.postcode;
914 IF NEW.postcode is null THEN
915 NEW.postcode := 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 NEW.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 -- make sure all names are in the word table
1024 IF NEW.admin_level = 2
1025 AND NEW.class = 'boundary' AND NEW.type = 'administrative'
1026 AND NEW.country_code IS NOT NULL AND NEW.osm_type = 'R'
1028 -- Update the list of country names. Adding an additional sanity
1029 -- check here: make sure the country does overlap with the area where
1030 -- we expect it to be as per static country grid.
1032 SELECT country_code FROM country_osm_grid
1033 WHERE ST_Covers(geometry, NEW.centroid) and country_code = NEW.country_code
1036 {% if debug %}RAISE WARNING 'Updating names for country '%' with: %', NEW.country_code, NEW.name;{% endif %}
1037 UPDATE country_name SET name = name || NEW.name WHERE country_code = NEW.country_code;
1041 -- For linear features we need the full geometry for determining the address
1042 -- because they may go through several administrative entities. Otherwise use
1043 -- the centroid for performance reasons.
1044 IF ST_GeometryType(NEW.geometry) in ('ST_LineString', 'ST_MultiLineString') THEN
1045 geom := NEW.geometry;
1047 geom := NEW.centroid;
1050 IF NEW.rank_address = 0 THEN
1051 max_rank := geometry_to_rank(NEW.rank_search, NEW.geometry, NEW.country_code);
1052 -- Rank 0 features may also span multiple administrative areas (e.g. lakes)
1053 -- so use the geometry here too. Just make sure the areas don't become too
1055 IF NEW.class = 'natural' or max_rank > 10 THEN
1056 geom := NEW.geometry;
1058 ELSEIF NEW.rank_address > 25 THEN
1061 max_rank = NEW.rank_address;
1064 SELECT * FROM insert_addresslines(NEW.place_id, NEW.partition, max_rank,
1065 NEW.address, geom, NEW.country_code)
1066 INTO NEW.parent_place_id, NEW.postcode, nameaddress_vector;
1068 {% if debug %}RAISE WARNING 'RETURN insert_addresslines: %, %, %', NEW.parent_place_id, NEW.postcode, nameaddress_vector;{% endif %}
1070 IF NEW.address is not null AND NEW.address ? 'postcode'
1071 AND NEW.address->'postcode' not similar to '%(,|;)%' THEN
1072 NEW.postcode := upper(trim(NEW.address->'postcode'));
1075 IF NEW.postcode is null AND NEW.rank_search > 8 THEN
1076 NEW.postcode := get_nearest_postcode(NEW.country_code, NEW.geometry);
1079 -- if we have a name add this to the name search table
1080 IF NEW.name IS NOT NULL THEN
1081 -- Initialise the name vector using our name
1082 NEW.name := add_default_place_name(NEW.country_code, NEW.name);
1083 name_vector := token_get_name_search_tokens(NEW.token_info);
1085 IF NEW.rank_search <= 25 and NEW.rank_address > 0 THEN
1086 result := add_location(NEW.place_id, NEW.country_code, NEW.partition,
1087 name_vector, NEW.rank_search, NEW.rank_address,
1088 upper(trim(NEW.address->'postcode')), NEW.geometry, NEW.centroid);
1089 {% if debug %}RAISE WARNING 'added to location (full)';{% endif %}
1092 IF NEW.rank_search between 26 and 27 and NEW.class = 'highway' THEN
1093 result := insertLocationRoad(NEW.partition, NEW.place_id, NEW.country_code, NEW.geometry);
1094 {% if debug %}RAISE WARNING 'insert into road location table (full)';{% endif %}
1097 result := insertSearchName(NEW.partition, NEW.place_id,
1098 token_get_name_match_tokens(NEW.token_info),
1099 NEW.rank_search, NEW.rank_address, NEW.geometry);
1100 {% if debug %}RAISE WARNING 'added to search name (full)';{% endif %}
1102 {% if not db.reverse_only %}
1103 INSERT INTO search_name (place_id, search_rank, address_rank,
1104 importance, country_code, name_vector,
1105 nameaddress_vector, centroid)
1106 VALUES (NEW.place_id, NEW.rank_search, NEW.rank_address,
1107 NEW.importance, NEW.country_code, name_vector,
1108 nameaddress_vector, NEW.centroid);
1113 {% if debug %}RAISE WARNING 'place update % % finsihed.', NEW.osm_type, NEW.osm_id;{% endif %}
1115 NEW.token_info := token_strip_info(NEW.token_info);
1122 CREATE OR REPLACE FUNCTION placex_delete()
1129 -- RAISE WARNING 'placex_delete % %',OLD.osm_type,OLD.osm_id;
1131 IF OLD.linked_place_id is null THEN
1132 update placex set linked_place_id = null, indexed_status = 2 where linked_place_id = OLD.place_id and indexed_status = 0;
1133 {% if debug %}RAISE WARNING 'placex_delete:01 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1134 update placex set linked_place_id = null where linked_place_id = OLD.place_id;
1135 {% if debug %}RAISE WARNING 'placex_delete:02 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1137 update placex set indexed_status = 2 where place_id = OLD.linked_place_id and indexed_status = 0;
1140 IF OLD.rank_address < 30 THEN
1142 -- mark everything linked to this place for re-indexing
1143 {% if debug %}RAISE WARNING 'placex_delete:03 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1144 UPDATE placex set indexed_status = 2 from place_addressline where address_place_id = OLD.place_id
1145 and placex.place_id = place_addressline.place_id and indexed_status = 0 and place_addressline.isaddress;
1147 {% if debug %}RAISE WARNING 'placex_delete:04 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1148 DELETE FROM place_addressline where address_place_id = OLD.place_id;
1150 {% if debug %}RAISE WARNING 'placex_delete:05 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1151 b := deleteRoad(OLD.partition, OLD.place_id);
1153 {% if debug %}RAISE WARNING 'placex_delete:06 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1154 update placex set indexed_status = 2 where parent_place_id = OLD.place_id and indexed_status = 0;
1155 {% if debug %}RAISE WARNING 'placex_delete:07 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1156 -- reparenting also for OSM Interpolation Lines (and for Tiger?)
1157 update location_property_osmline set indexed_status = 2 where indexed_status = 0 and parent_place_id = OLD.place_id;
1161 {% if debug %}RAISE WARNING 'placex_delete:08 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1163 IF OLD.rank_address < 26 THEN
1164 b := deleteLocationArea(OLD.partition, OLD.place_id, OLD.rank_search);
1167 {% if debug %}RAISE WARNING 'placex_delete:09 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1169 IF OLD.name is not null THEN
1170 {% if 'search_name' in db.tables %}
1171 DELETE from search_name WHERE place_id = OLD.place_id;
1173 b := deleteSearchName(OLD.partition, OLD.place_id);
1176 {% if debug %}RAISE WARNING 'placex_delete:10 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1178 DELETE FROM place_addressline where place_id = OLD.place_id;
1180 {% if debug %}RAISE WARNING 'placex_delete:11 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1182 -- remove from tables for special search
1183 classtable := 'place_classtype_' || OLD.class || '_' || OLD.type;
1184 SELECT count(*)>0 FROM pg_tables WHERE tablename = classtable and schemaname = current_schema() INTO b;
1186 EXECUTE 'DELETE FROM ' || classtable::regclass || ' WHERE place_id = $1' USING OLD.place_id;
1189 {% if debug %}RAISE WARNING 'placex_delete:12 % %',OLD.osm_type,OLD.osm_id;{% endif %}