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 --DEBUG: RAISE WARNING 'finding street for % %', poi_osm_type, poi_osm_id;
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 --DEBUG: RAISE WARNING 'Get parent from interpolation: %', parent.parent_place_id;
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 --DEBUG: RAISE WARNING 'Node is part of way % ', location.osm_id;
76 -- Way IS a road then we are on it - that must be our road
77 IF location.rank_search < 28 THEN
78 --DEBUG: RAISE WARNING 'node in way that is a street %',location;
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 --DEBUG: RAISE WARNING 'Checked for nearest way (%)', parent_place_id;
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')
145 IF bnd.osm_type = 'R' THEN
146 -- see if we have any special relation members
147 SELECT members FROM planet_osm_rels WHERE id = bnd.osm_id INTO relation_members;
148 --DEBUG: RAISE WARNING 'Got relation members';
150 -- Search for relation members with role 'lable'.
151 IF relation_members IS NOT NULL THEN
153 SELECT get_rel_node_members(relation_members, ARRAY['label']) as member
155 --DEBUG: RAISE WARNING 'Found label member %', rel_member.member;
159 WHERE osm_type = 'N' and osm_id = rel_member.member
162 --DEBUG: RAISE WARNING 'Linked label member';
163 RETURN linked_placex;
170 IF bnd.name ? 'name' THEN
171 bnd_name := make_standard_name(bnd.name->'name');
172 IF bnd_name = '' THEN
177 -- If extratags has a place tag, look for linked nodes by their place type.
178 -- Area and node still have to have the same name.
179 IF bnd.extratags ? 'place' and bnd_name is not null THEN
182 WHERE make_standard_name(name->'name') = bnd_name
183 AND placex.class = 'place' AND placex.type = bnd.extratags->'place'
184 AND placex.osm_type = 'N'
185 AND placex.rank_search < 26 -- needed to select the right index
186 AND _st_covers(bnd.geometry, placex.geometry)
188 --DEBUG: RAISE WARNING 'Found type-matching place node %', linked_placex.osm_id;
189 RETURN linked_placex;
193 IF bnd.extratags ? 'wikidata' THEN
196 WHERE placex.class = 'place' AND placex.osm_type = 'N'
197 AND placex.extratags ? 'wikidata' -- needed to select right index
198 AND placex.extratags->'wikidata' = bnd.extratags->'wikidata'
199 AND placex.rank_search < 26
200 AND _st_covers(bnd.geometry, placex.geometry)
201 ORDER BY make_standard_name(name->'name') = bnd_name desc
203 --DEBUG: RAISE WARNING 'Found wikidata-matching place node %', linked_placex.osm_id;
204 RETURN linked_placex;
208 -- Name searches can be done for ways as well as relations
209 IF bnd_name is not null THEN
210 --DEBUG: RAISE WARNING 'Looking for nodes with matching names';
212 SELECT placex.* from placex
213 WHERE make_standard_name(name->'name') = bnd_name
214 AND ((bnd.rank_address > 0 and placex.rank_address = bnd.rank_address)
215 OR (bnd.rank_address = 0 and placex.rank_search = bnd.rank_search))
216 AND placex.osm_type = 'N'
217 AND placex.rank_search < 26 -- needed to select the right index
218 AND _st_covers(bnd.geometry, placex.geometry)
220 --DEBUG: RAISE WARNING 'Found matching place node %', linked_placex.osm_id;
221 RETURN linked_placex;
228 LANGUAGE plpgsql STABLE;
231 -- Insert address of a place into the place_addressline table.
233 -- \param obj_place_id Place_id of the place to compute the address for.
234 -- \param partition Partition number where the place is in.
235 -- \param maxrank Rank of the place. All address features must have
236 -- a search rank lower than the given rank.
237 -- \param address Address terms for the place.
238 -- \param geometry Geometry to which the address objects should be close.
240 -- \retval parent_place_id Place_id of the address object that is the direct
242 -- \retval postcode Postcode computed from the address. This is the
243 -- addr:postcode of one of the address objects. If
244 -- more than one of has a postcode, the highest ranking
245 -- one is used. May be NULL.
246 -- \retval nameaddress_vector Search terms for the address. This is the sum
247 -- of name terms of all address objects.
248 CREATE OR REPLACE FUNCTION insert_addresslines(obj_place_id BIGINT,
253 OUT parent_place_id BIGINT,
255 OUT nameaddress_vector INT[])
258 current_rank_address INTEGER := 0;
259 location_distance FLOAT := 0;
260 location_parent GEOMETRY := NULL;
261 parent_place_id_rank SMALLINT := 0;
263 location_isaddress BOOLEAN;
265 address_havelevel BOOLEAN[];
266 location_keywords INT[];
274 parent_place_id := 0;
275 nameaddress_vector := '{}'::int[];
276 isin_tokens := '{}'::int[];
278 ---- convert address store to array of tokenids
279 IF address IS NOT NULL THEN
280 FOR addr_item IN SELECT * FROM each(address)
282 IF addr_item.key IN ('city', 'tiger:county', 'state', 'suburb', 'province',
283 'district', 'region', 'county', 'municipality',
284 'hamlet', 'village', 'subdistrict', 'town',
285 'neighbourhood', 'quarter', 'parish')
287 isin_tokens := array_merge(isin_tokens,
288 word_ids_from_name(addr_item.value));
289 IF NOT %REVERSE-ONLY% THEN
290 nameaddress_vector := array_merge(nameaddress_vector,
291 addr_ids_from_name(addr_item.value));
296 IF NOT %REVERSE-ONLY% THEN
297 nameaddress_vector := array_merge(nameaddress_vector, isin_tokens);
300 ---- now compute the address terms
302 address_havelevel[i] := false;
306 SELECT * FROM getNearFeatures(partition, geometry, maxrank, isin_tokens)
308 IF location.rank_address != current_rank_address THEN
309 current_rank_address := location.rank_address;
310 IF location.isguess THEN
311 location_distance := location.distance * 1.5;
313 IF location.rank_address <= 12 THEN
314 -- for county and above, if we have an area consider that exact
315 -- (It would be nice to relax the constraint for places close to
316 -- the boundary but we'd need the exact geometry for that. Too
318 location_distance = 0;
320 -- Below county level remain slightly fuzzy.
321 location_distance := location.distance * 0.5;
325 CONTINUE WHEN location.keywords <@ location_keywords;
328 IF location.distance < location_distance OR NOT location.isguess THEN
329 location_keywords := location.keywords;
331 location_isaddress := NOT address_havelevel[location.rank_address];
332 --DEBUG: RAISE WARNING 'should be address: %, is guess: %, rank: %', location_isaddress, location.isguess, location.rank_address;
333 IF location_isaddress AND location.isguess AND location_parent IS NOT NULL THEN
334 location_isaddress := ST_Contains(location_parent, location.centroid);
337 --DEBUG: RAISE WARNING '% isaddress: %', location.place_id, location_isaddress;
338 -- Add it to the list of search terms
339 IF NOT %REVERSE-ONLY% THEN
340 nameaddress_vector := array_merge(nameaddress_vector,
341 location.keywords::integer[]);
344 INSERT INTO place_addressline (place_id, address_place_id, fromarea,
345 isaddress, distance, cached_rank_address)
346 VALUES (obj_place_id, location.place_id, true,
347 location_isaddress, location.distance, location.rank_address);
349 IF location_isaddress THEN
350 -- add postcode if we have one
351 -- (If multiple postcodes are available, we end up with the highest ranking one.)
352 IF location.postcode is not null THEN
353 postcode = location.postcode;
356 address_havelevel[location.rank_address] := true;
357 -- add a hack against postcode ranks
358 IF NOT location.isguess
359 AND location.rank_address != 11 AND location.rank_address != 5
361 SELECT p.geometry FROM placex p
362 WHERE p.place_id = location.place_id INTO location_parent;
365 IF location.rank_address > parent_place_id_rank THEN
366 parent_place_id = location.place_id;
367 parent_place_id_rank = location.rank_address;
378 CREATE OR REPLACE FUNCTION placex_insert()
385 country_code VARCHAR(2);
389 --DEBUG: RAISE WARNING '% % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
391 NEW.place_id := nextval('seq_place');
392 NEW.indexed_status := 1; --STATUS_NEW
394 NEW.country_code := lower(get_country_code(NEW.geometry));
396 NEW.partition := get_partition(NEW.country_code);
397 NEW.geometry_sector := geometry_sector(NEW.partition, NEW.geometry);
399 IF NEW.osm_type = 'X' THEN
400 -- E'X'ternal records should already be in the right format so do nothing
402 is_area := ST_GeometryType(NEW.geometry) IN ('ST_Polygon','ST_MultiPolygon');
404 IF NEW.class in ('place','boundary')
405 AND NEW.type in ('postcode','postal_code')
407 IF NEW.address IS NULL OR NOT NEW.address ? 'postcode' THEN
408 -- most likely just a part of a multipolygon postcode boundary, throw it away
412 NEW.name := hstore('ref', NEW.address->'postcode');
414 ELSEIF NEW.class = 'highway' AND is_area AND NEW.name is null
415 AND NEW.extratags ? 'area' AND NEW.extratags->'area' = 'yes'
418 ELSEIF NEW.class = 'boundary' AND NOT is_area
421 ELSEIF NEW.class = 'boundary' AND NEW.type = 'administrative'
422 AND NEW.admin_level <= 4 AND NEW.osm_type = 'W'
427 SELECT * INTO NEW.rank_search, NEW.rank_address
428 FROM compute_place_rank(NEW.country_code,
429 CASE WHEN is_area THEN 'A' ELSE NEW.osm_type END,
430 NEW.class, NEW.type, NEW.admin_level,
431 (NEW.extratags->'capital') = 'yes',
432 NEW.address->'postcode');
434 -- a country code make no sense below rank 4 (country)
435 IF NEW.rank_search < 4 THEN
436 NEW.country_code := NULL;
441 --DEBUG: RAISE WARNING 'placex_insert:END: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
443 RETURN NEW; -- %DIFFUPDATES% The following is not needed until doing diff updates, and slows the main index process down
445 IF NEW.osm_type = 'N' and NEW.rank_search > 28 THEN
446 -- might be part of an interpolation
447 result := osmline_reinsert(NEW.osm_id, NEW.geometry);
448 ELSEIF NEW.rank_address > 0 THEN
449 IF (ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon') AND ST_IsValid(NEW.geometry)) THEN
450 -- Performance: We just can't handle re-indexing for country level changes
451 IF st_area(NEW.geometry) < 1 THEN
452 -- mark items within the geometry for re-indexing
453 -- RAISE WARNING 'placex poly insert: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
455 UPDATE placex SET indexed_status = 2
456 WHERE ST_Intersects(NEW.geometry, placex.geometry)
457 and indexed_status = 0
458 and ((rank_address = 0 and rank_search > NEW.rank_address)
459 or rank_address > NEW.rank_address
460 or (class = 'place' and osm_type = 'N')
462 and (rank_search < 28
464 or (NEW.rank_address >= 16 and address ? 'place'));
467 -- mark nearby items for re-indexing, where 'nearby' depends on the features rank_search and is a complete guess :(
468 diameter := update_place_diameter(NEW.rank_search);
470 -- RAISE WARNING 'placex point insert: % % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type,diameter;
471 IF NEW.rank_search >= 26 THEN
472 -- roads may cause reparenting for >27 rank places
473 update placex set indexed_status = 2 where indexed_status = 0 and rank_search > NEW.rank_search and ST_DWithin(placex.geometry, NEW.geometry, diameter);
474 -- reparenting also for OSM Interpolation Lines (and for Tiger?)
475 update location_property_osmline set indexed_status = 2 where indexed_status = 0 and ST_DWithin(location_property_osmline.linegeo, NEW.geometry, diameter);
476 ELSEIF NEW.rank_search >= 16 THEN
477 -- up to rank 16, street-less addresses may need reparenting
478 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');
480 -- for all other places the search terms may change as well
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);
488 -- add to tables for special search
489 -- Note: won't work on initial import because the classtype tables
490 -- do not yet exist. It won't hurt either.
491 classtable := 'place_classtype_' || NEW.class || '_' || NEW.type;
492 SELECT count(*)>0 FROM pg_tables WHERE tablename = classtable and schemaname = current_schema() INTO result;
494 EXECUTE 'INSERT INTO ' || classtable::regclass || ' (place_id, centroid) VALUES ($1,$2)'
495 USING NEW.place_id, ST_Centroid(NEW.geometry);
504 CREATE OR REPLACE FUNCTION get_parent_address_level(geom GEOMETRY, in_level SMALLINT)
508 address_rank SMALLINT;
510 IF in_level <= 3 or in_level > 15 THEN
513 SELECT rank_address INTO address_rank
515 WHERE osm_type = 'R' and class = 'boundary' and type = 'administrative'
516 and admin_level < in_level
517 and geometry && geom and ST_Covers(geometry, geom)
518 ORDER BY admin_level desc LIMIT 1;
521 IF address_rank is NULL or address_rank <= 3 THEN
531 CREATE OR REPLACE FUNCTION placex_update()
537 relation_members TEXT[];
540 parent_address_level SMALLINT;
541 place_address_level SMALLINT;
546 name_vector INTEGER[];
547 nameaddress_vector INTEGER[];
548 addr_nameaddress_vector INTEGER[];
550 inherited_address HSTORE;
552 linked_node_id BIGINT;
553 linked_importance FLOAT;
554 linked_wikipedia TEXT;
559 IF OLD.indexed_status = 100 THEN
560 --DEBUG: RAISE WARNING 'placex_update delete % %',NEW.osm_type,NEW.osm_id;
561 delete from placex where place_id = OLD.place_id;
565 IF NEW.indexed_status != 0 OR OLD.indexed_status = 0 THEN
569 --DEBUG: RAISE WARNING 'placex_update % % (%)',NEW.osm_type,NEW.osm_id,NEW.place_id;
571 NEW.indexed_date = now();
573 IF NOT %REVERSE-ONLY% THEN
574 DELETE from search_name WHERE place_id = NEW.place_id;
576 result := deleteSearchName(NEW.partition, NEW.place_id);
577 DELETE FROM place_addressline WHERE place_id = NEW.place_id;
578 result := deleteRoad(NEW.partition, NEW.place_id);
579 result := deleteLocationArea(NEW.partition, NEW.place_id, NEW.rank_search);
580 UPDATE placex set linked_place_id = null, indexed_status = 2
581 where linked_place_id = NEW.place_id;
582 -- update not necessary for osmline, cause linked_place_id does not exist
584 NEW.extratags := NEW.extratags - 'linked_place'::TEXT;
585 NEW.address := NEW.address - '_unlisted_place'::TEXT;
587 IF NEW.linked_place_id is not null THEN
588 --DEBUG: RAISE WARNING 'place already linked to %', NEW.linked_place_id;
592 -- Postcodes are just here to compute the centroids. They are not searchable
593 -- unless they are a boundary=postal_code.
594 -- There was an error in the style so that boundary=postal_code used to be
595 -- imported as place=postcode. That's why relations are allowed to pass here.
596 -- This can go away in a couple of versions.
597 IF NEW.class = 'place' and NEW.type = 'postcode' and NEW.osm_type != 'R' THEN
601 -- Speed up searches - just use the centroid of the feature
602 -- cheaper but less acurate
603 NEW.centroid := ST_PointOnSurface(NEW.geometry);
604 --DEBUG: RAISE WARNING 'Computing preliminary centroid at %',ST_AsText(NEW.centroid);
606 -- recompute the ranks, they might change when linking changes
607 SELECT * INTO NEW.rank_search, NEW.rank_address
608 FROM compute_place_rank(NEW.country_code,
609 CASE WHEN ST_GeometryType(NEW.geometry)
610 IN ('ST_Polygon','ST_MultiPolygon')
611 THEN 'A' ELSE NEW.osm_type END,
612 NEW.class, NEW.type, NEW.admin_level,
613 (NEW.extratags->'capital') = 'yes',
614 NEW.address->'postcode');
615 -- We must always increase the address level relative to the admin boundary.
616 IF NEW.class = 'boundary' and NEW.type = 'administrative'
617 and NEW.osm_type = 'R' and NEW.rank_address > 0
619 parent_address_level := get_parent_address_level(NEW.centroid, NEW.admin_level);
620 IF parent_address_level >= NEW.rank_address THEN
621 IF parent_address_level >= 24 THEN
622 NEW.rank_address := 25;
624 NEW.rank_address := parent_address_level + 2;
627 -- If a place node is contained in a admin boundary with the same address level
628 -- and has not been linked, then make the node a subpart by increasing the
629 -- address rank (city level and above).
630 ELSEIF NEW.class = 'place' and NEW.osm_type = 'N'
631 and NEW.rank_address between 16 and 23
634 SELECT rank_address FROM placex
635 WHERE osm_type = 'R' and class = 'boundary' and type = 'administrative'
636 and rank_address = NEW.rank_address
637 and geometry && NEW.centroid and _ST_Covers(geometry, NEW.centroid)
640 NEW.rank_address = NEW.rank_address + 2;
643 parent_address_level := 3;
646 --DEBUG: RAISE WARNING 'Copy over address tags';
647 -- housenumber is a computed field, so start with an empty value
648 NEW.housenumber := NULL;
649 IF NEW.address is not NULL THEN
650 IF NEW.address ? 'conscriptionnumber' THEN
651 i := getorcreate_housenumber_id(make_standard_name(NEW.address->'conscriptionnumber'));
652 IF NEW.address ? 'streetnumber' THEN
653 i := getorcreate_housenumber_id(make_standard_name(NEW.address->'streetnumber'));
654 NEW.housenumber := (NEW.address->'conscriptionnumber') || '/' || (NEW.address->'streetnumber');
656 NEW.housenumber := NEW.address->'conscriptionnumber';
658 ELSEIF NEW.address ? 'streetnumber' THEN
659 NEW.housenumber := NEW.address->'streetnumber';
660 i := getorcreate_housenumber_id(make_standard_name(NEW.address->'streetnumber'));
661 ELSEIF NEW.address ? 'housenumber' THEN
662 NEW.housenumber := NEW.address->'housenumber';
663 i := getorcreate_housenumber_id(make_standard_name(NEW.housenumber));
666 addr_street := NEW.address->'street';
667 addr_place := NEW.address->'place';
669 IF NEW.address ? 'postcode' and NEW.address->'postcode' not similar to '%(:|,|;)%' THEN
670 i := getorcreate_postcode_id(NEW.address->'postcode');
674 NEW.postcode := null;
676 -- recalculate country and partition
677 IF NEW.rank_search = 4 AND NEW.address is not NULL AND NEW.address ? 'country' THEN
678 -- for countries, believe the mapped country code,
679 -- so that we remain in the right partition if the boundaries
681 NEW.country_code := lower(NEW.address->'country');
682 NEW.partition := get_partition(lower(NEW.country_code));
683 IF NEW.partition = 0 THEN
684 NEW.country_code := lower(get_country_code(NEW.centroid));
685 NEW.partition := get_partition(NEW.country_code);
688 IF NEW.rank_search >= 4 THEN
689 NEW.country_code := lower(get_country_code(NEW.centroid));
691 NEW.country_code := NULL;
693 NEW.partition := get_partition(NEW.country_code);
695 --DEBUG: RAISE WARNING 'Country updated: "%"', NEW.country_code;
697 -- waterway ways are linked when they are part of a relation and have the same class/type
698 IF NEW.osm_type = 'R' and NEW.class = 'waterway' THEN
699 FOR relation_members IN select members from planet_osm_rels r where r.id = NEW.osm_id and r.parts != array[]::bigint[]
701 FOR i IN 1..array_upper(relation_members, 1) BY 2 LOOP
702 IF relation_members[i+1] in ('', 'main_stream', 'side_stream') AND substring(relation_members[i],1,1) = 'w' THEN
703 --DEBUG: RAISE WARNING 'waterway parent %, child %/%', NEW.osm_id, i, relation_members[i];
704 FOR linked_node_id IN SELECT place_id FROM placex
705 WHERE osm_type = 'W' and osm_id = substring(relation_members[i],2,200)::bigint
706 and class = NEW.class and type in ('river', 'stream', 'canal', 'drain', 'ditch')
707 and ( relation_members[i+1] != 'side_stream' or NEW.name->'name' = name->'name')
709 UPDATE placex SET linked_place_id = NEW.place_id WHERE place_id = linked_node_id;
710 IF NOT %REVERSE-ONLY% THEN
711 DELETE FROM search_name WHERE place_id = linked_node_id;
717 --DEBUG: RAISE WARNING 'Waterway processed';
720 NEW.importance := null;
721 SELECT wikipedia, importance
722 FROM compute_importance(NEW.extratags, NEW.country_code, NEW.osm_type, NEW.osm_id)
723 INTO NEW.wikipedia,NEW.importance;
725 --DEBUG: RAISE WARNING 'Importance computed from wikipedia: %', NEW.importance;
727 -- ---------------------------------------------------------------------------
728 -- For low level elements we inherit from our parent road
729 IF NEW.rank_search > 27 THEN
731 --DEBUG: RAISE WARNING 'finding street for % %', NEW.osm_type, NEW.osm_id;
732 NEW.parent_place_id := null;
734 -- if we have a POI and there is no address information,
735 -- see if we can get it from a surrounding building
736 inherited_address := ''::HSTORE;
737 IF NEW.osm_type = 'N' AND addr_street IS NULL AND addr_place IS NULL
738 AND NEW.housenumber IS NULL THEN
740 -- The additional && condition works around the misguided query
741 -- planner of postgis 3.0.
742 SELECT address from placex where ST_Covers(geometry, NEW.centroid)
743 and geometry && NEW.centroid
744 and (address ? 'housenumber' or address ? 'street' or address ? 'place')
745 and rank_search > 28 AND ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon')
748 NEW.housenumber := location.address->'housenumber';
749 addr_street := location.address->'street';
750 addr_place := location.address->'place';
751 inherited_address := location.address;
755 -- We have to find our parent road.
756 NEW.parent_place_id := find_parent_for_poi(NEW.osm_type, NEW.osm_id,
758 ST_Envelope(NEW.geometry),
759 addr_street, addr_place);
761 -- If we found the road take a shortcut here.
762 -- Otherwise fall back to the full address getting method below.
763 IF NEW.parent_place_id is not null THEN
765 -- Get the details of the parent road
766 SELECT p.country_code, p.postcode, p.name FROM placex p
767 WHERE p.place_id = NEW.parent_place_id INTO location;
769 IF addr_street is null and addr_place is not null THEN
770 -- Check if the addr:place tag is part of the parent name
771 SELECT count(*) INTO i
772 FROM svals(location.name) AS pname WHERE pname = addr_place;
774 NEW.address = NEW.address || hstore('_unlisted_place', addr_place);
778 NEW.country_code := location.country_code;
779 --DEBUG: RAISE WARNING 'Got parent details from search name';
781 -- determine postcode
782 IF NEW.address is not null AND NEW.address ? 'postcode' THEN
783 NEW.postcode = upper(trim(NEW.address->'postcode'));
785 NEW.postcode := location.postcode;
787 IF NEW.postcode is null THEN
788 NEW.postcode := get_nearest_postcode(NEW.country_code, NEW.geometry);
791 IF NEW.name is not NULL THEN
792 NEW.name := add_default_place_name(NEW.country_code, NEW.name);
793 name_vector := make_keywords(NEW.name);
795 IF NEW.rank_search <= 25 and NEW.rank_address > 0 THEN
796 result := add_location(NEW.place_id, NEW.country_code, NEW.partition,
797 name_vector, NEW.rank_search, NEW.rank_address,
798 upper(trim(NEW.address->'postcode')), NEW.geometry);
799 --DEBUG: RAISE WARNING 'Place added to location table';
804 IF NOT %REVERSE-ONLY% THEN
805 SELECT * INTO name_vector, nameaddress_vector
806 FROM create_poi_search_terms(NEW.parent_place_id,
807 inherited_address || NEW.address,
808 NEW.housenumber, name_vector);
810 IF array_length(name_vector, 1) is not NULL THEN
811 INSERT INTO search_name (place_id, search_rank, address_rank,
812 importance, country_code, name_vector,
813 nameaddress_vector, centroid)
814 VALUES (NEW.place_id, NEW.rank_search, NEW.rank_address,
815 NEW.importance, NEW.country_code, name_vector,
816 nameaddress_vector, NEW.centroid);
817 --DEBUG: RAISE WARNING 'Place added to search table';
826 -- ---------------------------------------------------------------------------
828 --DEBUG: RAISE WARNING 'Using full index mode for % %', NEW.osm_type, NEW.osm_id;
829 SELECT * INTO location FROM find_linked_place(NEW);
830 IF location.place_id is not null THEN
831 --DEBUG: RAISE WARNING 'Linked %', location;
833 -- Use the linked point as the centre point of the geometry,
834 -- but only if it is within the area of the boundary.
835 centroid := coalesce(location.centroid, ST_Centroid(location.geometry));
836 IF centroid is not NULL AND ST_Within(centroid, NEW.geometry) THEN
837 NEW.centroid := centroid;
840 --DEBUG: RAISE WARNING 'parent address: % rank address: %', parent_address_level, location.rank_address;
841 IF location.rank_address > parent_address_level
842 and location.rank_address < 26
844 NEW.rank_address := location.rank_address;
847 -- merge in the label name
848 IF NOT location.name IS NULL THEN
849 NEW.name := location.name || NEW.name;
852 -- merge in extra tags
853 NEW.extratags := hstore('linked_' || location.class, location.type)
854 || coalesce(location.extratags, ''::hstore)
855 || coalesce(NEW.extratags, ''::hstore);
857 -- mark the linked place (excludes from search results)
858 UPDATE placex set linked_place_id = NEW.place_id
859 WHERE place_id = location.place_id;
860 -- ensure that those places are not found anymore
861 IF NOT %REVERSE-ONLY% THEN
862 DELETE FROM search_name WHERE place_id = location.place_id;
864 PERFORM deleteLocationArea(NEW.partition, location.place_id, NEW.rank_search);
866 SELECT wikipedia, importance
867 FROM compute_importance(location.extratags, NEW.country_code,
868 'N', location.osm_id)
869 INTO linked_wikipedia,linked_importance;
871 -- Use the maximum importance if one could be computed from the linked object.
872 IF linked_importance is not null AND
873 (NEW.importance is null or NEW.importance < linked_importance)
875 NEW.importance = linked_importance;
878 -- No linked place? As a last resort check if the boundary is tagged with
879 -- a place type and adapt the rank address.
880 IF NEW.rank_address > 0 and NEW.extratags ? 'place' THEN
881 SELECT address_rank INTO place_address_level
882 FROM compute_place_rank(NEW.country_code, 'A', 'place',
883 NEW.extratags->'place', 0::SMALLINT, False, null);
884 IF place_address_level > parent_address_level and
885 place_address_level < 26 THEN
886 NEW.rank_address := place_address_level;
891 -- Initialise the name vector using our name
892 NEW.name := add_default_place_name(NEW.country_code, NEW.name);
893 name_vector := make_keywords(NEW.name);
895 -- make sure all names are in the word table
896 IF NEW.admin_level = 2
897 AND NEW.class = 'boundary' AND NEW.type = 'administrative'
898 AND NEW.country_code IS NOT NULL AND NEW.osm_type = 'R'
900 PERFORM create_country(NEW.name, lower(NEW.country_code));
901 --DEBUG: RAISE WARNING 'Country names updated';
904 SELECT * FROM insert_addresslines(NEW.place_id, NEW.partition,
905 CASE WHEN NEW.rank_address = 0
906 THEN NEW.rank_search ELSE NEW.rank_address END,
908 CASE WHEN NEW.rank_search >= 26
909 AND NEW.rank_search < 30
910 THEN NEW.geometry ELSE NEW.centroid END)
911 INTO NEW.parent_place_id, NEW.postcode, nameaddress_vector;
913 --DEBUG: RAISE WARNING 'RETURN insert_addresslines: %, %, %', NEW.parent_place_id, NEW.postcode, nameaddress_vector;
915 IF NEW.address is not null AND NEW.address ? 'postcode'
916 AND NEW.address->'postcode' not similar to '%(,|;)%' THEN
917 NEW.postcode := upper(trim(NEW.address->'postcode'));
920 IF NEW.postcode is null AND NEW.rank_search > 8 THEN
921 NEW.postcode := get_nearest_postcode(NEW.country_code, NEW.geometry);
924 -- if we have a name add this to the name search table
925 IF NEW.name IS NOT NULL THEN
927 IF NEW.rank_search <= 25 and NEW.rank_address > 0 THEN
928 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);
929 --DEBUG: RAISE WARNING 'added to location (full)';
932 IF NEW.rank_search between 26 and 27 and NEW.class = 'highway' THEN
933 result := insertLocationRoad(NEW.partition, NEW.place_id, NEW.country_code, NEW.geometry);
934 --DEBUG: RAISE WARNING 'insert into road location table (full)';
937 result := insertSearchName(NEW.partition, NEW.place_id, name_vector,
938 NEW.rank_search, NEW.rank_address, NEW.geometry);
939 --DEBUG: RAISE WARNING 'added to search name (full)';
941 IF NOT %REVERSE-ONLY% THEN
942 INSERT INTO search_name (place_id, search_rank, address_rank,
943 importance, country_code, name_vector,
944 nameaddress_vector, centroid)
945 VALUES (NEW.place_id, NEW.rank_search, NEW.rank_address,
946 NEW.importance, NEW.country_code, name_vector,
947 nameaddress_vector, NEW.centroid);
952 --DEBUG: RAISE WARNING 'place update % % finsihed.', NEW.osm_type, NEW.osm_id;
960 CREATE OR REPLACE FUNCTION placex_delete()
967 -- RAISE WARNING 'placex_delete % %',OLD.osm_type,OLD.osm_id;
969 IF OLD.linked_place_id is null THEN
970 update placex set linked_place_id = null, indexed_status = 2 where linked_place_id = OLD.place_id and indexed_status = 0;
971 --DEBUG: RAISE WARNING 'placex_delete:01 % %',OLD.osm_type,OLD.osm_id;
972 update placex set linked_place_id = null where linked_place_id = OLD.place_id;
973 --DEBUG: RAISE WARNING 'placex_delete:02 % %',OLD.osm_type,OLD.osm_id;
975 update placex set indexed_status = 2 where place_id = OLD.linked_place_id and indexed_status = 0;
978 IF OLD.rank_address < 30 THEN
980 -- mark everything linked to this place for re-indexing
981 --DEBUG: RAISE WARNING 'placex_delete:03 % %',OLD.osm_type,OLD.osm_id;
982 UPDATE placex set indexed_status = 2 from place_addressline where address_place_id = OLD.place_id
983 and placex.place_id = place_addressline.place_id and indexed_status = 0 and place_addressline.isaddress;
985 --DEBUG: RAISE WARNING 'placex_delete:04 % %',OLD.osm_type,OLD.osm_id;
986 DELETE FROM place_addressline where address_place_id = OLD.place_id;
988 --DEBUG: RAISE WARNING 'placex_delete:05 % %',OLD.osm_type,OLD.osm_id;
989 b := deleteRoad(OLD.partition, OLD.place_id);
991 --DEBUG: RAISE WARNING 'placex_delete:06 % %',OLD.osm_type,OLD.osm_id;
992 update placex set indexed_status = 2 where parent_place_id = OLD.place_id and indexed_status = 0;
993 --DEBUG: RAISE WARNING 'placex_delete:07 % %',OLD.osm_type,OLD.osm_id;
994 -- reparenting also for OSM Interpolation Lines (and for Tiger?)
995 update location_property_osmline set indexed_status = 2 where indexed_status = 0 and parent_place_id = OLD.place_id;
999 --DEBUG: RAISE WARNING 'placex_delete:08 % %',OLD.osm_type,OLD.osm_id;
1001 IF OLD.rank_address < 26 THEN
1002 b := deleteLocationArea(OLD.partition, OLD.place_id, OLD.rank_search);
1005 --DEBUG: RAISE WARNING 'placex_delete:09 % %',OLD.osm_type,OLD.osm_id;
1007 IF OLD.name is not null THEN
1008 IF NOT %REVERSE-ONLY% THEN
1009 DELETE from search_name WHERE place_id = OLD.place_id;
1011 b := deleteSearchName(OLD.partition, OLD.place_id);
1014 --DEBUG: RAISE WARNING 'placex_delete:10 % %',OLD.osm_type,OLD.osm_id;
1016 DELETE FROM place_addressline where place_id = OLD.place_id;
1018 --DEBUG: RAISE WARNING 'placex_delete:11 % %',OLD.osm_type,OLD.osm_id;
1020 -- remove from tables for special search
1021 classtable := 'place_classtype_' || OLD.class || '_' || OLD.type;
1022 SELECT count(*)>0 FROM pg_tables WHERE tablename = classtable and schemaname = current_schema() INTO b;
1024 EXECUTE 'DELETE FROM ' || classtable::regclass || ' WHERE place_id = $1' USING OLD.place_id;
1027 --DEBUG: RAISE WARNING 'placex_delete:12 % %',OLD.osm_type,OLD.osm_id;