1 -- Trigger functions for the placex table.
3 -- Retrieve the data needed by the indexer for updating the place.
7 -- address list of address tags, either from the object or a surrounding
9 -- country_feature If the place is a country feature, this contains the
10 -- country code, otherwise it is null.
11 CREATE OR REPLACE FUNCTION placex_prepare_update(p placex,
14 OUT country_feature VARCHAR)
17 -- For POI nodes, check if the address should be derived from a surrounding
19 IF p.rank_search < 30 OR p.osm_type != 'N' OR p.address is not null THEN
20 RAISE WARNING 'self address for % %', p.osm_type, p.osm_id;
23 -- The additional && condition works around the misguided query
24 -- planner of postgis 3.0.
25 SELECT placex.address || hstore('_inherited', '') INTO address
27 WHERE ST_Covers(geometry, p.centroid)
28 and geometry && p.centroid
29 and placex.address is not null
30 and (placex.address ? 'housenumber' or placex.address ? 'street' or placex.address ? 'place')
31 and rank_search = 30 AND ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon')
33 RAISE WARNING 'other address for % %: % (%)', p.osm_type, p.osm_id, address, p.centroid;
36 address := address - '_unlisted_place'::TEXT;
39 country_feature := CASE WHEN p.admin_level = 2
40 and p.class = 'boundary' and p.type = 'administrative'
47 LANGUAGE plpgsql STABLE;
50 -- Find the parent road of a POI.
52 -- \returns Place ID of parent object or NULL if none
54 -- Copy data from linked items (POIs on ways, addr:street links, relations).
56 CREATE OR REPLACE FUNCTION find_parent_for_poi(poi_osm_type CHAR(1),
58 poi_partition SMALLINT,
66 parent_place_id BIGINT DEFAULT NULL;
70 {% if debug %}RAISE WARNING 'finding street for % %', poi_osm_type, poi_osm_id;{% endif %}
72 -- Is this object part of an associatedStreet relation?
74 SELECT members FROM planet_osm_rels
75 WHERE parts @> ARRAY[poi_osm_id]
76 and members @> ARRAY[lower(poi_osm_type) || poi_osm_id]
77 and tags @> ARRAY['associatedStreet']
79 FOR i IN 1..array_upper(location.members, 1) BY 2 LOOP
80 IF location.members[i+1] = 'street' THEN
82 SELECT place_id from placex
83 WHERE osm_type = 'W' and osm_id = substring(location.members[i],2)::bigint
85 and rank_search between 26 and 27
87 RETURN parent.place_id;
93 parent_place_id := find_parent_for_address(addr_street, addr_place,
95 IF parent_place_id is not null THEN
96 RETURN parent_place_id;
99 IF poi_osm_type = 'N' THEN
100 -- Is this node part of an interpolation?
102 SELECT q.parent_place_id
103 FROM location_property_osmline q, planet_osm_ways x
104 WHERE q.linegeo && bbox and x.id = q.osm_id
105 and poi_osm_id = any(x.nodes)
108 {% if debug %}RAISE WARNING 'Get parent from interpolation: %', parent.parent_place_id;{% endif %}
109 RETURN parent.parent_place_id;
112 -- Is this node part of any other way?
114 SELECT p.place_id, p.osm_id, p.rank_search, p.address,
115 coalesce(p.centroid, ST_Centroid(p.geometry)) as centroid
116 FROM placex p, planet_osm_ways w
117 WHERE p.osm_type = 'W' and p.rank_search >= 26
118 and p.geometry && bbox
119 and w.id = p.osm_id and poi_osm_id = any(w.nodes)
121 {% if debug %}RAISE WARNING 'Node is part of way % ', location.osm_id;{% endif %}
123 -- Way IS a road then we are on it - that must be our road
124 IF location.rank_search < 28 THEN
125 {% if debug %}RAISE WARNING 'node in way that is a street %',location;{% endif %}
126 return location.place_id;
129 SELECT find_parent_for_poi('W', location.osm_id, poi_partition,
131 location.address->'street',
132 location.address->'place',
134 INTO parent_place_id;
135 IF parent_place_id is not null THEN
136 RETURN parent_place_id;
142 IF addr_street is null and addr_place is not null THEN
143 -- The address is attached to a place we don't know.
144 -- Instead simply use the containing area with the largest rank.
146 SELECT place_id FROM placex
147 WHERE bbox && geometry AND _ST_Covers(geometry, ST_Centroid(bbox))
148 AND rank_address between 5 and 25
149 ORDER BY rank_address desc
151 RETURN location.place_id;
153 ELSEIF ST_Area(bbox) < 0.005 THEN
154 -- for smaller features get the nearest road
155 SELECT getNearestRoadPlaceId(poi_partition, bbox) INTO parent_place_id;
156 {% if debug %}RAISE WARNING 'Checked for nearest way (%)', parent_place_id;{% endif %}
158 -- for larger features simply find the area with the largest rank that
159 -- contains the bbox, only use addressable features
161 SELECT place_id FROM placex
162 WHERE bbox && geometry AND _ST_Covers(geometry, ST_Centroid(bbox))
163 AND rank_address between 5 and 25
164 ORDER BY rank_address desc
166 RETURN location.place_id;
171 RETURN parent_place_id;
174 LANGUAGE plpgsql STABLE;
176 -- Try to find a linked place for the given object.
177 CREATE OR REPLACE FUNCTION find_linked_place(bnd placex)
181 relation_members TEXT[];
183 linked_placex placex%ROWTYPE;
186 IF bnd.rank_search >= 26 or bnd.rank_address = 0
187 or ST_GeometryType(bnd.geometry) NOT IN ('ST_Polygon','ST_MultiPolygon')
188 or bnd.type IN ('postcode', 'postal_code')
193 IF bnd.osm_type = 'R' THEN
194 -- see if we have any special relation members
195 SELECT members FROM planet_osm_rels WHERE id = bnd.osm_id INTO relation_members;
196 {% if debug %}RAISE WARNING 'Got relation members';{% endif %}
198 -- Search for relation members with role 'lable'.
199 IF relation_members IS NOT NULL THEN
201 SELECT get_rel_node_members(relation_members, ARRAY['label']) as member
203 {% if debug %}RAISE WARNING 'Found label member %', rel_member.member;{% endif %}
207 WHERE osm_type = 'N' and osm_id = rel_member.member
210 {% if debug %}RAISE WARNING 'Linked label member';{% endif %}
211 RETURN linked_placex;
218 IF bnd.name ? 'name' THEN
219 bnd_name := lower(bnd.name->'name');
220 IF bnd_name = '' THEN
225 -- If extratags has a place tag, look for linked nodes by their place type.
226 -- Area and node still have to have the same name.
227 IF bnd.extratags ? 'place' and bnd_name is not null THEN
230 WHERE (position(lower(name->'name') in bnd_name) > 0
231 OR position(bnd_name in lower(name->'name')) > 0)
232 AND placex.class = 'place' AND placex.type = bnd.extratags->'place'
233 AND placex.osm_type = 'N'
234 AND placex.linked_place_id is null
235 AND placex.rank_search < 26 -- needed to select the right index
236 AND placex.type != 'postcode'
237 AND ST_Covers(bnd.geometry, placex.geometry)
239 {% if debug %}RAISE WARNING 'Found type-matching place node %', linked_placex.osm_id;{% endif %}
240 RETURN linked_placex;
244 IF bnd.extratags ? 'wikidata' THEN
247 WHERE placex.class = 'place' AND placex.osm_type = 'N'
248 AND placex.extratags ? 'wikidata' -- needed to select right index
249 AND placex.extratags->'wikidata' = bnd.extratags->'wikidata'
250 AND placex.linked_place_id is null
251 AND placex.rank_search < 26
252 AND _st_covers(bnd.geometry, placex.geometry)
253 ORDER BY lower(name->'name') = bnd_name desc
255 {% if debug %}RAISE WARNING 'Found wikidata-matching place node %', linked_placex.osm_id;{% endif %}
256 RETURN linked_placex;
260 -- Name searches can be done for ways as well as relations
261 IF bnd_name is not null THEN
262 {% if debug %}RAISE WARNING 'Looking for nodes with matching names';{% endif %}
264 SELECT placex.* from placex
265 WHERE lower(name->'name') = bnd_name
266 AND ((bnd.rank_address > 0
267 and bnd.rank_address = (compute_place_rank(placex.country_code,
269 placex.type, 15::SMALLINT,
270 false, placex.postcode)).address_rank)
271 OR (bnd.rank_address = 0 and placex.rank_search = bnd.rank_search))
272 AND placex.osm_type = 'N'
273 AND placex.class = 'place'
274 AND placex.linked_place_id is null
275 AND placex.rank_search < 26 -- needed to select the right index
276 AND placex.type != 'postcode'
277 AND ST_Covers(bnd.geometry, placex.geometry)
279 {% if debug %}RAISE WARNING 'Found matching place node %', linked_placex.osm_id;{% endif %}
280 RETURN linked_placex;
287 LANGUAGE plpgsql STABLE;
290 -- Insert address of a place into the place_addressline table.
292 -- \param obj_place_id Place_id of the place to compute the address for.
293 -- \param partition Partition number where the place is in.
294 -- \param maxrank Rank of the place. All address features must have
295 -- a search rank lower than the given rank.
296 -- \param address Address terms for the place.
297 -- \param geometry Geometry to which the address objects should be close.
299 -- \retval parent_place_id Place_id of the address object that is the direct
301 -- \retval postcode Postcode computed from the address. This is the
302 -- addr:postcode of one of the address objects. If
303 -- more than one of has a postcode, the highest ranking
304 -- one is used. May be NULL.
305 -- \retval nameaddress_vector Search terms for the address. This is the sum
306 -- of name terms of all address objects.
307 CREATE OR REPLACE FUNCTION insert_addresslines(obj_place_id BIGINT,
313 OUT parent_place_id BIGINT,
315 OUT nameaddress_vector INT[])
318 address_havelevel BOOLEAN[];
320 location_isaddress BOOLEAN;
321 current_boundary GEOMETRY := NULL;
322 current_node_area GEOMETRY := NULL;
324 parent_place_rank INT := 0;
325 addr_place_ids BIGINT[];
329 parent_place_id := 0;
330 nameaddress_vector := '{}'::int[];
332 address_havelevel := array_fill(false, ARRAY[maxrank]);
335 SELECT * FROM get_places_for_addr_tags(partition, geometry,
337 ORDER BY rank_address, distance, isguess desc
339 {% if not db.reverse_only %}
340 nameaddress_vector := array_merge(nameaddress_vector,
341 location.keywords::int[]);
344 IF location.place_id is not null THEN
345 location_isaddress := not address_havelevel[location.rank_address];
346 IF not address_havelevel[location.rank_address] THEN
347 address_havelevel[location.rank_address] := true;
348 IF parent_place_rank < location.rank_address THEN
349 parent_place_id := location.place_id;
350 parent_place_rank := location.rank_address;
354 INSERT INTO place_addressline (place_id, address_place_id, fromarea,
355 isaddress, distance, cached_rank_address)
356 VALUES (obj_place_id, location.place_id, not location.isguess,
357 true, location.distance, location.rank_address);
359 addr_place_ids := array_append(addr_place_ids, location.place_id);
364 SELECT * FROM getNearFeatures(partition, geometry, maxrank)
365 WHERE addr_place_ids is null or not addr_place_ids @> ARRAY[place_id]
366 ORDER BY rank_address, isguess asc,
368 CASE WHEN rank_address = 16 AND rank_search = 15 THEN 0.2
369 WHEN rank_address = 16 AND rank_search = 16 THEN 0.25
370 WHEN rank_address = 16 AND rank_search = 18 THEN 0.5
373 -- Ignore all place nodes that do not fit in a lower level boundary.
374 CONTINUE WHEN location.isguess
375 and current_boundary is not NULL
376 and not ST_Contains(current_boundary, location.centroid);
378 -- If this is the first item in the rank, then assume it is the address.
379 location_isaddress := not address_havelevel[location.rank_address];
381 -- Further sanity checks to ensure that the address forms a sane hierarchy.
382 IF location_isaddress THEN
383 IF location.isguess and current_node_area is not NULL THEN
384 location_isaddress := ST_Contains(current_node_area, location.centroid);
386 IF not location.isguess and current_boundary is not NULL
387 and location.rank_address != 11 AND location.rank_address != 5 THEN
388 location_isaddress := ST_Contains(current_boundary, location.centroid);
392 IF location_isaddress THEN
393 address_havelevel[location.rank_address] := true;
394 parent_place_id := location.place_id;
396 -- Set postcode if we have one.
397 -- (Returned will be the highest ranking one.)
398 IF location.postcode is not NULL THEN
399 postcode = location.postcode;
402 -- Recompute the areas we need for hierarchy sanity checks.
403 IF location.rank_address != 11 AND location.rank_address != 5 THEN
404 IF location.isguess THEN
405 current_node_area := place_node_fuzzy_area(location.centroid,
406 location.rank_search);
408 current_node_area := NULL;
409 SELECT p.geometry FROM placex p
410 WHERE p.place_id = location.place_id INTO current_boundary;
415 -- Add it to the list of search terms
416 {% if not db.reverse_only %}
417 nameaddress_vector := array_merge(nameaddress_vector,
418 location.keywords::integer[]);
421 INSERT INTO place_addressline (place_id, address_place_id, fromarea,
422 isaddress, distance, cached_rank_address)
423 VALUES (obj_place_id, location.place_id, not location.isguess,
424 location_isaddress, location.distance, location.rank_address);
431 CREATE OR REPLACE FUNCTION placex_insert()
438 country_code VARCHAR(2);
442 {% if debug %}RAISE WARNING '% % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;{% endif %}
444 NEW.place_id := nextval('seq_place');
445 NEW.indexed_status := 1; --STATUS_NEW
447 NEW.centroid := ST_PointOnSurface(NEW.geometry);
448 NEW.country_code := lower(get_country_code(NEW.centroid));
450 NEW.partition := get_partition(NEW.country_code);
451 NEW.geometry_sector := geometry_sector(NEW.partition, NEW.centroid);
453 IF NEW.osm_type = 'X' THEN
454 -- E'X'ternal records should already be in the right format so do nothing
456 is_area := ST_GeometryType(NEW.geometry) IN ('ST_Polygon','ST_MultiPolygon');
458 IF NEW.class in ('place','boundary')
459 AND NEW.type in ('postcode','postal_code')
461 IF NEW.address IS NULL OR NOT NEW.address ? 'postcode' THEN
462 -- most likely just a part of a multipolygon postcode boundary, throw it away
466 NEW.name := hstore('ref', NEW.address->'postcode');
468 ELSEIF NEW.class = 'highway' AND is_area AND NEW.name is null
469 AND NEW.extratags ? 'area' AND NEW.extratags->'area' = 'yes'
472 ELSEIF NEW.class = 'boundary' AND NOT is_area
475 ELSEIF NEW.class = 'boundary' AND NEW.type = 'administrative'
476 AND NEW.admin_level <= 4 AND NEW.osm_type = 'W'
481 SELECT * INTO NEW.rank_search, NEW.rank_address
482 FROM compute_place_rank(NEW.country_code,
483 CASE WHEN is_area THEN 'A' ELSE NEW.osm_type END,
484 NEW.class, NEW.type, NEW.admin_level,
485 (NEW.extratags->'capital') = 'yes',
486 NEW.address->'postcode');
488 -- a country code make no sense below rank 4 (country)
489 IF NEW.rank_search < 4 THEN
490 NEW.country_code := NULL;
495 {% if debug %}RAISE WARNING 'placex_insert:END: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;{% endif %}
497 {% if not disable_diff_updates %}
498 -- The following is not needed until doing diff updates, and slows the main index process down
500 IF NEW.osm_type = 'N' and NEW.rank_search > 28 THEN
501 -- might be part of an interpolation
502 result := osmline_reinsert(NEW.osm_id, NEW.geometry);
503 ELSEIF NEW.rank_address > 0 THEN
504 IF (ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon') AND ST_IsValid(NEW.geometry)) THEN
505 -- Performance: We just can't handle re-indexing for country level changes
506 IF st_area(NEW.geometry) < 1 THEN
507 -- mark items within the geometry for re-indexing
508 -- RAISE WARNING 'placex poly insert: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
510 UPDATE placex SET indexed_status = 2
511 WHERE ST_Intersects(NEW.geometry, placex.geometry)
512 and indexed_status = 0
513 and ((rank_address = 0 and rank_search > NEW.rank_address)
514 or rank_address > NEW.rank_address
515 or (class = 'place' and osm_type = 'N')
517 and (rank_search < 28
519 or (NEW.rank_address >= 16 and address ? 'place'));
522 -- mark nearby items for re-indexing, where 'nearby' depends on the features rank_search and is a complete guess :(
523 diameter := update_place_diameter(NEW.rank_search);
525 -- RAISE WARNING 'placex point insert: % % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type,diameter;
526 IF NEW.rank_search >= 26 THEN
527 -- roads may cause reparenting for >27 rank places
528 update placex set indexed_status = 2 where indexed_status = 0 and rank_search > NEW.rank_search and ST_DWithin(placex.geometry, NEW.geometry, diameter);
529 -- reparenting also for OSM Interpolation Lines (and for Tiger?)
530 update location_property_osmline set indexed_status = 2 where indexed_status = 0 and ST_DWithin(location_property_osmline.linegeo, NEW.geometry, diameter);
531 ELSEIF NEW.rank_search >= 16 THEN
532 -- up to rank 16, street-less addresses may need reparenting
533 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');
535 -- for all other places the search terms may change as well
536 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);
543 -- add to tables for special search
544 -- Note: won't work on initial import because the classtype tables
545 -- do not yet exist. It won't hurt either.
546 classtable := 'place_classtype_' || NEW.class || '_' || NEW.type;
547 SELECT count(*)>0 FROM pg_tables WHERE tablename = classtable and schemaname = current_schema() INTO result;
549 EXECUTE 'INSERT INTO ' || classtable::regclass || ' (place_id, centroid) VALUES ($1,$2)'
550 USING NEW.place_id, ST_Centroid(NEW.geometry);
553 {% endif %} -- not disable_diff_updates
561 CREATE OR REPLACE FUNCTION placex_update()
567 relation_members TEXT[];
570 parent_address_level SMALLINT;
571 place_address_level SMALLINT;
578 name_vector INTEGER[];
579 nameaddress_vector INTEGER[];
580 addr_nameaddress_vector INTEGER[];
582 linked_node_id BIGINT;
583 linked_importance FLOAT;
584 linked_wikipedia TEXT;
589 IF OLD.indexed_status = 100 THEN
590 {% if debug %}RAISE WARNING 'placex_update delete % %',NEW.osm_type,NEW.osm_id;{% endif %}
591 delete from placex where place_id = OLD.place_id;
595 IF NEW.indexed_status != 0 OR OLD.indexed_status = 0 THEN
599 {% if debug %}RAISE WARNING 'placex_update % % (%)',NEW.osm_type,NEW.osm_id,NEW.place_id;{% endif %}
601 NEW.indexed_date = now();
603 {% if 'search_name' in db.tables %}
604 DELETE from search_name WHERE place_id = NEW.place_id;
606 result := deleteSearchName(NEW.partition, NEW.place_id);
607 DELETE FROM place_addressline WHERE place_id = NEW.place_id;
608 result := deleteRoad(NEW.partition, NEW.place_id);
609 result := deleteLocationArea(NEW.partition, NEW.place_id, NEW.rank_search);
610 UPDATE placex set linked_place_id = null, indexed_status = 2
611 where linked_place_id = NEW.place_id;
612 -- update not necessary for osmline, cause linked_place_id does not exist
614 NEW.extratags := NEW.extratags - 'linked_place'::TEXT;
616 IF NEW.linked_place_id is not null THEN
617 {% if debug %}RAISE WARNING 'place already linked to %', NEW.linked_place_id;{% endif %}
621 -- Postcodes are just here to compute the centroids. They are not searchable
622 -- unless they are a boundary=postal_code.
623 -- There was an error in the style so that boundary=postal_code used to be
624 -- imported as place=postcode. That's why relations are allowed to pass here.
625 -- This can go away in a couple of versions.
626 IF NEW.class = 'place' and NEW.type = 'postcode' and NEW.osm_type != 'R' THEN
630 -- Speed up searches - just use the centroid of the feature
631 -- cheaper but less acurate
632 NEW.centroid := ST_PointOnSurface(NEW.geometry);
633 {% if debug %}RAISE WARNING 'Computing preliminary centroid at %',ST_AsText(NEW.centroid);{% endif %}
635 -- recompute the ranks, they might change when linking changes
636 SELECT * INTO NEW.rank_search, NEW.rank_address
637 FROM compute_place_rank(NEW.country_code,
638 CASE WHEN ST_GeometryType(NEW.geometry)
639 IN ('ST_Polygon','ST_MultiPolygon')
640 THEN 'A' ELSE NEW.osm_type END,
641 NEW.class, NEW.type, NEW.admin_level,
642 (NEW.extratags->'capital') = 'yes',
643 NEW.address->'postcode');
644 -- We must always increase the address level relative to the admin boundary.
645 IF NEW.class = 'boundary' and NEW.type = 'administrative'
646 and NEW.osm_type = 'R' and NEW.rank_address > 0
648 -- First, check that admin boundaries do not overtake each other rank-wise.
649 parent_address_level := 3;
652 (CASE WHEN extratags ? 'wikidata' and NEW.extratags ? 'wikidata'
653 and extratags->'wikidata' = NEW.extratags->'wikidata'
654 THEN ST_Equals(geometry, NEW.geometry)
655 ELSE false END) as is_same
657 WHERE osm_type = 'R' and class = 'boundary' and type = 'administrative'
658 and admin_level < NEW.admin_level and admin_level > 3
660 and geometry && NEW.centroid and _ST_Covers(geometry, NEW.centroid)
661 ORDER BY admin_level desc LIMIT 1
663 IF location.is_same THEN
664 -- Looks like the same boundary is replicated on multiple admin_levels.
665 -- Usual tagging in Poland. Remove our boundary from addresses.
666 NEW.rank_address := 0;
668 parent_address_level := location.rank_address;
669 IF location.rank_address >= NEW.rank_address THEN
670 IF location.rank_address >= 24 THEN
671 NEW.rank_address := 25;
673 NEW.rank_address := location.rank_address + 2;
679 IF NEW.rank_address > 9 THEN
680 -- Second check that the boundary is not completely contained in a
681 -- place area with a higher address rank
683 SELECT rank_address FROM placex
684 WHERE class = 'place' and rank_address < 24
685 and rank_address > NEW.rank_address
686 and geometry && NEW.geometry
687 and geometry ~ NEW.geometry -- needed because ST_Relate does not do bbox cover test
688 and ST_Relate(geometry, NEW.geometry, 'T*T***FF*') -- contains but not equal
689 ORDER BY rank_address desc LIMIT 1
691 NEW.rank_address := location.rank_address + 2;
694 ELSEIF NEW.class = 'place' and NEW.osm_type = 'N'
695 and NEW.rank_address between 16 and 23
697 -- If a place node is contained in a admin boundary with the same address level
698 -- and has not been linked, then make the node a subpart by increasing the
699 -- address rank (city level and above).
701 SELECT rank_address FROM placex
702 WHERE osm_type = 'R' and class = 'boundary' and type = 'administrative'
703 and rank_address = NEW.rank_address
704 and geometry && NEW.centroid and _ST_Covers(geometry, NEW.centroid)
707 NEW.rank_address = NEW.rank_address + 2;
710 parent_address_level := 3;
713 {% if debug %}RAISE WARNING 'Copy over address tags';{% endif %}
714 -- housenumber is a computed field, so start with an empty value
715 NEW.housenumber := NULL;
716 IF NEW.address is not NULL THEN
717 IF NEW.address ? 'conscriptionnumber' THEN
718 IF NEW.address ? 'streetnumber' THEN
719 NEW.housenumber := (NEW.address->'conscriptionnumber') || '/' || (NEW.address->'streetnumber');
721 NEW.housenumber := NEW.address->'conscriptionnumber';
723 ELSEIF NEW.address ? 'streetnumber' THEN
724 NEW.housenumber := NEW.address->'streetnumber';
725 ELSEIF NEW.address ? 'housenumber' THEN
726 NEW.housenumber := NEW.address->'housenumber';
728 NEW.housenumber := create_housenumber_id(NEW.housenumber);
730 addr_street := NEW.address->'street';
731 addr_place := NEW.address->'place';
733 IF NEW.address ? 'postcode' and NEW.address->'postcode' not similar to '%(:|,|;)%' THEN
734 i := getorcreate_postcode_id(NEW.address->'postcode');
738 NEW.postcode := null;
740 -- recalculate country and partition
741 IF NEW.rank_search = 4 AND NEW.address is not NULL AND NEW.address ? 'country' THEN
742 -- for countries, believe the mapped country code,
743 -- so that we remain in the right partition if the boundaries
745 NEW.country_code := lower(NEW.address->'country');
746 NEW.partition := get_partition(lower(NEW.country_code));
747 IF NEW.partition = 0 THEN
748 NEW.country_code := lower(get_country_code(NEW.centroid));
749 NEW.partition := get_partition(NEW.country_code);
752 IF NEW.rank_search >= 4 THEN
753 NEW.country_code := lower(get_country_code(NEW.centroid));
755 NEW.country_code := NULL;
757 NEW.partition := get_partition(NEW.country_code);
759 {% if debug %}RAISE WARNING 'Country updated: "%"', NEW.country_code;{% endif %}
761 -- waterway ways are linked when they are part of a relation and have the same class/type
762 IF NEW.osm_type = 'R' and NEW.class = 'waterway' THEN
763 FOR relation_members IN select members from planet_osm_rels r where r.id = NEW.osm_id and r.parts != array[]::bigint[]
765 FOR i IN 1..array_upper(relation_members, 1) BY 2 LOOP
766 IF relation_members[i+1] in ('', 'main_stream', 'side_stream') AND substring(relation_members[i],1,1) = 'w' THEN
767 {% if debug %}RAISE WARNING 'waterway parent %, child %/%', NEW.osm_id, i, relation_members[i];{% endif %}
768 FOR linked_node_id IN SELECT place_id FROM placex
769 WHERE osm_type = 'W' and osm_id = substring(relation_members[i],2,200)::bigint
770 and class = NEW.class and type in ('river', 'stream', 'canal', 'drain', 'ditch')
771 and ( relation_members[i+1] != 'side_stream' or NEW.name->'name' = name->'name')
773 UPDATE placex SET linked_place_id = NEW.place_id WHERE place_id = linked_node_id;
774 {% if 'search_name' in db.tables %}
775 DELETE FROM search_name WHERE place_id = linked_node_id;
781 {% if debug %}RAISE WARNING 'Waterway processed';{% endif %}
784 NEW.importance := null;
785 SELECT wikipedia, importance
786 FROM compute_importance(NEW.extratags, NEW.country_code, NEW.osm_type, NEW.osm_id)
787 INTO NEW.wikipedia,NEW.importance;
789 {% if debug %}RAISE WARNING 'Importance computed from wikipedia: %', NEW.importance;{% endif %}
791 -- ---------------------------------------------------------------------------
792 -- For low level elements we inherit from our parent road
793 IF NEW.rank_search > 27 THEN
795 {% if debug %}RAISE WARNING 'finding street for % %', NEW.osm_type, NEW.osm_id;{% endif %}
796 NEW.parent_place_id := null;
798 -- We have to find our parent road.
799 NEW.parent_place_id := find_parent_for_poi(NEW.osm_type, NEW.osm_id,
801 ST_Envelope(NEW.geometry),
802 addr_street, addr_place);
804 -- If we found the road take a shortcut here.
805 -- Otherwise fall back to the full address getting method below.
806 IF NEW.parent_place_id is not null THEN
808 -- Get the details of the parent road
809 SELECT p.country_code, p.postcode, p.name FROM placex p
810 WHERE p.place_id = NEW.parent_place_id INTO location;
812 IF addr_street is null and addr_place is not null THEN
813 -- Check if the addr:place tag is part of the parent name
814 SELECT count(*) INTO i
815 FROM svals(location.name) AS pname WHERE pname = addr_place;
817 NEW.address = NEW.address || hstore('_unlisted_place', addr_place);
821 NEW.country_code := location.country_code;
822 {% if debug %}RAISE WARNING 'Got parent details from search name';{% endif %}
824 -- determine postcode
825 IF NEW.address is not null AND NEW.address ? 'postcode' THEN
826 NEW.postcode = upper(trim(NEW.address->'postcode'));
828 NEW.postcode := location.postcode;
830 IF NEW.postcode is null THEN
831 NEW.postcode := get_nearest_postcode(NEW.country_code, NEW.geometry);
834 IF NEW.name is not NULL THEN
835 NEW.name := add_default_place_name(NEW.country_code, NEW.name);
836 name_vector := make_keywords(NEW.name);
838 IF NEW.rank_search <= 25 and NEW.rank_address > 0 THEN
839 result := add_location(NEW.place_id, NEW.country_code, NEW.partition,
840 name_vector, NEW.rank_search, NEW.rank_address,
841 upper(trim(NEW.address->'postcode')), NEW.geometry,
843 {% if debug %}RAISE WARNING 'Place added to location table';{% endif %}
848 {% if not db.reverse_only %}
849 IF array_length(name_vector, 1) is not NULL
850 OR NEW.address is not NULL
852 SELECT * INTO name_vector, nameaddress_vector
853 FROM create_poi_search_terms(NEW.place_id,
854 NEW.partition, NEW.parent_place_id,
856 NEW.country_code, NEW.housenumber,
857 name_vector, NEW.centroid);
859 IF array_length(name_vector, 1) is not NULL THEN
860 INSERT INTO search_name (place_id, search_rank, address_rank,
861 importance, country_code, name_vector,
862 nameaddress_vector, centroid)
863 VALUES (NEW.place_id, NEW.rank_search, NEW.rank_address,
864 NEW.importance, NEW.country_code, name_vector,
865 nameaddress_vector, NEW.centroid);
866 {% if debug %}RAISE WARNING 'Place added to search table';{% endif %}
871 -- If the address was inherited from a surrounding building,
872 -- do not add it permanently to the table.
873 IF NEW.address ? '_inherited' THEN
874 IF NEW.address ? '_unlisted_place' THEN
875 NEW.address := hstore('_unlisted_place', NEW.address->'_unlisted_place');
886 -- ---------------------------------------------------------------------------
888 {% if debug %}RAISE WARNING 'Using full index mode for % %', NEW.osm_type, NEW.osm_id;{% endif %}
889 SELECT * INTO location FROM find_linked_place(NEW);
890 IF location.place_id is not null THEN
891 {% if debug %}RAISE WARNING 'Linked %', location;{% endif %}
893 -- Use the linked point as the centre point of the geometry,
894 -- but only if it is within the area of the boundary.
895 geom := coalesce(location.centroid, ST_Centroid(location.geometry));
896 IF geom is not NULL AND ST_Within(geom, NEW.geometry) THEN
897 NEW.centroid := geom;
900 {% if debug %}RAISE WARNING 'parent address: % rank address: %', parent_address_level, location.rank_address;{% endif %}
901 IF location.rank_address > parent_address_level
902 and location.rank_address < 26
904 NEW.rank_address := location.rank_address;
907 -- merge in the label name
908 IF NOT location.name IS NULL THEN
909 NEW.name := location.name || NEW.name;
912 -- merge in extra tags
913 NEW.extratags := hstore('linked_' || location.class, location.type)
914 || coalesce(location.extratags, ''::hstore)
915 || coalesce(NEW.extratags, ''::hstore);
917 -- mark the linked place (excludes from search results)
918 UPDATE placex set linked_place_id = NEW.place_id
919 WHERE place_id = location.place_id;
920 -- ensure that those places are not found anymore
921 {% if 'search_name' in db.tables %}
922 DELETE FROM search_name WHERE place_id = location.place_id;
924 PERFORM deleteLocationArea(NEW.partition, location.place_id, NEW.rank_search);
926 SELECT wikipedia, importance
927 FROM compute_importance(location.extratags, NEW.country_code,
928 'N', location.osm_id)
929 INTO linked_wikipedia,linked_importance;
931 -- Use the maximum importance if one could be computed from the linked object.
932 IF linked_importance is not null AND
933 (NEW.importance is null or NEW.importance < linked_importance)
935 NEW.importance = linked_importance;
938 -- No linked place? As a last resort check if the boundary is tagged with
939 -- a place type and adapt the rank address.
940 IF NEW.rank_address > 0 and NEW.extratags ? 'place' THEN
941 SELECT address_rank INTO place_address_level
942 FROM compute_place_rank(NEW.country_code, 'A', 'place',
943 NEW.extratags->'place', 0::SMALLINT, False, null);
944 IF place_address_level > parent_address_level and
945 place_address_level < 26 THEN
946 NEW.rank_address := place_address_level;
951 -- Initialise the name vector using our name
952 NEW.name := add_default_place_name(NEW.country_code, NEW.name);
953 name_vector := make_keywords(NEW.name);
955 -- make sure all names are in the word table
956 IF NEW.admin_level = 2
957 AND NEW.class = 'boundary' AND NEW.type = 'administrative'
958 AND NEW.country_code IS NOT NULL AND NEW.osm_type = 'R'
960 PERFORM create_country(NEW.name, lower(NEW.country_code));
961 {% if debug %}RAISE WARNING 'Country names updated';{% endif %}
963 -- Also update the list of country names. Adding an additional sanity
964 -- check here: make sure the country does overlap with the area where
965 -- we expect it to be as per static country grid.
967 SELECT country_code FROM country_osm_grid
968 WHERE ST_Covers(geometry, NEW.centroid) and country_code = NEW.country_code
971 {% if debug %}RAISE WARNING 'Updating names for country '%' with: %', NEW.country_code, NEW.name;{% endif %}
972 UPDATE country_name SET name = name || NEW.name WHERE country_code = NEW.country_code;
976 -- For linear features we need the full geometry for determining the address
977 -- because they may go through several administrative entities. Otherwise use
978 -- the centroid for performance reasons.
979 IF ST_GeometryType(NEW.geometry) in ('ST_LineString', 'ST_MultiLineString') THEN
980 geom := NEW.geometry;
982 geom := NEW.centroid;
985 IF NEW.rank_address = 0 THEN
986 max_rank := geometry_to_rank(NEW.rank_search, NEW.geometry, NEW.country_code);
987 -- Rank 0 features may also span multiple administrative areas (e.g. lakes)
988 -- so use the geometry here too. Just make sure the areas don't become too
990 IF NEW.class = 'natural' or max_rank > 10 THEN
991 geom := NEW.geometry;
993 ELSEIF NEW.rank_address > 25 THEN
996 max_rank = NEW.rank_address;
999 SELECT * FROM insert_addresslines(NEW.place_id, NEW.partition, max_rank,
1000 NEW.address, geom, NEW.country_code)
1001 INTO NEW.parent_place_id, NEW.postcode, nameaddress_vector;
1003 {% if debug %}RAISE WARNING 'RETURN insert_addresslines: %, %, %', NEW.parent_place_id, NEW.postcode, nameaddress_vector;{% endif %}
1005 IF NEW.address is not null AND NEW.address ? 'postcode'
1006 AND NEW.address->'postcode' not similar to '%(,|;)%' THEN
1007 NEW.postcode := upper(trim(NEW.address->'postcode'));
1010 IF NEW.postcode is null AND NEW.rank_search > 8 THEN
1011 NEW.postcode := get_nearest_postcode(NEW.country_code, NEW.geometry);
1014 -- if we have a name add this to the name search table
1015 IF NEW.name IS NOT NULL THEN
1017 IF NEW.rank_search <= 25 and NEW.rank_address > 0 THEN
1018 result := add_location(NEW.place_id, NEW.country_code, NEW.partition, name_vector, NEW.rank_search, NEW.rank_address, upper(trim(NEW.address->'postcode')), NEW.geometry, NEW.centroid);
1019 {% if debug %}RAISE WARNING 'added to location (full)';{% endif %}
1022 IF NEW.rank_search between 26 and 27 and NEW.class = 'highway' THEN
1023 result := insertLocationRoad(NEW.partition, NEW.place_id, NEW.country_code, NEW.geometry);
1024 {% if debug %}RAISE WARNING 'insert into road location table (full)';{% endif %}
1027 result := insertSearchName(NEW.partition, NEW.place_id, name_vector,
1028 NEW.rank_search, NEW.rank_address, NEW.geometry);
1029 {% if debug %}RAISE WARNING 'added to search name (full)';{% endif %}
1031 {% if not db.reverse_only %}
1032 INSERT INTO search_name (place_id, search_rank, address_rank,
1033 importance, country_code, name_vector,
1034 nameaddress_vector, centroid)
1035 VALUES (NEW.place_id, NEW.rank_search, NEW.rank_address,
1036 NEW.importance, NEW.country_code, name_vector,
1037 nameaddress_vector, NEW.centroid);
1042 {% if debug %}RAISE WARNING 'place update % % finsihed.', NEW.osm_type, NEW.osm_id;{% endif %}
1050 CREATE OR REPLACE FUNCTION placex_delete()
1057 -- RAISE WARNING 'placex_delete % %',OLD.osm_type,OLD.osm_id;
1059 IF OLD.linked_place_id is null THEN
1060 update placex set linked_place_id = null, indexed_status = 2 where linked_place_id = OLD.place_id and indexed_status = 0;
1061 {% if debug %}RAISE WARNING 'placex_delete:01 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1062 update placex set linked_place_id = null where linked_place_id = OLD.place_id;
1063 {% if debug %}RAISE WARNING 'placex_delete:02 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1065 update placex set indexed_status = 2 where place_id = OLD.linked_place_id and indexed_status = 0;
1068 IF OLD.rank_address < 30 THEN
1070 -- mark everything linked to this place for re-indexing
1071 {% if debug %}RAISE WARNING 'placex_delete:03 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1072 UPDATE placex set indexed_status = 2 from place_addressline where address_place_id = OLD.place_id
1073 and placex.place_id = place_addressline.place_id and indexed_status = 0 and place_addressline.isaddress;
1075 {% if debug %}RAISE WARNING 'placex_delete:04 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1076 DELETE FROM place_addressline where address_place_id = OLD.place_id;
1078 {% if debug %}RAISE WARNING 'placex_delete:05 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1079 b := deleteRoad(OLD.partition, OLD.place_id);
1081 {% if debug %}RAISE WARNING 'placex_delete:06 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1082 update placex set indexed_status = 2 where parent_place_id = OLD.place_id and indexed_status = 0;
1083 {% if debug %}RAISE WARNING 'placex_delete:07 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1084 -- reparenting also for OSM Interpolation Lines (and for Tiger?)
1085 update location_property_osmline set indexed_status = 2 where indexed_status = 0 and parent_place_id = OLD.place_id;
1089 {% if debug %}RAISE WARNING 'placex_delete:08 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1091 IF OLD.rank_address < 26 THEN
1092 b := deleteLocationArea(OLD.partition, OLD.place_id, OLD.rank_search);
1095 {% if debug %}RAISE WARNING 'placex_delete:09 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1097 IF OLD.name is not null THEN
1098 {% if 'search_name' in db.tables %}
1099 DELETE from search_name WHERE place_id = OLD.place_id;
1101 b := deleteSearchName(OLD.partition, OLD.place_id);
1104 {% if debug %}RAISE WARNING 'placex_delete:10 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1106 DELETE FROM place_addressline where place_id = OLD.place_id;
1108 {% if debug %}RAISE WARNING 'placex_delete:11 % %',OLD.osm_type,OLD.osm_id;{% endif %}
1110 -- remove from tables for special search
1111 classtable := 'place_classtype_' || OLD.class || '_' || OLD.type;
1112 SELECT count(*)>0 FROM pg_tables WHERE tablename = classtable and schemaname = current_schema() INTO b;
1114 EXECUTE 'DELETE FROM ' || classtable::regclass || ' WHERE place_id = $1' USING OLD.place_id;
1117 {% if debug %}RAISE WARNING 'placex_delete:12 % %',OLD.osm_type,OLD.osm_id;{% endif %}