1 CREATE OR REPLACE FUNCTION geometry_sector(partition INTEGER, place geometry) RETURNS INTEGER
6 -- RAISE WARNING '%',place;
7 NEWgeometry := ST_PointOnSurface(place);
8 RETURN (partition*1000000) + (500-ST_X(NEWgeometry)::integer)*1000 + (500-ST_Y(NEWgeometry)::integer);
11 LANGUAGE plpgsql IMMUTABLE;
14 CREATE OR REPLACE FUNCTION array_merge(a INTEGER[], b INTEGER[])
21 IF array_upper(a, 1) IS NULL THEN
24 IF array_upper(b, 1) IS NULL THEN
28 FOR i IN 1..array_upper(b, 1) LOOP
29 IF NOT (ARRAY[b[i]] <@ r) THEN
36 LANGUAGE plpgsql IMMUTABLE;
38 CREATE OR REPLACE FUNCTION reverse_place_diameter(rank_search SMALLINT)
42 IF rank_search <= 4 THEN
44 ELSIF rank_search <= 8 THEN
46 ELSIF rank_search <= 12 THEN
48 ELSIF rank_search <= 17 THEN
50 ELSIF rank_search <= 18 THEN
52 ELSIF rank_search <= 19 THEN
59 LANGUAGE plpgsql IMMUTABLE;
61 CREATE OR REPLACE FUNCTION get_postcode_rank(country_code VARCHAR(2), postcode TEXT,
62 OUT rank_search SMALLINT, OUT rank_address SMALLINT)
69 postcode := upper(postcode);
71 IF country_code = 'gb' THEN
72 IF postcode ~ '^([A-Z][A-Z]?[0-9][0-9A-Z]? [0-9][A-Z][A-Z])$' THEN
75 ELSEIF postcode ~ '^([A-Z][A-Z]?[0-9][0-9A-Z]? [0-9])$' THEN
78 ELSEIF postcode ~ '^([A-Z][A-Z]?[0-9][0-9A-Z])$' THEN
83 ELSEIF country_code = 'sg' THEN
84 IF postcode ~ '^([0-9]{6})$' THEN
89 ELSEIF country_code = 'de' THEN
90 IF postcode ~ '^([0-9]{5})$' THEN
96 -- Guess at the postcode format and coverage (!)
97 IF postcode ~ '^[A-Z0-9]{1,5}$' THEN -- Probably too short to be very local
101 -- Does it look splitable into and area and local code?
102 part := substring(postcode from '^([- :A-Z0-9]+)([- :][A-Z0-9]+)$');
104 IF part IS NOT NULL THEN
107 ELSEIF postcode ~ '^[- :A-Z0-9]{6,}$' THEN
116 LANGUAGE plpgsql IMMUTABLE;
118 -- Find the nearest artificial postcode for the given geometry.
119 -- TODO For areas there should not be more than two inside the geometry.
120 CREATE OR REPLACE FUNCTION get_nearest_postcode(country VARCHAR(2), geom GEOMETRY) RETURNS TEXT
126 -- If the geometry is an area then only one postcode must be within
127 -- that area, otherwise consider the area as not having a postcode.
128 IF ST_GeometryType(geom) in ('ST_Polygon','ST_MultiPolygon') THEN
129 SELECT min(postcode), count(*) FROM
130 (SELECT postcode FROM location_postcode
131 WHERE ST_Contains(geom, location_postcode.geometry) LIMIT 2) sub
141 SELECT postcode FROM location_postcode
142 WHERE ST_DWithin(geom, location_postcode.geometry, 0.05)
143 AND location_postcode.country_code = country
144 ORDER BY ST_Distance(geom, location_postcode.geometry) LIMIT 1
153 CREATE OR REPLACE FUNCTION get_country_code(place geometry) RETURNS TEXT
156 place_centre GEOMETRY;
159 place_centre := ST_PointOnSurface(place);
161 -- RAISE WARNING 'get_country_code, start: %', ST_AsText(place_centre);
163 -- Try for a OSM polygon
164 FOR nearcountry IN select country_code from location_area_country where country_code is not null and st_covers(geometry, place_centre) limit 1
166 RETURN nearcountry.country_code;
169 -- RAISE WARNING 'osm fallback: %', ST_AsText(place_centre);
171 -- Try for OSM fallback data
172 -- The order is to deal with places like HongKong that are 'states' within another polygon
173 FOR nearcountry IN select country_code from country_osm_grid where st_covers(geometry, place_centre) order by area asc limit 1
175 RETURN nearcountry.country_code;
178 -- RAISE WARNING 'near osm fallback: %', ST_AsText(place_centre);
181 FOR nearcountry IN select country_code from country_osm_grid where st_dwithin(geometry, place_centre, 0.5) order by st_distance(geometry, place_centre) asc, area asc limit 1
183 RETURN nearcountry.country_code;
189 LANGUAGE plpgsql IMMUTABLE;
191 CREATE OR REPLACE FUNCTION get_country_language_code(search_country_code VARCHAR(2)) RETURNS TEXT
196 FOR nearcountry IN select distinct country_default_language_code from country_name where country_code = search_country_code limit 1
198 RETURN lower(nearcountry.country_default_language_code);
203 LANGUAGE plpgsql IMMUTABLE;
205 CREATE OR REPLACE FUNCTION get_country_language_codes(search_country_code VARCHAR(2)) RETURNS TEXT[]
210 FOR nearcountry IN select country_default_language_codes from country_name where country_code = search_country_code limit 1
212 RETURN lower(nearcountry.country_default_language_codes);
217 LANGUAGE plpgsql IMMUTABLE;
219 CREATE OR REPLACE FUNCTION get_partition(in_country_code VARCHAR(10)) RETURNS INTEGER
224 FOR nearcountry IN select partition from country_name where country_code = in_country_code
226 RETURN nearcountry.partition;
231 LANGUAGE plpgsql IMMUTABLE;
233 CREATE OR REPLACE FUNCTION delete_location(OLD_place_id BIGINT) RETURNS BOOLEAN
237 DELETE FROM location_area where place_id = OLD_place_id;
238 -- TODO:location_area
244 CREATE OR REPLACE FUNCTION add_location(
246 country_code varchar(2),
250 rank_address INTEGER,
266 IF rank_search > 25 THEN
267 RAISE EXCEPTION 'Adding location with rank > 25 (% rank %)', place_id, rank_search;
270 x := deleteLocationArea(partition, place_id, rank_search);
272 -- add postcode only if it contains a single entry, i.e. ignore postcode lists
274 IF in_postcode is not null AND in_postcode not similar to '%(,|;)%' THEN
275 postcode := upper(trim (in_postcode));
278 IF ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') THEN
279 centroid := ST_Centroid(geometry);
281 FOR secgeo IN select split_geometry(geometry) AS geom LOOP
282 x := insertLocationAreaLarge(partition, place_id, country_code, keywords, rank_search, rank_address, false, postcode, centroid, secgeo);
288 IF rank_address = 0 THEN
290 ELSEIF rank_search <= 14 THEN
292 ELSEIF rank_search <= 15 THEN
294 ELSEIF rank_search <= 16 THEN
296 ELSEIF rank_search <= 17 THEN
298 ELSEIF rank_search <= 21 THEN
300 ELSEIF rank_search = 25 THEN
304 -- RAISE WARNING 'adding % diameter %', place_id, diameter;
306 secgeo := ST_Buffer(geometry, diameter);
307 x := insertLocationAreaLarge(partition, place_id, country_code, keywords, rank_search, rank_address, true, postcode, ST_Centroid(geometry), secgeo);
317 CREATE OR REPLACE FUNCTION placex_insert() RETURNS TRIGGER
324 country_code VARCHAR(2);
325 default_language VARCHAR(10);
330 --DEBUG: RAISE WARNING '% % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
332 NEW.place_id := nextval('seq_place');
333 NEW.indexed_status := 1; --STATUS_NEW
335 NEW.country_code := lower(get_country_code(NEW.geometry));
337 NEW.partition := get_partition(NEW.country_code);
338 NEW.geometry_sector := geometry_sector(NEW.partition, NEW.geometry);
340 -- copy 'name' to or from the default language (if there is a default language)
341 IF NEW.name is not null AND array_upper(akeys(NEW.name),1) > 1 THEN
342 default_language := get_country_language_code(NEW.country_code);
343 IF default_language IS NOT NULL THEN
344 IF NEW.name ? 'name' AND NOT NEW.name ? ('name:'||default_language) THEN
345 NEW.name := NEW.name || hstore(('name:'||default_language), (NEW.name -> 'name'));
346 ELSEIF NEW.name ? ('name:'||default_language) AND NOT NEW.name ? 'name' THEN
347 NEW.name := NEW.name || hstore('name', (NEW.name -> ('name:'||default_language)));
352 IF NEW.osm_type = 'X' THEN
353 -- E'X'ternal records should already be in the right format so do nothing
355 is_area := ST_GeometryType(NEW.geometry) IN ('ST_Polygon','ST_MultiPolygon');
357 IF NEW.class in ('place','boundary')
358 AND NEW.type in ('postcode','postal_code') THEN
360 IF NEW.address IS NULL OR NOT NEW.address ? 'postcode' THEN
361 -- most likely just a part of a multipolygon postcode boundary, throw it away
365 NEW.name := hstore('ref', NEW.address->'postcode');
367 SELECT * FROM get_postcode_rank(NEW.country_code, NEW.address->'postcode')
368 INTO NEW.rank_search, NEW.rank_address;
371 NEW.rank_address := 0;
373 ELSEIF NEW.class = 'boundary' AND NOT is_area THEN
375 ELSEIF NEW.class = 'boundary' AND NEW.type = 'administrative'
376 AND NEW.admin_level <= 4 AND NEW.osm_type = 'W' THEN
378 ELSEIF NEW.class = 'railway' AND NEW.type in ('rail') THEN
380 ELSEIF NEW.osm_type = 'N' AND NEW.class = 'highway' THEN
381 NEW.rank_search = 30;
382 NEW.rank_address = 0;
383 ELSEIF NEW.class = 'landuse' AND NOT is_area THEN
384 NEW.rank_search = 30;
385 NEW.rank_address = 0;
387 -- do table lookup stuff
388 IF NEW.class = 'boundary' and NEW.type = 'administrative' THEN
389 classtype = NEW.type || NEW.admin_level::TEXT;
391 classtype = NEW.type;
393 SELECT l.rank_search, l.rank_address FROM address_levels l
394 WHERE (l.country_code = NEW.country_code or l.country_code is NULL)
395 AND l.class = NEW.class AND (l.type = classtype or l.type is NULL)
396 ORDER BY l.country_code, l.class, l.type LIMIT 1
397 INTO NEW.rank_search, NEW.rank_address;
399 IF NEW.rank_search is NULL THEN
400 NEW.rank_search := 30;
403 IF NEW.rank_address is NULL THEN
404 NEW.rank_address := 30;
408 -- some postcorrections
409 IF NEW.class = 'waterway' AND NEW.osm_type = 'R' THEN
410 -- Slightly promote waterway relations so that they are processed
411 -- before their members.
412 NEW.rank_search := NEW.rank_search - 1;
415 IF (NEW.extratags -> 'capital') = 'yes' THEN
416 NEW.rank_search := NEW.rank_search - 1;
421 -- a country code make no sense below rank 4 (country)
422 IF NEW.rank_search < 4 THEN
423 NEW.country_code := NULL;
426 --DEBUG: RAISE WARNING 'placex_insert:END: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
428 RETURN NEW; -- %DIFFUPDATES% The following is not needed until doing diff updates, and slows the main index process down
430 IF NEW.osm_type = 'N' and NEW.rank_search > 28 THEN
431 -- might be part of an interpolation
432 result := osmline_reinsert(NEW.osm_id, NEW.geometry);
433 ELSEIF NEW.rank_address > 0 THEN
434 IF (ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon') AND ST_IsValid(NEW.geometry)) THEN
435 -- Performance: We just can't handle re-indexing for country level changes
436 IF st_area(NEW.geometry) < 1 THEN
437 -- mark items within the geometry for re-indexing
438 -- RAISE WARNING 'placex poly insert: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
440 -- work around bug in postgis, this may have been fixed in 2.0.0 (see http://trac.osgeo.org/postgis/ticket/547)
441 update placex set indexed_status = 2 where (st_covers(NEW.geometry, placex.geometry) OR ST_Intersects(NEW.geometry, placex.geometry))
442 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'));
443 update placex set indexed_status = 2 where (st_covers(NEW.geometry, placex.geometry) OR ST_Intersects(NEW.geometry, placex.geometry))
444 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'));
447 -- mark nearby items for re-indexing, where 'nearby' depends on the features rank_search and is a complete guess :(
449 -- 16 = city, anything higher than city is effectively ignored (polygon required!)
450 IF NEW.type='postcode' THEN
452 ELSEIF NEW.rank_search < 16 THEN
454 ELSEIF NEW.rank_search < 18 THEN
456 ELSEIF NEW.rank_search < 20 THEN
458 ELSEIF NEW.rank_search = 21 THEN
460 ELSEIF NEW.rank_search < 24 THEN
462 ELSEIF NEW.rank_search < 26 THEN
463 diameter := 0.002; -- 100 to 200 meters
464 ELSEIF NEW.rank_search < 28 THEN
465 diameter := 0.001; -- 50 to 100 meters
468 -- RAISE WARNING 'placex point insert: % % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type,diameter;
469 IF NEW.rank_search >= 26 THEN
470 -- roads may cause reparenting for >27 rank places
471 update placex set indexed_status = 2 where indexed_status = 0 and rank_search > NEW.rank_search and ST_DWithin(placex.geometry, NEW.geometry, diameter);
472 -- reparenting also for OSM Interpolation Lines (and for Tiger?)
473 update location_property_osmline set indexed_status = 2 where indexed_status = 0 and ST_DWithin(location_property_osmline.linegeo, NEW.geometry, diameter);
474 ELSEIF NEW.rank_search >= 16 THEN
475 -- up to rank 16, street-less addresses may need reparenting
476 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');
478 -- for all other places the search terms may change as well
479 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);
486 -- add to tables for special search
487 -- Note: won't work on initial import because the classtype tables
488 -- do not yet exist. It won't hurt either.
489 classtable := 'place_classtype_' || NEW.class || '_' || NEW.type;
490 SELECT count(*)>0 FROM pg_tables WHERE tablename = classtable and schemaname = current_schema() INTO result;
492 EXECUTE 'INSERT INTO ' || classtable::regclass || ' (place_id, centroid) VALUES ($1,$2)'
493 USING NEW.place_id, ST_Centroid(NEW.geometry);
502 -- Trigger for updates of location_postcode
504 -- Computes the parent object the postcode most likely refers to.
505 -- This will be the place that determines the address displayed when
506 -- searching for this postcode.
507 CREATE OR REPLACE FUNCTION postcode_update() RETURNS
514 IF NEW.indexed_status != 0 OR OLD.indexed_status = 0 THEN
518 NEW.indexed_date = now();
520 partition := get_partition(NEW.country_code);
522 SELECT * FROM get_postcode_rank(NEW.country_code, NEW.postcode)
523 INTO NEW.rank_search, NEW.rank_address;
525 NEW.parent_place_id = 0;
528 FROM getNearFeatures(partition, NEW.geometry, NEW.rank_search, '{}'::int[])
529 WHERE NOT isguess ORDER BY rank_address DESC LIMIT 1
531 NEW.parent_place_id = location.place_id;
539 CREATE OR REPLACE FUNCTION placex_update() RETURNS
544 place_centroid GEOMETRY;
545 near_centroid GEOMETRY;
547 search_maxdistance FLOAT[];
548 search_mindistance FLOAT[];
549 address_havelevel BOOLEAN[];
556 relation_members TEXT[];
560 search_diameter FLOAT;
561 search_prevdiameter FLOAT;
562 search_maxrank INTEGER;
563 address_maxrank INTEGER;
564 address_street_word_id INTEGER;
565 address_street_word_ids INTEGER[];
566 parent_place_id_rank BIGINT;
574 location_rank_search INTEGER;
575 location_distance FLOAT;
576 location_parent GEOMETRY;
577 location_isaddress BOOLEAN;
578 location_keywords INTEGER[];
580 default_language TEXT;
581 name_vector INTEGER[];
582 nameaddress_vector INTEGER[];
584 linked_node_id BIGINT;
585 linked_importance FLOAT;
586 linked_wikipedia TEXT;
591 IF OLD.indexed_status = 100 THEN
592 --DEBUG: RAISE WARNING 'placex_update delete % %',NEW.osm_type,NEW.osm_id;
593 delete from placex where place_id = OLD.place_id;
597 IF NEW.indexed_status != 0 OR OLD.indexed_status = 0 THEN
601 --DEBUG: RAISE WARNING 'placex_update % % (%)',NEW.osm_type,NEW.osm_id,NEW.place_id;
603 NEW.indexed_date = now();
605 IF NOT %REVERSE-ONLY% THEN
606 DELETE from search_name WHERE place_id = NEW.place_id;
608 result := deleteSearchName(NEW.partition, NEW.place_id);
609 DELETE FROM place_addressline WHERE place_id = NEW.place_id;
610 result := deleteRoad(NEW.partition, NEW.place_id);
611 result := deleteLocationArea(NEW.partition, NEW.place_id, NEW.rank_search);
612 UPDATE placex set linked_place_id = null, indexed_status = 2
613 where linked_place_id = NEW.place_id;
614 -- update not necessary for osmline, cause linked_place_id does not exist
616 IF NEW.linked_place_id is not null THEN
617 --DEBUG: RAISE WARNING 'place already linked to %', NEW.linked_place_id;
621 --DEBUG: RAISE WARNING 'Copy over address tags';
622 -- housenumber is a computed field, so start with an empty value
623 NEW.housenumber := NULL;
624 IF NEW.address is not NULL THEN
625 IF NEW.address ? 'conscriptionnumber' THEN
626 i := getorcreate_housenumber_id(make_standard_name(NEW.address->'conscriptionnumber'));
627 IF NEW.address ? 'streetnumber' THEN
628 i := getorcreate_housenumber_id(make_standard_name(NEW.address->'streetnumber'));
629 NEW.housenumber := (NEW.address->'conscriptionnumber') || '/' || (NEW.address->'streetnumber');
631 NEW.housenumber := NEW.address->'conscriptionnumber';
633 ELSEIF NEW.address ? 'streetnumber' THEN
634 NEW.housenumber := NEW.address->'streetnumber';
635 i := getorcreate_housenumber_id(make_standard_name(NEW.address->'streetnumber'));
636 ELSEIF NEW.address ? 'housenumber' THEN
637 NEW.housenumber := NEW.address->'housenumber';
638 i := getorcreate_housenumber_id(make_standard_name(NEW.housenumber));
641 addr_street := NEW.address->'street';
642 addr_place := NEW.address->'place';
644 IF NEW.address ? 'postcode' and NEW.address->'postcode' not similar to '%(,|;)%' THEN
645 i := getorcreate_postcode_id(NEW.address->'postcode');
649 -- Speed up searches - just use the centroid of the feature
650 -- cheaper but less acurate
651 place_centroid := ST_PointOnSurface(NEW.geometry);
652 -- For searching near features rather use the centroid
653 near_centroid := ST_Envelope(NEW.geometry);
654 NEW.centroid := null;
655 NEW.postcode := null;
656 --DEBUG: RAISE WARNING 'Computing preliminary centroid at %',ST_AsText(place_centroid);
658 -- recalculate country and partition
659 IF NEW.rank_search = 4 AND NEW.address is not NULL AND NEW.address ? 'country' THEN
660 -- for countries, believe the mapped country code,
661 -- so that we remain in the right partition if the boundaries
663 NEW.country_code := lower(NEW.address->'country');
664 NEW.partition := get_partition(lower(NEW.country_code));
665 IF NEW.partition = 0 THEN
666 NEW.country_code := lower(get_country_code(place_centroid));
667 NEW.partition := get_partition(NEW.country_code);
670 IF NEW.rank_search >= 4 THEN
671 NEW.country_code := lower(get_country_code(place_centroid));
673 NEW.country_code := NULL;
675 NEW.partition := get_partition(NEW.country_code);
677 --DEBUG: RAISE WARNING 'Country updated: "%"', NEW.country_code;
679 -- waterway ways are linked when they are part of a relation and have the same class/type
680 IF NEW.osm_type = 'R' and NEW.class = 'waterway' THEN
681 FOR relation_members IN select members from planet_osm_rels r where r.id = NEW.osm_id and r.parts != array[]::bigint[]
683 FOR i IN 1..array_upper(relation_members, 1) BY 2 LOOP
684 IF relation_members[i+1] in ('', 'main_stream', 'side_stream') AND substring(relation_members[i],1,1) = 'w' THEN
685 --DEBUG: RAISE WARNING 'waterway parent %, child %/%', NEW.osm_id, i, relation_members[i];
686 FOR linked_node_id IN SELECT place_id FROM placex
687 WHERE osm_type = 'W' and osm_id = substring(relation_members[i],2,200)::bigint
688 and class = NEW.class and type in ('river', 'stream', 'canal', 'drain', 'ditch')
689 and ( relation_members[i+1] != 'side_stream' or NEW.name->'name' = name->'name')
691 UPDATE placex SET linked_place_id = NEW.place_id WHERE place_id = linked_node_id;
696 --DEBUG: RAISE WARNING 'Waterway processed';
699 -- What level are we searching from
700 search_maxrank := NEW.rank_search;
702 -- Thought this wasn't needed but when we add new languages to the country_name table
703 -- we need to update the existing names
704 IF NEW.name is not null AND array_upper(akeys(NEW.name),1) > 1 THEN
705 default_language := get_country_language_code(NEW.country_code);
706 IF default_language IS NOT NULL THEN
707 IF NEW.name ? 'name' AND NOT NEW.name ? ('name:'||default_language) THEN
708 NEW.name := NEW.name || hstore(('name:'||default_language), (NEW.name -> 'name'));
709 ELSEIF NEW.name ? ('name:'||default_language) AND NOT NEW.name ? 'name' THEN
710 NEW.name := NEW.name || hstore('name', (NEW.name -> ('name:'||default_language)));
714 --DEBUG: RAISE WARNING 'Local names updated';
716 -- Initialise the name vector using our name
717 name_vector := make_keywords(NEW.name);
718 nameaddress_vector := '{}'::int[];
721 address_havelevel[i] := false;
724 NEW.importance := null;
725 SELECT wikipedia, importance
726 FROM compute_importance(NEW.extratags, NEW.country_code, NEW.osm_type, NEW.osm_id)
727 INTO NEW.wikipedia,NEW.importance;
729 --DEBUG: RAISE WARNING 'Importance computed from wikipedia: %', NEW.importance;
731 -- ---------------------------------------------------------------------------
732 -- For low level elements we inherit from our parent road
733 IF (NEW.rank_search > 27 OR (NEW.type = 'postcode' AND NEW.rank_search = 25)) THEN
735 --DEBUG: RAISE WARNING 'finding street for % %', NEW.osm_type, NEW.osm_id;
737 -- We won't get a better centroid, besides these places are too small to care
738 NEW.centroid := place_centroid;
740 NEW.parent_place_id := null;
742 -- if we have a POI and there is no address information,
743 -- see if we can get it from a surrounding building
744 IF NEW.osm_type = 'N' AND addr_street IS NULL AND addr_place IS NULL
745 AND NEW.housenumber IS NULL THEN
746 FOR location IN select address from placex where ST_Covers(geometry, place_centroid)
747 and address is not null
748 and (address ? 'housenumber' or address ? 'street' or address ? 'place')
749 and rank_search > 28 AND ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon')
752 NEW.housenumber := location.address->'housenumber';
753 addr_street := location.address->'street';
754 addr_place := location.address->'place';
755 --DEBUG: RAISE WARNING 'Found surrounding building % %', location.osm_type, location.osm_id;
759 -- We have to find our parent road.
760 -- Copy data from linked items (points on ways, addr:street links, relations)
762 -- Is this object part of a relation?
763 FOR relation IN select * from planet_osm_rels where parts @> ARRAY[NEW.osm_id] and members @> ARRAY[lower(NEW.osm_type)||NEW.osm_id]
765 -- At the moment we only process one type of relation - associatedStreet
766 IF relation.tags @> ARRAY['associatedStreet'] THEN
767 FOR i IN 1..array_upper(relation.members, 1) BY 2 LOOP
768 IF NEW.parent_place_id IS NULL AND relation.members[i+1] = 'street' THEN
769 --RAISE WARNING 'node in relation %',relation;
770 SELECT place_id from placex where osm_type = 'W'
771 and osm_id = substring(relation.members[i],2,200)::bigint
772 and rank_search = 26 and name is not null INTO NEW.parent_place_id;
777 --DEBUG: RAISE WARNING 'Checked for street relation (%)', NEW.parent_place_id;
779 -- Note that addr:street links can only be indexed once the street itself is indexed
780 IF NEW.parent_place_id IS NULL AND addr_street IS NOT NULL THEN
781 address_street_word_ids := get_name_ids(make_standard_name(addr_street));
782 IF address_street_word_ids IS NOT NULL THEN
783 SELECT place_id from getNearestNamedRoadFeature(NEW.partition, near_centroid, address_street_word_ids) INTO NEW.parent_place_id;
786 --DEBUG: RAISE WARNING 'Checked for addr:street (%)', NEW.parent_place_id;
788 IF NEW.parent_place_id IS NULL AND addr_place IS NOT NULL THEN
789 address_street_word_ids := get_name_ids(make_standard_name(addr_place));
790 IF address_street_word_ids IS NOT NULL THEN
791 SELECT place_id from getNearestNamedPlaceFeature(NEW.partition, near_centroid, address_street_word_ids) INTO NEW.parent_place_id;
794 --DEBUG: RAISE WARNING 'Checked for addr:place (%)', NEW.parent_place_id;
796 -- Is this node part of an interpolation?
797 IF NEW.parent_place_id IS NULL AND NEW.osm_type = 'N' THEN
798 SELECT q.parent_place_id FROM location_property_osmline q, planet_osm_ways x
799 WHERE q.linegeo && NEW.geometry and x.id = q.osm_id and NEW.osm_id = any(x.nodes)
800 LIMIT 1 INTO NEW.parent_place_id;
802 --DEBUG: RAISE WARNING 'Checked for interpolation (%)', NEW.parent_place_id;
804 -- Is this node part of a way?
805 IF NEW.parent_place_id IS NULL AND NEW.osm_type = 'N' THEN
808 SELECT p.place_id, p.osm_id, p.rank_search, p.address from placex p, planet_osm_ways w
809 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)
811 --DEBUG: RAISE WARNING 'Node is part of way % ', location.osm_id;
813 -- Way IS a road then we are on it - that must be our road
814 IF location.rank_search < 28 THEN
815 --RAISE WARNING 'node in way that is a street %',location;
816 NEW.parent_place_id := location.place_id;
819 --DEBUG: RAISE WARNING 'Checked if way is street (%)', NEW.parent_place_id;
821 -- If the way mentions a street or place address, try that for parenting.
822 IF location.address is not null THEN
823 IF location.address ? 'street' THEN
824 address_street_word_ids := get_name_ids(make_standard_name(location.address->'street'));
825 IF address_street_word_ids IS NOT NULL THEN
826 SELECT place_id from getNearestNamedRoadFeature(NEW.partition, near_centroid, address_street_word_ids) INTO NEW.parent_place_id;
827 EXIT WHEN NEW.parent_place_id is not NULL;
830 --DEBUG: RAISE WARNING 'Checked for addr:street in way (%)', NEW.parent_place_id;
832 IF location.address ? 'place' THEN
833 address_street_word_ids := get_name_ids(make_standard_name(location.address->'place'));
834 IF address_street_word_ids IS NOT NULL THEN
835 SELECT place_id from getNearestNamedPlaceFeature(NEW.partition, near_centroid, address_street_word_ids) INTO NEW.parent_place_id;
836 EXIT WHEN NEW.parent_place_id is not NULL;
839 --DEBUG: RAISE WARNING 'Checked for addr:place in way (%)', NEW.parent_place_id;
842 -- Is the WAY part of a relation
843 FOR relation IN select * from planet_osm_rels where parts @> ARRAY[location.osm_id] and members @> ARRAY['w'||location.osm_id]
845 -- At the moment we only process one type of relation - associatedStreet
846 IF relation.tags @> ARRAY['associatedStreet'] AND array_upper(relation.members, 1) IS NOT NULL THEN
847 FOR i IN 1..array_upper(relation.members, 1) BY 2 LOOP
848 IF NEW.parent_place_id IS NULL AND relation.members[i+1] = 'street' THEN
849 --RAISE WARNING 'node in way that is in a relation %',relation;
850 SELECT place_id from placex where osm_type='W' and osm_id = substring(relation.members[i],2,200)::bigint
851 and rank_search = 26 and name is not null INTO NEW.parent_place_id;
856 EXIT WHEN NEW.parent_place_id is not null;
857 --DEBUG: RAISE WARNING 'Checked for street relation in way (%)', NEW.parent_place_id;
862 -- Still nothing, just use the nearest road
863 IF NEW.parent_place_id IS NULL THEN
864 SELECT place_id FROM getNearestRoadFeature(NEW.partition, near_centroid) INTO NEW.parent_place_id;
866 --DEBUG: RAISE WARNING 'Checked for nearest way (%)', NEW.parent_place_id;
869 -- If we didn't find any road fallback to standard method
870 IF NEW.parent_place_id IS NOT NULL THEN
872 -- Get the details of the parent road
873 SELECT p.country_code, p.postcode FROM placex p
874 WHERE p.place_id = NEW.parent_place_id INTO location;
876 NEW.country_code := location.country_code;
877 --DEBUG: RAISE WARNING 'Got parent details from search name';
879 -- determine postcode
880 IF NEW.rank_search > 4 THEN
881 IF NEW.address is not null AND NEW.address ? 'postcode' THEN
882 NEW.postcode = upper(trim(NEW.address->'postcode'));
884 NEW.postcode := location.postcode;
886 IF NEW.postcode is null THEN
887 NEW.postcode := get_nearest_postcode(NEW.country_code, NEW.geometry);
891 -- If there is no name it isn't searchable, don't bother to create a search record
892 IF NEW.name is NULL THEN
893 --DEBUG: RAISE WARNING 'Not a searchable place % %', NEW.osm_type, NEW.osm_id;
897 -- Performance, it would be more acurate to do all the rest of the import
898 -- process but it takes too long
899 -- Just be happy with inheriting from parent road only
900 IF NEW.rank_search <= 25 and NEW.rank_address > 0 THEN
901 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);
902 --DEBUG: RAISE WARNING 'Place added to location table';
905 result := insertSearchName(NEW.partition, NEW.place_id, name_vector,
906 NEW.rank_search, NEW.rank_address, NEW.geometry);
908 IF NOT %REVERSE-ONLY% THEN
909 -- Merge address from parent
910 SELECT s.name_vector, s.nameaddress_vector FROM search_name s
911 WHERE s.place_id = NEW.parent_place_id INTO location;
913 nameaddress_vector := array_merge(nameaddress_vector,
914 location.nameaddress_vector);
915 nameaddress_vector := array_merge(nameaddress_vector, location.name_vector);
917 INSERT INTO search_name (place_id, search_rank, address_rank,
918 importance, country_code, name_vector,
919 nameaddress_vector, centroid)
920 VALUES (NEW.place_id, NEW.rank_search, NEW.rank_address,
921 NEW.importance, NEW.country_code, name_vector,
922 nameaddress_vector, place_centroid);
923 --DEBUG: RAISE WARNING 'Place added to search table';
931 -- ---------------------------------------------------------------------------
933 --DEBUG: RAISE WARNING 'Using full index mode for % %', NEW.osm_type, NEW.osm_id;
935 IF NEW.osm_type = 'R' AND NEW.rank_search < 26 THEN
937 -- see if we have any special relation members
938 select members from planet_osm_rels where id = NEW.osm_id INTO relation_members;
939 --DEBUG: RAISE WARNING 'Got relation members';
941 IF relation_members IS NOT NULL THEN
942 FOR relMember IN select get_osm_rel_members(relation_members,ARRAY['label']) as member LOOP
943 --DEBUG: RAISE WARNING 'Found label member %', relMember.member;
945 FOR linkedPlacex IN select * from placex where osm_type = upper(substring(relMember.member,1,1))::char(1)
946 and osm_id = substring(relMember.member,2,10000)::bigint
947 and class = 'place' order by rank_search desc limit 1 LOOP
949 -- If we don't already have one use this as the centre point of the geometry
950 IF NEW.centroid IS NULL THEN
951 NEW.centroid := coalesce(linkedPlacex.centroid,st_centroid(linkedPlacex.geometry));
954 -- merge in the label name, re-init word vector
955 IF NOT linkedPlacex.name IS NULL THEN
956 NEW.name := linkedPlacex.name || NEW.name;
957 name_vector := array_merge(name_vector, make_keywords(linkedPlacex.name));
960 -- merge in extra tags
961 NEW.extratags := hstore(linkedPlacex.class, linkedPlacex.type) || coalesce(linkedPlacex.extratags, ''::hstore) || coalesce(NEW.extratags, ''::hstore);
963 -- mark the linked place (excludes from search results)
964 UPDATE placex set linked_place_id = NEW.place_id where place_id = linkedPlacex.place_id;
966 select wikipedia, importance
967 FROM compute_importance(linkedPlacex.extratags, NEW.country_code,
968 'N', linkedPlacex.osm_id)
969 INTO linked_wikipedia,linked_importance;
970 --DEBUG: RAISE WARNING 'Linked label member';
975 IF NEW.centroid IS NULL THEN
977 FOR relMember IN select get_osm_rel_members(relation_members,ARRAY['admin_center','admin_centre']) as member LOOP
978 --DEBUG: RAISE WARNING 'Found admin_center member %', relMember.member;
980 FOR linkedPlacex IN select * from placex where osm_type = upper(substring(relMember.member,1,1))::char(1)
981 and osm_id = substring(relMember.member,2,10000)::bigint
982 and class = 'place' order by rank_search desc limit 1 LOOP
984 -- For an admin centre we also want a name match - still not perfect, for example 'new york, new york'
985 -- But that can be fixed by explicitly setting the label in the data
986 IF make_standard_name(NEW.name->'name') = make_standard_name(linkedPlacex.name->'name')
987 AND NEW.rank_address = linkedPlacex.rank_address THEN
989 -- If we don't already have one use this as the centre point of the geometry
990 IF NEW.centroid IS NULL THEN
991 NEW.centroid := coalesce(linkedPlacex.centroid,st_centroid(linkedPlacex.geometry));
994 -- merge in the name, re-init word vector
995 IF NOT linkedPlacex.name IS NULL THEN
996 NEW.name := linkedPlacex.name || NEW.name;
997 name_vector := make_keywords(NEW.name);
1000 -- merge in extra tags
1001 NEW.extratags := hstore(linkedPlacex.class, linkedPlacex.type) || coalesce(linkedPlacex.extratags, ''::hstore) || coalesce(NEW.extratags, ''::hstore);
1003 -- mark the linked place (excludes from search results)
1004 UPDATE placex set linked_place_id = NEW.place_id where place_id = linkedPlacex.place_id;
1006 select wikipedia, importance
1007 FROM compute_importance(linkedPlacex.extratags, NEW.country_code,
1008 'N', linkedPlacex.osm_id)
1009 INTO linked_wikipedia,linked_importance;
1010 --DEBUG: RAISE WARNING 'Linked admin_center';
1022 -- Name searches can be done for ways as well as relations
1023 IF NEW.osm_type in ('W','R') AND NEW.rank_search < 26 AND NEW.rank_address > 0 THEN
1025 -- not found one yet? how about doing a name search
1026 IF NEW.centroid IS NULL AND (NEW.name->'name') is not null and make_standard_name(NEW.name->'name') != '' THEN
1028 --DEBUG: RAISE WARNING 'Looking for nodes with matching names';
1029 FOR linkedPlacex IN select placex.* from placex WHERE
1030 make_standard_name(name->'name') = make_standard_name(NEW.name->'name')
1031 AND placex.rank_address = NEW.rank_address
1032 AND placex.place_id != NEW.place_id
1033 AND placex.osm_type = 'N'::char(1) AND placex.rank_search < 26
1034 AND st_covers(NEW.geometry, placex.geometry)
1036 --DEBUG: RAISE WARNING 'Found matching place node %', linkedPlacex.osm_id;
1037 -- If we don't already have one use this as the centre point of the geometry
1038 IF NEW.centroid IS NULL THEN
1039 NEW.centroid := coalesce(linkedPlacex.centroid,st_centroid(linkedPlacex.geometry));
1042 -- merge in the name, re-init word vector
1043 NEW.name := linkedPlacex.name || NEW.name;
1044 name_vector := make_keywords(NEW.name);
1046 -- merge in extra tags
1047 NEW.extratags := hstore(linkedPlacex.class, linkedPlacex.type) || coalesce(linkedPlacex.extratags, ''::hstore) || coalesce(NEW.extratags, ''::hstore);
1049 -- mark the linked place (excludes from search results)
1050 UPDATE placex set linked_place_id = NEW.place_id where place_id = linkedPlacex.place_id;
1052 select wikipedia, importance
1053 FROM compute_importance(linkedPlacex.extratags, NEW.country_code,
1054 'N', linkedPlacex.osm_id)
1055 INTO linked_wikipedia,linked_importance;
1056 --DEBUG: RAISE WARNING 'Linked named place';
1060 IF NEW.centroid IS NOT NULL THEN
1061 place_centroid := NEW.centroid;
1062 -- Place might have had only a name tag before but has now received translations
1063 -- from the linked place. Make sure a name tag for the default language exists in
1065 IF NEW.name is not null AND array_upper(akeys(NEW.name),1) > 1 THEN
1066 default_language := get_country_language_code(NEW.country_code);
1067 IF default_language IS NOT NULL THEN
1068 IF NEW.name ? 'name' AND NOT NEW.name ? ('name:'||default_language) THEN
1069 NEW.name := NEW.name || hstore(('name:'||default_language), (NEW.name -> 'name'));
1070 ELSEIF NEW.name ? ('name:'||default_language) AND NOT NEW.name ? 'name' THEN
1071 NEW.name := NEW.name || hstore('name', (NEW.name -> ('name:'||default_language)));
1075 --DEBUG: RAISE WARNING 'Names updated from linked places';
1078 -- Use the maximum importance if a one could be computed from the linked object.
1079 IF linked_importance is not null AND
1080 (NEW.importance is null or NEW.importance < linked_importance) THEN
1081 NEW.importance = linked_importance;
1085 -- make sure all names are in the word table
1086 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
1087 perform create_country(NEW.name, lower(NEW.country_code));
1088 --DEBUG: RAISE WARNING 'Country names updated';
1091 NEW.parent_place_id = 0;
1092 parent_place_id_rank = 0;
1095 -- convert address store to array of tokenids
1096 --DEBUG: RAISE WARNING 'Starting address search';
1097 isin_tokens := '{}'::int[];
1098 IF NEW.address IS NOT NULL THEN
1099 FOR addr_item IN SELECT * FROM each(NEW.address)
1101 IF addr_item.key IN ('city', 'tiger:county', 'state', 'suburb', 'province', 'district', 'region', 'county', 'municipality', 'hamlet', 'village', 'subdistrict', 'town', 'neighbourhood', 'quarter', 'parish') THEN
1102 address_street_word_id := get_name_id(make_standard_name(addr_item.value));
1103 IF address_street_word_id IS NOT NULL AND NOT(ARRAY[address_street_word_id] <@ isin_tokens) THEN
1104 isin_tokens := isin_tokens || address_street_word_id;
1106 IF NOT %REVERSE-ONLY% THEN
1107 address_street_word_id := get_word_id(make_standard_name(addr_item.value));
1108 IF address_street_word_id IS NOT NULL THEN
1109 nameaddress_vector := array_merge(nameaddress_vector, ARRAY[address_street_word_id]);
1113 IF addr_item.key = 'is_in' THEN
1114 -- is_in items need splitting
1115 isin := regexp_split_to_array(addr_item.value, E'[;,]');
1116 IF array_upper(isin, 1) IS NOT NULL THEN
1117 FOR i IN 1..array_upper(isin, 1) LOOP
1118 address_street_word_id := get_name_id(make_standard_name(isin[i]));
1119 IF address_street_word_id IS NOT NULL AND NOT(ARRAY[address_street_word_id] <@ isin_tokens) THEN
1120 isin_tokens := isin_tokens || address_street_word_id;
1123 -- merge word into address vector
1124 IF NOT %REVERSE-ONLY% THEN
1125 address_street_word_id := get_word_id(make_standard_name(isin[i]));
1126 IF address_street_word_id IS NOT NULL THEN
1127 nameaddress_vector := array_merge(nameaddress_vector, ARRAY[address_street_word_id]);
1135 IF NOT %REVERSE-ONLY% THEN
1136 nameaddress_vector := array_merge(nameaddress_vector, isin_tokens);
1139 -- RAISE WARNING 'ISIN: %', isin_tokens;
1141 -- Process area matches
1142 location_rank_search := 0;
1143 location_distance := 0;
1144 location_parent := NULL;
1145 -- added ourself as address already
1146 address_havelevel[NEW.rank_address] := true;
1147 --DEBUG: RAISE WARNING ' getNearFeatures(%,''%'',%,''%'')',NEW.partition, place_centroid, search_maxrank, isin_tokens;
1149 SELECT * from getNearFeatures(NEW.partition,
1150 CASE WHEN NEW.rank_search >= 26
1151 AND NEW.rank_search < 30
1153 ELSE place_centroid END,
1154 search_maxrank, isin_tokens)
1156 IF location.rank_address != location_rank_search THEN
1157 location_rank_search := location.rank_address;
1158 IF location.isguess THEN
1159 location_distance := location.distance * 1.5;
1161 IF location.rank_address <= 12 THEN
1162 -- for county and above, if we have an area consider that exact
1163 -- (It would be nice to relax the constraint for places close to
1164 -- the boundary but we'd need the exact geometry for that. Too
1166 location_distance = 0;
1168 -- Below county level remain slightly fuzzy.
1169 location_distance := location.distance * 0.5;
1173 CONTINUE WHEN location.keywords <@ location_keywords;
1176 IF location.distance < location_distance OR NOT location.isguess THEN
1177 location_keywords := location.keywords;
1179 location_isaddress := NOT address_havelevel[location.rank_address];
1180 IF location_isaddress AND location.isguess AND location_parent IS NOT NULL THEN
1181 location_isaddress := ST_Contains(location_parent,location.centroid);
1184 -- RAISE WARNING '% isaddress: %', location.place_id, location_isaddress;
1185 -- Add it to the list of search terms
1186 IF NOT %REVERSE-ONLY% THEN
1187 nameaddress_vector := array_merge(nameaddress_vector, location.keywords::integer[]);
1189 INSERT INTO place_addressline (place_id, address_place_id, fromarea, isaddress, distance, cached_rank_address)
1190 VALUES (NEW.place_id, location.place_id, true, location_isaddress, location.distance, location.rank_address);
1192 IF location_isaddress THEN
1193 -- add postcode if we have one
1194 -- (If multiple postcodes are available, we end up with the highest ranking one.)
1195 IF location.postcode is not null THEN
1196 NEW.postcode = location.postcode;
1199 address_havelevel[location.rank_address] := true;
1200 IF NOT location.isguess THEN
1201 SELECT geometry FROM placex WHERE place_id = location.place_id INTO location_parent;
1204 IF location.rank_address > parent_place_id_rank THEN
1205 NEW.parent_place_id = location.place_id;
1206 parent_place_id_rank = location.rank_address;
1211 --DEBUG: RAISE WARNING ' Terms: (%) %',location, nameaddress_vector;
1216 --DEBUG: RAISE WARNING 'address computed';
1218 IF NEW.address is not null AND NEW.address ? 'postcode'
1219 AND NEW.address->'postcode' not similar to '%(,|;)%' THEN
1220 NEW.postcode := upper(trim(NEW.address->'postcode'));
1223 IF NEW.postcode is null AND NEW.rank_search > 8 THEN
1224 NEW.postcode := get_nearest_postcode(NEW.country_code, NEW.geometry);
1227 -- if we have a name add this to the name search table
1228 IF NEW.name IS NOT NULL THEN
1230 IF NEW.rank_search <= 25 and NEW.rank_address > 0 THEN
1231 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);
1232 --DEBUG: RAISE WARNING 'added to location (full)';
1235 IF NEW.rank_search between 26 and 27 and NEW.class = 'highway' THEN
1236 result := insertLocationRoad(NEW.partition, NEW.place_id, NEW.country_code, NEW.geometry);
1237 --DEBUG: RAISE WARNING 'insert into road location table (full)';
1240 result := insertSearchName(NEW.partition, NEW.place_id, name_vector,
1241 NEW.rank_search, NEW.rank_address, NEW.geometry);
1242 --DEBUG: RAISE WARNING 'added to search name (full)';
1244 IF NOT %REVERSE-ONLY% THEN
1245 INSERT INTO search_name (place_id, search_rank, address_rank,
1246 importance, country_code, name_vector,
1247 nameaddress_vector, centroid)
1248 VALUES (NEW.place_id, NEW.rank_search, NEW.rank_address,
1249 NEW.importance, NEW.country_code, name_vector,
1250 nameaddress_vector, place_centroid);
1255 -- If we've not managed to pick up a better one - default centroid
1256 IF NEW.centroid IS NULL THEN
1257 NEW.centroid := place_centroid;
1260 --DEBUG: RAISE WARNING 'place update % % finsihed.', NEW.osm_type, NEW.osm_id;
1267 CREATE OR REPLACE FUNCTION placex_delete() RETURNS TRIGGER
1273 -- RAISE WARNING 'placex_delete % %',OLD.osm_type,OLD.osm_id;
1275 update placex set linked_place_id = null, indexed_status = 2 where linked_place_id = OLD.place_id and indexed_status = 0;
1276 --DEBUG: RAISE WARNING 'placex_delete:01 % %',OLD.osm_type,OLD.osm_id;
1277 update placex set linked_place_id = null where linked_place_id = OLD.place_id;
1278 --DEBUG: RAISE WARNING 'placex_delete:02 % %',OLD.osm_type,OLD.osm_id;
1280 IF OLD.rank_address < 30 THEN
1282 -- mark everything linked to this place for re-indexing
1283 --DEBUG: RAISE WARNING 'placex_delete:03 % %',OLD.osm_type,OLD.osm_id;
1284 UPDATE placex set indexed_status = 2 from place_addressline where address_place_id = OLD.place_id
1285 and placex.place_id = place_addressline.place_id and indexed_status = 0 and place_addressline.isaddress;
1287 --DEBUG: RAISE WARNING 'placex_delete:04 % %',OLD.osm_type,OLD.osm_id;
1288 DELETE FROM place_addressline where address_place_id = OLD.place_id;
1290 --DEBUG: RAISE WARNING 'placex_delete:05 % %',OLD.osm_type,OLD.osm_id;
1291 b := deleteRoad(OLD.partition, OLD.place_id);
1293 --DEBUG: RAISE WARNING 'placex_delete:06 % %',OLD.osm_type,OLD.osm_id;
1294 update placex set indexed_status = 2 where parent_place_id = OLD.place_id and indexed_status = 0;
1295 --DEBUG: RAISE WARNING 'placex_delete:07 % %',OLD.osm_type,OLD.osm_id;
1296 -- reparenting also for OSM Interpolation Lines (and for Tiger?)
1297 update location_property_osmline set indexed_status = 2 where indexed_status = 0 and parent_place_id = OLD.place_id;
1301 --DEBUG: RAISE WARNING 'placex_delete:08 % %',OLD.osm_type,OLD.osm_id;
1303 IF OLD.rank_address < 26 THEN
1304 b := deleteLocationArea(OLD.partition, OLD.place_id, OLD.rank_search);
1307 --DEBUG: RAISE WARNING 'placex_delete:09 % %',OLD.osm_type,OLD.osm_id;
1309 IF OLD.name is not null THEN
1310 IF NOT %REVERSE-ONLY% THEN
1311 DELETE from search_name WHERE place_id = OLD.place_id;
1313 b := deleteSearchName(OLD.partition, OLD.place_id);
1316 --DEBUG: RAISE WARNING 'placex_delete:10 % %',OLD.osm_type,OLD.osm_id;
1318 DELETE FROM place_addressline where place_id = OLD.place_id;
1320 --DEBUG: RAISE WARNING 'placex_delete:11 % %',OLD.osm_type,OLD.osm_id;
1322 -- remove from tables for special search
1323 classtable := 'place_classtype_' || OLD.class || '_' || OLD.type;
1324 SELECT count(*)>0 FROM pg_tables WHERE tablename = classtable and schemaname = current_schema() INTO b;
1326 EXECUTE 'DELETE FROM ' || classtable::regclass || ' WHERE place_id = $1' USING OLD.place_id;
1329 --DEBUG: RAISE WARNING 'placex_delete:12 % %',OLD.osm_type,OLD.osm_id;
1337 CREATE OR REPLACE FUNCTION get_osm_rel_members(members TEXT[], member TEXT) RETURNS TEXT[]
1344 FOR i IN 1..ARRAY_UPPER(members,1) BY 2 LOOP
1345 IF members[i+1] = member THEN
1346 result := result || members[i];
1355 CREATE OR REPLACE FUNCTION get_osm_rel_members(members TEXT[], memberLabels TEXT[]) RETURNS SETOF TEXT
1361 FOR i IN 1..ARRAY_UPPER(members,1) BY 2 LOOP
1362 IF members[i+1] = ANY(memberLabels) THEN
1363 RETURN NEXT members[i];
1372 CREATE OR REPLACE FUNCTION quad_split_geometry(geometry GEOMETRY, maxarea FLOAT, maxdepth INTEGER)
1373 RETURNS SETOF GEOMETRY
1387 remainingdepth INTEGER;
1392 -- RAISE WARNING 'quad_split_geometry: maxarea=%, depth=%',maxarea,maxdepth;
1394 IF (ST_GeometryType(geometry) not in ('ST_Polygon','ST_MultiPolygon') OR NOT ST_IsValid(geometry)) THEN
1395 RETURN NEXT geometry;
1399 remainingdepth := maxdepth - 1;
1400 area := ST_AREA(geometry);
1401 IF remainingdepth < 1 OR area < maxarea THEN
1402 RETURN NEXT geometry;
1406 xmin := st_xmin(geometry);
1407 xmax := st_xmax(geometry);
1408 ymin := st_ymin(geometry);
1409 ymax := st_ymax(geometry);
1410 secbox := ST_SetSRID(ST_MakeBox2D(ST_Point(ymin,xmin),ST_Point(ymax,xmax)),4326);
1412 -- if the geometry completely covers the box don't bother to slice any more
1413 IF ST_AREA(secbox) = area THEN
1414 RETURN NEXT geometry;
1418 xmid := (xmin+xmax)/2;
1419 ymid := (ymin+ymax)/2;
1422 FOR seg IN 1..4 LOOP
1425 secbox := ST_SetSRID(ST_MakeBox2D(ST_Point(xmin,ymin),ST_Point(xmid,ymid)),4326);
1428 secbox := ST_SetSRID(ST_MakeBox2D(ST_Point(xmin,ymid),ST_Point(xmid,ymax)),4326);
1431 secbox := ST_SetSRID(ST_MakeBox2D(ST_Point(xmid,ymin),ST_Point(xmax,ymid)),4326);
1434 secbox := ST_SetSRID(ST_MakeBox2D(ST_Point(xmid,ymid),ST_Point(xmax,ymax)),4326);
1437 IF st_intersects(geometry, secbox) THEN
1438 secgeo := st_intersection(geometry, secbox);
1439 IF NOT ST_IsEmpty(secgeo) AND ST_GeometryType(secgeo) in ('ST_Polygon','ST_MultiPolygon') THEN
1440 FOR geo IN select quad_split_geometry(secgeo, maxarea, remainingdepth) as geom LOOP
1441 IF NOT ST_IsEmpty(geo.geom) AND ST_GeometryType(geo.geom) in ('ST_Polygon','ST_MultiPolygon') THEN
1443 RETURN NEXT geo.geom;
1455 CREATE OR REPLACE FUNCTION split_geometry(geometry GEOMETRY)
1456 RETURNS SETOF GEOMETRY
1461 -- 10000000000 is ~~ 1x1 degree
1462 FOR geo IN select quad_split_geometry(geometry, 0.25, 20) as geom LOOP
1463 RETURN NEXT geo.geom;
1471 CREATE OR REPLACE FUNCTION place_force_delete(placeid BIGINT) RETURNS BOOLEAN
1475 osmtype character(1);
1479 SELECT osm_type, osm_id, class, type FROM placex WHERE place_id = placeid INTO osmtype, osmid, pclass, ptype;
1480 DELETE FROM import_polygon_delete where osm_type = osmtype and osm_id = osmid and class = pclass and type = ptype;
1481 DELETE FROM import_polygon_error where osm_type = osmtype and osm_id = osmid and class = pclass and type = ptype;
1482 -- force delete from place/placex by making it a very small geometry
1483 UPDATE place set geometry = ST_SetSRID(ST_Point(0,0), 4326) where osm_type = osmtype and osm_id = osmid and class = pclass and type = ptype;
1484 DELETE FROM place where osm_type = osmtype and osm_id = osmid and class = pclass and type = ptype;
1491 CREATE OR REPLACE FUNCTION place_force_update(placeid BIGINT) RETURNS BOOLEAN
1499 UPDATE placex SET indexed_status = 2 WHERE place_id = placeid;
1500 SELECT geometry, rank_search FROM placex WHERE place_id = placeid INTO placegeom, rank;
1501 IF placegeom IS NOT NULL AND ST_IsValid(placegeom) THEN
1502 IF ST_GeometryType(placegeom) in ('ST_Polygon','ST_MultiPolygon') THEN
1503 FOR geom IN select split_geometry(placegeom) FROM placex WHERE place_id = placeid LOOP
1504 update placex set indexed_status = 2 where (st_covers(geom, placex.geometry) OR ST_Intersects(geom, placex.geometry))
1505 AND rank_search > rank and indexed_status = 0 and ST_geometrytype(placex.geometry) = 'ST_Point' and (rank_search < 28 or name is not null or (rank >= 16 and address ? 'place'));
1506 update placex set indexed_status = 2 where (st_covers(geom, placex.geometry) OR ST_Intersects(geom, placex.geometry))
1507 AND rank_search > rank and indexed_status = 0 and ST_geometrytype(placex.geometry) != 'ST_Point' and (rank_search < 28 or name is not null or (rank >= 16 and address ? 'place'));
1513 ELSEIF rank < 18 THEN
1515 ELSEIF rank < 20 THEN
1517 ELSEIF rank = 21 THEN
1519 ELSEIF rank < 24 THEN
1521 ELSEIF rank < 26 THEN
1522 diameter := 0.002; -- 100 to 200 meters
1523 ELSEIF rank < 28 THEN
1524 diameter := 0.001; -- 50 to 100 meters
1526 IF diameter > 0 THEN
1528 -- roads may cause reparenting for >27 rank places
1529 update placex set indexed_status = 2 where indexed_status = 0 and rank_search > rank and ST_DWithin(placex.geometry, placegeom, diameter);
1530 ELSEIF rank >= 16 THEN
1531 -- up to rank 16, street-less addresses may need reparenting
1532 update placex set indexed_status = 2 where indexed_status = 0 and rank_search > rank and ST_DWithin(placex.geometry, placegeom, diameter) and (rank_search < 28 or name is not null or address ? 'place');
1534 -- for all other places the search terms may change as well
1535 update placex set indexed_status = 2 where indexed_status = 0 and rank_search > rank and ST_DWithin(placex.geometry, placegeom, diameter) and (rank_search < 28 or name is not null);