- -- Have we already done this place?
- select * from place where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type INTO existing;
-
- -- Get the existing place_id
- select * from placex where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type INTO existingplacex;
-
- -- Handle a place changing type by removing the old data
- -- My generated 'place' types are causing havok because they overlap with real keys
- -- TODO: move them to their own special purpose key/class to avoid collisions
- IF existing.osm_type IS NULL AND (NEW.type not in ('postcode','house','houses')) THEN
- DELETE FROM place where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type not in ('postcode','house','houses');
- END IF;
-
- --DEBUG: RAISE WARNING 'Existing: %',existing.osm_id;
- --DEBUG: RAISE WARNING 'Existing PlaceX: %',existingplacex.place_id;
+ -- Get the existing place_id
+ select * from location_property_osmline where osm_id = NEW.osm_id INTO existingline;
+
+ -- Handle a place changing type by removing the old data (this trigger is executed BEFORE INSERT of the NEW tupel)
+ -- My generated 'place' types are causing havok because they overlap with real keys
+ -- TODO: move them to their own special purpose key/class to avoid collisions
+ IF existing.osm_type IS NULL THEN
+ DELETE FROM place where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class;
+ END IF;
+
+ DELETE from import_polygon_error where osm_type = NEW.osm_type and osm_id = NEW.osm_id;
+ DELETE from import_polygon_delete where osm_type = NEW.osm_type and osm_id = NEW.osm_id;
+
+ -- To paraphrase, if there isn't an existing item
+ IF existingline.osm_id IS NULL THEN
+ 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;
+ END IF;
+ -- insert new line into location_property_osmline, use function insert_osmline
+ i = insert_osmline(NEW.osm_id, NEW.housenumber, NEW.street, NEW.addr_place, NEW.postcode, NEW.country_code, NEW.geometry);
+ RETURN NEW;
+ END IF;
+ IF coalesce(existing.name::text, '') != coalesce(NEW.name::text, '')
+ OR coalesce(existing.extratags::text, '') != coalesce(NEW.extratags::text, '')
+ OR coalesce(existing.housenumber, '') != coalesce(NEW.housenumber, '')
+ OR coalesce(existing.street, '') != coalesce(NEW.street, '')
+ OR coalesce(existing.addr_place, '') != coalesce(NEW.addr_place, '')
+ OR coalesce(existing.isin, '') != coalesce(NEW.isin, '')
+ OR coalesce(existing.postcode, '') != coalesce(NEW.postcode, '')
+ OR coalesce(existing.country_code, '') != coalesce(NEW.country_code, '')
+ OR coalesce(existing.admin_level, 15) != coalesce(NEW.admin_level, 15)
+ OR existing.geometry::text != NEW.geometry::text
+ THEN
+
+ update place set
+ name = NEW.name,
+ housenumber = NEW.housenumber,
+ street = NEW.street,
+ addr_place = NEW.addr_place,
+ isin = NEW.isin,
+ postcode = NEW.postcode,
+ country_code = NEW.country_code,
+ extratags = NEW.extratags,
+ admin_level = NEW.admin_level,
+ geometry = NEW.geometry
+ where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type;
+
+ -- update method for interpolation lines: delete all old interpolation lines with same osm_id (update on place) and insert the new one(s) (they can be split up, if they have > 2 nodes)
+ delete from location_property_osmline where osm_id = NEW.osm_id;
+ i = insert_osmline(NEW.osm_id, NEW.housenumber, NEW.street, NEW.addr_place, NEW.postcode, NEW.country_code, NEW.geometry);
+ END IF;
+
+ -- for interpolations invalidate all nodes on the line
+ update placex p set indexed_status = 2 from planet_osm_ways w where w.id = NEW.osm_id and p.osm_type = 'N' and p.osm_id = any(w.nodes);
+ RAISE WARNING 'X3399 - updated nodes of interpolation line';
+ RETURN NULL;
+
+ ELSE -- insert to placex
+
+ IF FALSE and NEW.osm_type = 'R' THEN
+ select * from placex where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type INTO existingplacex;
+ --DEBUG: RAISE WARNING '%', existingplacex;
+ END IF;
+ -- Patch in additional country names
+ IF NEW.admin_level = 2 AND NEW.type = 'administrative' AND NEW.country_code is not null THEN
+ select coalesce(country_name.name || NEW.name,NEW.name) from country_name where country_name.country_code = lower(NEW.country_code) INTO NEW.name;
+ END IF;
+
+ -- Have we already done this place?
+ select * from place where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type INTO existing;