1 -- Trigger functions for the placex table.
3 -- Find the parent road of a POI.
5 -- \returns Place ID of parent object or NULL if none
7 -- Copy data from linked items (POIs on ways, addr:street links, relations).
9 CREATE OR REPLACE FUNCTION find_parent_for_poi(poi_osm_type CHAR(1),
11 poi_partition SMALLINT,
19 parent_place_id BIGINT DEFAULT NULL;
23 {% if debug %}RAISE WARNING 'finding street for % %', poi_osm_type, poi_osm_id;{% endif %}
25 -- Is this object part of an associatedStreet relation?
27 SELECT members FROM planet_osm_rels
28 WHERE parts @> ARRAY[poi_osm_id]
29 and members @> ARRAY[lower(poi_osm_type) || poi_osm_id]
30 and tags @> ARRAY['associatedStreet']
32 FOR i IN 1..array_upper(location.members, 1) BY 2 LOOP
33 IF location.members[i+1] = 'street' THEN
35 SELECT place_id from placex
36 WHERE osm_type = 'W' and osm_id = substring(location.members[i],2)::bigint
38 and rank_search between 26 and 27
40 RETURN parent.place_id;
46 parent_place_id := find_parent_for_address(addr_street, addr_place,
48 IF parent_place_id is not null THEN
49 RETURN parent_place_id;
52 IF poi_osm_type = 'N' THEN
53 -- Is this node part of an interpolation?
55 SELECT q.parent_place_id
56 FROM location_property_osmline q, planet_osm_ways x
57 WHERE q.linegeo && bbox and x.id = q.osm_id
58 and poi_osm_id = any(x.nodes)
61 {% if debug %}RAISE WARNING 'Get parent from interpolation: %', parent.parent_place_id;{% endif %}
62 RETURN parent.parent_place_id;
65 -- Is this node part of any other way?
67 SELECT p.place_id, p.osm_id, p.rank_search, p.address,
68 coalesce(p.centroid, ST_Centroid(p.geometry)) as centroid
69 FROM placex p, planet_osm_ways w
70 WHERE p.osm_type = 'W' and p.rank_search >= 26
71 and p.geometry && bbox
72 and w.id = p.osm_id and poi_osm_id = any(w.nodes)
74 {% if debug %}RAISE WARNING 'Node is part of way % ', location.osm_id;{% endif %}
76 -- Way IS a road then we are on it - that must be our road
77 IF location.rank_search < 28 THEN
78 {% if debug %}RAISE WARNING 'node in way that is a street %',location;{% endif %}
79 return location.place_id;
82 SELECT find_parent_for_poi('W', location.osm_id, poi_partition,
84 location.address->'street',
85 location.address->'place',
88 IF parent_place_id is not null THEN
89 RETURN parent_place_id;
95 IF addr_street is null and addr_place is not null THEN
96 -- The address is attached to a place we don't know.
97 -- Instead simply use the containing area with the largest rank.
99 SELECT place_id FROM placex
100 WHERE bbox && geometry AND _ST_Covers(geometry, ST_Centroid(bbox))
101 AND rank_address between 5 and 25
102 ORDER BY rank_address desc
104 RETURN location.place_id;
106 ELSEIF ST_Area(bbox) < 0.005 THEN
107 -- for smaller features get the nearest road
108 SELECT getNearestRoadPlaceId(poi_partition, bbox) INTO parent_place_id;
109 {% if debug %}RAISE WARNING 'Checked for nearest way (%)', parent_place_id;{% endif %}
111 -- for larger features simply find the area with the largest rank that
112 -- contains the bbox, only use addressable features
114 SELECT place_id FROM placex
115 WHERE bbox && geometry AND _ST_Covers(geometry, ST_Centroid(bbox))
116 AND rank_address between 5 and 25
117 ORDER BY rank_address desc
119 RETURN location.place_id;
124 RETURN parent_place_id;
127 LANGUAGE plpgsql STABLE;
129 -- Try to find a linked place for the given object.
130 CREATE OR REPLACE FUNCTION find_linked_place(bnd placex)
134 relation_members TEXT[];
136 linked_placex placex%ROWTYPE;
139 IF bnd.rank_search >= 26 or bnd.rank_address = 0
140 or ST_GeometryType(bnd.geometry) NOT IN ('ST_Polygon','ST_MultiPolygon')
141 or bnd.type IN ('postcode', 'postal_code')
146 IF bnd.osm_type = 'R' THEN
147 -- see if we have any special relation members
148 SELECT members FROM planet_osm_rels WHERE id = bnd.osm_id INTO relation_members;
149 {% if debug %}RAISE WARNING 'Got relation members';{% endif %}
151 -- Search for relation members with role 'lable'.
152 IF relation_members IS NOT NULL THEN
154 SELECT get_rel_node_members(relation_members, ARRAY['label']) as member
156 {% if debug %}RAISE WARNING 'Found label member %', rel_member.member;{% endif %}
160 WHERE osm_type = 'N' and osm_id = rel_member.member
163 {% if debug %}RAISE WARNING 'Linked label member';{% endif %}
164 RETURN linked_placex;
171 IF bnd.name ? 'name' THEN
172 bnd_name := make_standard_name(bnd.name->'name');
173 IF bnd_name = '' THEN
178 -- If extratags has a place tag, look for linked nodes by their place type.
179 -- Area and node still have to have the same name.
180 IF bnd.extratags ? 'place' and bnd_name is not null THEN
183 WHERE make_standard_name(name->'name') = bnd_name
184 AND placex.class = 'place' AND placex.type = bnd.extratags->'place'
185 AND placex.osm_type = 'N'
186 AND placex.linked_place_id is null
187 AND placex.rank_search < 26 -- needed to select the right index
188 AND _st_covers(bnd.geometry, placex.geometry)
190 {% if debug %}RAISE WARNING 'Found type-matching place node %', linked_placex.osm_id;{% endif %}
191 RETURN linked_placex;
195 IF bnd.extratags ? 'wikidata' THEN
198 WHERE placex.class = 'place' AND placex.osm_type = 'N'
199 AND placex.extratags ? 'wikidata' -- needed to select right index
200 AND placex.extratags->'wikidata' = bnd.extratags->'wikidata'
201 AND placex.linked_place_id is null
202 AND placex.rank_search < 26
203 AND _st_covers(bnd.geometry, placex.geometry)
204 ORDER BY make_standard_name(name->'name') = bnd_name desc
206 {% if debug %}RAISE WARNING 'Found wikidata-matching place node %', linked_placex.osm_id;{% endif %}
207 RETURN linked_placex;
211 -- Name searches can be done for ways as well as relations
212 IF bnd_name is not null THEN
213 {% if debug %}RAISE WARNING 'Looking for nodes with matching names';{% endif %}
215 SELECT placex.* from placex
216 WHERE make_standard_name(name->'name') = bnd_name
217 AND ((bnd.rank_address > 0
218 and bnd.rank_address = (compute_place_rank(placex.country_code,
220 placex.type, 15::SMALLINT,
221 false, placex.postcode)).address_rank)
222 OR (bnd.rank_address = 0 and placex.rank_search = bnd.rank_search))
223 AND placex.osm_type = 'N'
224 AND placex.linked_place_id is null
225 AND placex.rank_search < 26 -- needed to select the right index
226 AND _st_covers(bnd.geometry, placex.geometry)
228 {% if debug %}RAISE WARNING 'Found matching place node %', linked_placex.osm_id;{% endif %}
229 RETURN linked_placex;
236 LANGUAGE plpgsql STABLE;
239 -- Insert address of a place into the place_addressline table.
241 -- \param obj_place_id Place_id of the place to compute the address for.
242 -- \param partition Partition number where the place is in.
243 -- \param maxrank Rank of the place. All address features must have
244 -- a search rank lower than the given rank.
245 -- \param address Address terms for the place.
246 -- \param geometry Geometry to which the address objects should be close.
248 -- \retval parent_place_id Place_id of the address object that is the direct
250 -- \retval postcode Postcode computed from the address. This is the
251 -- addr:postcode of one of the address objects. If
252 -- more than one of has a postcode, the highest ranking
253 -- one is used. May be NULL.
254 -- \retval nameaddress_vector Search terms for the address. This is the sum
255 -- of name terms of all address objects.
256 CREATE OR REPLACE FUNCTION insert_addresslines(obj_place_id BIGINT,
262 OUT parent_place_id BIGINT,
264 OUT nameaddress_vector INT[])
267 address_havelevel BOOLEAN[];
269 location_isaddress BOOLEAN;
270 current_boundary GEOMETRY := NULL;
271 current_node_area GEOMETRY := NULL;
273 parent_place_rank INT := 0;
274 addr_place_ids BIGINT[];
278 parent_place_id := 0;
279 nameaddress_vector := '{}'::int[];
281 address_havelevel := array_fill(false, ARRAY[maxrank]);
284 SELECT * FROM get_places_for_addr_tags(partition, geometry,
286 ORDER BY rank_address, distance, isguess desc
288 {% if not db.reverse_only %}
289 nameaddress_vector := array_merge(nameaddress_vector,
290 location.keywords::int[]);
293 IF location.place_id is not null THEN
294 location_isaddress := not address_havelevel[location.rank_address];
295 IF not address_havelevel[location.rank_address] THEN
296 address_havelevel[location.rank_address] := true;
297 IF parent_place_rank < location.rank_address THEN
298 parent_place_id := location.place_id;
299 parent_place_rank := location.rank_address;
303 INSERT INTO place_addressline (place_id, address_place_id, fromarea,
304 isaddress, distance, cached_rank_address)
305 VALUES (obj_place_id, location.place_id, not location.isguess,
306 true, location.distance, location.rank_address);
308 addr_place_ids := array_append(addr_place_ids, location.place_id);
313 SELECT * FROM getNearFeatures(partition, geometry, maxrank)
314 WHERE addr_place_ids is null or not addr_place_ids @> ARRAY[place_id]
315 ORDER BY rank_address, isguess asc,
317 CASE WHEN rank_address = 16 AND rank_search = 15 THEN 0.2
318 WHEN rank_address = 16 AND rank_search = 16 THEN 0.25
319 WHEN rank_address = 16 AND rank_search = 18 THEN 0.5
322 -- Ignore all place nodes that do not fit in a lower level boundary.
323 CONTINUE WHEN location.isguess
324 and current_boundary is not NULL
325 and not ST_Contains(current_boundary, location.centroid);
327 -- If this is the first item in the rank, then assume it is the address.
328 location_isaddress := not address_havelevel[location.rank_address];
330 -- Further sanity checks to ensure that the address forms a sane hierarchy.
331 IF location_isaddress THEN
332 IF location.isguess and current_node_area is not NULL THEN
333 location_isaddress := ST_Contains(current_node_area, location.centroid);
335 IF not location.isguess and current_boundary is not NULL
336 and location.rank_address != 11 AND location.rank_address != 5 THEN
337 location_isaddress := ST_Contains(current_boundary, location.centroid);
341 IF location_isaddress THEN
342 address_havelevel[location.rank_address] := true;
343 parent_place_id := location.place_id;
345 -- Set postcode if we have one.
346 -- (Returned will be the highest ranking one.)
347 IF location.postcode is not NULL THEN
348 postcode = location.postcode;
351 -- Recompute the areas we need for hierarchy sanity checks.
352 IF location.rank_address != 11 AND location.rank_address != 5 THEN
353 IF location.isguess THEN
354 current_node_area := place_node_fuzzy_area(location.centroid,
355 location.rank_search);
357 current_node_area := NULL;
358 SELECT p.geometry FROM placex p
359 WHERE p.place_id = location.place_id INTO current_boundary;
364 -- Add it to the list of search terms
365 {% if not db.reverse_only %}
366 nameaddress_vector := array_merge(nameaddress_vector,
367 location.keywords::integer[]);
370 INSERT INTO place_addressline (place_id, address_place_id, fromarea,
371 isaddress, distance, cached_rank_address)
372 VALUES (obj_place_id, location.place_id, not location.isguess,
373 location_isaddress, location.distance, location.rank_address);
380 CREATE OR REPLACE FUNCTION placex_insert()
387 country_code VARCHAR(2);
391 {% if debug %}RAISE WARNING '% % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;{% endif %}
393 NEW.place_id := nextval('seq_place');
394 NEW.indexed_status := 1; --STATUS_NEW
396 NEW.country_code := lower(get_country_code(NEW.geometry));
398 NEW.partition := get_partition(NEW.country_code);
399 NEW.geometry_sector := geometry_sector(NEW.partition, NEW.geometry);
401 IF NEW.osm_type = 'X' THEN
402 -- E'X'ternal records should already be in the right format so do nothing
404 is_area := ST_GeometryType(NEW.geometry) IN ('ST_Polygon','ST_MultiPolygon');
406 IF NEW.class in ('place','boundary')
407 AND NEW.type in ('postcode','postal_code')
409 IF NEW.address IS NULL OR NOT NEW.address ? 'postcode' THEN
410 -- most likely just a part of a multipolygon postcode boundary, throw it away
414 NEW.name := hstore('ref', NEW.address->'postcode');
416 ELSEIF NEW.class = 'highway' AND is_area AND NEW.name is null
417 AND NEW.extratags ? 'area' AND NEW.extratags->'area' = 'yes'
420 ELSEIF NEW.class = 'boundary' AND NOT is_area
423 ELSEIF NEW.class = 'boundary' AND NEW.type = 'administrative'
424 AND NEW.admin_level <= 4 AND NEW.osm_type = 'W'
429 SELECT * INTO NEW.rank_search, NEW.rank_address
430 FROM compute_place_rank(NEW.country_code,
431 CASE WHEN is_area THEN 'A' ELSE NEW.osm_type END,
432 NEW.class, NEW.type, NEW.admin_level,
433 (NEW.extratags->'capital') = 'yes',
434 NEW.address->'postcode');
436 -- a country code make no sense below rank 4 (country)
437 IF NEW.rank_search < 4 THEN
438 NEW.country_code := NULL;
443 {% if debug %}RAISE WARNING 'placex_insert:END: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;{% endif %}
445 {% if not disable_diff_updates %}
446 -- The following is not needed until doing diff updates, and slows the main index process down
448 IF NEW.osm_type = 'N' and NEW.rank_search > 28 THEN
449 -- might be part of an interpolation
450 result := osmline_reinsert(NEW.osm_id, NEW.geometry);
451 ELSEIF NEW.rank_address > 0 THEN
452 IF (ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon') AND ST_IsValid(NEW.geometry)) THEN
453 -- Performance: We just can't handle re-indexing for country level changes
454 IF st_area(NEW.geometry) < 1 THEN
455 -- mark items within the geometry for re-indexing
456 -- RAISE WARNING 'placex poly insert: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
458 UPDATE placex SET indexed_status = 2
459 WHERE ST_Intersects(NEW.geometry, placex.geometry)
460 and indexed_status = 0
461 and ((rank_address = 0 and rank_search > NEW.rank_address)
462 or rank_address > NEW.rank_address
463 or (class = 'place' and osm_type = 'N')
465 and (rank_search < 28
467 or (NEW.rank_address >= 16 and address ? 'place'));
470 -- mark nearby items for re-indexing, where 'nearby' depends on the features rank_search and is a complete guess :(
471 diameter := update_place_diameter(NEW.rank_search);
473 -- RAISE WARNING 'placex point insert: % % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type,diameter;
474 IF NEW.rank_search >= 26 THEN
475 -- roads may cause reparenting for >27 rank places
476 update placex set indexed_status = 2 where indexed_status = 0 and rank_search > NEW.rank_search and ST_DWithin(placex.geometry, NEW.geometry, diameter);
477 -- reparenting also for OSM Interpolation Lines (and for Tiger?)
478 update location_property_osmline set indexed_status = 2 where indexed_status = 0 and ST_DWithin(location_property_osmline.linegeo, NEW.geometry, diameter);
479 ELSEIF NEW.rank_search >= 16 THEN
480 -- up to rank 16, street-less addresses may need reparenting
481 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');
483 -- for all other places the search terms may change as well
484 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);
491 -- add to tables for special search
492 -- Note: won't work on initial import because the classtype tables
493 -- do not yet exist. It won't hurt either.
494 classtable := 'place_classtype_' || NEW.class || '_' || NEW.type;
495 SELECT count(*)>0 FROM pg_tables WHERE tablename = classtable and schemaname = current_schema() INTO result;
497 EXECUTE 'INSERT INTO ' || classtable::regclass || ' (place_id, centroid) VALUES ($1,$2)'
498 USING NEW.place_id, ST_Centroid(NEW.geometry);
501 {% endif %} -- not disable_diff_updates
509 CREATE OR REPLACE FUNCTION placex_update()
515 relation_members TEXT[];
518 parent_address_level SMALLINT;
519 place_address_level SMALLINT;
526 name_vector INTEGER[];
527 nameaddress_vector INTEGER[];
528 addr_nameaddress_vector INTEGER[];
530 inherited_address HSTORE;
532 linked_node_id BIGINT;
533 linked_importance FLOAT;
534 linked_wikipedia TEXT;
539 IF OLD.indexed_status = 100 THEN
540 {% if debug %}RAISE WARNING 'placex_update delete % %',NEW.osm_type,NEW.osm_id;{% endif %}
541 delete from placex where place_id = OLD.place_id;
545 IF NEW.indexed_status != 0 OR OLD.indexed_status = 0 THEN
549 {% if debug %}RAISE WARNING 'placex_update % % (%)',NEW.osm_type,NEW.osm_id,NEW.place_id;{% endif %}
551 NEW.indexed_date = now();
553 {% if 'search_name' in db.tables %}
554 DELETE from search_name WHERE place_id = NEW.place_id;
556 result := deleteSearchName(NEW.partition, NEW.place_id);
557 DELETE FROM place_addressline WHERE place_id = NEW.place_id;
558 result := deleteRoad(NEW.partition, NEW.place_id);
559 result := deleteLocationArea(NEW.partition, NEW.place_id, NEW.rank_search);
560 UPDATE placex set linked_place_id = null, indexed_status = 2
561 where linked_place_id = NEW.place_id;
562 -- update not necessary for osmline, cause linked_place_id does not exist
564 NEW.extratags := NEW.extratags - 'linked_place'::TEXT;
565 NEW.address := NEW.address - '_unlisted_place'::TEXT;
567 IF NEW.linked_place_id is not null THEN
568 {% if debug %}RAISE WARNING 'place already linked to %', NEW.linked_place_id;{% endif %}
572 -- Postcodes are just here to compute the centroids. They are not searchable
573 -- unless they are a boundary=postal_code.
574 -- There was an error in the style so that boundary=postal_code used to be
575 -- imported as place=postcode. That's why relations are allowed to pass here.
576 -- This can go away in a couple of versions.
577 IF NEW.class = 'place' and NEW.type = 'postcode' and NEW.osm_type != 'R' THEN
581 -- Speed up searches - just use the centroid of the feature
582 -- cheaper but less acurate
583 NEW.centroid := ST_PointOnSurface(NEW.geometry);
584 {% if debug %}RAISE WARNING 'Computing preliminary centroid at %',ST_AsText(NEW.centroid);{% endif %}
586 -- recompute the ranks, they might change when linking changes
587 SELECT * INTO NEW.rank_search, NEW.rank_address
588 FROM compute_place_rank(NEW.country_code,
589 CASE WHEN ST_GeometryType(NEW.geometry)
590 IN ('ST_Polygon','ST_MultiPolygon')
591 THEN 'A' ELSE NEW.osm_type END,
592 NEW.class, NEW.type, NEW.admin_level,
593 (NEW.extratags->'capital') = 'yes',
594 NEW.address->'postcode');
595 -- We must always increase the address level relative to the admin boundary.
596 IF NEW.class = 'boundary' and NEW.type = 'administrative'
597 and NEW.osm_type = 'R' and NEW.rank_address > 0
599 -- First, check that admin boundaries do not overtake each other rank-wise.
600 parent_address_level := 3;
603 (CASE WHEN extratags ? 'wikidata' and NEW.extratags ? 'wikidata'
604 and extratags->'wikidata' = NEW.extratags->'wikidata'
605 THEN ST_Equals(geometry, NEW.geometry)
606 ELSE false END) as is_same
608 WHERE osm_type = 'R' and class = 'boundary' and type = 'administrative'
609 and admin_level < NEW.admin_level and admin_level > 3
611 and geometry && NEW.centroid and _ST_Covers(geometry, NEW.centroid)
612 ORDER BY admin_level desc LIMIT 1
614 IF location.is_same THEN
615 -- Looks like the same boundary is replicated on multiple admin_levels.
616 -- Usual tagging in Poland. Remove our boundary from addresses.
617 NEW.rank_address := 0;
619 parent_address_level := location.rank_address;
620 IF location.rank_address >= NEW.rank_address THEN
621 IF location.rank_address >= 24 THEN
622 NEW.rank_address := 25;
624 NEW.rank_address := location.rank_address + 2;
630 IF NEW.rank_address > 9 THEN
631 -- Second check that the boundary is not completely contained in a
632 -- place area with a higher address rank
634 SELECT rank_address FROM placex
635 WHERE class = 'place' and rank_address < 24
636 and rank_address > NEW.rank_address
637 and geometry && NEW.geometry
638 and geometry ~ NEW.geometry -- needed because ST_Relate does not do bbox cover test
639 and ST_Relate(geometry, NEW.geometry, 'T*T***FF*') -- contains but not equal
640 ORDER BY rank_address desc LIMIT 1
642 NEW.rank_address := location.rank_address + 2;
645 ELSEIF NEW.class = 'place' and NEW.osm_type = 'N'
646 and NEW.rank_address between 16 and 23
648 -- If a place node is contained in a admin boundary with the same address level
649 -- and has not been linked, then make the node a subpart by increasing the
650 -- address rank (city level and above).
652 SELECT rank_address FROM placex
653 WHERE osm_type = 'R' and class = 'boundary' and type = 'administrative'
654 and rank_address = NEW.rank_address
655 and geometry && NEW.centroid and _ST_Covers(geometry, NEW.centroid)
658 NEW.rank_address = NEW.rank_address + 2;
661 parent_address_level := 3;
664 {% if debug %}RAISE WARNING 'Copy over address tags';{% endif %}
665 -- housenumber is a computed field, so start with an empty value
666 NEW.housenumber := NULL;
667 IF NEW.address is not NULL THEN
668 IF NEW.address ? 'conscriptionnumber' THEN
669 i := getorcreate_housenumber_id(make_standard_name(NEW.address->'conscriptionnumber'));
670 IF NEW.address ? 'streetnumber' THEN
671 i := getorcreate_housenumber_id(make_standard_name(NEW.address->'streetnumber'));
672 NEW.housenumber := (NEW.address->'conscriptionnumber') || '/' || (NEW.address->'streetnumber');
674 NEW.housenumber := NEW.address->'conscriptionnumber';
676 ELSEIF NEW.address ? 'streetnumber' THEN
677 NEW.housenumber := NEW.address->'streetnumber';
678 i := getorcreate_housenumber_id(make_standard_name(NEW.address->'streetnumber'));
679 ELSEIF NEW.address ? 'housenumber' THEN
680 NEW.housenumber := NEW.address->'housenumber';
681 i := getorcreate_housenumber_id(make_standard_name(NEW.housenumber));
684 addr_street := NEW.address->'street';
685 addr_place := NEW.address->'place';
687 IF NEW.address ? 'postcode' and NEW.address->'postcode' not similar to '%(:|,|;)%' THEN
688 i := getorcreate_postcode_id(NEW.address->'postcode');
692 NEW.postcode := null;
694 -- recalculate country and partition
695 IF NEW.rank_search = 4 AND NEW.address is not NULL AND NEW.address ? 'country' THEN
696 -- for countries, believe the mapped country code,
697 -- so that we remain in the right partition if the boundaries
699 NEW.country_code := lower(NEW.address->'country');
700 NEW.partition := get_partition(lower(NEW.country_code));
701 IF NEW.partition = 0 THEN
702 NEW.country_code := lower(get_country_code(NEW.centroid));
703 NEW.partition := get_partition(NEW.country_code);
706 IF NEW.rank_search >= 4 THEN
707 NEW.country_code := lower(get_country_code(NEW.centroid));
709 NEW.country_code := NULL;
711 NEW.partition := get_partition(NEW.country_code);
713 {% if debug %}RAISE WARNING 'Country updated: "%"', NEW.country_code;{% endif %}
715 -- waterway ways are linked when they are part of a relation and have the same class/type
716 IF NEW.osm_type = 'R' and NEW.class = 'waterway' THEN
717 FOR relation_members IN select members from planet_osm_rels r where r.id = NEW.osm_id and r.parts != array[]::bigint[]
719 FOR i IN 1..array_upper(relation_members, 1) BY 2 LOOP
720 IF relation_members[i+1] in ('', 'main_stream', 'side_stream') AND substring(relation_members[i],1,1) = 'w' THEN
721 {% if debug %}RAISE WARNING 'waterway parent %, child %/%', NEW.osm_id, i, relation_members[i];{% endif %}
722 FOR linked_node_id IN SELECT place_id FROM placex
723 WHERE osm_type = 'W' and osm_id = substring(relation_members[i],2,200)::bigint
724 and class = NEW.class and type in ('river', 'stream', 'canal', 'drain', 'ditch')
725 and ( relation_members[i+1] != 'side_stream' or NEW.name->'name' = name->'name')
727 UPDATE placex SET linked_place_id = NEW.place_id WHERE place_id = linked_node_id;
728 {% if 'search_name' in db.tables %}
729 DELETE FROM search_name WHERE place_id = linked_node_id;
735 {% if debug %}RAISE WARNING 'Waterway processed';{% endif %}
738 NEW.importance := null;
739 SELECT wikipedia, importance
740 FROM compute_importance(NEW.extratags, NEW.country_code, NEW.osm_type, NEW.osm_id)
741 INTO NEW.wikipedia,NEW.importance;
743 {% if debug %}RAISE WARNING 'Importance computed from wikipedia: %', NEW.importance;{% endif %}
745 -- ---------------------------------------------------------------------------
746 -- For low level elements we inherit from our parent road
747 IF NEW.rank_search > 27 THEN
749 {% if debug %}RAISE WARNING 'finding street for % %', NEW.osm_type, NEW.osm_id;{% endif %}
750 NEW.parent_place_id := null;
752 -- if we have a POI and there is no address information,
753 -- see if we can get it from a surrounding building
754 inherited_address := ''::HSTORE;
755 IF NEW.osm_type = 'N' AND addr_street IS NULL AND addr_place IS NULL
756 AND NEW.housenumber IS NULL THEN
758 -- The additional && condition works around the misguided query
759 -- planner of postgis 3.0.
760 SELECT address from placex where ST_Covers(geometry, NEW.centroid)
761 and geometry && NEW.centroid
762 and (address ? 'housenumber' or address ? 'street' or address ? 'place')
763 and rank_search > 28 AND ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon')
766 NEW.housenumber := location.address->'housenumber';
767 addr_street := location.address->'street';
768 addr_place := location.address->'place';
769 inherited_address := location.address;
773 -- We have to find our parent road.
774 NEW.parent_place_id := find_parent_for_poi(NEW.osm_type, NEW.osm_id,
776 ST_Envelope(NEW.geometry),
777 addr_street, addr_place);
779 -- If we found the road take a shortcut here.
780 -- Otherwise fall back to the full address getting method below.
781 IF NEW.parent_place_id is not null THEN
783 -- Get the details of the parent road
784 SELECT p.country_code, p.postcode, p.name FROM placex p
785 WHERE p.place_id = NEW.parent_place_id INTO location;
787 IF addr_street is null and addr_place is not null THEN
788 -- Check if the addr:place tag is part of the parent name
789 SELECT count(*) INTO i
790 FROM svals(location.name) AS pname WHERE pname = addr_place;
792 NEW.address = NEW.address || hstore('_unlisted_place', addr_place);
796 NEW.country_code := location.country_code;
797 {% if debug %}RAISE WARNING 'Got parent details from search name';{% endif %}
799 -- determine postcode
800 IF NEW.address is not null AND NEW.address ? 'postcode' THEN
801 NEW.postcode = upper(trim(NEW.address->'postcode'));
803 NEW.postcode := location.postcode;
805 IF NEW.postcode is null THEN
806 NEW.postcode := get_nearest_postcode(NEW.country_code, NEW.geometry);
809 IF NEW.name is not NULL THEN
810 NEW.name := add_default_place_name(NEW.country_code, NEW.name);
811 name_vector := make_keywords(NEW.name);
813 IF NEW.rank_search <= 25 and NEW.rank_address > 0 THEN
814 result := add_location(NEW.place_id, NEW.country_code, NEW.partition,
815 name_vector, NEW.rank_search, NEW.rank_address,
816 upper(trim(NEW.address->'postcode')), NEW.geometry,
818 {% if debug %}RAISE WARNING 'Place added to location table';{% endif %}
823 {% if not db.reverse_only %}
824 IF array_length(name_vector, 1) is not NULL
825 OR inherited_address is not NULL OR NEW.address is not NULL
827 SELECT * INTO name_vector, nameaddress_vector
828 FROM create_poi_search_terms(NEW.place_id,
829 NEW.partition, NEW.parent_place_id,
830 inherited_address || NEW.address,
831 NEW.country_code, NEW.housenumber,
832 name_vector, NEW.centroid);
834 IF array_length(name_vector, 1) is not NULL THEN
835 INSERT INTO search_name (place_id, search_rank, address_rank,
836 importance, country_code, name_vector,
837 nameaddress_vector, centroid)
838 VALUES (NEW.place_id, NEW.rank_search, NEW.rank_address,
839 NEW.importance, NEW.country_code, name_vector,
840 nameaddress_vector, NEW.centroid);
841 {% if debug %}RAISE WARNING 'Place added to search table';{% endif %}
851 -- ---------------------------------------------------------------------------
853 {% if debug %}RAISE WARNING 'Using full index mode for % %', NEW.osm_type, NEW.osm_id;{% endif %}
854 SELECT * INTO location FROM find_linked_place(NEW);
855 IF location.place_id is not null THEN
856 {% if debug %}RAISE WARNING 'Linked %', location;{% endif %}
858 -- Use the linked point as the centre point of the geometry,
859 -- but only if it is within the area of the boundary.
860 geom := coalesce(location.centroid, ST_Centroid(location.geometry));
861 IF geom is not NULL AND ST_Within(geom, NEW.geometry) THEN
862 NEW.centroid := geom;
865 {% if debug %}RAISE WARNING 'parent address: % rank address: %', parent_address_level, location.rank_address;{% endif %}
866 IF location.rank_address > parent_address_level
867 and location.rank_address < 26
869 NEW.rank_address := location.rank_address;
872 -- merge in the label name
873 IF NOT location.name IS NULL THEN
874 NEW.name := location.name || NEW.name;
877 -- merge in extra tags
878 NEW.extratags := hstore('linked_' || location.class, location.type)
879 || coalesce(location.extratags, ''::hstore)
880 || coalesce(NEW.extratags, ''::hstore);
882 -- mark the linked place (excludes from search results)
883 UPDATE placex set linked_place_id = NEW.place_id
884 WHERE place_id = location.place_id;
885 -- ensure that those places are not found anymore
886 {% if 'search_name' in db.tables %}
887 DELETE FROM search_name WHERE place_id = location.place_id;
889 PERFORM deleteLocationArea(NEW.partition, location.place_id, NEW.rank_search);
891 SELECT wikipedia, importance
892 FROM compute_importance(location.extratags, NEW.country_code,
893 'N', location.osm_id)
894 INTO linked_wikipedia,linked_importance;
896 -- Use the maximum importance if one could be computed from the linked object.
897 IF linked_importance is not null AND
898 (NEW.importance is null or NEW.importance < linked_importance)
900 NEW.importance = linked_importance;
903 -- No linked place? As a last resort check if the boundary is tagged with
904 -- a place type and adapt the rank address.
905 IF NEW.rank_address > 0 and NEW.extratags ? 'place' THEN
906 SELECT address_rank INTO place_address_level
907 FROM compute_place_rank(NEW.country_code, 'A', 'place',
908 NEW.extratags->'place', 0::SMALLINT, False, null);
909 IF place_address_level > parent_address_level and
910 place_address_level < 26 THEN
911 NEW.rank_address := place_address_level;
916 -- Initialise the name vector using our name
917 NEW.name := add_default_place_name(NEW.country_code, NEW.name);
918 name_vector := make_keywords(NEW.name);
920 -- make sure all names are in the word table
921 IF NEW.admin_level = 2
922 AND NEW.class = 'boundary' AND NEW.type = 'administrative'
923 AND NEW.country_code IS NOT NULL AND NEW.osm_type = 'R'
925 PERFORM create_country(NEW.name, lower(NEW.country_code));
926 {% if debug %}RAISE WARNING 'Country names updated';{% endif %}
928 -- Also update the list of country names. Adding an additional sanity
929 -- check here: make sure the country does overlap with the area where
930 -- we expect it to be as per static country grid.
932 SELECT country_code FROM country_osm_grid
933 WHERE ST_Covers(geometry, NEW.centroid) and country_code = NEW.country_code
936 {% if debug %}RAISE WARNING 'Updating names for country '%' with: %', NEW.country_code, NEW.name;{% endif %}
937 UPDATE country_name SET name = name || NEW.name WHERE country_code = NEW.country_code;
941 -- For linear features we need the full geometry for determining the address
942 -- because they may go through several administrative entities. Otherwise use
943 -- the centroid for performance reasons.
944 IF ST_GeometryType(NEW.geometry) in ('ST_LineString', 'ST_MultiLineString') THEN
945 geom := NEW.geometry;
947 geom := NEW.centroid;
950 IF NEW.rank_address = 0 THEN
951 max_rank := geometry_to_rank(NEW.rank_search, NEW.geometry, NEW.country_code);
952 -- Rank 0 features may also span multiple administrative areas (e.g. lakes)
953 -- so use the geometry here too. Just make sure the areas don't become too
955 IF NEW.class = 'natural' or max_rank > 10 THEN
956 geom := NEW.geometry;
958 ELSEIF NEW.rank_address > 25 THEN
961 max_rank = NEW.rank_address;
964 SELECT * FROM insert_addresslines(NEW.place_id, NEW.partition, max_rank,
965 NEW.address, geom, NEW.country_code)
966 INTO NEW.parent_place_id, NEW.postcode, nameaddress_vector;
968 {% if debug %}RAISE WARNING 'RETURN insert_addresslines: %, %, %', NEW.parent_place_id, NEW.postcode, nameaddress_vector;{% endif %}
970 IF NEW.address is not null AND NEW.address ? 'postcode'
971 AND NEW.address->'postcode' not similar to '%(,|;)%' THEN
972 NEW.postcode := upper(trim(NEW.address->'postcode'));
975 IF NEW.postcode is null AND NEW.rank_search > 8 THEN
976 NEW.postcode := get_nearest_postcode(NEW.country_code, NEW.geometry);
979 -- if we have a name add this to the name search table
980 IF NEW.name IS NOT NULL THEN
982 IF NEW.rank_search <= 25 and NEW.rank_address > 0 THEN
983 result := add_location(NEW.place_id, NEW.country_code, NEW.partition, name_vector, NEW.rank_search, NEW.rank_address, upper(trim(NEW.address->'postcode')), NEW.geometry, NEW.centroid);
984 {% if debug %}RAISE WARNING 'added to location (full)';{% endif %}
987 IF NEW.rank_search between 26 and 27 and NEW.class = 'highway' THEN
988 result := insertLocationRoad(NEW.partition, NEW.place_id, NEW.country_code, NEW.geometry);
989 {% if debug %}RAISE WARNING 'insert into road location table (full)';{% endif %}
992 result := insertSearchName(NEW.partition, NEW.place_id, name_vector,
993 NEW.rank_search, NEW.rank_address, NEW.geometry);
994 {% if debug %}RAISE WARNING 'added to search name (full)';{% endif %}
996 {% if not db.reverse_only %}
997 INSERT INTO search_name (place_id, search_rank, address_rank,
998 importance, country_code, name_vector,
999 nameaddress_vector, centroid)
1000 VALUES (NEW.place_id, NEW.rank_search, NEW.rank_address,
1001 NEW.importance, NEW.country_code, name_vector,
1002 nameaddress_vector, NEW.centroid);
1007 {% if debug %}RAISE WARNING 'place update % % finsihed.', NEW.osm_type, NEW.osm_id;{% endif %}
1015 CREATE OR REPLACE FUNCTION placex_delete()
1022 -- RAISE WARNING 'placex_delete % %',OLD.osm_type,OLD.osm_id;
1024 IF OLD.linked_place_id is null THEN
1025 update placex set linked_place_id = null, indexed_status = 2 where linked_place_id = OLD.place_id and indexed_status = 0;
1026 {% if debug %}RAISE WARNING 'placex_delete:01 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1027 update placex set linked_place_id = null where linked_place_id = OLD.place_id;
1028 {% if debug %}RAISE WARNING 'placex_delete:02 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1030 update placex set indexed_status = 2 where place_id = OLD.linked_place_id and indexed_status = 0;
1033 IF OLD.rank_address < 30 THEN
1035 -- mark everything linked to this place for re-indexing
1036 {% if debug %}RAISE WARNING 'placex_delete:03 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1037 UPDATE placex set indexed_status = 2 from place_addressline where address_place_id = OLD.place_id
1038 and placex.place_id = place_addressline.place_id and indexed_status = 0 and place_addressline.isaddress;
1040 {% if debug %}RAISE WARNING 'placex_delete:04 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1041 DELETE FROM place_addressline where address_place_id = OLD.place_id;
1043 {% if debug %}RAISE WARNING 'placex_delete:05 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1044 b := deleteRoad(OLD.partition, OLD.place_id);
1046 {% if debug %}RAISE WARNING 'placex_delete:06 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1047 update placex set indexed_status = 2 where parent_place_id = OLD.place_id and indexed_status = 0;
1048 {% if debug %}RAISE WARNING 'placex_delete:07 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1049 -- reparenting also for OSM Interpolation Lines (and for Tiger?)
1050 update location_property_osmline set indexed_status = 2 where indexed_status = 0 and parent_place_id = OLD.place_id;
1054 {% if debug %}RAISE WARNING 'placex_delete:08 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1056 IF OLD.rank_address < 26 THEN
1057 b := deleteLocationArea(OLD.partition, OLD.place_id, OLD.rank_search);
1060 {% if debug %}RAISE WARNING 'placex_delete:09 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1062 IF OLD.name is not null THEN
1063 {% if 'search_name' in db.tables %}
1064 DELETE from search_name WHERE place_id = OLD.place_id;
1066 b := deleteSearchName(OLD.partition, OLD.place_id);
1069 {% if debug %}RAISE WARNING 'placex_delete:10 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1071 DELETE FROM place_addressline where place_id = OLD.place_id;
1073 {% if debug %}RAISE WARNING 'placex_delete:11 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1075 -- remove from tables for special search
1076 classtable := 'place_classtype_' || OLD.class || '_' || OLD.type;
1077 SELECT count(*)>0 FROM pg_tables WHERE tablename = classtable and schemaname = current_schema() INTO b;
1079 EXECUTE 'DELETE FROM ' || classtable::regclass || ' WHERE place_id = $1' USING OLD.place_id;
1082 {% if debug %}RAISE WARNING 'placex_delete:12 % %',OLD.osm_type,OLD.osm_id;{% endif %}