+
+-- Insert address of a place into the place_addressline table.
+--
+-- \param obj_place_id Place_id of the place to compute the address for.
+-- \param partition Partition number where the place is in.
+-- \param maxrank Rank of the place. All address features must have
+-- a search rank lower than the given rank.
+-- \param address Address terms for the place.
+-- \param geometry Geometry to which the address objects should be close.
+--
+-- \retval parent_place_id Place_id of the address object that is the direct
+-- ancestor.
+-- \retval postcode Postcode computed from the address. This is the
+-- addr:postcode of one of the address objects. If
+-- more than one of has a postcode, the highest ranking
+-- one is used. May be NULL.
+-- \retval nameaddress_vector Search terms for the address. This is the sum
+-- of name terms of all address objects.
+CREATE OR REPLACE FUNCTION insert_addresslines(obj_place_id BIGINT,
+ partition SMALLINT,
+ maxrank SMALLINT,
+ address HSTORE,
+ geometry GEOMETRY,
+ country TEXT,
+ OUT parent_place_id BIGINT,
+ OUT postcode TEXT,
+ OUT nameaddress_vector INT[])
+ AS $$
+DECLARE
+ address_havelevel BOOLEAN[];
+
+ location_isaddress BOOLEAN;
+ current_boundary GEOMETRY := NULL;
+ current_node_area GEOMETRY := NULL;
+
+ parent_place_rank INT := 0;
+ addr_place_ids BIGINT[];
+
+ location RECORD;
+BEGIN
+ parent_place_id := 0;
+ nameaddress_vector := '{}'::int[];
+
+ address_havelevel := array_fill(false, ARRAY[maxrank]);
+
+ FOR location IN
+ SELECT * FROM get_places_for_addr_tags(partition, geometry,
+ address, country)
+ ORDER BY rank_address, distance, isguess desc
+ LOOP
+ IF NOT %REVERSE-ONLY% THEN
+ nameaddress_vector := array_merge(nameaddress_vector,
+ location.keywords::int[]);
+ END IF;
+
+ IF location.place_id is not null THEN
+ location_isaddress := not address_havelevel[location.rank_address];
+ IF not address_havelevel[location.rank_address] THEN
+ address_havelevel[location.rank_address] := true;
+ IF parent_place_rank < location.rank_address THEN
+ parent_place_id := location.place_id;
+ parent_place_rank := location.rank_address;
+ END IF;
+ END IF;
+
+ INSERT INTO place_addressline (place_id, address_place_id, fromarea,
+ isaddress, distance, cached_rank_address)
+ VALUES (obj_place_id, location.place_id, not location.isguess,
+ true, location.distance, location.rank_address);
+
+ addr_place_ids := array_append(addr_place_ids, location.place_id);
+ END IF;
+ END LOOP;
+
+ FOR location IN
+ SELECT * FROM getNearFeatures(partition, geometry, maxrank)
+ WHERE addr_place_ids is null or not addr_place_ids @> ARRAY[place_id]
+ ORDER BY rank_address, isguess asc,
+ distance *
+ CASE WHEN rank_address = 16 AND rank_search = 15 THEN 0.2
+ WHEN rank_address = 16 AND rank_search = 16 THEN 0.25
+ WHEN rank_address = 16 AND rank_search = 18 THEN 0.5
+ ELSE 1 END ASC
+ LOOP
+ -- Ignore all place nodes that do not fit in a lower level boundary.
+ CONTINUE WHEN location.isguess
+ and current_boundary is not NULL
+ and not ST_Contains(current_boundary, location.centroid);
+
+ -- If this is the first item in the rank, then assume it is the address.
+ location_isaddress := not address_havelevel[location.rank_address];
+
+ -- Further sanity checks to ensure that the address forms a sane hierarchy.
+ IF location_isaddress THEN
+ IF location.isguess and current_node_area is not NULL THEN
+ location_isaddress := ST_Contains(current_node_area, location.centroid);
+ END IF;
+ IF not location.isguess and current_boundary is not NULL
+ and location.rank_address != 11 AND location.rank_address != 5 THEN
+ location_isaddress := ST_Contains(current_boundary, location.centroid);
+ END IF;
+ END IF;
+
+ IF location_isaddress THEN
+ address_havelevel[location.rank_address] := true;
+ parent_place_id := location.place_id;
+
+ -- Set postcode if we have one.
+ -- (Returned will be the highest ranking one.)
+ IF location.postcode is not NULL THEN
+ postcode = location.postcode;
+ END IF;
+
+ -- Recompute the areas we need for hierarchy sanity checks.
+ IF location.rank_address != 11 AND location.rank_address != 5 THEN
+ IF location.isguess THEN
+ current_node_area := place_node_fuzzy_area(location.centroid,
+ location.rank_search);
+ ELSE
+ current_node_area := NULL;
+ SELECT p.geometry FROM placex p
+ WHERE p.place_id = location.place_id INTO current_boundary;
+ END IF;
+ END IF;
+ END IF;
+
+ -- Add it to the list of search terms
+ IF NOT %REVERSE-ONLY% THEN
+ nameaddress_vector := array_merge(nameaddress_vector,
+ location.keywords::integer[]);
+ END IF;
+
+ INSERT INTO place_addressline (place_id, address_place_id, fromarea,
+ isaddress, distance, cached_rank_address)
+ VALUES (obj_place_id, location.place_id, not location.isguess,
+ location_isaddress, location.distance, location.rank_address);
+ END LOOP;
+END;
+$$
+LANGUAGE plpgsql;
+
+