+
+-- 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,
+ 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;
+
+ location RECORD;
+ addr_item RECORD;
+
+ isin_tokens INT[];
+BEGIN
+ parent_place_id := 0;
+ nameaddress_vector := '{}'::int[];
+ isin_tokens := '{}'::int[];
+
+ ---- convert address store to array of tokenids
+ IF address IS NOT NULL THEN
+ FOR addr_item IN SELECT * FROM each(address)
+ LOOP
+ IF addr_item.key IN ('city', 'tiger:county', 'state', 'suburb', 'province',
+ 'district', 'region', 'county', 'municipality',
+ 'hamlet', 'village', 'subdistrict', 'town',
+ 'neighbourhood', 'quarter', 'parish')
+ THEN
+ isin_tokens := array_merge(isin_tokens,
+ word_ids_from_name(addr_item.value));
+ IF NOT %REVERSE-ONLY% THEN
+ nameaddress_vector := array_merge(nameaddress_vector,
+ addr_ids_from_name(addr_item.value));
+ END IF;
+ END IF;
+ END LOOP;
+ END IF;
+ IF NOT %REVERSE-ONLY% THEN
+ nameaddress_vector := array_merge(nameaddress_vector, isin_tokens);
+ END IF;
+
+ ---- now compute the address terms
+ FOR i IN 1..maxrank LOOP
+ address_havelevel[i] := false;
+ END LOOP;
+
+ FOR location IN
+ SELECT * FROM getNearFeatures(partition, geometry, maxrank)
+ ORDER BY rank_address, isin_tokens && keywords desc, 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, true,
+ location_isaddress, location.distance, location.rank_address);
+ END LOOP;
+END;
+$$
+LANGUAGE plpgsql;
+
+