1 CREATE OR REPLACE FUNCTION place_insert()
9 existinggeometry GEOMETRY;
10 existingplace_id BIGINT;
16 RAISE WARNING '-----------------------------------------------------------------------------------';
17 RAISE WARNING 'place_insert: % % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type,st_area(NEW.geometry);
19 -- filter wrong tupels
20 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
21 INSERT INTO import_polygon_error (osm_type, osm_id, class, type, name, country_code, updated, errormessage, prevgeometry, newgeometry)
22 VALUES (NEW.osm_type, NEW.osm_id, NEW.class, NEW.type, NEW.name, NEW.address->'country', now(), ST_IsValidReason(NEW.geometry), null, NEW.geometry);
23 -- RAISE WARNING 'Invalid Geometry: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
27 -- decide, whether it is an osm interpolation line => insert intoosmline, or else just placex
28 IF NEW.class='place' and NEW.type='houses' and NEW.osm_type='W' and ST_GeometryType(NEW.geometry) = 'ST_LineString' THEN
29 -- Have we already done this place?
30 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;
32 -- Get the existing place_id
33 select * from location_property_osmline where osm_id = NEW.osm_id INTO existingline;
35 -- Handle a place changing type by removing the old data (this trigger is executed BEFORE INSERT of the NEW tupel)
36 IF existing.osm_type IS NULL THEN
37 DELETE FROM place where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class;
40 DELETE from import_polygon_error where osm_type = NEW.osm_type and osm_id = NEW.osm_id;
41 DELETE from import_polygon_delete where osm_type = NEW.osm_type and osm_id = NEW.osm_id;
43 -- 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)
44 IF existingline.osm_id IS NOT NULL THEN
45 delete from location_property_osmline where osm_id = NEW.osm_id;
48 -- for interpolations invalidate all nodes on the line
49 update placex p set indexed_status = 2
50 from planet_osm_ways w
51 where w.id = NEW.osm_id and p.osm_type = 'N' and p.osm_id = any(w.nodes);
54 INSERT INTO location_property_osmline (osm_id, address, linegeo)
55 VALUES (NEW.osm_id, NEW.address, NEW.geometry);
58 IF existing.osm_type IS NULL THEN
62 IF coalesce(existing.address, ''::hstore) != coalesce(NEW.address, ''::hstore)
63 OR (coalesce(existing.extratags, ''::hstore) != coalesce(NEW.extratags, ''::hstore))
64 OR existing.geometry::text != NEW.geometry::text
69 address = NEW.address,
70 extratags = NEW.extratags,
71 admin_level = NEW.admin_level,
72 geometry = NEW.geometry
73 where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type;
78 ELSE -- insert to placex
80 -- Pure postcodes are never queried from placex so we don't add them.
81 -- location_postcodes is filled from the place table directly.
82 IF NEW.class = 'place' AND NEW.type = 'postcode' THEN
86 -- Patch in additional country names
87 IF NEW.admin_level = 2 AND NEW.type = 'administrative'
88 AND NEW.address is not NULL AND NEW.address ? 'country' THEN
89 SELECT name FROM country_name WHERE country_code = lower(NEW.address->'country') INTO existing;
90 IF existing.name IS NOT NULL THEN
91 NEW.name = existing.name || NEW.name;
95 -- Have we already done this place?
96 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;
98 -- Get the existing place_id
99 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;
101 -- Handle a place changing type by removing the old data
102 -- My generated 'place' types are causing havok because they overlap with real keys
103 -- TODO: move them to their own special purpose key/class to avoid collisions
104 IF existing.osm_type IS NULL THEN
105 DELETE FROM place where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class;
108 {% if debug %}RAISE WARNING 'Existing: %',existing.osm_id;{% endif %}
109 {% if debug %}RAISE WARNING 'Existing PlaceX: %',existingplacex.place_id;{% endif %}
112 IF existing.geometry is not null AND st_isvalid(existing.geometry)
113 AND st_area(existing.geometry) > 0.02
114 AND ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon')
115 AND st_area(NEW.geometry) < st_area(existing.geometry)*0.5
117 INSERT INTO import_polygon_error (osm_type, osm_id, class, type, name, country_code, updated, errormessage, prevgeometry, newgeometry)
118 VALUES (NEW.osm_type, NEW.osm_id, NEW.class, NEW.type, NEW.name, NEW.address->'country', now(),
119 'Area reduced from '||st_area(existing.geometry)||' to '||st_area(NEW.geometry), existing.geometry, NEW.geometry);
123 DELETE from import_polygon_error where osm_type = NEW.osm_type and osm_id = NEW.osm_id;
124 DELETE from import_polygon_delete where osm_type = NEW.osm_type and osm_id = NEW.osm_id;
126 -- To paraphrase, if there isn't an existing item, OR if the admin level has changed
127 IF existingplacex.osm_type IS NULL OR
128 (existingplacex.class = 'boundary' AND
129 ((coalesce(existingplacex.admin_level, 15) != coalesce(NEW.admin_level, 15) AND existingplacex.type = 'administrative') OR
130 (existingplacex.type != NEW.type)))
133 {% if config.get_bool('LIMIT_REINDEXING') %}
134 IF existingplacex.osm_type IS NOT NULL THEN
135 -- sanity check: ignore admin_level changes on places with too many active children
136 -- or we end up reindexing entire countries because somebody accidentally deleted admin_level
137 SELECT count(*) INTO i FROM
138 (SELECT 'a' FROM placex, place_addressline
139 WHERE address_place_id = existingplacex.place_id
140 and placex.place_id = place_addressline.place_id
141 and indexed_status = 0 and place_addressline.isaddress LIMIT 100001) sub;
148 IF existing.osm_type IS NOT NULL THEN
149 -- pathological case caused by the triggerless copy into place during initial import
150 -- force delete even for large areas, it will be reinserted later
151 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;
152 DELETE from place where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type;
155 -- No - process it as a new insertion (hopefully of low rank or it will be slow)
156 insert into placex (osm_type, osm_id, class, type, name,
157 admin_level, address, extratags, geometry)
158 values (NEW.osm_type, NEW.osm_id, NEW.class, NEW.type, NEW.name,
159 NEW.admin_level, NEW.address, NEW.extratags, NEW.geometry);
161 {% if debug %}RAISE WARNING 'insert done % % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type,NEW.name;{% endif %}
166 -- Special case for polygon shape changes because they tend to be large and we can be a bit clever about how we handle them
167 IF existing.geometry::text != NEW.geometry::text
168 AND ST_GeometryType(existing.geometry) in ('ST_Polygon','ST_MultiPolygon')
169 AND ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon')
172 -- Get the version of the geometry actually used (in placex table)
173 select geometry from placex where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type into existinggeometry;
176 IF st_area(NEW.geometry) < 0.000000001 AND st_area(existinggeometry) < 1 THEN
178 -- re-index points that have moved in / out of the polygon, could be done as a single query but postgres gets the index usage wrong
179 update placex set indexed_status = 2 where indexed_status = 0
180 AND ST_Intersects(NEW.geometry, placex.geometry)
181 AND NOT ST_Intersects(existinggeometry, placex.geometry)
182 AND rank_search > existingplacex.rank_search AND (rank_search < 28 or name is not null);
184 update placex set indexed_status = 2 where indexed_status = 0
185 AND ST_Intersects(existinggeometry, placex.geometry)
186 AND NOT ST_Intersects(NEW.geometry, placex.geometry)
187 AND rank_search > existingplacex.rank_search AND (rank_search < 28 or name is not null);
194 IF coalesce(existing.name::text, '') != coalesce(NEW.name::text, '')
195 OR coalesce(existing.extratags::text, '') != coalesce(NEW.extratags::text, '')
196 OR coalesce(existing.address, ''::hstore) != coalesce(NEW.address, ''::hstore)
197 OR coalesce(existing.admin_level, 15) != coalesce(NEW.admin_level, 15)
198 OR existing.geometry::text != NEW.geometry::text
203 address = NEW.address,
204 extratags = NEW.extratags,
205 admin_level = NEW.admin_level,
206 geometry = NEW.geometry
207 where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type;
210 IF NEW.class in ('place','boundary') AND NEW.type in ('postcode','postal_code') THEN
211 IF NEW.address is NULL OR NOT NEW.address ? 'postcode' THEN
212 -- postcode was deleted, no longer retain in placex
213 DELETE FROM placex where place_id = existingplacex.place_id;
217 NEW.name := hstore('ref', NEW.address->'postcode');
220 IF NEW.class in ('boundary')
221 AND ST_GeometryType(NEW.geometry) not in ('ST_Polygon','ST_MultiPolygon') THEN
222 DELETE FROM placex where place_id = existingplacex.place_id;
228 address = NEW.address,
229 parent_place_id = null,
230 extratags = NEW.extratags,
231 admin_level = NEW.admin_level,
233 geometry = NEW.geometry
234 where place_id = existingplacex.place_id;
235 -- if a node(=>house), which is part of a interpolation line, changes (e.g. the street attribute) => mark this line for reparenting
236 -- (already here, because interpolation lines are reindexed before nodes, so in the second call it would be too late)
238 and (coalesce(existing.address, ''::hstore) != coalesce(NEW.address, ''::hstore)
239 or existing.geometry::text != NEW.geometry::text)
241 result:= osmline_reinsert(NEW.osm_id, NEW.geometry);
244 -- linked places should get potential new naming and addresses
245 IF existingplacex.linked_place_id is not NULL THEN
248 extratags = p.extratags,
251 where x.place_id = existingplacex.linked_place_id
252 and x.indexed_status = 0
253 and x.osm_type = p.osm_type
254 and x.osm_id = p.osm_id
255 and x.class = p.class;
260 -- Abort the add (we modified the existing place instead)
268 CREATE OR REPLACE FUNCTION place_delete()
275 {% if debug %}RAISE WARNING 'delete: % % % %',OLD.osm_type,OLD.osm_id,OLD.class,OLD.type;{% endif %}
277 -- deleting large polygons can have a massive effect on the system - require manual intervention to let them through
278 IF st_area(OLD.geometry) > 2 and st_isvalid(OLD.geometry) THEN
279 SELECT bool_or(not (rank_address = 0 or rank_address > 25)) as ranked FROM placex WHERE osm_type = OLD.osm_type and osm_id = OLD.osm_id and class = OLD.class and type = OLD.type INTO has_rank;
281 insert into import_polygon_delete (osm_type, osm_id, class, type) values (OLD.osm_type,OLD.osm_id,OLD.class,OLD.type);
287 UPDATE placex set indexed_status = 100 where osm_type = OLD.osm_type and osm_id = OLD.osm_id and class = OLD.class and type = OLD.type;
289 -- interpolations are special
290 IF OLD.osm_type='W' and OLD.class = 'place' and OLD.type = 'houses' THEN
291 UPDATE location_property_osmline set indexed_status = 100 where osm_id = OLD.osm_id; -- osm_id = wayid (=old.osm_id)