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
34 --DEBUG: RAISE WARNING 'node in relation %',relation;
36 SELECT place_id from placex
37 WHERE osm_type = 'W' and osm_id = substring(location.members[i],2)::bigint
39 and rank_search between 26 and 27
41 RETURN parent.place_id;
47 parent_place_id := find_parent_for_address(addr_street, addr_place,
49 IF parent_place_id is not null THEN
50 RETURN parent_place_id;
53 IF poi_osm_type = 'N' THEN
54 -- Is this node part of an interpolation?
56 SELECT q.parent_place_id
57 FROM location_property_osmline q, planet_osm_ways x
58 WHERE q.linegeo && bbox and x.id = q.osm_id
59 and poi_osm_id = any(x.nodes)
62 --DEBUG: RAISE WARNING 'Get parent from interpolation: %', parent.parent_place_id;
63 RETURN parent.parent_place_id;
66 -- Is this node part of any other way?
68 SELECT p.place_id, p.osm_id, p.rank_search, p.address,
69 coalesce(p.centroid, ST_Centroid(p.geometry)) as centroid
70 FROM placex p, planet_osm_ways w
71 WHERE p.osm_type = 'W' and p.rank_search >= 26
72 and p.geometry && bbox
73 and w.id = p.osm_id and poi_osm_id = any(w.nodes)
75 --DEBUG: RAISE WARNING 'Node is part of way % ', location.osm_id;
77 -- Way IS a road then we are on it - that must be our road
78 IF location.rank_search < 28 THEN
79 --DEBUG: RAISE WARNING 'node in way that is a street %',location;
80 return location.place_id;
83 SELECT find_parent_for_poi('W', location.osm_id, poi_partition,
85 location.address->'street',
86 location.address->'place',
89 IF parent_place_id is not null THEN
90 RETURN parent_place_id;
96 IF ST_Area(bbox) < 0.01 THEN
97 -- for smaller features get the nearest road
98 SELECT getNearestRoadPlaceId(poi_partition, bbox) INTO parent_place_id;
99 --DEBUG: RAISE WARNING 'Checked for nearest way (%)', parent_place_id;
101 -- for larger features simply find the area with the largest rank that
104 SELECT place_id FROM placex
105 WHERE bbox @ geometry AND _ST_Covers(geometry, ST_Centroid(bbox))
106 AND rank_search between 5 and 25
107 ORDER BY rank_search desc
109 RETURN location.place_id;
114 RETURN parent_place_id;
117 LANGUAGE plpgsql STABLE;
119 -- Try to find a linked place for the given object.
120 CREATE OR REPLACE FUNCTION find_linked_place(bnd placex)
124 relation_members TEXT[];
126 linked_placex placex%ROWTYPE;
129 IF bnd.rank_search >= 26 or bnd.rank_address = 0
130 or ST_GeometryType(bnd.geometry) NOT IN ('ST_Polygon','ST_MultiPolygon')
135 IF bnd.osm_type = 'R' THEN
136 -- see if we have any special relation members
137 SELECT members FROM planet_osm_rels WHERE id = bnd.osm_id INTO relation_members;
138 --DEBUG: RAISE WARNING 'Got relation members';
140 -- Search for relation members with role 'lable'.
141 IF relation_members IS NOT NULL THEN
143 SELECT get_rel_node_members(relation_members, ARRAY['label']) as member
145 --DEBUG: RAISE WARNING 'Found label member %', rel_member.member;
149 WHERE osm_type = 'N' and osm_id = rel_member.member
152 --DEBUG: RAISE WARNING 'Linked label member';
153 RETURN linked_placex;
160 IF bnd.name ? 'name' THEN
161 bnd_name := make_standard_name(bnd.name->'name');
162 IF bnd_name = '' THEN
167 -- If extratags has a place tag, look for linked nodes by their place type.
168 -- Area and node still have to have the same name.
169 IF bnd.extratags ? 'place' and bnd_name is not null THEN
172 WHERE make_standard_name(name->'name') = bnd_name
173 AND placex.class = 'place' AND placex.type = bnd.extratags->'place'
174 AND placex.osm_type = 'N'
175 AND placex.rank_search < 26 -- needed to select the right index
176 AND _st_covers(bnd.geometry, placex.geometry)
178 --DEBUG: RAISE WARNING 'Found type-matching place node %', linked_placex.osm_id;
179 RETURN linked_placex;
183 IF bnd.extratags ? 'wikidata' THEN
186 WHERE placex.class = 'place' AND placex.osm_type = 'N'
187 AND placex.extratags ? 'wikidata' -- needed to select right index
188 AND placex.extratags->'wikidata' = bnd.extratags->'wikidata'
189 AND placex.rank_search < 26
190 AND _st_covers(bnd.geometry, placex.geometry)
191 ORDER BY make_standard_name(name->'name') = bnd_name desc
193 --DEBUG: RAISE WARNING 'Found wikidata-matching place node %', linked_placex.osm_id;
194 RETURN linked_placex;
198 -- Name searches can be done for ways as well as relations
199 IF bnd_name is not null THEN
200 --DEBUG: RAISE WARNING 'Looking for nodes with matching names';
202 SELECT placex.* from placex
203 WHERE make_standard_name(name->'name') = bnd_name
204 AND ((bnd.rank_address > 0 and placex.rank_address = bnd.rank_address)
205 OR (bnd.rank_address = 0 and placex.rank_search = bnd.rank_search))
206 AND placex.osm_type = 'N'
207 AND placex.rank_search < 26 -- needed to select the right index
208 AND _st_covers(bnd.geometry, placex.geometry)
210 --DEBUG: RAISE WARNING 'Found matching place node %', linked_placex.osm_id;
211 RETURN linked_placex;
218 LANGUAGE plpgsql STABLE;
221 -- Insert address of a place into the place_addressline table.
223 -- \param obj_place_id Place_id of the place to compute the address for.
224 -- \param partition Partition number where the place is in.
225 -- \param maxrank Rank of the place. All address features must have
226 -- a search rank lower than the given rank.
227 -- \param address Address terms for the place.
228 -- \param geoemtry Geometry to which the address objects should be close.
230 -- \retval parent_place_id Place_id of the address object that is the direct
232 -- \retval postcode Postcode computed from the address. This is the
233 -- addr:postcode of one of the address objects. If
234 -- more than one of has a postcode, the highest ranking
235 -- one is used. May be NULL.
236 -- \retval nameaddress_vector Search terms for the address. This is the sum
237 -- of name terms of all address objects.
238 CREATE OR REPLACE FUNCTION insert_addresslines(obj_place_id BIGINT,
243 OUT parent_place_id BIGINT,
245 OUT nameaddress_vector INT[])
248 current_rank_address INTEGER := 0;
249 location_distance FLOAT := 0;
250 location_parent GEOMETRY := NULL;
251 parent_place_id_rank SMALLINT := 0;
253 location_isaddress BOOLEAN;
255 address_havelevel BOOLEAN[];
256 location_keywords INT[];
264 parent_place_id := 0;
265 nameaddress_vector := '{}'::int[];
266 isin_tokens := '{}'::int[];
268 ---- convert address store to array of tokenids
269 IF address IS NOT NULL THEN
270 FOR addr_item IN SELECT * FROM each(address)
272 IF addr_item.key IN ('city', 'tiger:county', 'state', 'suburb', 'province',
273 'district', 'region', 'county', 'municipality',
274 'hamlet', 'village', 'subdistrict', 'town',
275 'neighbourhood', 'quarter', 'parish')
277 isin_tokens := array_merge(isin_tokens,
278 word_ids_from_name(addr_item.value));
279 IF NOT %REVERSE-ONLY% THEN
280 nameaddress_vector := array_merge(nameaddress_vector,
281 addr_ids_from_name(addr_item.value));
286 IF address ? 'is_in' THEN
287 -- is_in items need splitting
288 isin := regexp_split_to_array(address->'is_in', E'[;,]');
289 IF array_upper(isin, 1) IS NOT NULL THEN
290 FOR i IN 1..array_upper(isin, 1) LOOP
291 isin_tokens := array_merge(isin_tokens,
292 word_ids_from_name(isin[i]));
294 -- merge word into address vector
295 IF NOT %REVERSE-ONLY% THEN
296 nameaddress_vector := array_merge(nameaddress_vector,
297 addr_ids_from_name(isin[i]));
303 IF NOT %REVERSE-ONLY% THEN
304 nameaddress_vector := array_merge(nameaddress_vector, isin_tokens);
307 ---- now compute the address terms
309 address_havelevel[i] := false;
313 SELECT * FROM getNearFeatures(partition, geometry, maxrank, isin_tokens)
315 IF location.rank_address != current_rank_address THEN
316 current_rank_address := location.rank_address;
317 IF location.isguess THEN
318 location_distance := location.distance * 1.5;
320 IF location.rank_address <= 12 THEN
321 -- for county and above, if we have an area consider that exact
322 -- (It would be nice to relax the constraint for places close to
323 -- the boundary but we'd need the exact geometry for that. Too
325 location_distance = 0;
327 -- Below county level remain slightly fuzzy.
328 location_distance := location.distance * 0.5;
332 CONTINUE WHEN location.keywords <@ location_keywords;
335 IF location.distance < location_distance OR NOT location.isguess THEN
336 location_keywords := location.keywords;
338 location_isaddress := NOT address_havelevel[location.rank_address];
339 --DEBUG: RAISE WARNING 'should be address: %, is guess: %, rank: %', location_isaddress, location.isguess, location.rank_address;
340 IF location_isaddress AND location.isguess AND location_parent IS NOT NULL THEN
341 location_isaddress := ST_Contains(location_parent, location.centroid);
344 --DEBUG: RAISE WARNING '% isaddress: %', location.place_id, location_isaddress;
345 -- Add it to the list of search terms
346 IF NOT %REVERSE-ONLY% THEN
347 nameaddress_vector := array_merge(nameaddress_vector,
348 location.keywords::integer[]);
351 INSERT INTO place_addressline (place_id, address_place_id, fromarea,
352 isaddress, distance, cached_rank_address)
353 VALUES (obj_place_id, location.place_id, true,
354 location_isaddress, location.distance, location.rank_address);
356 IF location_isaddress THEN
357 -- add postcode if we have one
358 -- (If multiple postcodes are available, we end up with the highest ranking one.)
359 IF location.postcode is not null THEN
360 postcode = location.postcode;
363 address_havelevel[location.rank_address] := true;
364 -- add a hack against postcode ranks
365 IF NOT location.isguess
366 AND location.rank_address != 11 AND location.rank_address != 5
368 SELECT p.geometry FROM placex p
369 WHERE p.place_id = location.place_id INTO location_parent;
372 IF location.rank_address > parent_place_id_rank THEN
373 parent_place_id = location.place_id;
374 parent_place_id_rank = location.rank_address;
385 CREATE OR REPLACE FUNCTION placex_insert()
392 country_code VARCHAR(2);
397 --DEBUG: RAISE WARNING '% % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
399 NEW.place_id := nextval('seq_place');
400 NEW.indexed_status := 1; --STATUS_NEW
402 NEW.country_code := lower(get_country_code(NEW.geometry));
404 NEW.partition := get_partition(NEW.country_code);
405 NEW.geometry_sector := geometry_sector(NEW.partition, NEW.geometry);
407 IF NEW.osm_type = 'X' THEN
408 -- E'X'ternal records should already be in the right format so do nothing
410 is_area := ST_GeometryType(NEW.geometry) IN ('ST_Polygon','ST_MultiPolygon');
412 IF NEW.class in ('place','boundary')
413 AND NEW.type in ('postcode','postal_code') THEN
415 IF NEW.address IS NULL OR NOT NEW.address ? 'postcode' THEN
416 -- most likely just a part of a multipolygon postcode boundary, throw it away
420 NEW.name := hstore('ref', NEW.address->'postcode');
422 SELECT * FROM get_postcode_rank(NEW.country_code, NEW.address->'postcode')
423 INTO NEW.rank_search, NEW.rank_address;
426 NEW.rank_address := 0;
428 ELSEIF NEW.class = 'boundary' AND NOT is_area THEN
430 ELSEIF NEW.class = 'boundary' AND NEW.type = 'administrative'
431 AND NEW.admin_level <= 4 AND NEW.osm_type = 'W' THEN
433 ELSEIF NEW.osm_type = 'N' AND NEW.class = 'highway' THEN
434 NEW.rank_search = 30;
435 NEW.rank_address = 0;
436 ELSEIF NEW.class = 'landuse' AND NOT is_area THEN
437 NEW.rank_search = 30;
438 NEW.rank_address = 0;
440 -- do table lookup stuff
441 IF NEW.class = 'boundary' and NEW.type = 'administrative' THEN
442 classtype = NEW.type || NEW.admin_level::TEXT;
444 classtype = NEW.type;
446 SELECT l.rank_search, l.rank_address FROM address_levels l
447 WHERE (l.country_code = NEW.country_code or l.country_code is NULL)
448 AND l.class = NEW.class AND (l.type = classtype or l.type is NULL)
449 ORDER BY l.country_code, l.class, l.type LIMIT 1
450 INTO NEW.rank_search, NEW.rank_address;
452 IF NEW.rank_search is NULL THEN
453 NEW.rank_search := 30;
456 IF NEW.rank_address is NULL THEN
457 NEW.rank_address := 30;
461 -- some postcorrections
462 IF NEW.class = 'waterway' AND NEW.osm_type = 'R' THEN
463 -- Slightly promote waterway relations so that they are processed
464 -- before their members.
465 NEW.rank_search := NEW.rank_search - 1;
468 IF (NEW.extratags -> 'capital') = 'yes' THEN
469 NEW.rank_search := NEW.rank_search - 1;
474 -- a country code make no sense below rank 4 (country)
475 IF NEW.rank_search < 4 THEN
476 NEW.country_code := NULL;
479 --DEBUG: RAISE WARNING 'placex_insert:END: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
481 RETURN NEW; -- %DIFFUPDATES% The following is not needed until doing diff updates, and slows the main index process down
483 IF NEW.osm_type = 'N' and NEW.rank_search > 28 THEN
484 -- might be part of an interpolation
485 result := osmline_reinsert(NEW.osm_id, NEW.geometry);
486 ELSEIF NEW.rank_address > 0 THEN
487 IF (ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon') AND ST_IsValid(NEW.geometry)) THEN
488 -- Performance: We just can't handle re-indexing for country level changes
489 IF st_area(NEW.geometry) < 1 THEN
490 -- mark items within the geometry for re-indexing
491 -- RAISE WARNING 'placex poly insert: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
493 -- work around bug in postgis, this may have been fixed in 2.0.0 (see http://trac.osgeo.org/postgis/ticket/547)
494 update placex set indexed_status = 2 where (st_covers(NEW.geometry, placex.geometry) OR ST_Intersects(NEW.geometry, placex.geometry))
495 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'));
496 update placex set indexed_status = 2 where (st_covers(NEW.geometry, placex.geometry) OR ST_Intersects(NEW.geometry, placex.geometry))
497 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'));
500 -- mark nearby items for re-indexing, where 'nearby' depends on the features rank_search and is a complete guess :(
502 -- 16 = city, anything higher than city is effectively ignored (polygon required!)
503 IF NEW.type='postcode' THEN
505 ELSEIF NEW.rank_search < 16 THEN
507 ELSEIF NEW.rank_search < 18 THEN
509 ELSEIF NEW.rank_search < 20 THEN
511 ELSEIF NEW.rank_search = 21 THEN
513 ELSEIF NEW.rank_search < 24 THEN
515 ELSEIF NEW.rank_search < 26 THEN
516 diameter := 0.002; -- 100 to 200 meters
517 ELSEIF NEW.rank_search < 28 THEN
518 diameter := 0.001; -- 50 to 100 meters
521 -- RAISE WARNING 'placex point insert: % % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type,diameter;
522 IF NEW.rank_search >= 26 THEN
523 -- roads may cause reparenting for >27 rank places
524 update placex set indexed_status = 2 where indexed_status = 0 and rank_search > NEW.rank_search and ST_DWithin(placex.geometry, NEW.geometry, diameter);
525 -- reparenting also for OSM Interpolation Lines (and for Tiger?)
526 update location_property_osmline set indexed_status = 2 where indexed_status = 0 and ST_DWithin(location_property_osmline.linegeo, NEW.geometry, diameter);
527 ELSEIF NEW.rank_search >= 16 THEN
528 -- up to rank 16, street-less addresses may need reparenting
529 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');
531 -- for all other places the search terms may change as well
532 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);
539 -- add to tables for special search
540 -- Note: won't work on initial import because the classtype tables
541 -- do not yet exist. It won't hurt either.
542 classtable := 'place_classtype_' || NEW.class || '_' || NEW.type;
543 SELECT count(*)>0 FROM pg_tables WHERE tablename = classtable and schemaname = current_schema() INTO result;
545 EXECUTE 'INSERT INTO ' || classtable::regclass || ' (place_id, centroid) VALUES ($1,$2)'
546 USING NEW.place_id, ST_Centroid(NEW.geometry);
556 CREATE OR REPLACE FUNCTION placex_update()
562 relation_members TEXT[];
569 name_vector INTEGER[];
570 nameaddress_vector INTEGER[];
572 linked_node_id BIGINT;
573 linked_importance FLOAT;
574 linked_wikipedia TEXT;
579 IF OLD.indexed_status = 100 THEN
580 --DEBUG: RAISE WARNING 'placex_update delete % %',NEW.osm_type,NEW.osm_id;
581 delete from placex where place_id = OLD.place_id;
585 IF NEW.indexed_status != 0 OR OLD.indexed_status = 0 THEN
589 --DEBUG: RAISE WARNING 'placex_update % % (%)',NEW.osm_type,NEW.osm_id,NEW.place_id;
591 NEW.indexed_date = now();
593 IF NOT %REVERSE-ONLY% THEN
594 DELETE from search_name WHERE place_id = NEW.place_id;
596 result := deleteSearchName(NEW.partition, NEW.place_id);
597 DELETE FROM place_addressline WHERE place_id = NEW.place_id;
598 result := deleteRoad(NEW.partition, NEW.place_id);
599 result := deleteLocationArea(NEW.partition, NEW.place_id, NEW.rank_search);
600 UPDATE placex set linked_place_id = null, indexed_status = 2
601 where linked_place_id = NEW.place_id;
602 -- update not necessary for osmline, cause linked_place_id does not exist
604 IF NEW.linked_place_id is not null THEN
605 --DEBUG: RAISE WARNING 'place already linked to %', NEW.linked_place_id;
609 --DEBUG: RAISE WARNING 'Copy over address tags';
610 -- housenumber is a computed field, so start with an empty value
611 NEW.housenumber := NULL;
612 IF NEW.address is not NULL THEN
613 IF NEW.address ? 'conscriptionnumber' THEN
614 i := getorcreate_housenumber_id(make_standard_name(NEW.address->'conscriptionnumber'));
615 IF NEW.address ? 'streetnumber' THEN
616 i := getorcreate_housenumber_id(make_standard_name(NEW.address->'streetnumber'));
617 NEW.housenumber := (NEW.address->'conscriptionnumber') || '/' || (NEW.address->'streetnumber');
619 NEW.housenumber := NEW.address->'conscriptionnumber';
621 ELSEIF NEW.address ? 'streetnumber' THEN
622 NEW.housenumber := NEW.address->'streetnumber';
623 i := getorcreate_housenumber_id(make_standard_name(NEW.address->'streetnumber'));
624 ELSEIF NEW.address ? 'housenumber' THEN
625 NEW.housenumber := NEW.address->'housenumber';
626 i := getorcreate_housenumber_id(make_standard_name(NEW.housenumber));
629 addr_street := NEW.address->'street';
630 addr_place := NEW.address->'place';
632 IF NEW.address ? 'postcode' and NEW.address->'postcode' not similar to '%(,|;)%' THEN
633 i := getorcreate_postcode_id(NEW.address->'postcode');
637 -- Speed up searches - just use the centroid of the feature
638 -- cheaper but less acurate
639 NEW.centroid := ST_PointOnSurface(NEW.geometry);
640 --DEBUG: RAISE WARNING 'Computing preliminary centroid at %',ST_AsText(NEW.centroid);
642 NEW.postcode := null;
644 -- recalculate country and partition
645 IF NEW.rank_search = 4 AND NEW.address is not NULL AND NEW.address ? 'country' THEN
646 -- for countries, believe the mapped country code,
647 -- so that we remain in the right partition if the boundaries
649 NEW.country_code := lower(NEW.address->'country');
650 NEW.partition := get_partition(lower(NEW.country_code));
651 IF NEW.partition = 0 THEN
652 NEW.country_code := lower(get_country_code(NEW.centroid));
653 NEW.partition := get_partition(NEW.country_code);
656 IF NEW.rank_search >= 4 THEN
657 NEW.country_code := lower(get_country_code(NEW.centroid));
659 NEW.country_code := NULL;
661 NEW.partition := get_partition(NEW.country_code);
663 --DEBUG: RAISE WARNING 'Country updated: "%"', NEW.country_code;
665 -- waterway ways are linked when they are part of a relation and have the same class/type
666 IF NEW.osm_type = 'R' and NEW.class = 'waterway' THEN
667 FOR relation_members IN select members from planet_osm_rels r where r.id = NEW.osm_id and r.parts != array[]::bigint[]
669 FOR i IN 1..array_upper(relation_members, 1) BY 2 LOOP
670 IF relation_members[i+1] in ('', 'main_stream', 'side_stream') AND substring(relation_members[i],1,1) = 'w' THEN
671 --DEBUG: RAISE WARNING 'waterway parent %, child %/%', NEW.osm_id, i, relation_members[i];
672 FOR linked_node_id IN SELECT place_id FROM placex
673 WHERE osm_type = 'W' and osm_id = substring(relation_members[i],2,200)::bigint
674 and class = NEW.class and type in ('river', 'stream', 'canal', 'drain', 'ditch')
675 and ( relation_members[i+1] != 'side_stream' or NEW.name->'name' = name->'name')
677 UPDATE placex SET linked_place_id = NEW.place_id WHERE place_id = linked_node_id;
678 IF NOT %REVERSE-ONLY% THEN
679 DELETE FROM search_name WHERE place_id = linked_node_id;
685 --DEBUG: RAISE WARNING 'Waterway processed';
688 NEW.importance := null;
689 SELECT wikipedia, importance
690 FROM compute_importance(NEW.extratags, NEW.country_code, NEW.osm_type, NEW.osm_id)
691 INTO NEW.wikipedia,NEW.importance;
693 --DEBUG: RAISE WARNING 'Importance computed from wikipedia: %', NEW.importance;
695 -- ---------------------------------------------------------------------------
696 -- For low level elements we inherit from our parent road
697 IF (NEW.rank_search > 27 OR (NEW.type = 'postcode' AND NEW.rank_search = 25)) THEN
699 --DEBUG: RAISE WARNING 'finding street for % %', NEW.osm_type, NEW.osm_id;
700 NEW.parent_place_id := null;
702 -- if we have a POI and there is no address information,
703 -- see if we can get it from a surrounding building
704 IF NEW.osm_type = 'N' AND addr_street IS NULL AND addr_place IS NULL
705 AND NEW.housenumber IS NULL THEN
707 -- The additional && condition works around the misguided query
708 -- planner of postgis 3.0.
709 SELECT address from placex where ST_Covers(geometry, NEW.centroid)
710 and geometry && NEW.centroid
711 and (address ? 'housenumber' or address ? 'street' or address ? 'place')
712 and rank_search > 28 AND ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon')
715 NEW.housenumber := location.address->'housenumber';
716 addr_street := location.address->'street';
717 addr_place := location.address->'place';
718 --DEBUG: RAISE WARNING 'Found surrounding building % %', location.osm_type, location.osm_id;
722 -- We have to find our parent road.
723 NEW.parent_place_id := find_parent_for_poi(NEW.osm_type, NEW.osm_id,
725 ST_Envelope(NEW.geometry),
726 addr_street, addr_place);
728 -- If we found the road take a shortcut here.
729 -- Otherwise fall back to the full address getting method below.
730 IF NEW.parent_place_id is not null THEN
732 -- Get the details of the parent road
733 SELECT p.country_code, p.postcode FROM placex p
734 WHERE p.place_id = NEW.parent_place_id INTO location;
736 NEW.country_code := location.country_code;
737 --DEBUG: RAISE WARNING 'Got parent details from search name';
739 -- determine postcode
740 IF NEW.address is not null AND NEW.address ? 'postcode' THEN
741 NEW.postcode = upper(trim(NEW.address->'postcode'));
743 NEW.postcode := location.postcode;
745 IF NEW.postcode is null THEN
746 NEW.postcode := get_nearest_postcode(NEW.country_code, NEW.geometry);
749 -- If there is no name it isn't searchable, don't bother to create a search record
750 IF NEW.name is NULL THEN
751 --DEBUG: RAISE WARNING 'Not a searchable place % %', NEW.osm_type, NEW.osm_id;
755 NEW.name := add_default_place_name(NEW.country_code, NEW.name);
756 name_vector := make_keywords(NEW.name);
758 -- Performance, it would be more acurate to do all the rest of the import
759 -- process but it takes too long
760 -- Just be happy with inheriting from parent road only
761 IF NEW.rank_search <= 25 and NEW.rank_address > 0 THEN
762 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);
763 --DEBUG: RAISE WARNING 'Place added to location table';
766 result := insertSearchName(NEW.partition, NEW.place_id, name_vector,
767 NEW.rank_search, NEW.rank_address, NEW.geometry);
769 IF NOT %REVERSE-ONLY% THEN
770 -- Merge address from parent
771 SELECT array_merge(s.name_vector, s.nameaddress_vector)
772 INTO nameaddress_vector
774 WHERE s.place_id = NEW.parent_place_id;
776 INSERT INTO search_name (place_id, search_rank, address_rank,
777 importance, country_code, name_vector,
778 nameaddress_vector, centroid)
779 VALUES (NEW.place_id, NEW.rank_search, NEW.rank_address,
780 NEW.importance, NEW.country_code, name_vector,
781 nameaddress_vector, NEW.centroid);
782 --DEBUG: RAISE WARNING 'Place added to search table';
790 -- ---------------------------------------------------------------------------
792 --DEBUG: RAISE WARNING 'Using full index mode for % %', NEW.osm_type, NEW.osm_id;
793 SELECT * INTO location FROM find_linked_place(NEW);
794 IF location.place_id is not null THEN
795 --DEBUG: RAISE WARNING 'Linked %', location;
797 -- Use the linked point as the centre point of the geometry,
798 -- but only if it is within the area of the boundary.
799 centroid := coalesce(location.centroid, ST_Centroid(location.geometry));
800 IF centroid is not NULL AND ST_Within(centroid, NEW.geometry) THEN
801 NEW.centroid := centroid;
804 -- Use the address rank of the linked place, if it has one
805 IF location.rank_address between 5 and 25 THEN
806 NEW.rank_address := location.rank_address;
809 -- merge in the label name
810 IF NOT location.name IS NULL THEN
811 NEW.name := location.name || NEW.name;
814 -- merge in extra tags
815 NEW.extratags := hstore('linked_' || location.class, location.type)
816 || coalesce(location.extratags, ''::hstore)
817 || coalesce(NEW.extratags, ''::hstore);
819 -- mark the linked place (excludes from search results)
820 UPDATE placex set linked_place_id = NEW.place_id
821 WHERE place_id = location.place_id;
822 -- ensure that those places are not found anymore
823 IF NOT %REVERSE-ONLY% THEN
824 DELETE FROM search_name WHERE place_id = location.place_id;
827 SELECT wikipedia, importance
828 FROM compute_importance(location.extratags, NEW.country_code,
829 'N', location.osm_id)
830 INTO linked_wikipedia,linked_importance;
832 -- Use the maximum importance if one could be computed from the linked object.
833 IF linked_importance is not null AND
834 (NEW.importance is null or NEW.importance < linked_importance)
836 NEW.importance = linked_importance;
840 -- Initialise the name vector using our name
841 NEW.name := add_default_place_name(NEW.country_code, NEW.name);
842 name_vector := make_keywords(NEW.name);
844 -- make sure all names are in the word table
845 IF NEW.admin_level = 2
846 AND NEW.class = 'boundary' AND NEW.type = 'administrative'
847 AND NEW.country_code IS NOT NULL AND NEW.osm_type = 'R'
849 PERFORM create_country(NEW.name, lower(NEW.country_code));
850 --DEBUG: RAISE WARNING 'Country names updated';
853 SELECT * FROM insert_addresslines(NEW.place_id, NEW.partition,
854 NEW.rank_search, NEW.address,
855 CASE WHEN NEW.rank_search >= 26
856 AND NEW.rank_search < 30
857 THEN NEW.geometry ELSE NEW.centroid END)
858 INTO NEW.parent_place_id, NEW.postcode, nameaddress_vector;
860 --DEBUG: RAISE WARNING 'RETURN insert_addresslines: %, %, %', NEW.parent_place_id, NEW.postcode, nameaddress_vector;
862 IF NEW.address is not null AND NEW.address ? 'postcode'
863 AND NEW.address->'postcode' not similar to '%(,|;)%' THEN
864 NEW.postcode := upper(trim(NEW.address->'postcode'));
867 IF NEW.postcode is null AND NEW.rank_search > 8 THEN
868 NEW.postcode := get_nearest_postcode(NEW.country_code, NEW.geometry);
871 -- if we have a name add this to the name search table
872 IF NEW.name IS NOT NULL THEN
874 IF NEW.rank_search <= 25 and NEW.rank_address > 0 THEN
875 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);
876 --DEBUG: RAISE WARNING 'added to location (full)';
879 IF NEW.rank_search between 26 and 27 and NEW.class = 'highway' THEN
880 result := insertLocationRoad(NEW.partition, NEW.place_id, NEW.country_code, NEW.geometry);
881 --DEBUG: RAISE WARNING 'insert into road location table (full)';
884 result := insertSearchName(NEW.partition, NEW.place_id, name_vector,
885 NEW.rank_search, NEW.rank_address, NEW.geometry);
886 --DEBUG: RAISE WARNING 'added to search name (full)';
888 IF NOT %REVERSE-ONLY% THEN
889 INSERT INTO search_name (place_id, search_rank, address_rank,
890 importance, country_code, name_vector,
891 nameaddress_vector, centroid)
892 VALUES (NEW.place_id, NEW.rank_search, NEW.rank_address,
893 NEW.importance, NEW.country_code, name_vector,
894 nameaddress_vector, NEW.centroid);
899 --DEBUG: RAISE WARNING 'place update % % finsihed.', NEW.osm_type, NEW.osm_id;
907 CREATE OR REPLACE FUNCTION placex_delete()
914 -- RAISE WARNING 'placex_delete % %',OLD.osm_type,OLD.osm_id;
916 update placex set linked_place_id = null, indexed_status = 2 where linked_place_id = OLD.place_id and indexed_status = 0;
917 --DEBUG: RAISE WARNING 'placex_delete:01 % %',OLD.osm_type,OLD.osm_id;
918 update placex set linked_place_id = null where linked_place_id = OLD.place_id;
919 --DEBUG: RAISE WARNING 'placex_delete:02 % %',OLD.osm_type,OLD.osm_id;
921 IF OLD.rank_address < 30 THEN
923 -- mark everything linked to this place for re-indexing
924 --DEBUG: RAISE WARNING 'placex_delete:03 % %',OLD.osm_type,OLD.osm_id;
925 UPDATE placex set indexed_status = 2 from place_addressline where address_place_id = OLD.place_id
926 and placex.place_id = place_addressline.place_id and indexed_status = 0 and place_addressline.isaddress;
928 --DEBUG: RAISE WARNING 'placex_delete:04 % %',OLD.osm_type,OLD.osm_id;
929 DELETE FROM place_addressline where address_place_id = OLD.place_id;
931 --DEBUG: RAISE WARNING 'placex_delete:05 % %',OLD.osm_type,OLD.osm_id;
932 b := deleteRoad(OLD.partition, OLD.place_id);
934 --DEBUG: RAISE WARNING 'placex_delete:06 % %',OLD.osm_type,OLD.osm_id;
935 update placex set indexed_status = 2 where parent_place_id = OLD.place_id and indexed_status = 0;
936 --DEBUG: RAISE WARNING 'placex_delete:07 % %',OLD.osm_type,OLD.osm_id;
937 -- reparenting also for OSM Interpolation Lines (and for Tiger?)
938 update location_property_osmline set indexed_status = 2 where indexed_status = 0 and parent_place_id = OLD.place_id;
942 --DEBUG: RAISE WARNING 'placex_delete:08 % %',OLD.osm_type,OLD.osm_id;
944 IF OLD.rank_address < 26 THEN
945 b := deleteLocationArea(OLD.partition, OLD.place_id, OLD.rank_search);
948 --DEBUG: RAISE WARNING 'placex_delete:09 % %',OLD.osm_type,OLD.osm_id;
950 IF OLD.name is not null THEN
951 IF NOT %REVERSE-ONLY% THEN
952 DELETE from search_name WHERE place_id = OLD.place_id;
954 b := deleteSearchName(OLD.partition, OLD.place_id);
957 --DEBUG: RAISE WARNING 'placex_delete:10 % %',OLD.osm_type,OLD.osm_id;
959 DELETE FROM place_addressline where place_id = OLD.place_id;
961 --DEBUG: RAISE WARNING 'placex_delete:11 % %',OLD.osm_type,OLD.osm_id;
963 -- remove from tables for special search
964 classtable := 'place_classtype_' || OLD.class || '_' || OLD.type;
965 SELECT count(*)>0 FROM pg_tables WHERE tablename = classtable and schemaname = current_schema() INTO b;
967 EXECUTE 'DELETE FROM ' || classtable::regclass || ' WHERE place_id = $1' USING OLD.place_id;
970 --DEBUG: RAISE WARNING 'placex_delete:12 % %',OLD.osm_type,OLD.osm_id;