+-- Find the parent road of a POI.
+--
+-- \returns Place ID of parent object or NULL if none
+--
+-- Copy data from linked items (POIs on ways, addr:street links, relations).
+--
+CREATE OR REPLACE FUNCTION find_parent_for_poi(poi_osm_type CHAR(1),
+ poi_osm_id BIGINT,
+ poi_partition SMALLINT,
+ bbox GEOMETRY,
+ addr_street TEXT,
+ addr_place TEXT,
+ fallback BOOL = true)
+ RETURNS BIGINT
+ AS $$
+DECLARE
+ parent_place_id BIGINT DEFAULT NULL;
+ location RECORD;
+ parent RECORD;
+BEGIN
+ --DEBUG: RAISE WARNING 'finding street for % %', poi_osm_type, poi_osm_id;
+
+ -- Is this object part of an associatedStreet relation?
+ FOR location IN
+ SELECT members FROM planet_osm_rels
+ WHERE parts @> ARRAY[poi_osm_id]
+ and members @> ARRAY[lower(poi_osm_type) || poi_osm_id]
+ and tags @> ARRAY['associatedStreet']
+ LOOP
+ FOR i IN 1..array_upper(location.members, 1) BY 2 LOOP
+ IF location.members[i+1] = 'street' THEN
+ --DEBUG: RAISE WARNING 'node in relation %',relation;
+ FOR parent IN
+ SELECT place_id from placex
+ WHERE osm_type = 'W' and osm_id = substring(location.members[i],2)::bigint
+ and name is not null
+ and rank_search between 26 and 27
+ LOOP
+ RETURN parent.place_id;
+ END LOOP;
+ END IF;
+ END LOOP;
+ END LOOP;
+
+ parent_place_id := find_parent_for_address(addr_street, addr_place,
+ poi_partition, bbox);
+ IF parent_place_id is not null THEN
+ RETURN parent_place_id;
+ END IF;
+
+ IF poi_osm_type = 'N' THEN
+ -- Is this node part of an interpolation?
+ FOR parent IN
+ SELECT q.parent_place_id
+ FROM location_property_osmline q, planet_osm_ways x
+ WHERE q.linegeo && bbox and x.id = q.osm_id
+ and poi_osm_id = any(x.nodes)
+ LIMIT 1
+ LOOP
+ --DEBUG: RAISE WARNING 'Get parent from interpolation: %', parent.parent_place_id;
+ RETURN parent.parent_place_id;
+ END LOOP;
+
+ -- Is this node part of any other way?
+ FOR location IN
+ SELECT p.place_id, p.osm_id, p.rank_search, p.address,
+ coalesce(p.centroid, ST_Centroid(p.geometry)) as centroid
+ FROM placex p, planet_osm_ways w
+ WHERE p.osm_type = 'W' and p.rank_search >= 26
+ and p.geometry && bbox
+ and w.id = p.osm_id and poi_osm_id = any(w.nodes)
+ LOOP
+ --DEBUG: RAISE WARNING 'Node is part of way % ', location.osm_id;
+
+ -- Way IS a road then we are on it - that must be our road
+ IF location.rank_search < 28 THEN
+ --DEBUG: RAISE WARNING 'node in way that is a street %',location;
+ return location.place_id;
+ END IF;
+
+ SELECT find_parent_for_poi('W', location.osm_id, poi_partition,
+ location.centroid,
+ location.address->'street',
+ location.address->'place',
+ false)
+ INTO parent_place_id;
+ IF parent_place_id is not null THEN
+ RETURN parent_place_id;
+ END IF;
+ END LOOP;
+ END IF;
+
+ IF fallback THEN
+ IF ST_Area(bbox) < 0.01 THEN
+ -- for smaller features get the nearest road
+ SELECT getNearestRoadPlaceId(poi_partition, bbox) INTO parent_place_id;
+ --DEBUG: RAISE WARNING 'Checked for nearest way (%)', parent_place_id;
+ ELSE
+ -- for larger features simply find the area with the largest rank that
+ -- contains the bbox
+ FOR location IN
+ SELECT place_id FROM placex
+ WHERE bbox @ geometry AND _ST_Covers(geometry, ST_Centroid(bbox))
+ AND rank_search between 5 and 25
+ ORDER BY rank_search desc
+ LOOP
+ RETURN location.place_id;
+ END LOOP;
+ END IF;
+ END IF;
+
+ RETURN parent_place_id;
+END;
+$$
+LANGUAGE plpgsql STABLE;
+
+-- Try to find a linked place for the given object.
+CREATE OR REPLACE FUNCTION find_linked_place(bnd placex)
+ RETURNS placex
+ AS $$
+DECLARE
+ relation_members TEXT[];
+ rel_member RECORD;
+ linked_placex placex%ROWTYPE;
+ bnd_name TEXT;
+BEGIN
+ IF bnd.rank_search >= 26 or bnd.rank_address = 0
+ or ST_GeometryType(bnd.geometry) NOT IN ('ST_Polygon','ST_MultiPolygon')
+ THEN
+ RETURN NULL;
+ END IF;
+
+ IF bnd.osm_type = 'R' THEN
+ -- see if we have any special relation members
+ SELECT members FROM planet_osm_rels WHERE id = bnd.osm_id INTO relation_members;
+ --DEBUG: RAISE WARNING 'Got relation members';
+
+ -- Search for relation members with role 'lable'.
+ IF relation_members IS NOT NULL THEN
+ FOR rel_member IN
+ SELECT get_rel_node_members(relation_members, ARRAY['label']) as member
+ LOOP
+ --DEBUG: RAISE WARNING 'Found label member %', rel_member.member;
+
+ FOR linked_placex IN
+ SELECT * from placex
+ WHERE osm_type = 'N' and osm_id = rel_member.member
+ and class = 'place'
+ LOOP
+ --DEBUG: RAISE WARNING 'Linked label member';
+ RETURN linked_placex;
+ END LOOP;
+
+ END LOOP;
+ END IF;
+ END IF;
+
+ IF bnd.name ? 'name' THEN
+ bnd_name := make_standard_name(bnd.name->'name');
+ IF bnd_name = '' THEN
+ bnd_name := NULL;
+ END IF;
+ END IF;
+
+ -- If extratags has a place tag, look for linked nodes by their place type.
+ -- Area and node still have to have the same name.
+ IF bnd.extratags ? 'place' and bnd_name is not null THEN
+ FOR linked_placex IN
+ SELECT * FROM placex
+ WHERE make_standard_name(name->'name') = bnd_name
+ AND placex.class = 'place' AND placex.type = bnd.extratags->'place'
+ AND placex.osm_type = 'N'
+ AND placex.rank_search < 26 -- needed to select the right index
+ AND _st_covers(bnd.geometry, placex.geometry)
+ LOOP
+ --DEBUG: RAISE WARNING 'Found type-matching place node %', linked_placex.osm_id;
+ RETURN linked_placex;
+ END LOOP;
+ END IF;
+
+ IF bnd.extratags ? 'wikidata' THEN
+ FOR linked_placex IN
+ SELECT * FROM placex
+ WHERE placex.class = 'place' AND placex.osm_type = 'N'
+ AND placex.extratags ? 'wikidata' -- needed to select right index
+ AND placex.extratags->'wikidata' = bnd.extratags->'wikidata'
+ AND placex.rank_search < 26
+ AND _st_covers(bnd.geometry, placex.geometry)
+ ORDER BY make_standard_name(name->'name') = bnd_name desc
+ LOOP
+ --DEBUG: RAISE WARNING 'Found wikidata-matching place node %', linked_placex.osm_id;
+ RETURN linked_placex;
+ END LOOP;
+ END IF;
+
+ -- Name searches can be done for ways as well as relations
+ IF bnd_name is not null THEN
+ --DEBUG: RAISE WARNING 'Looking for nodes with matching names';
+ FOR linked_placex IN
+ SELECT placex.* from placex
+ WHERE make_standard_name(name->'name') = bnd_name
+ AND ((bnd.rank_address > 0 and placex.rank_address = bnd.rank_address)
+ OR (bnd.rank_address = 0 and placex.rank_search = bnd.rank_search))
+ AND placex.osm_type = 'N'
+ AND placex.rank_search < 26 -- needed to select the right index
+ AND _st_covers(bnd.geometry, placex.geometry)
+ LOOP
+ --DEBUG: RAISE WARNING 'Found matching place node %', linked_placex.osm_id;
+ RETURN linked_placex;
+ END LOOP;
+ END IF;
+
+ RETURN NULL;
+END;
+$$
+LANGUAGE plpgsql STABLE;
+
+
+-- 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 geoemtry 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
+ current_rank_address INTEGER := 0;
+ location_distance FLOAT := 0;
+ location_parent GEOMETRY := NULL;
+ parent_place_id_rank SMALLINT := 0;
+
+ location_isaddress BOOLEAN;
+
+ address_havelevel BOOLEAN[];
+ location_keywords INT[];
+
+ location RECORD;
+ addr_item RECORD;
+
+ isin_tokens INT[];
+ isin TEXT[];
+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;
+
+ IF address ? 'is_in' THEN
+ -- is_in items need splitting
+ isin := regexp_split_to_array(address->'is_in', E'[;,]');
+ IF array_upper(isin, 1) IS NOT NULL THEN
+ FOR i IN 1..array_upper(isin, 1) LOOP
+ isin_tokens := array_merge(isin_tokens,
+ word_ids_from_name(isin[i]));
+
+ -- merge word into address vector
+ IF NOT %REVERSE-ONLY% THEN
+ nameaddress_vector := array_merge(nameaddress_vector,
+ addr_ids_from_name(isin[i]));
+ END IF;
+ END LOOP;
+ END IF;
+ END IF;
+ 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..28 LOOP
+ address_havelevel[i] := false;
+ END LOOP;
+
+ FOR location IN
+ SELECT * FROM getNearFeatures(partition, geometry, maxrank, isin_tokens)
+ LOOP
+ IF location.rank_address != current_rank_address THEN
+ current_rank_address := location.rank_address;
+ IF location.isguess THEN
+ location_distance := location.distance * 1.5;
+ ELSE
+ IF location.rank_address <= 12 THEN
+ -- for county and above, if we have an area consider that exact
+ -- (It would be nice to relax the constraint for places close to
+ -- the boundary but we'd need the exact geometry for that. Too
+ -- expensive.)
+ location_distance = 0;
+ ELSE
+ -- Below county level remain slightly fuzzy.
+ location_distance := location.distance * 0.5;
+ END IF;
+ END IF;
+ ELSE
+ CONTINUE WHEN location.keywords <@ location_keywords;
+ END IF;
+
+ IF location.distance < location_distance OR NOT location.isguess THEN
+ location_keywords := location.keywords;
+
+ location_isaddress := NOT address_havelevel[location.rank_address];
+ --DEBUG: RAISE WARNING 'should be address: %, is guess: %, rank: %', location_isaddress, location.isguess, location.rank_address;
+ IF location_isaddress AND location.isguess AND location_parent IS NOT NULL THEN
+ location_isaddress := ST_Contains(location_parent, location.centroid);
+ END IF;
+
+ --DEBUG: RAISE WARNING '% isaddress: %', location.place_id, location_isaddress;
+ -- 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);
+
+ IF location_isaddress THEN
+ -- add postcode if we have one
+ -- (If multiple postcodes are available, we end up with the highest ranking one.)
+ IF location.postcode is not null THEN
+ postcode = location.postcode;
+ END IF;
+
+ address_havelevel[location.rank_address] := true;
+ -- add a hack against postcode ranks
+ IF NOT location.isguess
+ AND location.rank_address != 11 AND location.rank_address != 5
+ THEN
+ SELECT p.geometry FROM placex p
+ WHERE p.place_id = location.place_id INTO location_parent;
+ END IF;
+
+ IF location.rank_address > parent_place_id_rank THEN
+ parent_place_id = location.place_id;
+ parent_place_id_rank = location.rank_address;
+ END IF;
+ END IF;
+ END IF;
+
+ END LOOP;
+END;
+$$
+LANGUAGE plpgsql;
+
+