+ ELSEIF NEW.class = 'mountain_pass' THEN
+ NEW.rank_search := 20;
+ NEW.rank_address := 0;
+ END IF;
+
+ END IF;
+
+ IF NEW.rank_search > 30 THEN
+ NEW.rank_search := 30;
+ END IF;
+
+ IF NEW.rank_address > 30 THEN
+ NEW.rank_address := 30;
+ END IF;
+
+ IF (NEW.extratags -> 'capital') = 'yes' THEN
+ NEW.rank_search := NEW.rank_search - 1;
+ END IF;
+
+ -- a country code make no sense below rank 4 (country)
+ IF NEW.rank_search < 4 THEN
+ NEW.country_code := NULL;
+ END IF;
+
+-- Block import below rank 22
+-- IF NEW.rank_search > 22 THEN
+-- RETURN NULL;
+-- END IF;
+
+ --DEBUG: RAISE WARNING 'placex_insert:END: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
+
+ RETURN NEW; -- %DIFFUPDATES% The following is not needed until doing diff updates, and slows the main index process down
+
+ IF NEW.rank_address > 0 THEN
+ IF (ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon') AND ST_IsValid(NEW.geometry)) THEN
+ -- Performance: We just can't handle re-indexing for country level changes
+ IF st_area(NEW.geometry) < 1 THEN
+ -- mark items within the geometry for re-indexing
+ -- RAISE WARNING 'placex poly insert: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
+
+ -- work around bug in postgis, this may have been fixed in 2.0.0 (see http://trac.osgeo.org/postgis/ticket/547)
+ update placex set indexed_status = 2 where (st_covers(NEW.geometry, placex.geometry) OR ST_Intersects(NEW.geometry, placex.geometry))
+ 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'));
+ update placex set indexed_status = 2 where (st_covers(NEW.geometry, placex.geometry) OR ST_Intersects(NEW.geometry, placex.geometry))
+ 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'));
+ END IF;
+ ELSE
+ -- mark nearby items for re-indexing, where 'nearby' depends on the features rank_search and is a complete guess :(
+ diameter := 0;
+ -- 16 = city, anything higher than city is effectively ignored (polygon required!)
+ IF NEW.type='postcode' THEN
+ diameter := 0.05;
+ ELSEIF NEW.rank_search < 16 THEN
+ diameter := 0;
+ ELSEIF NEW.rank_search < 18 THEN
+ diameter := 0.1;
+ ELSEIF NEW.rank_search < 20 THEN
+ diameter := 0.05;
+ ELSEIF NEW.rank_search = 21 THEN
+ diameter := 0.001;
+ ELSEIF NEW.rank_search < 24 THEN
+ diameter := 0.02;
+ ELSEIF NEW.rank_search < 26 THEN
+ diameter := 0.002; -- 100 to 200 meters
+ ELSEIF NEW.rank_search < 28 THEN
+ diameter := 0.001; -- 50 to 100 meters
+ END IF;
+ IF diameter > 0 THEN
+ -- RAISE WARNING 'placex point insert: % % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type,diameter;
+ IF NEW.rank_search >= 26 THEN
+ -- roads may cause reparenting for >27 rank places
+ update placex set indexed_status = 2 where indexed_status = 0 and rank_search > NEW.rank_search and ST_DWithin(placex.geometry, NEW.geometry, diameter);
+ -- reparenting also for OSM Interpolation Lines (and for Tiger?)
+ update location_property_osmline set indexed_status = 2 where indexed_status = 0 and ST_DWithin(location_property_osmline.linegeo, NEW.geometry, diameter);
+ ELSEIF NEW.rank_search >= 16 THEN
+ -- up to rank 16, street-less addresses may need reparenting
+ 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');
+ ELSE
+ -- for all other places the search terms may change as well
+ 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);
+ END IF;
+ END IF;
+ END IF;
+ END IF;
+
+
+ -- add to tables for special search
+ -- Note: won't work on initial import because the classtype tables
+ -- do not yet exist. It won't hurt either.
+ classtable := 'place_classtype_' || NEW.class || '_' || NEW.type;
+ SELECT count(*)>0 FROM pg_tables WHERE tablename = classtable and schemaname = current_schema() INTO result;
+ IF result THEN
+ EXECUTE 'INSERT INTO ' || classtable::regclass || ' (place_id, centroid) VALUES ($1,$2)'
+ USING NEW.place_id, ST_Centroid(NEW.geometry);
+ END IF;
+
+ RETURN NEW;
+
+END;
+$$
+LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION osmline_update() RETURNS
+TRIGGER
+ AS $$
+DECLARE
+ place_centroid GEOMETRY;
+ waynodes BIGINT[];
+ prevnode RECORD;
+ nextnode RECORD;
+ startnumber INTEGER;
+ endnumber INTEGER;
+ housenum INTEGER;
+ linegeo GEOMETRY;
+ splitline GEOMETRY;
+ sectiongeo GEOMETRY;
+ postcode TEXT;
+ seg_postcode TEXT;
+BEGIN
+ -- deferred delete
+ IF OLD.indexed_status = 100 THEN
+ delete from location_property_osmline where place_id = OLD.place_id;
+ RETURN NULL;
+ END IF;
+
+ IF NEW.indexed_status != 0 OR OLD.indexed_status = 0 THEN
+ RETURN NEW;
+ END IF;
+
+ NEW.interpolationtype = NEW.address->'interpolation';
+
+ place_centroid := ST_PointOnSurface(NEW.linegeo);
+ NEW.parent_place_id = get_interpolation_parent(NEW.osm_id, NEW.address->'street',
+ NEW.address->'place',
+ NEW.partition, place_centroid, NEW.linegeo);
+
+
+ IF NEW.address is not NULL and NEW.address ? 'postcode' THEN
+ NEW.postcode = NEW.address->'postcode';
+ END IF;
+
+ -- if the line was newly inserted, split the line as necessary
+ IF OLD.indexed_status = 1 THEN
+ select nodes from planet_osm_ways where id = NEW.osm_id INTO waynodes;
+
+ IF array_upper(waynodes, 1) IS NULL THEN
+ RETURN NEW;
+ END IF;
+
+ linegeo := NEW.linegeo;
+ startnumber := NULL;
+ postcode := NEW.postcode;
+
+ FOR nodeidpos in 1..array_upper(waynodes, 1) LOOP
+
+ select osm_id, address, geometry
+ from place where osm_type = 'N' and osm_id = waynodes[nodeidpos]::BIGINT
+ and address is not NULL and address ? 'housenumber' limit 1 INTO nextnode;
+ --RAISE NOTICE 'Nextnode.place_id: %s', nextnode.place_id;
+ IF nextnode.osm_id IS NOT NULL THEN
+ --RAISE NOTICE 'place_id is not null';
+ IF nodeidpos > 1 and nodeidpos < array_upper(waynodes, 1) THEN
+ -- Make sure that the point is actually on the line. That might
+ -- be a bit paranoid but ensures that the algorithm still works
+ -- should osm2pgsql attempt to repair geometries.
+ splitline := split_line_on_node(linegeo, nextnode.geometry);
+ sectiongeo := ST_GeometryN(splitline, 1);
+ linegeo := ST_GeometryN(splitline, 2);
+ ELSE
+ sectiongeo = linegeo;
+ END IF;
+ endnumber := substring(nextnode.address->'housenumber','[0-9]+')::integer;
+
+ IF startnumber IS NOT NULL AND endnumber IS NOT NULL
+ AND startnumber != endnumber
+ AND ST_GeometryType(sectiongeo) = 'ST_LineString' THEN
+
+ IF (startnumber > endnumber) THEN
+ housenum := endnumber;
+ endnumber := startnumber;
+ startnumber := housenum;
+ sectiongeo := ST_Reverse(sectiongeo);
+ END IF;
+
+ seg_postcode := coalesce(postcode,
+ prevnode.address->'postcode',
+ nextnode.address->'postcode');
+
+ IF NEW.startnumber IS NULL THEN
+ NEW.startnumber := startnumber;
+ NEW.endnumber := endnumber;
+ NEW.linegeo := sectiongeo;
+ NEW.postcode := seg_postcode;
+ ELSE
+ insert into location_property_osmline
+ (linegeo, partition, osm_id, parent_place_id,
+ startnumber, endnumber, interpolationtype,
+ address, postcode, country_code,
+ geometry_sector, indexed_status)
+ values (sectiongeo, NEW.partition, NEW.osm_id, NEW.parent_place_id,
+ startnumber, endnumber, NEW.interpolationtype,
+ NEW.address, seg_postcode,
+ NEW.country_code, NEW.geometry_sector, 0);
+ END IF;
+ END IF;
+
+ -- early break if we are out of line string,
+ -- might happen when a line string loops back on itself
+ IF ST_GeometryType(linegeo) != 'ST_LineString' THEN
+ RETURN NEW;
+ END IF;
+
+ startnumber := substring(nextnode.address->'housenumber','[0-9]+')::integer;
+ prevnode := nextnode;
+ END IF;
+ END LOOP;
+ END IF;
+
+ -- marking descendants for reparenting is not needed, because there are
+ -- actually no descendants for interpolation lines
+ RETURN NEW;
+END;
+$$
+LANGUAGE plpgsql;
+
+
+
+CREATE OR REPLACE FUNCTION placex_update() RETURNS
+TRIGGER
+ AS $$
+DECLARE
+
+ place_centroid GEOMETRY;
+
+ search_maxdistance FLOAT[];
+ search_mindistance FLOAT[];
+ address_havelevel BOOLEAN[];
+
+ i INTEGER;
+ iMax FLOAT;
+ location RECORD;
+ way RECORD;
+ relation RECORD;
+ relation_members TEXT[];
+ relMember RECORD;
+ linkedplacex RECORD;
+ search_diameter FLOAT;
+ search_prevdiameter FLOAT;
+ search_maxrank INTEGER;
+ address_maxrank INTEGER;
+ address_street_word_id INTEGER;
+ address_street_word_ids INTEGER[];
+ parent_place_id_rank BIGINT;
+
+ addr_street TEXT;
+ addr_place TEXT;
+
+ isin TEXT[];
+ isin_tokens INT[];
+
+ location_rank_search INTEGER;
+ location_distance FLOAT;
+ location_parent GEOMETRY;
+ location_isaddress BOOLEAN;
+ location_keywords INTEGER[];
+
+ default_language TEXT;
+ name_vector INTEGER[];
+ nameaddress_vector INTEGER[];
+
+ linked_node_id BIGINT;
+ linked_importance FLOAT;
+ linked_wikipedia TEXT;
+
+ result BOOLEAN;
+BEGIN
+ -- deferred delete
+ IF OLD.indexed_status = 100 THEN
+ --DEBUG: RAISE WARNING 'placex_update_delete % %',NEW.osm_type,NEW.osm_id;
+ delete from placex where place_id = OLD.place_id;
+ RETURN NULL;
+ END IF;
+
+ IF NEW.indexed_status != 0 OR OLD.indexed_status = 0 THEN
+ RETURN NEW;
+ END IF;
+
+ --DEBUG: RAISE WARNING 'placex_update % %',NEW.osm_type,NEW.osm_id;
+
+--RAISE WARNING '%',NEW.place_id;
+--RAISE WARNING '%', NEW;
+
+ IF NEW.class = 'place' AND NEW.type = 'postcodearea' THEN
+ -- Silently do nothing
+ RETURN NEW;
+ END IF;
+
+ NEW.indexed_date = now();
+
+ result := deleteSearchName(NEW.partition, NEW.place_id);
+ DELETE FROM place_addressline WHERE place_id = NEW.place_id;
+ result := deleteRoad(NEW.partition, NEW.place_id);
+ result := deleteLocationArea(NEW.partition, NEW.place_id, NEW.rank_search);
+ UPDATE placex set linked_place_id = null, indexed_status = 2
+ where linked_place_id = NEW.place_id;
+ -- update not necessary for osmline, cause linked_place_id does not exist
+
+ IF NEW.linked_place_id is not null THEN
+ RETURN NEW;
+ END IF;
+
+ IF NEW.address is not NULL THEN
+ IF NEW.address ? 'conscriptionnumber' THEN
+ i := getorcreate_housenumber_id(make_standard_name(NEW.address->'conscriptionnumber'));
+ IF NEW.address ? 'streetnumber' THEN
+ i := getorcreate_housenumber_id(make_standard_name(NEW.address->'streetnumber'));
+ NEW.housenumber := (NEW.address->'conscriptionnumber') || '/' || (NEW.address->'streetnumber');
+ ELSE
+ NEW.housenumber := NEW.address->'conscriptionnumber';
+ END IF;
+ ELSEIF NEW.address ? 'streetnumber' THEN
+ NEW.housenumber := NEW.address->'streetnumber';
+ i := getorcreate_housenumber_id(make_standard_name(NEW.address->'streetnumber'));
+ ELSEIF NEW.address ? 'housenumber' THEN
+ NEW.housenumber := NEW.address->'housenumber';
+ i := getorcreate_housenumber_id(make_standard_name(NEW.housenumber));
+ END IF;
+
+ addr_street = NEW.address->'street';
+ addr_place = NEW.address->'place';
+
+ NEW.postcode = NEW.address->'postcode';
+ END IF;
+
+ -- Speed up searches - just use the centroid of the feature
+ -- cheaper but less acurate
+ place_centroid := ST_PointOnSurface(NEW.geometry);
+ NEW.centroid := null;
+
+ -- recalculate country and partition
+ IF NEW.rank_search = 4 AND NEW.address is not NULL AND NEW.address ? 'country' THEN
+ -- for countries, believe the mapped country code,
+ -- so that we remain in the right partition if the boundaries
+ -- suddenly expand.
+ NEW.country_code := lower(NEW.address->'country');
+ NEW.partition := get_partition(lower(NEW.country_code));
+ IF NEW.partition = 0 THEN
+ NEW.country_code := lower(get_country_code(place_centroid));
+ NEW.partition := get_partition(NEW.country_code);
+ END IF;
+ ELSE
+ IF NEW.rank_search >= 4 THEN
+ NEW.country_code := lower(get_country_code(place_centroid));
+ ELSE
+ NEW.country_code := NULL;
+ END IF;
+ NEW.partition := get_partition(NEW.country_code);
+ END IF;
+
+ -- waterway ways are linked when they are part of a relation and have the same class/type
+ IF NEW.osm_type = 'R' and NEW.class = 'waterway' THEN
+ FOR relation_members IN select members from planet_osm_rels r where r.id = NEW.osm_id and r.parts != array[]::bigint[]
+ LOOP
+ FOR i IN 1..array_upper(relation_members, 1) BY 2 LOOP
+ IF relation_members[i+1] in ('', 'main_stream', 'side_stream') AND substring(relation_members[i],1,1) = 'w' THEN
+ --DEBUG: RAISE WARNING 'waterway parent %, child %/%', NEW.osm_id, i, relation.members[i];
+ FOR linked_node_id IN SELECT place_id FROM placex
+ WHERE osm_type = 'W' and osm_id = substring(relation_members[i],2,200)::bigint
+ and class = NEW.class and type = NEW.type
+ and ( relation_members[i+1] != 'side_stream' or NEW.name->'name' = name->'name')
+ LOOP
+ UPDATE placex SET linked_place_id = NEW.place_id WHERE place_id = linked_node_id;
+ END LOOP;
+ END IF;
+ END LOOP;
+ END LOOP;
+ END IF;
+
+ -- Adding ourselves to the list simplifies address calculations later
+ INSERT INTO place_addressline (place_id, address_place_id, fromarea, isaddress, distance, cached_rank_address)
+ VALUES (NEW.place_id, NEW.place_id, true, true, 0, NEW.rank_address);
+
+ -- What level are we searching from
+ search_maxrank := NEW.rank_search;
+
+ -- Thought this wasn't needed but when we add new languages to the country_name table
+ -- we need to update the existing names
+ IF NEW.name is not null AND array_upper(akeys(NEW.name),1) > 1 THEN
+ default_language := get_country_language_code(NEW.country_code);
+ IF default_language IS NOT NULL THEN
+ IF NEW.name ? 'name' AND NOT NEW.name ? ('name:'||default_language) THEN
+ NEW.name := NEW.name || hstore(('name:'||default_language), (NEW.name -> 'name'));
+ ELSEIF NEW.name ? ('name:'||default_language) AND NOT NEW.name ? 'name' THEN
+ NEW.name := NEW.name || hstore('name', (NEW.name -> ('name:'||default_language)));
+ END IF;
+ END IF;
+ END IF;
+
+ -- Initialise the name vector using our name
+ name_vector := make_keywords(NEW.name);
+ nameaddress_vector := '{}'::int[];
+
+ FOR i IN 1..28 LOOP
+ address_havelevel[i] := false;
+ END LOOP;
+
+ NEW.importance := null;
+ select language||':'||title,importance from get_wikipedia_match(NEW.extratags, NEW.country_code) INTO NEW.wikipedia,NEW.importance;
+ IF NEW.importance IS NULL THEN
+ select language||':'||title,importance from wikipedia_article where osm_type = NEW.osm_type and osm_id = NEW.osm_id order by importance desc limit 1 INTO NEW.wikipedia,NEW.importance;
+ END IF;
+
+--RAISE WARNING 'before low level% %', NEW.place_id, NEW.rank_search;
+
+ -- ---------------------------------------------------------------------------
+ -- For low level elements we inherit from our parent road
+ IF (NEW.rank_search > 27 OR (NEW.type = 'postcode' AND NEW.rank_search = 25)) THEN
+
+--RAISE WARNING 'finding street for %', NEW;
+
+ -- We won't get a better centroid, besides these places are too small to care
+ NEW.centroid := place_centroid;
+
+ NEW.parent_place_id := null;
+
+ -- if we have a POI and there is no address information,
+ -- see if we can get it from a surrounding building
+ IF NEW.osm_type = 'N' AND addr_street IS NULL AND addr_place IS NULL
+ AND NEW.housenumber IS NULL THEN
+ FOR location IN select * from placex where ST_Covers(geometry, place_centroid)
+ and address is not null
+ and (address ? 'housenumber' or address ? 'street' or address ? 'place')
+ and rank_search > 28 AND ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon')
+ limit 1
+ LOOP
+ NEW.housenumber := location.address->'housenumber';
+ addr_street := location.address->'street';
+ addr_place := location.address->'place';
+ END LOOP;
+ END IF;
+
+ -- We have to find our parent road.
+ -- Copy data from linked items (points on ways, addr:street links, relations)
+
+ -- Is this object part of a relation?
+ FOR relation IN select * from planet_osm_rels where parts @> ARRAY[NEW.osm_id] and members @> ARRAY[lower(NEW.osm_type)||NEW.osm_id]
+ LOOP
+ -- At the moment we only process one type of relation - associatedStreet
+ IF relation.tags @> ARRAY['associatedStreet'] THEN
+ FOR i IN 1..array_upper(relation.members, 1) BY 2 LOOP
+ IF NEW.parent_place_id IS NULL AND relation.members[i+1] = 'street' THEN
+--RAISE WARNING 'node in relation %',relation;
+ SELECT place_id from placex where osm_type = 'W'
+ and osm_id = substring(relation.members[i],2,200)::bigint
+ and rank_search = 26 and name is not null INTO NEW.parent_place_id;
+ END IF;
+ END LOOP;
+ END IF;
+ END LOOP;
+
+
+ -- Note that addr:street links can only be indexed once the street itself is indexed
+ IF NEW.parent_place_id IS NULL AND addr_street IS NOT NULL THEN
+ address_street_word_ids := get_name_ids(make_standard_name(addr_street));
+ IF address_street_word_ids IS NOT NULL THEN
+ FOR location IN SELECT * from getNearestNamedRoadFeature(NEW.partition, place_centroid, address_street_word_ids) LOOP
+ NEW.parent_place_id := location.place_id;
+ END LOOP;
+ END IF;
+ END IF;
+
+ IF NEW.parent_place_id IS NULL AND addr_place IS NOT NULL THEN
+ address_street_word_ids := get_name_ids(make_standard_name(addr_place));
+ IF address_street_word_ids IS NOT NULL THEN
+ FOR location IN SELECT * from getNearestNamedPlaceFeature(NEW.partition, place_centroid, address_street_word_ids) LOOP
+ NEW.parent_place_id := location.place_id;
+ END LOOP;
+ END IF;
+ END IF;
+
+ -- Is this node part of an interpolation?
+ IF NEW.parent_place_id IS NULL AND NEW.osm_type = 'N' THEN
+ FOR location IN
+ SELECT q.parent_place_id FROM location_property_osmline q, planet_osm_ways x
+ WHERE q.linegeo && NEW.geometry and x.id = q.osm_id and NEW.osm_id = any(x.nodes)
+ LIMIT 1
+ LOOP
+ NEW.parent_place_id := location.parent_place_id;
+ END LOOP;
+ END IF;
+
+ -- Is this node part of a way?
+ IF NEW.parent_place_id IS NULL AND NEW.osm_type = 'N' THEN
+
+ FOR location IN select p.place_id, p.osm_id, p.parent_place_id, p.rank_search, p.address from placex p, planet_osm_ways w
+ 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)
+ LOOP
+
+ -- Way IS a road then we are on it - that must be our road
+ IF location.rank_search < 28 AND NEW.parent_place_id IS NULL THEN
+--RAISE WARNING 'node in way that is a street %',location;
+ NEW.parent_place_id := location.place_id;
+ END IF;
+
+ -- If the way mentions a street or place address, try that for parenting.
+ IF NEW.parent_place_id IS NULL AND location.address ? 'street' THEN
+ address_street_word_ids := get_name_ids(make_standard_name(location.address->'street'));
+ IF address_street_word_ids IS NOT NULL THEN
+ FOR linkedplacex IN SELECT place_id from getNearestNamedRoadFeature(NEW.partition, place_centroid, address_street_word_ids) LOOP
+ NEW.parent_place_id := linkedplacex.place_id;
+ END LOOP;
+ END IF;
+ END IF;
+
+ IF NEW.parent_place_id IS NULL AND location.address ? 'place' THEN
+ address_street_word_ids := get_name_ids(make_standard_name(location.address->'place'));
+ IF address_street_word_ids IS NOT NULL THEN
+ FOR linkedplacex IN SELECT place_id from getNearestNamedPlaceFeature(NEW.partition, place_centroid, address_street_word_ids) LOOP
+ NEW.parent_place_id := linkedplacex.place_id;
+ END LOOP;
+ END IF;
+ END IF;
+
+ -- Is the WAY part of a relation
+ IF NEW.parent_place_id IS NULL THEN
+ FOR relation IN select * from planet_osm_rels where parts @> ARRAY[location.osm_id] and members @> ARRAY['w'||location.osm_id]
+ LOOP
+ -- At the moment we only process one type of relation - associatedStreet
+ IF relation.tags @> ARRAY['associatedStreet'] AND array_upper(relation.members, 1) IS NOT NULL THEN
+ FOR i IN 1..array_upper(relation.members, 1) BY 2 LOOP
+ IF NEW.parent_place_id IS NULL AND relation.members[i+1] = 'street' THEN
+ --RAISE WARNING 'node in way that is in a relation %',relation;
+ SELECT place_id from placex where osm_type='W' and osm_id = substring(relation.members[i],2,200)::bigint
+ and rank_search = 26 and name is not null INTO NEW.parent_place_id;
+ END IF;
+ END LOOP;
+ END IF;
+ END LOOP;
+ END IF;
+
+ END LOOP;
+
+ END IF;
+
+--RAISE WARNING 'x4 %',NEW.parent_place_id;
+ -- Still nothing, just use the nearest road
+ IF NEW.parent_place_id IS NULL THEN
+ FOR location IN SELECT place_id FROM getNearestRoadFeature(NEW.partition, place_centroid) LOOP
+ NEW.parent_place_id := location.place_id;
+ END LOOP;
+ END IF;
+
+--return NEW;
+--RAISE WARNING 'x6 %',NEW.parent_place_id;
+
+ -- If we didn't find any road fallback to standard method
+ IF NEW.parent_place_id IS NOT NULL THEN
+
+ -- Get the details of the parent road
+ select * from search_name where place_id = NEW.parent_place_id INTO location;
+ NEW.country_code := location.country_code;
+
+ -- Merge the postcode into the parent's address if necessary
+ IF NEW.postcode IS NOT NULL THEN
+ isin_tokens := '{}'::int[];
+ address_street_word_id := getorcreate_word_id(make_standard_name(NEW.postcode));
+ IF address_street_word_id is not null
+ and not ARRAY[address_street_word_id] <@ location.nameaddress_vector THEN
+ isin_tokens := isin_tokens || address_street_word_id;
+ END IF;
+ address_street_word_id := getorcreate_name_id(make_standard_name(NEW.postcode));
+ IF address_street_word_id is not null
+ and not ARRAY[address_street_word_id] <@ location.nameaddress_vector THEN
+ isin_tokens := isin_tokens || address_street_word_id;
+ END IF;
+ IF isin_tokens != '{}'::int[] THEN
+ UPDATE search_name
+ SET nameaddress_vector = search_name.nameaddress_vector || isin_tokens
+ WHERE place_id = NEW.parent_place_id;
+ END IF;
+ END IF;
+
+--RAISE WARNING '%', NEW.name;
+ -- If there is no name it isn't searchable, don't bother to create a search record
+ IF NEW.name is NULL THEN
+ return NEW;
+ END IF;
+
+ -- Merge address from parent
+ nameaddress_vector := array_merge(nameaddress_vector, location.nameaddress_vector);
+ nameaddress_vector := array_merge(nameaddress_vector, location.name_vector);
+
+ -- Performance, it would be more acurate to do all the rest of the import
+ -- process but it takes too long
+ -- Just be happy with inheriting from parent road only
+
+ IF NEW.rank_search <= 25 and NEW.rank_address > 0 THEN
+ result := add_location(NEW.place_id, NEW.country_code, NEW.partition, name_vector, NEW.rank_search, NEW.rank_address, NEW.geometry);
+ END IF;
+
+ result := insertSearchName(NEW.partition, NEW.place_id, NEW.country_code, name_vector, nameaddress_vector, NEW.rank_search, NEW.rank_address, NEW.importance, place_centroid, NEW.geometry);
+
+ return NEW;