1 -- Trigger functions for the placex table.
3 CREATE OR REPLACE FUNCTION get_rel_node_members(members TEXT[], memberLabels TEXT[])
9 FOR i IN 1..ARRAY_UPPER(members,1) BY 2 LOOP
10 IF members[i+1] = ANY(memberLabels)
11 AND upper(substring(members[i], 1, 1))::char(1) = 'N'
13 RETURN NEXT substring(members[i], 2)::bigint;
20 LANGUAGE plpgsql IMMUTABLE;
23 -- Try to find a linked place for the given object.
24 CREATE OR REPLACE FUNCTION find_linked_place(bnd placex)
28 relation_members TEXT[];
30 linked_placex placex%ROWTYPE;
33 IF bnd.rank_search >= 26 or bnd.rank_address = 0
34 or ST_GeometryType(bnd.geometry) NOT IN ('ST_Polygon','ST_MultiPolygon')
39 IF bnd.osm_type = 'R' THEN
40 -- see if we have any special relation members
41 SELECT members FROM planet_osm_rels WHERE id = bnd.osm_id INTO relation_members;
42 --DEBUG: RAISE WARNING 'Got relation members';
44 -- Search for relation members with role 'lable'.
45 IF relation_members IS NOT NULL THEN
47 SELECT get_rel_node_members(relation_members, ARRAY['label']) as member
49 --DEBUG: RAISE WARNING 'Found label member %', rel_member.member;
53 WHERE osm_type = 'N' and osm_id = rel_member.member
56 --DEBUG: RAISE WARNING 'Linked label member';
64 IF bnd.name ? 'name' THEN
65 bnd_name := make_standard_name(bnd.name->'name');
71 -- Search for relation members with role admin_center.
72 IF bnd.osm_type = 'R' and bnd_name is not null
73 and relation_members is not null THEN
75 SELECT get_rel_node_members(relation_members,
76 ARRAY['admin_center','admin_centre']) as member
78 --DEBUG: RAISE WARNING 'Found admin_center member %', rel_member.member;
81 WHERE osm_type = 'N' and osm_id = rel_member.member
84 -- For an admin centre we also want a name match - still not perfect,
85 -- for example 'new york, new york'
86 -- But that can be fixed by explicitly setting the label in the data
87 IF bnd_name = make_standard_name(linked_placex.name->'name')
88 AND bnd.rank_address = linked_placex.rank_address
92 --DEBUG: RAISE WARNING 'Linked admin_center';
97 -- Name searches can be done for ways as well as relations
98 IF bnd.osm_type in ('W','R') and bnd_name is not null THEN
99 --DEBUG: RAISE WARNING 'Looking for nodes with matching names';
101 SELECT placex.* from placex
102 WHERE make_standard_name(name->'name') = bnd_name
103 AND placex.rank_address = bnd.rank_address
104 AND placex.osm_type = 'N'
105 AND st_covers(geometry, placex.geometry)
107 --DEBUG: RAISE WARNING 'Found matching place node %', linkedPlacex.osm_id;
108 RETURN linked_placex;
117 CREATE OR REPLACE FUNCTION placex_insert()
125 country_code VARCHAR(2);
126 default_language VARCHAR(10);
131 --DEBUG: RAISE WARNING '% % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
133 NEW.place_id := nextval('seq_place');
134 NEW.indexed_status := 1; --STATUS_NEW
136 NEW.country_code := lower(get_country_code(NEW.geometry));
138 NEW.partition := get_partition(NEW.country_code);
139 NEW.geometry_sector := geometry_sector(NEW.partition, NEW.geometry);
141 -- copy 'name' to or from the default language (if there is a default language)
142 IF NEW.name is not null AND array_upper(akeys(NEW.name),1) > 1 THEN
143 default_language := get_country_language_code(NEW.country_code);
144 IF default_language IS NOT NULL THEN
145 IF NEW.name ? 'name' AND NOT NEW.name ? ('name:'||default_language) THEN
146 NEW.name := NEW.name || hstore(('name:'||default_language), (NEW.name -> 'name'));
147 ELSEIF NEW.name ? ('name:'||default_language) AND NOT NEW.name ? 'name' THEN
148 NEW.name := NEW.name || hstore('name', (NEW.name -> ('name:'||default_language)));
153 IF NEW.osm_type = 'X' THEN
154 -- E'X'ternal records should already be in the right format so do nothing
156 is_area := ST_GeometryType(NEW.geometry) IN ('ST_Polygon','ST_MultiPolygon');
158 IF NEW.class in ('place','boundary')
159 AND NEW.type in ('postcode','postal_code') THEN
161 IF NEW.address IS NULL OR NOT NEW.address ? 'postcode' THEN
162 -- most likely just a part of a multipolygon postcode boundary, throw it away
166 NEW.name := hstore('ref', NEW.address->'postcode');
168 SELECT * FROM get_postcode_rank(NEW.country_code, NEW.address->'postcode')
169 INTO NEW.rank_search, NEW.rank_address;
172 NEW.rank_address := 0;
174 ELSEIF NEW.class = 'boundary' AND NOT is_area THEN
176 ELSEIF NEW.class = 'boundary' AND NEW.type = 'administrative'
177 AND NEW.admin_level <= 4 AND NEW.osm_type = 'W' THEN
179 ELSEIF NEW.osm_type = 'N' AND NEW.class = 'highway' THEN
180 NEW.rank_search = 30;
181 NEW.rank_address = 0;
182 ELSEIF NEW.class = 'landuse' AND NOT is_area THEN
183 NEW.rank_search = 30;
184 NEW.rank_address = 0;
186 -- do table lookup stuff
187 IF NEW.class = 'boundary' and NEW.type = 'administrative' THEN
188 classtype = NEW.type || NEW.admin_level::TEXT;
190 classtype = NEW.type;
192 SELECT l.rank_search, l.rank_address FROM address_levels l
193 WHERE (l.country_code = NEW.country_code or l.country_code is NULL)
194 AND l.class = NEW.class AND (l.type = classtype or l.type is NULL)
195 ORDER BY l.country_code, l.class, l.type LIMIT 1
196 INTO NEW.rank_search, NEW.rank_address;
198 IF NEW.rank_search is NULL THEN
199 NEW.rank_search := 30;
202 IF NEW.rank_address is NULL THEN
203 NEW.rank_address := 30;
207 -- some postcorrections
208 IF NEW.class = 'waterway' AND NEW.osm_type = 'R' THEN
209 -- Slightly promote waterway relations so that they are processed
210 -- before their members.
211 NEW.rank_search := NEW.rank_search - 1;
214 IF (NEW.extratags -> 'capital') = 'yes' THEN
215 NEW.rank_search := NEW.rank_search - 1;
220 -- a country code make no sense below rank 4 (country)
221 IF NEW.rank_search < 4 THEN
222 NEW.country_code := NULL;
225 --DEBUG: RAISE WARNING 'placex_insert:END: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
227 RETURN NEW; -- %DIFFUPDATES% The following is not needed until doing diff updates, and slows the main index process down
229 IF NEW.osm_type = 'N' and NEW.rank_search > 28 THEN
230 -- might be part of an interpolation
231 result := osmline_reinsert(NEW.osm_id, NEW.geometry);
232 ELSEIF NEW.rank_address > 0 THEN
233 IF (ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon') AND ST_IsValid(NEW.geometry)) THEN
234 -- Performance: We just can't handle re-indexing for country level changes
235 IF st_area(NEW.geometry) < 1 THEN
236 -- mark items within the geometry for re-indexing
237 -- RAISE WARNING 'placex poly insert: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
239 -- work around bug in postgis, this may have been fixed in 2.0.0 (see http://trac.osgeo.org/postgis/ticket/547)
240 update placex set indexed_status = 2 where (st_covers(NEW.geometry, placex.geometry) OR ST_Intersects(NEW.geometry, placex.geometry))
241 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'));
242 update placex set indexed_status = 2 where (st_covers(NEW.geometry, placex.geometry) OR ST_Intersects(NEW.geometry, placex.geometry))
243 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'));
246 -- mark nearby items for re-indexing, where 'nearby' depends on the features rank_search and is a complete guess :(
248 -- 16 = city, anything higher than city is effectively ignored (polygon required!)
249 IF NEW.type='postcode' THEN
251 ELSEIF NEW.rank_search < 16 THEN
253 ELSEIF NEW.rank_search < 18 THEN
255 ELSEIF NEW.rank_search < 20 THEN
257 ELSEIF NEW.rank_search = 21 THEN
259 ELSEIF NEW.rank_search < 24 THEN
261 ELSEIF NEW.rank_search < 26 THEN
262 diameter := 0.002; -- 100 to 200 meters
263 ELSEIF NEW.rank_search < 28 THEN
264 diameter := 0.001; -- 50 to 100 meters
267 -- RAISE WARNING 'placex point insert: % % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type,diameter;
268 IF NEW.rank_search >= 26 THEN
269 -- roads may cause reparenting for >27 rank places
270 update placex set indexed_status = 2 where indexed_status = 0 and rank_search > NEW.rank_search and ST_DWithin(placex.geometry, NEW.geometry, diameter);
271 -- reparenting also for OSM Interpolation Lines (and for Tiger?)
272 update location_property_osmline set indexed_status = 2 where indexed_status = 0 and ST_DWithin(location_property_osmline.linegeo, NEW.geometry, diameter);
273 ELSEIF NEW.rank_search >= 16 THEN
274 -- up to rank 16, street-less addresses may need reparenting
275 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');
277 -- for all other places the search terms may change as well
278 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);
285 -- add to tables for special search
286 -- Note: won't work on initial import because the classtype tables
287 -- do not yet exist. It won't hurt either.
288 classtable := 'place_classtype_' || NEW.class || '_' || NEW.type;
289 SELECT count(*)>0 FROM pg_tables WHERE tablename = classtable and schemaname = current_schema() INTO result;
291 EXECUTE 'INSERT INTO ' || classtable::regclass || ' (place_id, centroid) VALUES ($1,$2)'
292 USING NEW.place_id, ST_Centroid(NEW.geometry);
302 CREATE OR REPLACE FUNCTION placex_update()
307 place_centroid GEOMETRY;
308 near_centroid GEOMETRY;
310 search_maxdistance FLOAT[];
311 search_mindistance FLOAT[];
312 address_havelevel BOOLEAN[];
319 relation_members TEXT[];
322 search_diameter FLOAT;
323 search_prevdiameter FLOAT;
324 search_maxrank INTEGER;
325 address_maxrank INTEGER;
326 address_street_word_id INTEGER;
327 address_street_word_ids INTEGER[];
328 parent_place_id_rank BIGINT;
336 location_rank_search INTEGER;
337 location_distance FLOAT;
338 location_parent GEOMETRY;
339 location_isaddress BOOLEAN;
340 location_keywords INTEGER[];
342 default_language TEXT;
343 name_vector INTEGER[];
344 nameaddress_vector INTEGER[];
346 linked_node_id BIGINT;
347 linked_importance FLOAT;
348 linked_wikipedia TEXT;
353 IF OLD.indexed_status = 100 THEN
354 --DEBUG: RAISE WARNING 'placex_update delete % %',NEW.osm_type,NEW.osm_id;
355 delete from placex where place_id = OLD.place_id;
359 IF NEW.indexed_status != 0 OR OLD.indexed_status = 0 THEN
363 --DEBUG: RAISE WARNING 'placex_update % % (%)',NEW.osm_type,NEW.osm_id,NEW.place_id;
365 NEW.indexed_date = now();
367 IF NOT %REVERSE-ONLY% THEN
368 DELETE from search_name WHERE place_id = NEW.place_id;
370 result := deleteSearchName(NEW.partition, NEW.place_id);
371 DELETE FROM place_addressline WHERE place_id = NEW.place_id;
372 result := deleteRoad(NEW.partition, NEW.place_id);
373 result := deleteLocationArea(NEW.partition, NEW.place_id, NEW.rank_search);
374 UPDATE placex set linked_place_id = null, indexed_status = 2
375 where linked_place_id = NEW.place_id;
376 -- update not necessary for osmline, cause linked_place_id does not exist
378 IF NEW.linked_place_id is not null THEN
379 --DEBUG: RAISE WARNING 'place already linked to %', NEW.linked_place_id;
383 --DEBUG: RAISE WARNING 'Copy over address tags';
384 -- housenumber is a computed field, so start with an empty value
385 NEW.housenumber := NULL;
386 IF NEW.address is not NULL THEN
387 IF NEW.address ? 'conscriptionnumber' THEN
388 i := getorcreate_housenumber_id(make_standard_name(NEW.address->'conscriptionnumber'));
389 IF NEW.address ? 'streetnumber' THEN
390 i := getorcreate_housenumber_id(make_standard_name(NEW.address->'streetnumber'));
391 NEW.housenumber := (NEW.address->'conscriptionnumber') || '/' || (NEW.address->'streetnumber');
393 NEW.housenumber := NEW.address->'conscriptionnumber';
395 ELSEIF NEW.address ? 'streetnumber' THEN
396 NEW.housenumber := NEW.address->'streetnumber';
397 i := getorcreate_housenumber_id(make_standard_name(NEW.address->'streetnumber'));
398 ELSEIF NEW.address ? 'housenumber' THEN
399 NEW.housenumber := NEW.address->'housenumber';
400 i := getorcreate_housenumber_id(make_standard_name(NEW.housenumber));
403 addr_street := NEW.address->'street';
404 addr_place := NEW.address->'place';
406 IF NEW.address ? 'postcode' and NEW.address->'postcode' not similar to '%(,|;)%' THEN
407 i := getorcreate_postcode_id(NEW.address->'postcode');
411 -- Speed up searches - just use the centroid of the feature
412 -- cheaper but less acurate
413 place_centroid := ST_PointOnSurface(NEW.geometry);
414 -- For searching near features rather use the centroid
415 near_centroid := ST_Envelope(NEW.geometry);
416 NEW.centroid := null;
417 NEW.postcode := null;
418 --DEBUG: RAISE WARNING 'Computing preliminary centroid at %',ST_AsText(place_centroid);
420 -- recalculate country and partition
421 IF NEW.rank_search = 4 AND NEW.address is not NULL AND NEW.address ? 'country' THEN
422 -- for countries, believe the mapped country code,
423 -- so that we remain in the right partition if the boundaries
425 NEW.country_code := lower(NEW.address->'country');
426 NEW.partition := get_partition(lower(NEW.country_code));
427 IF NEW.partition = 0 THEN
428 NEW.country_code := lower(get_country_code(place_centroid));
429 NEW.partition := get_partition(NEW.country_code);
432 IF NEW.rank_search >= 4 THEN
433 NEW.country_code := lower(get_country_code(place_centroid));
435 NEW.country_code := NULL;
437 NEW.partition := get_partition(NEW.country_code);
439 --DEBUG: RAISE WARNING 'Country updated: "%"', NEW.country_code;
441 -- waterway ways are linked when they are part of a relation and have the same class/type
442 IF NEW.osm_type = 'R' and NEW.class = 'waterway' THEN
443 FOR relation_members IN select members from planet_osm_rels r where r.id = NEW.osm_id and r.parts != array[]::bigint[]
445 FOR i IN 1..array_upper(relation_members, 1) BY 2 LOOP
446 IF relation_members[i+1] in ('', 'main_stream', 'side_stream') AND substring(relation_members[i],1,1) = 'w' THEN
447 --DEBUG: RAISE WARNING 'waterway parent %, child %/%', NEW.osm_id, i, relation_members[i];
448 FOR linked_node_id IN SELECT place_id FROM placex
449 WHERE osm_type = 'W' and osm_id = substring(relation_members[i],2,200)::bigint
450 and class = NEW.class and type in ('river', 'stream', 'canal', 'drain', 'ditch')
451 and ( relation_members[i+1] != 'side_stream' or NEW.name->'name' = name->'name')
453 UPDATE placex SET linked_place_id = NEW.place_id WHERE place_id = linked_node_id;
458 --DEBUG: RAISE WARNING 'Waterway processed';
461 -- What level are we searching from
462 search_maxrank := NEW.rank_search;
464 -- Thought this wasn't needed but when we add new languages to the country_name table
465 -- we need to update the existing names
466 IF NEW.name is not null AND array_upper(akeys(NEW.name),1) > 1 THEN
467 default_language := get_country_language_code(NEW.country_code);
468 IF default_language IS NOT NULL THEN
469 IF NEW.name ? 'name' AND NOT NEW.name ? ('name:'||default_language) THEN
470 NEW.name := NEW.name || hstore(('name:'||default_language), (NEW.name -> 'name'));
471 ELSEIF NEW.name ? ('name:'||default_language) AND NOT NEW.name ? 'name' THEN
472 NEW.name := NEW.name || hstore('name', (NEW.name -> ('name:'||default_language)));
476 --DEBUG: RAISE WARNING 'Local names updated';
478 -- Initialise the name vector using our name
479 name_vector := make_keywords(NEW.name);
480 nameaddress_vector := '{}'::int[];
483 address_havelevel[i] := false;
486 NEW.importance := null;
487 SELECT wikipedia, importance
488 FROM compute_importance(NEW.extratags, NEW.country_code, NEW.osm_type, NEW.osm_id)
489 INTO NEW.wikipedia,NEW.importance;
491 --DEBUG: RAISE WARNING 'Importance computed from wikipedia: %', NEW.importance;
493 -- ---------------------------------------------------------------------------
494 -- For low level elements we inherit from our parent road
495 IF (NEW.rank_search > 27 OR (NEW.type = 'postcode' AND NEW.rank_search = 25)) THEN
497 --DEBUG: RAISE WARNING 'finding street for % %', NEW.osm_type, NEW.osm_id;
499 -- We won't get a better centroid, besides these places are too small to care
500 NEW.centroid := place_centroid;
502 NEW.parent_place_id := null;
504 -- if we have a POI and there is no address information,
505 -- see if we can get it from a surrounding building
506 IF NEW.osm_type = 'N' AND addr_street IS NULL AND addr_place IS NULL
507 AND NEW.housenumber IS NULL THEN
508 FOR location IN select address from placex where ST_Covers(geometry, place_centroid)
509 and address is not null
510 and (address ? 'housenumber' or address ? 'street' or address ? 'place')
511 and rank_search > 28 AND ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon')
514 NEW.housenumber := location.address->'housenumber';
515 addr_street := location.address->'street';
516 addr_place := location.address->'place';
517 --DEBUG: RAISE WARNING 'Found surrounding building % %', location.osm_type, location.osm_id;
521 -- We have to find our parent road.
522 -- Copy data from linked items (points on ways, addr:street links, relations)
524 -- Is this object part of a relation?
525 FOR relation IN select * from planet_osm_rels where parts @> ARRAY[NEW.osm_id] and members @> ARRAY[lower(NEW.osm_type)||NEW.osm_id]
527 -- At the moment we only process one type of relation - associatedStreet
528 IF relation.tags @> ARRAY['associatedStreet'] THEN
529 FOR i IN 1..array_upper(relation.members, 1) BY 2 LOOP
530 IF NEW.parent_place_id IS NULL AND relation.members[i+1] = 'street' THEN
531 --RAISE WARNING 'node in relation %',relation;
532 SELECT place_id from placex where osm_type = 'W'
533 and osm_id = substring(relation.members[i],2,200)::bigint
534 and rank_search = 26 and name is not null INTO NEW.parent_place_id;
539 --DEBUG: RAISE WARNING 'Checked for street relation (%)', NEW.parent_place_id;
541 -- Note that addr:street links can only be indexed once the street itself is indexed
542 IF NEW.parent_place_id IS NULL AND addr_street IS NOT NULL THEN
543 address_street_word_ids := get_name_ids(make_standard_name(addr_street));
544 IF address_street_word_ids IS NOT NULL THEN
545 SELECT place_id from getNearestNamedRoadFeature(NEW.partition, near_centroid, address_street_word_ids) INTO NEW.parent_place_id;
548 --DEBUG: RAISE WARNING 'Checked for addr:street (%)', NEW.parent_place_id;
550 IF NEW.parent_place_id IS NULL AND addr_place IS NOT NULL THEN
551 address_street_word_ids := get_name_ids(make_standard_name(addr_place));
552 IF address_street_word_ids IS NOT NULL THEN
553 SELECT place_id from getNearestNamedPlaceFeature(NEW.partition, near_centroid, address_street_word_ids) INTO NEW.parent_place_id;
556 --DEBUG: RAISE WARNING 'Checked for addr:place (%)', NEW.parent_place_id;
558 -- Is this node part of an interpolation?
559 IF NEW.parent_place_id IS NULL AND NEW.osm_type = 'N' THEN
560 SELECT q.parent_place_id FROM location_property_osmline q, planet_osm_ways x
561 WHERE q.linegeo && NEW.geometry and x.id = q.osm_id and NEW.osm_id = any(x.nodes)
562 LIMIT 1 INTO NEW.parent_place_id;
564 --DEBUG: RAISE WARNING 'Checked for interpolation (%)', NEW.parent_place_id;
566 -- Is this node part of a way?
567 IF NEW.parent_place_id IS NULL AND NEW.osm_type = 'N' THEN
570 SELECT p.place_id, p.osm_id, p.rank_search, p.address from placex p, planet_osm_ways w
571 WHERE p.osm_type = 'W' and p.rank_search >= 26 and p.geometry && NEW.geometry and w.id = p.osm_id and NEW.osm_id = any(w.nodes)
573 --DEBUG: RAISE WARNING 'Node is part of way % ', location.osm_id;
575 -- Way IS a road then we are on it - that must be our road
576 IF location.rank_search < 28 THEN
577 --RAISE WARNING 'node in way that is a street %',location;
578 NEW.parent_place_id := location.place_id;
581 --DEBUG: RAISE WARNING 'Checked if way is street (%)', NEW.parent_place_id;
583 -- If the way mentions a street or place address, try that for parenting.
584 IF location.address is not null THEN
585 IF location.address ? 'street' THEN
586 address_street_word_ids := get_name_ids(make_standard_name(location.address->'street'));
587 IF address_street_word_ids IS NOT NULL THEN
588 SELECT place_id from getNearestNamedRoadFeature(NEW.partition, near_centroid, address_street_word_ids) INTO NEW.parent_place_id;
589 EXIT WHEN NEW.parent_place_id is not NULL;
592 --DEBUG: RAISE WARNING 'Checked for addr:street in way (%)', NEW.parent_place_id;
594 IF location.address ? 'place' THEN
595 address_street_word_ids := get_name_ids(make_standard_name(location.address->'place'));
596 IF address_street_word_ids IS NOT NULL THEN
597 SELECT place_id from getNearestNamedPlaceFeature(NEW.partition, near_centroid, address_street_word_ids) INTO NEW.parent_place_id;
598 EXIT WHEN NEW.parent_place_id is not NULL;
601 --DEBUG: RAISE WARNING 'Checked for addr:place in way (%)', NEW.parent_place_id;
604 -- Is the WAY part of a relation
605 FOR relation IN select * from planet_osm_rels where parts @> ARRAY[location.osm_id] and members @> ARRAY['w'||location.osm_id]
607 -- At the moment we only process one type of relation - associatedStreet
608 IF relation.tags @> ARRAY['associatedStreet'] AND array_upper(relation.members, 1) IS NOT NULL THEN
609 FOR i IN 1..array_upper(relation.members, 1) BY 2 LOOP
610 IF NEW.parent_place_id IS NULL AND relation.members[i+1] = 'street' THEN
611 --RAISE WARNING 'node in way that is in a relation %',relation;
612 SELECT place_id from placex where osm_type='W' and osm_id = substring(relation.members[i],2,200)::bigint
613 and rank_search = 26 and name is not null INTO NEW.parent_place_id;
618 EXIT WHEN NEW.parent_place_id is not null;
619 --DEBUG: RAISE WARNING 'Checked for street relation in way (%)', NEW.parent_place_id;
624 -- Still nothing, just use the nearest road
625 IF NEW.parent_place_id IS NULL THEN
626 SELECT place_id FROM getNearestRoadFeature(NEW.partition, near_centroid) INTO NEW.parent_place_id;
628 --DEBUG: RAISE WARNING 'Checked for nearest way (%)', NEW.parent_place_id;
631 -- If we didn't find any road fallback to standard method
632 IF NEW.parent_place_id IS NOT NULL THEN
634 -- Get the details of the parent road
635 SELECT p.country_code, p.postcode FROM placex p
636 WHERE p.place_id = NEW.parent_place_id INTO location;
638 NEW.country_code := location.country_code;
639 --DEBUG: RAISE WARNING 'Got parent details from search name';
641 -- determine postcode
642 IF NEW.rank_search > 4 THEN
643 IF NEW.address is not null AND NEW.address ? 'postcode' THEN
644 NEW.postcode = upper(trim(NEW.address->'postcode'));
646 NEW.postcode := location.postcode;
648 IF NEW.postcode is null THEN
649 NEW.postcode := get_nearest_postcode(NEW.country_code, NEW.geometry);
653 -- If there is no name it isn't searchable, don't bother to create a search record
654 IF NEW.name is NULL THEN
655 --DEBUG: RAISE WARNING 'Not a searchable place % %', NEW.osm_type, NEW.osm_id;
659 -- Performance, it would be more acurate to do all the rest of the import
660 -- process but it takes too long
661 -- Just be happy with inheriting from parent road only
662 IF NEW.rank_search <= 25 and NEW.rank_address > 0 THEN
663 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);
664 --DEBUG: RAISE WARNING 'Place added to location table';
667 result := insertSearchName(NEW.partition, NEW.place_id, name_vector,
668 NEW.rank_search, NEW.rank_address, NEW.geometry);
670 IF NOT %REVERSE-ONLY% THEN
671 -- Merge address from parent
672 SELECT s.name_vector, s.nameaddress_vector FROM search_name s
673 WHERE s.place_id = NEW.parent_place_id INTO location;
675 nameaddress_vector := array_merge(nameaddress_vector,
676 location.nameaddress_vector);
677 nameaddress_vector := array_merge(nameaddress_vector, location.name_vector);
679 INSERT INTO search_name (place_id, search_rank, address_rank,
680 importance, country_code, name_vector,
681 nameaddress_vector, centroid)
682 VALUES (NEW.place_id, NEW.rank_search, NEW.rank_address,
683 NEW.importance, NEW.country_code, name_vector,
684 nameaddress_vector, place_centroid);
685 --DEBUG: RAISE WARNING 'Place added to search table';
693 -- ---------------------------------------------------------------------------
695 --DEBUG: RAISE WARNING 'Using full index mode for % %', NEW.osm_type, NEW.osm_id;
697 FOR linkedPlacex IN SELECT * FROM find_linked_place(NEW) LOOP
698 -- If we don't already have one use this as the centre point of the geometry
699 IF NEW.centroid IS NULL THEN
700 --DEBUG: RAISE WARNING 'Linked %', linkedPlacex;
701 NEW.centroid := coalesce(linkedPlacex.centroid,
702 ST_Centroid(linkedPlacex.geometry));
704 place_centroid := NEW.centroid;
706 -- merge in the label name, re-init word vector
707 IF NOT linkedPlacex.name IS NULL THEN
708 NEW.name := linkedPlacex.name || NEW.name;
709 name_vector := array_merge(name_vector, make_keywords(linkedPlacex.name));
711 -- Place might have had only a name tag before but has now received
712 -- translations from the linked place. Make sure a name tag for the
713 -- default language exists in this case.
714 IF array_upper(akeys(NEW.name), 1) > 1 THEN
715 default_language := get_country_language_code(NEW.country_code);
716 IF default_language IS NOT NULL THEN
717 IF NEW.name ? 'name' AND NOT NEW.name ? ('name:'||default_language) THEN
718 NEW.name := NEW.name || hstore(('name:'||default_language), (NEW.name -> 'name'));
719 ELSEIF NEW.name ? ('name:'||default_language) AND NOT NEW.name ? 'name' THEN
720 NEW.name := NEW.name || hstore('name', (NEW.name -> ('name:'||default_language)));
726 -- merge in extra tags
727 NEW.extratags := hstore(linkedPlacex.class, linkedPlacex.type)
728 || coalesce(linkedPlacex.extratags, ''::hstore)
729 || coalesce(NEW.extratags, ''::hstore);
731 -- mark the linked place (excludes from search results)
732 UPDATE placex set linked_place_id = NEW.place_id
733 WHERE place_id = linkedPlacex.place_id;
735 SELECT wikipedia, importance
736 FROM compute_importance(linkedPlacex.extratags, NEW.country_code,
737 'N', linkedPlacex.osm_id)
738 INTO linked_wikipedia,linked_importance;
740 -- Use the maximum importance if a one could be computed from the linked object.
741 IF linked_importance is not null AND
742 (NEW.importance is null or NEW.importance < linked_importance)
744 NEW.importance = linked_importance;
749 -- make sure all names are in the word table
750 IF NEW.admin_level = 2 AND NEW.class = 'boundary' AND NEW.type = 'administrative' AND NEW.country_code IS NOT NULL AND NEW.osm_type = 'R' THEN
751 perform create_country(NEW.name, lower(NEW.country_code));
752 --DEBUG: RAISE WARNING 'Country names updated';
755 NEW.parent_place_id = 0;
756 parent_place_id_rank = 0;
759 -- convert address store to array of tokenids
760 --DEBUG: RAISE WARNING 'Starting address search';
761 isin_tokens := '{}'::int[];
762 IF NEW.address IS NOT NULL THEN
763 FOR addr_item IN SELECT * FROM each(NEW.address)
765 IF addr_item.key IN ('city', 'tiger:county', 'state', 'suburb', 'province', 'district', 'region', 'county', 'municipality', 'hamlet', 'village', 'subdistrict', 'town', 'neighbourhood', 'quarter', 'parish') THEN
766 address_street_word_id := get_name_id(make_standard_name(addr_item.value));
767 IF address_street_word_id IS NOT NULL AND NOT(ARRAY[address_street_word_id] <@ isin_tokens) THEN
768 isin_tokens := isin_tokens || address_street_word_id;
770 IF NOT %REVERSE-ONLY% THEN
771 address_street_word_id := get_word_id(make_standard_name(addr_item.value));
772 IF address_street_word_id IS NOT NULL THEN
773 nameaddress_vector := array_merge(nameaddress_vector, ARRAY[address_street_word_id]);
777 IF addr_item.key = 'is_in' THEN
778 -- is_in items need splitting
779 isin := regexp_split_to_array(addr_item.value, E'[;,]');
780 IF array_upper(isin, 1) IS NOT NULL THEN
781 FOR i IN 1..array_upper(isin, 1) LOOP
782 address_street_word_id := get_name_id(make_standard_name(isin[i]));
783 IF address_street_word_id IS NOT NULL AND NOT(ARRAY[address_street_word_id] <@ isin_tokens) THEN
784 isin_tokens := isin_tokens || address_street_word_id;
787 -- merge word into address vector
788 IF NOT %REVERSE-ONLY% THEN
789 address_street_word_id := get_word_id(make_standard_name(isin[i]));
790 IF address_street_word_id IS NOT NULL THEN
791 nameaddress_vector := array_merge(nameaddress_vector, ARRAY[address_street_word_id]);
799 IF NOT %REVERSE-ONLY% THEN
800 nameaddress_vector := array_merge(nameaddress_vector, isin_tokens);
803 -- RAISE WARNING 'ISIN: %', isin_tokens;
805 -- Process area matches
806 location_rank_search := 0;
807 location_distance := 0;
808 location_parent := NULL;
809 -- added ourself as address already
810 address_havelevel[NEW.rank_address] := true;
811 --DEBUG: RAISE WARNING ' getNearFeatures(%,''%'',%,''%'')',NEW.partition, place_centroid, search_maxrank, isin_tokens;
813 SELECT * from getNearFeatures(NEW.partition,
814 CASE WHEN NEW.rank_search >= 26
815 AND NEW.rank_search < 30
817 ELSE place_centroid END,
818 search_maxrank, isin_tokens)
820 IF location.rank_address != location_rank_search THEN
821 location_rank_search := location.rank_address;
822 IF location.isguess THEN
823 location_distance := location.distance * 1.5;
825 IF location.rank_address <= 12 THEN
826 -- for county and above, if we have an area consider that exact
827 -- (It would be nice to relax the constraint for places close to
828 -- the boundary but we'd need the exact geometry for that. Too
830 location_distance = 0;
832 -- Below county level remain slightly fuzzy.
833 location_distance := location.distance * 0.5;
837 CONTINUE WHEN location.keywords <@ location_keywords;
840 IF location.distance < location_distance OR NOT location.isguess THEN
841 location_keywords := location.keywords;
843 location_isaddress := NOT address_havelevel[location.rank_address];
844 IF location_isaddress AND location.isguess AND location_parent IS NOT NULL THEN
845 location_isaddress := ST_Contains(location_parent,location.centroid);
848 -- RAISE WARNING '% isaddress: %', location.place_id, location_isaddress;
849 -- Add it to the list of search terms
850 IF NOT %REVERSE-ONLY% THEN
851 nameaddress_vector := array_merge(nameaddress_vector, location.keywords::integer[]);
853 INSERT INTO place_addressline (place_id, address_place_id, fromarea, isaddress, distance, cached_rank_address)
854 VALUES (NEW.place_id, location.place_id, true, location_isaddress, location.distance, location.rank_address);
856 IF location_isaddress THEN
857 -- add postcode if we have one
858 -- (If multiple postcodes are available, we end up with the highest ranking one.)
859 IF location.postcode is not null THEN
860 NEW.postcode = location.postcode;
863 address_havelevel[location.rank_address] := true;
864 IF NOT location.isguess THEN
865 SELECT geometry FROM placex WHERE place_id = location.place_id INTO location_parent;
868 IF location.rank_address > parent_place_id_rank THEN
869 NEW.parent_place_id = location.place_id;
870 parent_place_id_rank = location.rank_address;
875 --DEBUG: RAISE WARNING ' Terms: (%) %',location, nameaddress_vector;
880 --DEBUG: RAISE WARNING 'address computed';
882 IF NEW.address is not null AND NEW.address ? 'postcode'
883 AND NEW.address->'postcode' not similar to '%(,|;)%' THEN
884 NEW.postcode := upper(trim(NEW.address->'postcode'));
887 IF NEW.postcode is null AND NEW.rank_search > 8 THEN
888 NEW.postcode := get_nearest_postcode(NEW.country_code, NEW.geometry);
891 -- if we have a name add this to the name search table
892 IF NEW.name IS NOT NULL THEN
894 IF NEW.rank_search <= 25 and NEW.rank_address > 0 THEN
895 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);
896 --DEBUG: RAISE WARNING 'added to location (full)';
899 IF NEW.rank_search between 26 and 27 and NEW.class = 'highway' THEN
900 result := insertLocationRoad(NEW.partition, NEW.place_id, NEW.country_code, NEW.geometry);
901 --DEBUG: RAISE WARNING 'insert into road location table (full)';
904 result := insertSearchName(NEW.partition, NEW.place_id, name_vector,
905 NEW.rank_search, NEW.rank_address, NEW.geometry);
906 --DEBUG: RAISE WARNING 'added to search name (full)';
908 IF NOT %REVERSE-ONLY% THEN
909 INSERT INTO search_name (place_id, search_rank, address_rank,
910 importance, country_code, name_vector,
911 nameaddress_vector, centroid)
912 VALUES (NEW.place_id, NEW.rank_search, NEW.rank_address,
913 NEW.importance, NEW.country_code, name_vector,
914 nameaddress_vector, place_centroid);
919 -- If we've not managed to pick up a better one - default centroid
920 IF NEW.centroid IS NULL THEN
921 NEW.centroid := place_centroid;
924 --DEBUG: RAISE WARNING 'place update % % finsihed.', NEW.osm_type, NEW.osm_id;
932 CREATE OR REPLACE FUNCTION placex_delete()
939 -- RAISE WARNING 'placex_delete % %',OLD.osm_type,OLD.osm_id;
941 update placex set linked_place_id = null, indexed_status = 2 where linked_place_id = OLD.place_id and indexed_status = 0;
942 --DEBUG: RAISE WARNING 'placex_delete:01 % %',OLD.osm_type,OLD.osm_id;
943 update placex set linked_place_id = null where linked_place_id = OLD.place_id;
944 --DEBUG: RAISE WARNING 'placex_delete:02 % %',OLD.osm_type,OLD.osm_id;
946 IF OLD.rank_address < 30 THEN
948 -- mark everything linked to this place for re-indexing
949 --DEBUG: RAISE WARNING 'placex_delete:03 % %',OLD.osm_type,OLD.osm_id;
950 UPDATE placex set indexed_status = 2 from place_addressline where address_place_id = OLD.place_id
951 and placex.place_id = place_addressline.place_id and indexed_status = 0 and place_addressline.isaddress;
953 --DEBUG: RAISE WARNING 'placex_delete:04 % %',OLD.osm_type,OLD.osm_id;
954 DELETE FROM place_addressline where address_place_id = OLD.place_id;
956 --DEBUG: RAISE WARNING 'placex_delete:05 % %',OLD.osm_type,OLD.osm_id;
957 b := deleteRoad(OLD.partition, OLD.place_id);
959 --DEBUG: RAISE WARNING 'placex_delete:06 % %',OLD.osm_type,OLD.osm_id;
960 update placex set indexed_status = 2 where parent_place_id = OLD.place_id and indexed_status = 0;
961 --DEBUG: RAISE WARNING 'placex_delete:07 % %',OLD.osm_type,OLD.osm_id;
962 -- reparenting also for OSM Interpolation Lines (and for Tiger?)
963 update location_property_osmline set indexed_status = 2 where indexed_status = 0 and parent_place_id = OLD.place_id;
967 --DEBUG: RAISE WARNING 'placex_delete:08 % %',OLD.osm_type,OLD.osm_id;
969 IF OLD.rank_address < 26 THEN
970 b := deleteLocationArea(OLD.partition, OLD.place_id, OLD.rank_search);
973 --DEBUG: RAISE WARNING 'placex_delete:09 % %',OLD.osm_type,OLD.osm_id;
975 IF OLD.name is not null THEN
976 IF NOT %REVERSE-ONLY% THEN
977 DELETE from search_name WHERE place_id = OLD.place_id;
979 b := deleteSearchName(OLD.partition, OLD.place_id);
982 --DEBUG: RAISE WARNING 'placex_delete:10 % %',OLD.osm_type,OLD.osm_id;
984 DELETE FROM place_addressline where place_id = OLD.place_id;
986 --DEBUG: RAISE WARNING 'placex_delete:11 % %',OLD.osm_type,OLD.osm_id;
988 -- remove from tables for special search
989 classtable := 'place_classtype_' || OLD.class || '_' || OLD.type;
990 SELECT count(*)>0 FROM pg_tables WHERE tablename = classtable and schemaname = current_schema() INTO b;
992 EXECUTE 'DELETE FROM ' || classtable::regclass || ' WHERE place_id = $1' USING OLD.place_id;
995 --DEBUG: RAISE WARNING 'placex_delete:12 % %',OLD.osm_type,OLD.osm_id;