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. Find the
97 -- nearest place instead.
99 SELECT place_id FROM getNearFeatures(poi_partition, bbox, 26, '{}'::INTEGER[])
100 ORDER BY rank_address DESC, isguess asc, distance LIMIT 1
102 parent_place_id := location.place_id;
104 ELSEIF ST_Area(bbox) < 0.005 THEN
105 -- for smaller features get the nearest road
106 SELECT getNearestRoadPlaceId(poi_partition, bbox) INTO parent_place_id;
107 --DEBUG: RAISE WARNING 'Checked for nearest way (%)', parent_place_id;
109 -- for larger features simply find the area with the largest rank that
110 -- contains the bbox, only use addressable features
112 SELECT place_id FROM placex
113 WHERE bbox @ geometry AND _ST_Covers(geometry, ST_Centroid(bbox))
114 AND rank_address between 5 and 25
115 ORDER BY rank_address desc
117 RETURN location.place_id;
122 RETURN parent_place_id;
125 LANGUAGE plpgsql STABLE;
127 -- Try to find a linked place for the given object.
128 CREATE OR REPLACE FUNCTION find_linked_place(bnd placex)
132 relation_members TEXT[];
134 linked_placex placex%ROWTYPE;
137 IF bnd.rank_search >= 26 or bnd.rank_address = 0
138 or ST_GeometryType(bnd.geometry) NOT IN ('ST_Polygon','ST_MultiPolygon')
143 IF bnd.osm_type = 'R' THEN
144 -- see if we have any special relation members
145 SELECT members FROM planet_osm_rels WHERE id = bnd.osm_id INTO relation_members;
146 --DEBUG: RAISE WARNING 'Got relation members';
148 -- Search for relation members with role 'lable'.
149 IF relation_members IS NOT NULL THEN
151 SELECT get_rel_node_members(relation_members, ARRAY['label']) as member
153 --DEBUG: RAISE WARNING 'Found label member %', rel_member.member;
157 WHERE osm_type = 'N' and osm_id = rel_member.member
160 --DEBUG: RAISE WARNING 'Linked label member';
161 RETURN linked_placex;
168 IF bnd.name ? 'name' THEN
169 bnd_name := make_standard_name(bnd.name->'name');
170 IF bnd_name = '' THEN
175 -- If extratags has a place tag, look for linked nodes by their place type.
176 -- Area and node still have to have the same name.
177 IF bnd.extratags ? 'place' and bnd_name is not null THEN
180 WHERE make_standard_name(name->'name') = bnd_name
181 AND placex.class = 'place' AND placex.type = bnd.extratags->'place'
182 AND placex.osm_type = 'N'
183 AND placex.rank_search < 26 -- needed to select the right index
184 AND _st_covers(bnd.geometry, placex.geometry)
186 --DEBUG: RAISE WARNING 'Found type-matching place node %', linked_placex.osm_id;
187 RETURN linked_placex;
191 IF bnd.extratags ? 'wikidata' THEN
194 WHERE placex.class = 'place' AND placex.osm_type = 'N'
195 AND placex.extratags ? 'wikidata' -- needed to select right index
196 AND placex.extratags->'wikidata' = bnd.extratags->'wikidata'
197 AND placex.rank_search < 26
198 AND _st_covers(bnd.geometry, placex.geometry)
199 ORDER BY make_standard_name(name->'name') = bnd_name desc
201 --DEBUG: RAISE WARNING 'Found wikidata-matching place node %', linked_placex.osm_id;
202 RETURN linked_placex;
206 -- Name searches can be done for ways as well as relations
207 IF bnd_name is not null THEN
208 --DEBUG: RAISE WARNING 'Looking for nodes with matching names';
210 SELECT placex.* from placex
211 WHERE make_standard_name(name->'name') = bnd_name
212 AND ((bnd.rank_address > 0 and placex.rank_address = bnd.rank_address)
213 OR (bnd.rank_address = 0 and placex.rank_search = bnd.rank_search))
214 AND placex.osm_type = 'N'
215 AND placex.rank_search < 26 -- needed to select the right index
216 AND _st_covers(bnd.geometry, placex.geometry)
218 --DEBUG: RAISE WARNING 'Found matching place node %', linked_placex.osm_id;
219 RETURN linked_placex;
226 LANGUAGE plpgsql STABLE;
229 -- Insert address of a place into the place_addressline table.
231 -- \param obj_place_id Place_id of the place to compute the address for.
232 -- \param partition Partition number where the place is in.
233 -- \param maxrank Rank of the place. All address features must have
234 -- a search rank lower than the given rank.
235 -- \param address Address terms for the place.
236 -- \param geometry Geometry to which the address objects should be close.
238 -- \retval parent_place_id Place_id of the address object that is the direct
240 -- \retval postcode Postcode computed from the address. This is the
241 -- addr:postcode of one of the address objects. If
242 -- more than one of has a postcode, the highest ranking
243 -- one is used. May be NULL.
244 -- \retval nameaddress_vector Search terms for the address. This is the sum
245 -- of name terms of all address objects.
246 CREATE OR REPLACE FUNCTION insert_addresslines(obj_place_id BIGINT,
251 OUT parent_place_id BIGINT,
253 OUT nameaddress_vector INT[])
256 current_rank_address INTEGER := 0;
257 location_distance FLOAT := 0;
258 location_parent GEOMETRY := NULL;
259 parent_place_id_rank SMALLINT := 0;
261 location_isaddress BOOLEAN;
263 address_havelevel BOOLEAN[];
264 location_keywords INT[];
272 parent_place_id := 0;
273 nameaddress_vector := '{}'::int[];
274 isin_tokens := '{}'::int[];
276 ---- convert address store to array of tokenids
277 IF address IS NOT NULL THEN
278 FOR addr_item IN SELECT * FROM each(address)
280 IF addr_item.key IN ('city', 'tiger:county', 'state', 'suburb', 'province',
281 'district', 'region', 'county', 'municipality',
282 'hamlet', 'village', 'subdistrict', 'town',
283 'neighbourhood', 'quarter', 'parish')
285 isin_tokens := array_merge(isin_tokens,
286 word_ids_from_name(addr_item.value));
287 IF NOT %REVERSE-ONLY% THEN
288 nameaddress_vector := array_merge(nameaddress_vector,
289 addr_ids_from_name(addr_item.value));
294 IF NOT %REVERSE-ONLY% THEN
295 nameaddress_vector := array_merge(nameaddress_vector, isin_tokens);
298 ---- now compute the address terms
300 address_havelevel[i] := false;
304 SELECT * FROM getNearFeatures(partition, geometry, maxrank, isin_tokens)
306 IF location.rank_address != current_rank_address THEN
307 current_rank_address := location.rank_address;
308 IF location.isguess THEN
309 location_distance := location.distance * 1.5;
311 IF location.rank_address <= 12 THEN
312 -- for county and above, if we have an area consider that exact
313 -- (It would be nice to relax the constraint for places close to
314 -- the boundary but we'd need the exact geometry for that. Too
316 location_distance = 0;
318 -- Below county level remain slightly fuzzy.
319 location_distance := location.distance * 0.5;
323 CONTINUE WHEN location.keywords <@ location_keywords;
326 IF location.distance < location_distance OR NOT location.isguess THEN
327 location_keywords := location.keywords;
329 location_isaddress := NOT address_havelevel[location.rank_address];
330 --DEBUG: RAISE WARNING 'should be address: %, is guess: %, rank: %', location_isaddress, location.isguess, location.rank_address;
331 IF location_isaddress AND location.isguess AND location_parent IS NOT NULL THEN
332 location_isaddress := ST_Contains(location_parent, location.centroid);
335 --DEBUG: RAISE WARNING '% isaddress: %', location.place_id, location_isaddress;
336 -- Add it to the list of search terms
337 IF NOT %REVERSE-ONLY% THEN
338 nameaddress_vector := array_merge(nameaddress_vector,
339 location.keywords::integer[]);
342 INSERT INTO place_addressline (place_id, address_place_id, fromarea,
343 isaddress, distance, cached_rank_address)
344 VALUES (obj_place_id, location.place_id, true,
345 location_isaddress, location.distance, location.rank_address);
347 IF location_isaddress THEN
348 -- add postcode if we have one
349 -- (If multiple postcodes are available, we end up with the highest ranking one.)
350 IF location.postcode is not null THEN
351 postcode = location.postcode;
354 address_havelevel[location.rank_address] := true;
355 -- add a hack against postcode ranks
356 IF NOT location.isguess
357 AND location.rank_address != 11 AND location.rank_address != 5
359 SELECT p.geometry FROM placex p
360 WHERE p.place_id = location.place_id INTO location_parent;
363 IF location.rank_address > parent_place_id_rank THEN
364 parent_place_id = location.place_id;
365 parent_place_id_rank = location.rank_address;
376 CREATE OR REPLACE FUNCTION placex_insert()
383 country_code VARCHAR(2);
387 --DEBUG: RAISE WARNING '% % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
389 NEW.place_id := nextval('seq_place');
390 NEW.indexed_status := 1; --STATUS_NEW
392 NEW.country_code := lower(get_country_code(NEW.geometry));
394 NEW.partition := get_partition(NEW.country_code);
395 NEW.geometry_sector := geometry_sector(NEW.partition, NEW.geometry);
397 IF NEW.osm_type = 'X' THEN
398 -- E'X'ternal records should already be in the right format so do nothing
400 is_area := ST_GeometryType(NEW.geometry) IN ('ST_Polygon','ST_MultiPolygon');
402 IF NEW.class in ('place','boundary')
403 AND NEW.type in ('postcode','postal_code')
405 IF NEW.address IS NULL OR NOT NEW.address ? 'postcode' THEN
406 -- most likely just a part of a multipolygon postcode boundary, throw it away
410 NEW.name := hstore('ref', NEW.address->'postcode');
412 ELSEIF NEW.class = 'highway' AND is_area AND NEW.name is null
413 AND NEW.extratags ? 'area' AND NEW.extratags->'area' = 'yes'
416 ELSEIF NEW.class = 'boundary' AND NOT is_area
419 ELSEIF NEW.class = 'boundary' AND NEW.type = 'administrative'
420 AND NEW.admin_level <= 4 AND NEW.osm_type = 'W'
425 SELECT * INTO NEW.rank_search, NEW.rank_address
426 FROM compute_place_rank(NEW.country_code,
427 CASE WHEN is_area THEN 'A' ELSE NEW.osm_type END,
428 NEW.class, NEW.type, NEW.admin_level,
429 (NEW.extratags->'capital') = 'yes',
430 NEW.address->'postcode');
432 -- a country code make no sense below rank 4 (country)
433 IF NEW.rank_search < 4 THEN
434 NEW.country_code := NULL;
439 --DEBUG: RAISE WARNING 'placex_insert:END: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
441 RETURN NEW; -- %DIFFUPDATES% The following is not needed until doing diff updates, and slows the main index process down
443 IF NEW.osm_type = 'N' and NEW.rank_search > 28 THEN
444 -- might be part of an interpolation
445 result := osmline_reinsert(NEW.osm_id, NEW.geometry);
446 ELSEIF NEW.rank_address > 0 THEN
447 IF (ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon') AND ST_IsValid(NEW.geometry)) THEN
448 -- Performance: We just can't handle re-indexing for country level changes
449 IF st_area(NEW.geometry) < 1 THEN
450 -- mark items within the geometry for re-indexing
451 -- RAISE WARNING 'placex poly insert: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
453 -- work around bug in postgis, this may have been fixed in 2.0.0 (see http://trac.osgeo.org/postgis/ticket/547)
454 update placex set indexed_status = 2 where (st_covers(NEW.geometry, placex.geometry) OR ST_Intersects(NEW.geometry, placex.geometry))
455 AND rank_search > NEW.rank_search and indexed_status = 0 and ST_geometrytype(placex.geometry) = 'ST_Point' and (rank_search < 28 or name is not null or (NEW.rank_search >= 16 and address ? 'place'));
456 update placex set indexed_status = 2 where (st_covers(NEW.geometry, placex.geometry) OR ST_Intersects(NEW.geometry, placex.geometry))
457 AND rank_search > NEW.rank_search and indexed_status = 0 and ST_geometrytype(placex.geometry) != 'ST_Point' and (rank_search < 28 or name is not null or (NEW.rank_search >= 16 and address ? 'place'));
460 -- mark nearby items for re-indexing, where 'nearby' depends on the features rank_search and is a complete guess :(
461 diameter := update_place_diameter(NEW.rank_search);
463 -- RAISE WARNING 'placex point insert: % % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type,diameter;
464 IF NEW.rank_search >= 26 THEN
465 -- roads may cause reparenting for >27 rank places
466 update placex set indexed_status = 2 where indexed_status = 0 and rank_search > NEW.rank_search and ST_DWithin(placex.geometry, NEW.geometry, diameter);
467 -- reparenting also for OSM Interpolation Lines (and for Tiger?)
468 update location_property_osmline set indexed_status = 2 where indexed_status = 0 and ST_DWithin(location_property_osmline.linegeo, NEW.geometry, diameter);
469 ELSEIF NEW.rank_search >= 16 THEN
470 -- up to rank 16, street-less addresses may need reparenting
471 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');
473 -- for all other places the search terms may change as well
474 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);
481 -- add to tables for special search
482 -- Note: won't work on initial import because the classtype tables
483 -- do not yet exist. It won't hurt either.
484 classtable := 'place_classtype_' || NEW.class || '_' || NEW.type;
485 SELECT count(*)>0 FROM pg_tables WHERE tablename = classtable and schemaname = current_schema() INTO result;
487 EXECUTE 'INSERT INTO ' || classtable::regclass || ' (place_id, centroid) VALUES ($1,$2)'
488 USING NEW.place_id, ST_Centroid(NEW.geometry);
497 CREATE OR REPLACE FUNCTION get_parent_address_level(geom GEOMETRY, in_level SMALLINT)
501 address_rank SMALLINT;
503 IF in_level <= 3 or in_level > 15 THEN
506 SELECT rank_address INTO address_rank
508 WHERE osm_type = 'R' and class = 'boundary' and type = 'administrative'
509 and admin_level < in_level
510 and geometry && geom and ST_Covers(geometry, geom)
511 ORDER BY admin_level desc LIMIT 1;
514 IF address_rank is NULL or address_rank <= 3 THEN
524 CREATE OR REPLACE FUNCTION placex_update()
530 relation_members TEXT[];
533 parent_address_level SMALLINT;
534 place_address_level SMALLINT;
539 name_vector INTEGER[];
540 nameaddress_vector INTEGER[];
541 addr_nameaddress_vector INTEGER[];
543 inherited_address HSTORE;
545 linked_node_id BIGINT;
546 linked_importance FLOAT;
547 linked_wikipedia TEXT;
552 IF OLD.indexed_status = 100 THEN
553 --DEBUG: RAISE WARNING 'placex_update delete % %',NEW.osm_type,NEW.osm_id;
554 delete from placex where place_id = OLD.place_id;
558 IF NEW.indexed_status != 0 OR OLD.indexed_status = 0 THEN
562 --DEBUG: RAISE WARNING 'placex_update % % (%)',NEW.osm_type,NEW.osm_id,NEW.place_id;
564 NEW.indexed_date = now();
566 IF NOT %REVERSE-ONLY% THEN
567 DELETE from search_name WHERE place_id = NEW.place_id;
569 result := deleteSearchName(NEW.partition, NEW.place_id);
570 DELETE FROM place_addressline WHERE place_id = NEW.place_id;
571 result := deleteRoad(NEW.partition, NEW.place_id);
572 result := deleteLocationArea(NEW.partition, NEW.place_id, NEW.rank_search);
573 UPDATE placex set linked_place_id = null, indexed_status = 2
574 where linked_place_id = NEW.place_id;
575 -- update not necessary for osmline, cause linked_place_id does not exist
577 NEW.extratags := NEW.extratags - 'linked_place'::TEXT;
578 NEW.address := NEW.address - '_unlisted_place'::TEXT;
580 IF NEW.linked_place_id is not null THEN
581 --DEBUG: RAISE WARNING 'place already linked to %', NEW.linked_place_id;
585 -- Postcodes are just here to compute the centroids. They are not searchable
586 -- unless they are a boundary=postal_code.
587 -- There was an error in the style so that boundary=postal_code used to be
588 -- imported as place=postcode. That's why relations are allowed to pass here.
589 -- This can go away in a couple of versions.
590 IF NEW.class = 'place' and NEW.type = 'postcode' and NEW.osm_type != 'R' THEN
594 -- Speed up searches - just use the centroid of the feature
595 -- cheaper but less acurate
596 NEW.centroid := ST_PointOnSurface(NEW.geometry);
597 --DEBUG: RAISE WARNING 'Computing preliminary centroid at %',ST_AsText(NEW.centroid);
599 -- recompute the ranks, they might change when linking changes
600 SELECT * INTO NEW.rank_search, NEW.rank_address
601 FROM compute_place_rank(NEW.country_code,
602 CASE WHEN ST_GeometryType(NEW.geometry)
603 IN ('ST_Polygon','ST_MultiPolygon')
604 THEN 'A' ELSE NEW.osm_type END,
605 NEW.class, NEW.type, NEW.admin_level,
606 (NEW.extratags->'capital') = 'yes',
607 NEW.address->'postcode');
608 -- We must always increase the address level relative to the admin boundary.
609 IF NEW.class = 'boundary' and NEW.type = 'administrative'
610 and NEW.osm_type = 'R' and NEW.rank_address > 0
612 parent_address_level := get_parent_address_level(NEW.centroid, NEW.admin_level);
613 IF parent_address_level >= NEW.rank_address THEN
614 IF parent_address_level >= 24 THEN
615 NEW.rank_address := 25;
617 NEW.rank_address := parent_address_level + 2;
621 parent_address_level := 3;
624 --DEBUG: RAISE WARNING 'Copy over address tags';
625 -- housenumber is a computed field, so start with an empty value
626 NEW.housenumber := NULL;
627 IF NEW.address is not NULL THEN
628 IF NEW.address ? 'conscriptionnumber' THEN
629 i := getorcreate_housenumber_id(make_standard_name(NEW.address->'conscriptionnumber'));
630 IF NEW.address ? 'streetnumber' THEN
631 i := getorcreate_housenumber_id(make_standard_name(NEW.address->'streetnumber'));
632 NEW.housenumber := (NEW.address->'conscriptionnumber') || '/' || (NEW.address->'streetnumber');
634 NEW.housenumber := NEW.address->'conscriptionnumber';
636 ELSEIF NEW.address ? 'streetnumber' THEN
637 NEW.housenumber := NEW.address->'streetnumber';
638 i := getorcreate_housenumber_id(make_standard_name(NEW.address->'streetnumber'));
639 ELSEIF NEW.address ? 'housenumber' THEN
640 NEW.housenumber := NEW.address->'housenumber';
641 i := getorcreate_housenumber_id(make_standard_name(NEW.housenumber));
644 addr_street := NEW.address->'street';
645 addr_place := NEW.address->'place';
647 IF NEW.address ? 'postcode' and NEW.address->'postcode' not similar to '%(:|,|;)%' THEN
648 i := getorcreate_postcode_id(NEW.address->'postcode');
652 NEW.postcode := null;
654 -- recalculate country and partition
655 IF NEW.rank_search = 4 AND NEW.address is not NULL AND NEW.address ? 'country' THEN
656 -- for countries, believe the mapped country code,
657 -- so that we remain in the right partition if the boundaries
659 NEW.country_code := lower(NEW.address->'country');
660 NEW.partition := get_partition(lower(NEW.country_code));
661 IF NEW.partition = 0 THEN
662 NEW.country_code := lower(get_country_code(NEW.centroid));
663 NEW.partition := get_partition(NEW.country_code);
666 IF NEW.rank_search >= 4 THEN
667 NEW.country_code := lower(get_country_code(NEW.centroid));
669 NEW.country_code := NULL;
671 NEW.partition := get_partition(NEW.country_code);
673 --DEBUG: RAISE WARNING 'Country updated: "%"', NEW.country_code;
675 -- waterway ways are linked when they are part of a relation and have the same class/type
676 IF NEW.osm_type = 'R' and NEW.class = 'waterway' THEN
677 FOR relation_members IN select members from planet_osm_rels r where r.id = NEW.osm_id and r.parts != array[]::bigint[]
679 FOR i IN 1..array_upper(relation_members, 1) BY 2 LOOP
680 IF relation_members[i+1] in ('', 'main_stream', 'side_stream') AND substring(relation_members[i],1,1) = 'w' THEN
681 --DEBUG: RAISE WARNING 'waterway parent %, child %/%', NEW.osm_id, i, relation_members[i];
682 FOR linked_node_id IN SELECT place_id FROM placex
683 WHERE osm_type = 'W' and osm_id = substring(relation_members[i],2,200)::bigint
684 and class = NEW.class and type in ('river', 'stream', 'canal', 'drain', 'ditch')
685 and ( relation_members[i+1] != 'side_stream' or NEW.name->'name' = name->'name')
687 UPDATE placex SET linked_place_id = NEW.place_id WHERE place_id = linked_node_id;
688 IF NOT %REVERSE-ONLY% THEN
689 DELETE FROM search_name WHERE place_id = linked_node_id;
695 --DEBUG: RAISE WARNING 'Waterway processed';
698 NEW.importance := null;
699 SELECT wikipedia, importance
700 FROM compute_importance(NEW.extratags, NEW.country_code, NEW.osm_type, NEW.osm_id)
701 INTO NEW.wikipedia,NEW.importance;
703 --DEBUG: RAISE WARNING 'Importance computed from wikipedia: %', NEW.importance;
705 -- ---------------------------------------------------------------------------
706 -- For low level elements we inherit from our parent road
707 IF NEW.rank_search > 27 THEN
709 --DEBUG: RAISE WARNING 'finding street for % %', NEW.osm_type, NEW.osm_id;
710 NEW.parent_place_id := null;
712 -- if we have a POI and there is no address information,
713 -- see if we can get it from a surrounding building
714 inherited_address := ''::HSTORE;
715 IF NEW.osm_type = 'N' AND addr_street IS NULL AND addr_place IS NULL
716 AND NEW.housenumber IS NULL THEN
718 -- The additional && condition works around the misguided query
719 -- planner of postgis 3.0.
720 SELECT address from placex where ST_Covers(geometry, NEW.centroid)
721 and geometry && NEW.centroid
722 and (address ? 'housenumber' or address ? 'street' or address ? 'place')
723 and rank_search > 28 AND ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon')
726 NEW.housenumber := location.address->'housenumber';
727 addr_street := location.address->'street';
728 addr_place := location.address->'place';
729 inherited_address := location.address;
733 -- We have to find our parent road.
734 NEW.parent_place_id := find_parent_for_poi(NEW.osm_type, NEW.osm_id,
736 ST_Envelope(NEW.geometry),
737 addr_street, addr_place);
739 -- If we found the road take a shortcut here.
740 -- Otherwise fall back to the full address getting method below.
741 IF NEW.parent_place_id is not null THEN
743 -- Get the details of the parent road
744 SELECT p.country_code, p.postcode, p.name FROM placex p
745 WHERE p.place_id = NEW.parent_place_id INTO location;
747 IF addr_street is null and addr_place is not null THEN
748 -- Check if the addr:place tag is part of the parent name
749 SELECT count(*) INTO i
750 FROM svals(location.name) AS pname WHERE pname = addr_place;
752 NEW.address = NEW.address || hstore('_unlisted_place', addr_place);
756 NEW.country_code := location.country_code;
757 --DEBUG: RAISE WARNING 'Got parent details from search name';
759 -- determine postcode
760 IF NEW.address is not null AND NEW.address ? 'postcode' THEN
761 NEW.postcode = upper(trim(NEW.address->'postcode'));
763 NEW.postcode := location.postcode;
765 IF NEW.postcode is null THEN
766 NEW.postcode := get_nearest_postcode(NEW.country_code, NEW.geometry);
769 IF NEW.name is not NULL THEN
770 NEW.name := add_default_place_name(NEW.country_code, NEW.name);
771 name_vector := make_keywords(NEW.name);
773 IF NEW.rank_search <= 25 and NEW.rank_address > 0 THEN
774 result := add_location(NEW.place_id, NEW.country_code, NEW.partition,
775 name_vector, NEW.rank_search, NEW.rank_address,
776 upper(trim(NEW.address->'postcode')), NEW.geometry);
777 --DEBUG: RAISE WARNING 'Place added to location table';
782 IF NOT %REVERSE-ONLY% THEN
783 SELECT * INTO name_vector, nameaddress_vector
784 FROM create_poi_search_terms(NEW.parent_place_id,
785 inherited_address || NEW.address,
786 NEW.housenumber, name_vector);
788 IF array_length(name_vector, 1) is not NULL THEN
789 INSERT INTO search_name (place_id, search_rank, address_rank,
790 importance, country_code, name_vector,
791 nameaddress_vector, centroid)
792 VALUES (NEW.place_id, NEW.rank_search, NEW.rank_address,
793 NEW.importance, NEW.country_code, name_vector,
794 nameaddress_vector, NEW.centroid);
795 --DEBUG: RAISE WARNING 'Place added to search table';
804 -- ---------------------------------------------------------------------------
806 --DEBUG: RAISE WARNING 'Using full index mode for % %', NEW.osm_type, NEW.osm_id;
807 SELECT * INTO location FROM find_linked_place(NEW);
808 IF location.place_id is not null THEN
809 --DEBUG: RAISE WARNING 'Linked %', location;
811 -- Use the linked point as the centre point of the geometry,
812 -- but only if it is within the area of the boundary.
813 centroid := coalesce(location.centroid, ST_Centroid(location.geometry));
814 IF centroid is not NULL AND ST_Within(centroid, NEW.geometry) THEN
815 NEW.centroid := centroid;
818 --DEBUG: RAISE WARNING 'parent address: % rank address: %', parent_address_level, location.rank_address;
819 IF location.rank_address > parent_address_level
820 and location.rank_address < 26
822 NEW.rank_address := location.rank_address;
825 -- merge in the label name
826 IF NOT location.name IS NULL THEN
827 NEW.name := location.name || NEW.name;
830 -- merge in extra tags
831 NEW.extratags := hstore('linked_' || location.class, location.type)
832 || coalesce(location.extratags, ''::hstore)
833 || coalesce(NEW.extratags, ''::hstore);
835 -- mark the linked place (excludes from search results)
836 UPDATE placex set linked_place_id = NEW.place_id
837 WHERE place_id = location.place_id;
838 -- ensure that those places are not found anymore
839 IF NOT %REVERSE-ONLY% THEN
840 DELETE FROM search_name WHERE place_id = location.place_id;
842 PERFORM deleteLocationArea(NEW.partition, location.place_id, NEW.rank_search);
844 SELECT wikipedia, importance
845 FROM compute_importance(location.extratags, NEW.country_code,
846 'N', location.osm_id)
847 INTO linked_wikipedia,linked_importance;
849 -- Use the maximum importance if one could be computed from the linked object.
850 IF linked_importance is not null AND
851 (NEW.importance is null or NEW.importance < linked_importance)
853 NEW.importance = linked_importance;
856 -- No linked place? As a last resort check if the boundary is tagged with
857 -- a place type and adapt the rank address.
858 IF NEW.rank_address > 0 and NEW.extratags ? 'place' THEN
859 SELECT address_rank INTO place_address_level
860 FROM compute_place_rank(NEW.country_code, 'A', 'place',
861 NEW.extratags->'place', 0::SMALLINT, False, null);
862 IF place_address_level > parent_address_level and
863 place_address_level < 26 THEN
864 NEW.rank_address := place_address_level;
869 -- Initialise the name vector using our name
870 NEW.name := add_default_place_name(NEW.country_code, NEW.name);
871 name_vector := make_keywords(NEW.name);
873 -- make sure all names are in the word table
874 IF NEW.admin_level = 2
875 AND NEW.class = 'boundary' AND NEW.type = 'administrative'
876 AND NEW.country_code IS NOT NULL AND NEW.osm_type = 'R'
878 PERFORM create_country(NEW.name, lower(NEW.country_code));
879 --DEBUG: RAISE WARNING 'Country names updated';
882 SELECT * FROM insert_addresslines(NEW.place_id, NEW.partition,
883 CASE WHEN NEW.rank_address = 0
884 THEN NEW.rank_search ELSE NEW.rank_address END,
886 CASE WHEN NEW.rank_search >= 26
887 AND NEW.rank_search < 30
888 THEN NEW.geometry ELSE NEW.centroid END)
889 INTO NEW.parent_place_id, NEW.postcode, nameaddress_vector;
891 --DEBUG: RAISE WARNING 'RETURN insert_addresslines: %, %, %', NEW.parent_place_id, NEW.postcode, nameaddress_vector;
893 IF NEW.address is not null AND NEW.address ? 'postcode'
894 AND NEW.address->'postcode' not similar to '%(,|;)%' THEN
895 NEW.postcode := upper(trim(NEW.address->'postcode'));
898 IF NEW.postcode is null AND NEW.rank_search > 8 THEN
899 NEW.postcode := get_nearest_postcode(NEW.country_code, NEW.geometry);
902 -- if we have a name add this to the name search table
903 IF NEW.name IS NOT NULL THEN
905 IF NEW.rank_search <= 25 and NEW.rank_address > 0 THEN
906 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);
907 --DEBUG: RAISE WARNING 'added to location (full)';
910 IF NEW.rank_search between 26 and 27 and NEW.class = 'highway' THEN
911 result := insertLocationRoad(NEW.partition, NEW.place_id, NEW.country_code, NEW.geometry);
912 --DEBUG: RAISE WARNING 'insert into road location table (full)';
915 result := insertSearchName(NEW.partition, NEW.place_id, name_vector,
916 NEW.rank_search, NEW.rank_address, NEW.geometry);
917 --DEBUG: RAISE WARNING 'added to search name (full)';
919 IF NOT %REVERSE-ONLY% THEN
920 INSERT INTO search_name (place_id, search_rank, address_rank,
921 importance, country_code, name_vector,
922 nameaddress_vector, centroid)
923 VALUES (NEW.place_id, NEW.rank_search, NEW.rank_address,
924 NEW.importance, NEW.country_code, name_vector,
925 nameaddress_vector, NEW.centroid);
930 --DEBUG: RAISE WARNING 'place update % % finsihed.', NEW.osm_type, NEW.osm_id;
938 CREATE OR REPLACE FUNCTION placex_delete()
945 -- RAISE WARNING 'placex_delete % %',OLD.osm_type,OLD.osm_id;
947 IF OLD.linked_place_id is null THEN
948 update placex set linked_place_id = null, indexed_status = 2 where linked_place_id = OLD.place_id and indexed_status = 0;
949 --DEBUG: RAISE WARNING 'placex_delete:01 % %',OLD.osm_type,OLD.osm_id;
950 update placex set linked_place_id = null where linked_place_id = OLD.place_id;
951 --DEBUG: RAISE WARNING 'placex_delete:02 % %',OLD.osm_type,OLD.osm_id;
953 update placex set indexed_status = 2 where place_id = OLD.linked_place_id and indexed_status = 0;
956 IF OLD.rank_address < 30 THEN
958 -- mark everything linked to this place for re-indexing
959 --DEBUG: RAISE WARNING 'placex_delete:03 % %',OLD.osm_type,OLD.osm_id;
960 UPDATE placex set indexed_status = 2 from place_addressline where address_place_id = OLD.place_id
961 and placex.place_id = place_addressline.place_id and indexed_status = 0 and place_addressline.isaddress;
963 --DEBUG: RAISE WARNING 'placex_delete:04 % %',OLD.osm_type,OLD.osm_id;
964 DELETE FROM place_addressline where address_place_id = OLD.place_id;
966 --DEBUG: RAISE WARNING 'placex_delete:05 % %',OLD.osm_type,OLD.osm_id;
967 b := deleteRoad(OLD.partition, OLD.place_id);
969 --DEBUG: RAISE WARNING 'placex_delete:06 % %',OLD.osm_type,OLD.osm_id;
970 update placex set indexed_status = 2 where parent_place_id = OLD.place_id and indexed_status = 0;
971 --DEBUG: RAISE WARNING 'placex_delete:07 % %',OLD.osm_type,OLD.osm_id;
972 -- reparenting also for OSM Interpolation Lines (and for Tiger?)
973 update location_property_osmline set indexed_status = 2 where indexed_status = 0 and parent_place_id = OLD.place_id;
977 --DEBUG: RAISE WARNING 'placex_delete:08 % %',OLD.osm_type,OLD.osm_id;
979 IF OLD.rank_address < 26 THEN
980 b := deleteLocationArea(OLD.partition, OLD.place_id, OLD.rank_search);
983 --DEBUG: RAISE WARNING 'placex_delete:09 % %',OLD.osm_type,OLD.osm_id;
985 IF OLD.name is not null THEN
986 IF NOT %REVERSE-ONLY% THEN
987 DELETE from search_name WHERE place_id = OLD.place_id;
989 b := deleteSearchName(OLD.partition, OLD.place_id);
992 --DEBUG: RAISE WARNING 'placex_delete:10 % %',OLD.osm_type,OLD.osm_id;
994 DELETE FROM place_addressline where place_id = OLD.place_id;
996 --DEBUG: RAISE WARNING 'placex_delete:11 % %',OLD.osm_type,OLD.osm_id;
998 -- remove from tables for special search
999 classtable := 'place_classtype_' || OLD.class || '_' || OLD.type;
1000 SELECT count(*)>0 FROM pg_tables WHERE tablename = classtable and schemaname = current_schema() INTO b;
1002 EXECUTE 'DELETE FROM ' || classtable::regclass || ' WHERE place_id = $1' USING OLD.place_id;
1005 --DEBUG: RAISE WARNING 'placex_delete:12 % %',OLD.osm_type,OLD.osm_id;