-BEGIN
-
- IF FALSE and NEW.osm_type = 'R' THEN
- RAISE WARNING '-----------------------------------------------------------------------------------';
- RAISE WARNING 'place_insert: % % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type,st_area(NEW.geometry);
- 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;
- RAISE WARNING '%', existingplacex;
- END IF;
-
- -- Just block these - lots and pointless
- IF NEW.class = 'highway' and NEW.type in ('turning_circle','traffic_signals','mini_roundabout','noexit','crossing') THEN
- RETURN null;
- END IF;
- IF NEW.class in ('landuse','natural') and NEW.name is null THEN
- RETURN null;
- END IF;
-
- IF ST_IsEmpty(NEW.geometry) OR NOT ST_IsValid(NEW.geometry) OR ST_X(ST_Centroid(NEW.geometry))::text in ('NaN','Infinity','-Infinity') OR ST_Y(ST_Centroid(NEW.geometry))::text in ('NaN','Infinity','-Infinity') THEN
- INSERT INTO import_polygon_error values (NEW.osm_type, NEW.osm_id, NEW.class, NEW.type, NEW.name, NEW.country_code,
- now(), ST_IsValidReason(NEW.geometry), null, NEW.geometry);
--- RAISE WARNING 'Invalid Geometry: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
- RETURN null;
- 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 country_name.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;
-
- -- 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 tags
- -- 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;
-
--- RAISE WARNING 'Existing: %',existing.place_id;
-
- -- Log and discard
- IF existing.geometry is not null AND st_isvalid(existing.geometry)
- AND st_area(existing.geometry) > 0.02
- AND ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon')
- AND st_area(NEW.geometry) < st_area(existing.geometry)*0.5
- THEN
- INSERT INTO import_polygon_error values (NEW.osm_type, NEW.osm_id, NEW.class, NEW.type, NEW.name, NEW.country_code, now(),
- 'Area reduced from '||st_area(existing.geometry)||' to '||st_area(NEW.geometry), existing.geometry, NEW.geometry);
- RETURN null;
- 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, OR if the admin level has changed, OR if it is a major change in geometry
- IF existing.osm_type IS NULL
- OR existingplacex.osm_type IS NULL
- OR coalesce(existing.admin_level, 100) != coalesce(NEW.admin_level, 100)
- OR coalesce(existing.country_code, '') != coalesce(NEW.country_code, '')
- OR (existing.geometry::text != NEW.geometry::text AND ST_Distance(ST_Centroid(existing.geometry),ST_Centroid(NEW.geometry)) > 0.01 AND NOT
- (ST_GeometryType(existing.geometry) in ('ST_Polygon','ST_MultiPolygon') AND ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon')))
- THEN
-
--- IF existing.osm_type IS NULL THEN
--- RAISE WARNING 'no existing place';
--- END IF;
--- IF existingplacex.osm_type IS NULL THEN
--- RAISE WARNING 'no existing placex %', existingplacex;
--- END IF;
-
--- RAISE WARNING 'delete and replace';
-
- IF existing.osm_type IS NOT NULL THEN
--- RAISE WARNING 'insert delete % % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type,ST_Distance(ST_Centroid(existing.geometry),ST_Centroid(NEW.geometry)),existing;
- 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;
-
--- RAISE WARNING 'delete and replace2';
-
- -- No - process it as a new insertion (hopefully of low rank or it will be slow)
- insert into placex (osm_type, osm_id, class, type, name, admin_level, housenumber,
- street, isin, postcode, country_code, extratags, geometry)
- values (NEW.osm_type
- ,NEW.osm_id
- ,NEW.class
- ,NEW.type
- ,NEW.name
- ,NEW.admin_level
- ,NEW.housenumber
- ,NEW.street
- ,NEW.isin
- ,NEW.postcode
- ,NEW.country_code
- ,NEW.extratags
- ,NEW.geometry
- );
-
--- RAISE WARNING 'insert done % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;