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,
298 OUT name_vector INTEGER[],
299 OUT nameaddress_vector INTEGER[])
302 parent_name_vector INTEGER[];
303 parent_address_vector INTEGER[];
304 addr_place_ids 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 IF housenumber is not null and not nameaddress_vector <@ parent_address_vector THEN
362 name_vector := array_merge(name_vector,
363 ARRAY[getorcreate_housenumber_id(make_standard_name(housenumber))]);
366 IF not address ? 'street' and address ? 'place' THEN
367 addr_place_ids := addr_ids_from_name(address->'place');
368 IF not addr_place_ids <@ parent_name_vector THEN
369 -- make sure addr:place terms are always searchable
370 nameaddress_vector := array_merge(nameaddress_vector, addr_place_ids);
371 -- If there is a housenumber, also add the place name as a name,
372 -- so we can search it by the usual housenumber+place algorithms.
373 IF housenumber is not null THEN
374 name_vector := array_merge(name_vector,
375 ARRAY[getorcreate_name_id(make_standard_name(address->'place'))]);
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[];
429 parent_place_id := 0;
430 nameaddress_vector := '{}'::int[];
432 address_havelevel := array_fill(false, ARRAY[maxrank]);
435 SELECT * FROM get_places_for_addr_tags(partition, geometry,
437 ORDER BY rank_address, distance, isguess desc
439 {% if not db.reverse_only %}
440 nameaddress_vector := array_merge(nameaddress_vector,
441 location.keywords::int[]);
444 IF location.place_id is not null THEN
445 location_isaddress := not address_havelevel[location.rank_address];
446 IF not address_havelevel[location.rank_address] THEN
447 address_havelevel[location.rank_address] := true;
448 IF parent_place_rank < location.rank_address THEN
449 parent_place_id := location.place_id;
450 parent_place_rank := location.rank_address;
454 INSERT INTO place_addressline (place_id, address_place_id, fromarea,
455 isaddress, distance, cached_rank_address)
456 VALUES (obj_place_id, location.place_id, not location.isguess,
457 true, location.distance, location.rank_address);
459 addr_place_ids := array_append(addr_place_ids, location.place_id);
464 SELECT * FROM getNearFeatures(partition, geometry, maxrank)
465 WHERE addr_place_ids is null or not addr_place_ids @> ARRAY[place_id]
466 ORDER BY rank_address, isguess asc,
468 CASE WHEN rank_address = 16 AND rank_search = 15 THEN 0.2
469 WHEN rank_address = 16 AND rank_search = 16 THEN 0.25
470 WHEN rank_address = 16 AND rank_search = 18 THEN 0.5
473 -- Ignore all place nodes that do not fit in a lower level boundary.
474 CONTINUE WHEN location.isguess
475 and current_boundary is not NULL
476 and not ST_Contains(current_boundary, location.centroid);
478 -- If this is the first item in the rank, then assume it is the address.
479 location_isaddress := not address_havelevel[location.rank_address];
481 -- Further sanity checks to ensure that the address forms a sane hierarchy.
482 IF location_isaddress THEN
483 IF location.isguess and current_node_area is not NULL THEN
484 location_isaddress := ST_Contains(current_node_area, location.centroid);
486 IF not location.isguess and current_boundary is not NULL
487 and location.rank_address != 11 AND location.rank_address != 5 THEN
488 location_isaddress := ST_Contains(current_boundary, location.centroid);
492 IF location_isaddress THEN
493 address_havelevel[location.rank_address] := true;
494 parent_place_id := location.place_id;
496 -- Set postcode if we have one.
497 -- (Returned will be the highest ranking one.)
498 IF location.postcode is not NULL THEN
499 postcode = location.postcode;
502 -- Recompute the areas we need for hierarchy sanity checks.
503 IF location.rank_address != 11 AND location.rank_address != 5 THEN
504 IF location.isguess THEN
505 current_node_area := place_node_fuzzy_area(location.centroid,
506 location.rank_search);
508 current_node_area := NULL;
509 SELECT p.geometry FROM placex p
510 WHERE p.place_id = location.place_id INTO current_boundary;
515 -- Add it to the list of search terms
516 {% if not db.reverse_only %}
517 nameaddress_vector := array_merge(nameaddress_vector,
518 location.keywords::integer[]);
521 INSERT INTO place_addressline (place_id, address_place_id, fromarea,
522 isaddress, distance, cached_rank_address)
523 VALUES (obj_place_id, location.place_id, not location.isguess,
524 location_isaddress, location.distance, location.rank_address);
531 CREATE OR REPLACE FUNCTION placex_insert()
538 country_code VARCHAR(2);
542 {% if debug %}RAISE WARNING '% % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;{% endif %}
544 NEW.place_id := nextval('seq_place');
545 NEW.indexed_status := 1; --STATUS_NEW
547 NEW.centroid := ST_PointOnSurface(NEW.geometry);
548 NEW.country_code := lower(get_country_code(NEW.centroid));
550 NEW.partition := get_partition(NEW.country_code);
551 NEW.geometry_sector := geometry_sector(NEW.partition, NEW.centroid);
553 IF NEW.osm_type = 'X' THEN
554 -- E'X'ternal records should already be in the right format so do nothing
556 is_area := ST_GeometryType(NEW.geometry) IN ('ST_Polygon','ST_MultiPolygon');
558 IF NEW.class in ('place','boundary')
559 AND NEW.type in ('postcode','postal_code')
561 IF NEW.address IS NULL OR NOT NEW.address ? 'postcode' THEN
562 -- most likely just a part of a multipolygon postcode boundary, throw it away
566 NEW.name := hstore('ref', NEW.address->'postcode');
568 ELSEIF NEW.class = 'highway' AND is_area AND NEW.name is null
569 AND NEW.extratags ? 'area' AND NEW.extratags->'area' = 'yes'
572 ELSEIF NEW.class = 'boundary' AND NOT is_area
575 ELSEIF NEW.class = 'boundary' AND NEW.type = 'administrative'
576 AND NEW.admin_level <= 4 AND NEW.osm_type = 'W'
581 SELECT * INTO NEW.rank_search, NEW.rank_address
582 FROM compute_place_rank(NEW.country_code,
583 CASE WHEN is_area THEN 'A' ELSE NEW.osm_type END,
584 NEW.class, NEW.type, NEW.admin_level,
585 (NEW.extratags->'capital') = 'yes',
586 NEW.address->'postcode');
588 -- a country code make no sense below rank 4 (country)
589 IF NEW.rank_search < 4 THEN
590 NEW.country_code := NULL;
595 {% if debug %}RAISE WARNING 'placex_insert:END: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;{% endif %}
597 {% if not disable_diff_updates %}
598 -- The following is not needed until doing diff updates, and slows the main index process down
600 IF NEW.osm_type = 'N' and NEW.rank_search > 28 THEN
601 -- might be part of an interpolation
602 result := osmline_reinsert(NEW.osm_id, NEW.geometry);
603 ELSEIF NEW.rank_address > 0 THEN
604 IF (ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon') AND ST_IsValid(NEW.geometry)) THEN
605 -- Performance: We just can't handle re-indexing for country level changes
606 IF st_area(NEW.geometry) < 1 THEN
607 -- mark items within the geometry for re-indexing
608 -- RAISE WARNING 'placex poly insert: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
610 UPDATE placex SET indexed_status = 2
611 WHERE ST_Intersects(NEW.geometry, placex.geometry)
612 and indexed_status = 0
613 and ((rank_address = 0 and rank_search > NEW.rank_address)
614 or rank_address > NEW.rank_address
615 or (class = 'place' and osm_type = 'N')
617 and (rank_search < 28
619 or (NEW.rank_address >= 16 and address ? 'place'));
622 -- mark nearby items for re-indexing, where 'nearby' depends on the features rank_search and is a complete guess :(
623 diameter := update_place_diameter(NEW.rank_search);
625 -- RAISE WARNING 'placex point insert: % % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type,diameter;
626 IF NEW.rank_search >= 26 THEN
627 -- roads may cause reparenting for >27 rank places
628 update placex set indexed_status = 2 where indexed_status = 0 and rank_search > NEW.rank_search and ST_DWithin(placex.geometry, NEW.geometry, diameter);
629 -- reparenting also for OSM Interpolation Lines (and for Tiger?)
630 update location_property_osmline set indexed_status = 2 where indexed_status = 0 and ST_DWithin(location_property_osmline.linegeo, NEW.geometry, diameter);
631 ELSEIF NEW.rank_search >= 16 THEN
632 -- up to rank 16, street-less addresses may need reparenting
633 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');
635 -- for all other places the search terms may change as well
636 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);
643 -- add to tables for special search
644 -- Note: won't work on initial import because the classtype tables
645 -- do not yet exist. It won't hurt either.
646 classtable := 'place_classtype_' || NEW.class || '_' || NEW.type;
647 SELECT count(*)>0 FROM pg_tables WHERE tablename = classtable and schemaname = current_schema() INTO result;
649 EXECUTE 'INSERT INTO ' || classtable::regclass || ' (place_id, centroid) VALUES ($1,$2)'
650 USING NEW.place_id, ST_Centroid(NEW.geometry);
653 {% endif %} -- not disable_diff_updates
661 CREATE OR REPLACE FUNCTION placex_update()
667 relation_members TEXT[];
670 parent_address_level SMALLINT;
671 place_address_level SMALLINT;
678 name_vector INTEGER[];
679 nameaddress_vector INTEGER[];
680 addr_nameaddress_vector INTEGER[];
682 linked_node_id BIGINT;
683 linked_importance FLOAT;
684 linked_wikipedia TEXT;
689 IF OLD.indexed_status = 100 THEN
690 {% if debug %}RAISE WARNING 'placex_update delete % %',NEW.osm_type,NEW.osm_id;{% endif %}
691 delete from placex where place_id = OLD.place_id;
695 IF NEW.indexed_status != 0 OR OLD.indexed_status = 0 THEN
699 {% if debug %}RAISE WARNING 'placex_update % % (%)',NEW.osm_type,NEW.osm_id,NEW.place_id;{% endif %}
701 NEW.indexed_date = now();
703 {% if 'search_name' in db.tables %}
704 DELETE from search_name WHERE place_id = NEW.place_id;
706 result := deleteSearchName(NEW.partition, NEW.place_id);
707 DELETE FROM place_addressline WHERE place_id = NEW.place_id;
708 result := deleteRoad(NEW.partition, NEW.place_id);
709 result := deleteLocationArea(NEW.partition, NEW.place_id, NEW.rank_search);
710 UPDATE placex set linked_place_id = null, indexed_status = 2
711 where linked_place_id = NEW.place_id;
712 -- update not necessary for osmline, cause linked_place_id does not exist
714 NEW.extratags := NEW.extratags - 'linked_place'::TEXT;
716 IF NEW.linked_place_id is not null THEN
717 {% if debug %}RAISE WARNING 'place already linked to %', NEW.linked_place_id;{% endif %}
721 -- Postcodes are just here to compute the centroids. They are not searchable
722 -- unless they are a boundary=postal_code.
723 -- There was an error in the style so that boundary=postal_code used to be
724 -- imported as place=postcode. That's why relations are allowed to pass here.
725 -- This can go away in a couple of versions.
726 IF NEW.class = 'place' and NEW.type = 'postcode' and NEW.osm_type != 'R' THEN
730 -- Speed up searches - just use the centroid of the feature
731 -- cheaper but less acurate
732 NEW.centroid := ST_PointOnSurface(NEW.geometry);
733 {% if debug %}RAISE WARNING 'Computing preliminary centroid at %',ST_AsText(NEW.centroid);{% endif %}
735 -- recompute the ranks, they might change when linking changes
736 SELECT * INTO NEW.rank_search, NEW.rank_address
737 FROM compute_place_rank(NEW.country_code,
738 CASE WHEN ST_GeometryType(NEW.geometry)
739 IN ('ST_Polygon','ST_MultiPolygon')
740 THEN 'A' ELSE NEW.osm_type END,
741 NEW.class, NEW.type, NEW.admin_level,
742 (NEW.extratags->'capital') = 'yes',
743 NEW.address->'postcode');
744 -- We must always increase the address level relative to the admin boundary.
745 IF NEW.class = 'boundary' and NEW.type = 'administrative'
746 and NEW.osm_type = 'R' and NEW.rank_address > 0
748 -- First, check that admin boundaries do not overtake each other rank-wise.
749 parent_address_level := 3;
752 (CASE WHEN extratags ? 'wikidata' and NEW.extratags ? 'wikidata'
753 and extratags->'wikidata' = NEW.extratags->'wikidata'
754 THEN ST_Equals(geometry, NEW.geometry)
755 ELSE false END) as is_same
757 WHERE osm_type = 'R' and class = 'boundary' and type = 'administrative'
758 and admin_level < NEW.admin_level and admin_level > 3
760 and geometry && NEW.centroid and _ST_Covers(geometry, NEW.centroid)
761 ORDER BY admin_level desc LIMIT 1
763 IF location.is_same THEN
764 -- Looks like the same boundary is replicated on multiple admin_levels.
765 -- Usual tagging in Poland. Remove our boundary from addresses.
766 NEW.rank_address := 0;
768 parent_address_level := location.rank_address;
769 IF location.rank_address >= NEW.rank_address THEN
770 IF location.rank_address >= 24 THEN
771 NEW.rank_address := 25;
773 NEW.rank_address := location.rank_address + 2;
779 IF NEW.rank_address > 9 THEN
780 -- Second check that the boundary is not completely contained in a
781 -- place area with a higher address rank
783 SELECT rank_address FROM placex
784 WHERE class = 'place' and rank_address < 24
785 and rank_address > NEW.rank_address
786 and geometry && NEW.geometry
787 and geometry ~ NEW.geometry -- needed because ST_Relate does not do bbox cover test
788 and ST_Relate(geometry, NEW.geometry, 'T*T***FF*') -- contains but not equal
789 ORDER BY rank_address desc LIMIT 1
791 NEW.rank_address := location.rank_address + 2;
794 ELSEIF NEW.class = 'place' and NEW.osm_type = 'N'
795 and NEW.rank_address between 16 and 23
797 -- If a place node is contained in a admin boundary with the same address level
798 -- and has not been linked, then make the node a subpart by increasing the
799 -- address rank (city level and above).
801 SELECT rank_address FROM placex
802 WHERE osm_type = 'R' and class = 'boundary' and type = 'administrative'
803 and rank_address = NEW.rank_address
804 and geometry && NEW.centroid and _ST_Covers(geometry, NEW.centroid)
807 NEW.rank_address = NEW.rank_address + 2;
810 parent_address_level := 3;
813 {% if debug %}RAISE WARNING 'Copy over address tags';{% endif %}
814 -- housenumber is a computed field, so start with an empty value
815 NEW.housenumber := NULL;
816 IF NEW.address is not NULL THEN
817 IF NEW.address ? 'conscriptionnumber' THEN
818 IF NEW.address ? 'streetnumber' THEN
819 NEW.housenumber := (NEW.address->'conscriptionnumber') || '/' || (NEW.address->'streetnumber');
821 NEW.housenumber := NEW.address->'conscriptionnumber';
823 ELSEIF NEW.address ? 'streetnumber' THEN
824 NEW.housenumber := NEW.address->'streetnumber';
825 ELSEIF NEW.address ? 'housenumber' THEN
826 NEW.housenumber := NEW.address->'housenumber';
828 NEW.housenumber := create_housenumber_id(NEW.housenumber);
830 addr_street := NEW.address->'street';
831 addr_place := NEW.address->'place';
833 IF NEW.address ? 'postcode' and NEW.address->'postcode' not similar to '%(:|,|;)%' THEN
834 i := getorcreate_postcode_id(NEW.address->'postcode');
838 NEW.postcode := null;
840 -- recalculate country and partition
841 IF NEW.rank_search = 4 AND NEW.address is not NULL AND NEW.address ? 'country' THEN
842 -- for countries, believe the mapped country code,
843 -- so that we remain in the right partition if the boundaries
845 NEW.country_code := lower(NEW.address->'country');
846 NEW.partition := get_partition(lower(NEW.country_code));
847 IF NEW.partition = 0 THEN
848 NEW.country_code := lower(get_country_code(NEW.centroid));
849 NEW.partition := get_partition(NEW.country_code);
852 IF NEW.rank_search >= 4 THEN
853 NEW.country_code := lower(get_country_code(NEW.centroid));
855 NEW.country_code := NULL;
857 NEW.partition := get_partition(NEW.country_code);
859 {% if debug %}RAISE WARNING 'Country updated: "%"', NEW.country_code;{% endif %}
861 -- waterway ways are linked when they are part of a relation and have the same class/type
862 IF NEW.osm_type = 'R' and NEW.class = 'waterway' THEN
863 FOR relation_members IN select members from planet_osm_rels r where r.id = NEW.osm_id and r.parts != array[]::bigint[]
865 FOR i IN 1..array_upper(relation_members, 1) BY 2 LOOP
866 IF relation_members[i+1] in ('', 'main_stream', 'side_stream') AND substring(relation_members[i],1,1) = 'w' THEN
867 {% if debug %}RAISE WARNING 'waterway parent %, child %/%', NEW.osm_id, i, relation_members[i];{% endif %}
868 FOR linked_node_id IN SELECT place_id FROM placex
869 WHERE osm_type = 'W' and osm_id = substring(relation_members[i],2,200)::bigint
870 and class = NEW.class and type in ('river', 'stream', 'canal', 'drain', 'ditch')
871 and ( relation_members[i+1] != 'side_stream' or NEW.name->'name' = name->'name')
873 UPDATE placex SET linked_place_id = NEW.place_id WHERE place_id = linked_node_id;
874 {% if 'search_name' in db.tables %}
875 DELETE FROM search_name WHERE place_id = linked_node_id;
881 {% if debug %}RAISE WARNING 'Waterway processed';{% endif %}
884 NEW.importance := null;
885 SELECT wikipedia, importance
886 FROM compute_importance(NEW.extratags, NEW.country_code, NEW.osm_type, NEW.osm_id)
887 INTO NEW.wikipedia,NEW.importance;
889 {% if debug %}RAISE WARNING 'Importance computed from wikipedia: %', NEW.importance;{% endif %}
891 -- ---------------------------------------------------------------------------
892 -- For low level elements we inherit from our parent road
893 IF NEW.rank_search > 27 THEN
895 {% if debug %}RAISE WARNING 'finding street for % %', NEW.osm_type, NEW.osm_id;{% endif %}
896 NEW.parent_place_id := null;
898 -- We have to find our parent road.
899 NEW.parent_place_id := find_parent_for_poi(NEW.osm_type, NEW.osm_id,
901 ST_Envelope(NEW.geometry),
902 addr_street, addr_place);
904 -- If we found the road take a shortcut here.
905 -- Otherwise fall back to the full address getting method below.
906 IF NEW.parent_place_id is not null THEN
908 -- Get the details of the parent road
909 SELECT p.country_code, p.postcode, p.name FROM placex p
910 WHERE p.place_id = NEW.parent_place_id INTO location;
912 IF addr_street is null and addr_place is not null THEN
913 -- Check if the addr:place tag is part of the parent name
914 SELECT count(*) INTO i
915 FROM svals(location.name) AS pname WHERE pname = addr_place;
917 NEW.address = NEW.address || hstore('_unlisted_place', addr_place);
921 NEW.country_code := location.country_code;
922 {% if debug %}RAISE WARNING 'Got parent details from search name';{% endif %}
924 -- determine postcode
925 IF NEW.address is not null AND NEW.address ? 'postcode' THEN
926 NEW.postcode = upper(trim(NEW.address->'postcode'));
928 NEW.postcode := location.postcode;
930 IF NEW.postcode is null THEN
931 NEW.postcode := get_nearest_postcode(NEW.country_code, NEW.geometry);
934 IF NEW.name is not NULL THEN
935 NEW.name := add_default_place_name(NEW.country_code, NEW.name);
938 {% if not db.reverse_only %}
939 IF NEW.name is not NULL OR NEW.address is not NULL THEN
940 SELECT * INTO name_vector, nameaddress_vector
941 FROM create_poi_search_terms(NEW.place_id,
942 NEW.partition, NEW.parent_place_id,
944 NEW.country_code, NEW.housenumber,
945 NEW.token_info, NEW.centroid);
947 IF array_length(name_vector, 1) is not NULL THEN
948 INSERT INTO search_name (place_id, search_rank, address_rank,
949 importance, country_code, name_vector,
950 nameaddress_vector, centroid)
951 VALUES (NEW.place_id, NEW.rank_search, NEW.rank_address,
952 NEW.importance, NEW.country_code, name_vector,
953 nameaddress_vector, NEW.centroid);
954 {% if debug %}RAISE WARNING 'Place added to search table';{% endif %}
959 NEW.token_info := token_strip_info(NEW.token_info);
960 -- If the address was inherited from a surrounding building,
961 -- do not add it permanently to the table.
962 IF NEW.address ? '_inherited' THEN
963 IF NEW.address ? '_unlisted_place' THEN
964 NEW.address := hstore('_unlisted_place', NEW.address->'_unlisted_place');
975 -- ---------------------------------------------------------------------------
977 {% if debug %}RAISE WARNING 'Using full index mode for % %', NEW.osm_type, NEW.osm_id;{% endif %}
978 SELECT * INTO location FROM find_linked_place(NEW);
979 IF location.place_id is not null THEN
980 {% if debug %}RAISE WARNING 'Linked %', location;{% endif %}
982 -- Use the linked point as the centre point of the geometry,
983 -- but only if it is within the area of the boundary.
984 geom := coalesce(location.centroid, ST_Centroid(location.geometry));
985 IF geom is not NULL AND ST_Within(geom, NEW.geometry) THEN
986 NEW.centroid := geom;
989 {% if debug %}RAISE WARNING 'parent address: % rank address: %', parent_address_level, location.rank_address;{% endif %}
990 IF location.rank_address > parent_address_level
991 and location.rank_address < 26
993 NEW.rank_address := location.rank_address;
996 -- merge in the label name
997 IF NOT location.name IS NULL THEN
998 NEW.name := location.name || NEW.name;
1001 -- merge in extra tags
1002 NEW.extratags := hstore('linked_' || location.class, location.type)
1003 || coalesce(location.extratags, ''::hstore)
1004 || coalesce(NEW.extratags, ''::hstore);
1006 -- mark the linked place (excludes from search results)
1007 UPDATE placex set linked_place_id = NEW.place_id
1008 WHERE place_id = location.place_id;
1009 -- ensure that those places are not found anymore
1010 {% if 'search_name' in db.tables %}
1011 DELETE FROM search_name WHERE place_id = location.place_id;
1013 PERFORM deleteLocationArea(NEW.partition, location.place_id, NEW.rank_search);
1015 SELECT wikipedia, importance
1016 FROM compute_importance(location.extratags, NEW.country_code,
1017 'N', location.osm_id)
1018 INTO linked_wikipedia,linked_importance;
1020 -- Use the maximum importance if one could be computed from the linked object.
1021 IF linked_importance is not null AND
1022 (NEW.importance is null or NEW.importance < linked_importance)
1024 NEW.importance = linked_importance;
1027 -- No linked place? As a last resort check if the boundary is tagged with
1028 -- a place type and adapt the rank address.
1029 IF NEW.rank_address > 0 and NEW.extratags ? 'place' THEN
1030 SELECT address_rank INTO place_address_level
1031 FROM compute_place_rank(NEW.country_code, 'A', 'place',
1032 NEW.extratags->'place', 0::SMALLINT, False, null);
1033 IF place_address_level > parent_address_level and
1034 place_address_level < 26 THEN
1035 NEW.rank_address := place_address_level;
1040 -- make sure all names are in the word table
1041 IF NEW.admin_level = 2
1042 AND NEW.class = 'boundary' AND NEW.type = 'administrative'
1043 AND NEW.country_code IS NOT NULL AND NEW.osm_type = 'R'
1045 -- Update the list of country names. Adding an additional sanity
1046 -- check here: make sure the country does overlap with the area where
1047 -- we expect it to be as per static country grid.
1049 SELECT country_code FROM country_osm_grid
1050 WHERE ST_Covers(geometry, NEW.centroid) and country_code = NEW.country_code
1053 {% if debug %}RAISE WARNING 'Updating names for country '%' with: %', NEW.country_code, NEW.name;{% endif %}
1054 UPDATE country_name SET name = name || NEW.name WHERE country_code = NEW.country_code;
1058 -- For linear features we need the full geometry for determining the address
1059 -- because they may go through several administrative entities. Otherwise use
1060 -- the centroid for performance reasons.
1061 IF ST_GeometryType(NEW.geometry) in ('ST_LineString', 'ST_MultiLineString') THEN
1062 geom := NEW.geometry;
1064 geom := NEW.centroid;
1067 IF NEW.rank_address = 0 THEN
1068 max_rank := geometry_to_rank(NEW.rank_search, NEW.geometry, NEW.country_code);
1069 -- Rank 0 features may also span multiple administrative areas (e.g. lakes)
1070 -- so use the geometry here too. Just make sure the areas don't become too
1072 IF NEW.class = 'natural' or max_rank > 10 THEN
1073 geom := NEW.geometry;
1075 ELSEIF NEW.rank_address > 25 THEN
1078 max_rank = NEW.rank_address;
1081 SELECT * FROM insert_addresslines(NEW.place_id, NEW.partition, max_rank,
1082 NEW.address, geom, NEW.country_code)
1083 INTO NEW.parent_place_id, NEW.postcode, nameaddress_vector;
1085 {% if debug %}RAISE WARNING 'RETURN insert_addresslines: %, %, %', NEW.parent_place_id, NEW.postcode, nameaddress_vector;{% endif %}
1087 IF NEW.address is not null AND NEW.address ? 'postcode'
1088 AND NEW.address->'postcode' not similar to '%(,|;)%' THEN
1089 NEW.postcode := upper(trim(NEW.address->'postcode'));
1092 IF NEW.postcode is null AND NEW.rank_search > 8 THEN
1093 NEW.postcode := get_nearest_postcode(NEW.country_code, NEW.geometry);
1096 -- if we have a name add this to the name search table
1097 IF NEW.name IS NOT NULL THEN
1098 -- Initialise the name vector using our name
1099 NEW.name := add_default_place_name(NEW.country_code, NEW.name);
1100 name_vector := token_get_name_search_tokens(NEW.token_info);
1102 IF NEW.rank_search <= 25 and NEW.rank_address > 0 THEN
1103 result := add_location(NEW.place_id, NEW.country_code, NEW.partition,
1104 name_vector, NEW.rank_search, NEW.rank_address,
1105 upper(trim(NEW.address->'postcode')), NEW.geometry, NEW.centroid);
1106 {% if debug %}RAISE WARNING 'added to location (full)';{% endif %}
1109 IF NEW.rank_search between 26 and 27 and NEW.class = 'highway' THEN
1110 result := insertLocationRoad(NEW.partition, NEW.place_id, NEW.country_code, NEW.geometry);
1111 {% if debug %}RAISE WARNING 'insert into road location table (full)';{% endif %}
1114 result := insertSearchName(NEW.partition, NEW.place_id,
1115 token_get_name_match_tokens(NEW.token_info),
1116 NEW.rank_search, NEW.rank_address, NEW.geometry);
1117 {% if debug %}RAISE WARNING 'added to search name (full)';{% endif %}
1119 {% if not db.reverse_only %}
1120 INSERT INTO search_name (place_id, search_rank, address_rank,
1121 importance, country_code, name_vector,
1122 nameaddress_vector, centroid)
1123 VALUES (NEW.place_id, NEW.rank_search, NEW.rank_address,
1124 NEW.importance, NEW.country_code, name_vector,
1125 nameaddress_vector, NEW.centroid);
1130 {% if debug %}RAISE WARNING 'place update % % finsihed.', NEW.osm_type, NEW.osm_id;{% endif %}
1132 NEW.token_info := token_strip_info(NEW.token_info);
1139 CREATE OR REPLACE FUNCTION placex_delete()
1146 -- RAISE WARNING 'placex_delete % %',OLD.osm_type,OLD.osm_id;
1148 IF OLD.linked_place_id is null THEN
1149 update placex set linked_place_id = null, indexed_status = 2 where linked_place_id = OLD.place_id and indexed_status = 0;
1150 {% if debug %}RAISE WARNING 'placex_delete:01 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1151 update placex set linked_place_id = null where linked_place_id = OLD.place_id;
1152 {% if debug %}RAISE WARNING 'placex_delete:02 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1154 update placex set indexed_status = 2 where place_id = OLD.linked_place_id and indexed_status = 0;
1157 IF OLD.rank_address < 30 THEN
1159 -- mark everything linked to this place for re-indexing
1160 {% if debug %}RAISE WARNING 'placex_delete:03 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1161 UPDATE placex set indexed_status = 2 from place_addressline where address_place_id = OLD.place_id
1162 and placex.place_id = place_addressline.place_id and indexed_status = 0 and place_addressline.isaddress;
1164 {% if debug %}RAISE WARNING 'placex_delete:04 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1165 DELETE FROM place_addressline where address_place_id = OLD.place_id;
1167 {% if debug %}RAISE WARNING 'placex_delete:05 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1168 b := deleteRoad(OLD.partition, OLD.place_id);
1170 {% if debug %}RAISE WARNING 'placex_delete:06 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1171 update placex set indexed_status = 2 where parent_place_id = OLD.place_id and indexed_status = 0;
1172 {% if debug %}RAISE WARNING 'placex_delete:07 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1173 -- reparenting also for OSM Interpolation Lines (and for Tiger?)
1174 update location_property_osmline set indexed_status = 2 where indexed_status = 0 and parent_place_id = OLD.place_id;
1178 {% if debug %}RAISE WARNING 'placex_delete:08 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1180 IF OLD.rank_address < 26 THEN
1181 b := deleteLocationArea(OLD.partition, OLD.place_id, OLD.rank_search);
1184 {% if debug %}RAISE WARNING 'placex_delete:09 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1186 IF OLD.name is not null THEN
1187 {% if 'search_name' in db.tables %}
1188 DELETE from search_name WHERE place_id = OLD.place_id;
1190 b := deleteSearchName(OLD.partition, OLD.place_id);
1193 {% if debug %}RAISE WARNING 'placex_delete:10 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1195 DELETE FROM place_addressline where place_id = OLD.place_id;
1197 {% if debug %}RAISE WARNING 'placex_delete:11 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1199 -- remove from tables for special search
1200 classtable := 'place_classtype_' || OLD.class || '_' || OLD.type;
1201 SELECT count(*)>0 FROM pg_tables WHERE tablename = classtable and schemaname = current_schema() INTO b;
1203 EXECUTE 'DELETE FROM ' || classtable::regclass || ' WHERE place_id = $1' USING OLD.place_id;
1206 {% if debug %}RAISE WARNING 'placex_delete:12 % %',OLD.osm_type,OLD.osm_id;{% endif %}