+ -- Get the existing placex entry.
+ SELECT * INTO existingplacex
+ FROM placex
+ WHERE osm_type = NEW.osm_type and osm_id = NEW.osm_id
+ and class = NEW.class and type = NEW.type;
+
+ {% if debug %}RAISE WARNING 'Existing PlaceX: %',existingplacex.place_id;{% endif %}
+
+ -- To paraphrase: if there isn't an existing item, OR if the admin level has changed
+ IF existingplacex.osm_type IS NULL
+ or (existingplacex.class = 'boundary'
+ and ((coalesce(existingplacex.admin_level, 15) != coalesce(NEW.admin_level, 15)
+ and existingplacex.type = 'administrative')
+ or existingplacex.type != NEW.type))
+ THEN
+ {% if config.get_bool('LIMIT_REINDEXING') %}
+ -- sanity check: ignore admin_level changes on places with too many active children
+ -- or we end up reindexing entire countries because somebody accidentally deleted admin_level
+ IF existingplacex.osm_type IS NOT NULL THEN
+ SELECT count(*) INTO i FROM
+ (SELECT 'a' FROM placex, place_addressline
+ WHERE address_place_id = existingplacex.place_id
+ and placex.place_id = place_addressline.place_id
+ and indexed_status = 0 and place_addressline.isaddress LIMIT 100001) sub;
+ IF i > 100000 THEN
+ RETURN null;
+ END IF;
+ END IF;
+ {% endif %}
+
+ IF existing.osm_type IS NOT NULL THEN
+ -- Pathological case caused by the triggerless copy into place during initial import
+ -- force delete even for large areas, it will be reinserted later
+ UPDATE place SET geometry = ST_SetSRID(ST_Point(0,0), 4326)
+ WHERE osm_type = NEW.osm_type and osm_id = NEW.osm_id
+ and class = NEW.class and type = NEW.type;
+ DELETE FROM place
+ WHERE osm_type = NEW.osm_type and osm_id = NEW.osm_id
+ and class = NEW.class and type = NEW.type;