1 -- Assorted helper functions for the triggers.
3 CREATE OR REPLACE FUNCTION geometry_sector(partition INTEGER, place geometry)
9 -- RAISE WARNING '%',place;
10 NEWgeometry := ST_PointOnSurface(place);
11 RETURN (partition*1000000) + (500-ST_X(NEWgeometry)::integer)*1000 + (500-ST_Y(NEWgeometry)::integer);
14 LANGUAGE plpgsql IMMUTABLE;
17 CREATE OR REPLACE FUNCTION array_merge(a INTEGER[], b INTEGER[])
24 IF array_upper(a, 1) IS NULL THEN
27 IF array_upper(b, 1) IS NULL THEN
31 FOR i IN 1..array_upper(b, 1) LOOP
32 IF NOT (ARRAY[b[i]] <@ r) THEN
39 LANGUAGE plpgsql IMMUTABLE;
41 -- Return the node members with a given label from a relation member list
44 -- \param members Member list in osm2pgsql middle format.
45 -- \param memberLabels Array of labels to accept.
47 -- \returns Set of OSM ids of nodes that are found.
49 CREATE OR REPLACE FUNCTION get_rel_node_members(members TEXT[],
56 FOR i IN 1..ARRAY_UPPER(members,1) BY 2 LOOP
57 IF members[i+1] = ANY(memberLabels)
58 AND upper(substring(members[i], 1, 1))::char(1) = 'N'
60 RETURN NEXT substring(members[i], 2)::bigint;
67 LANGUAGE plpgsql IMMUTABLE;
69 -- Copy 'name' to or from the default language.
71 -- \param country_code Country code of the object being named.
72 -- \param[inout] name List of names of the object.
74 -- If the country named by country_code has a single default language,
75 -- then a `name` tag is copied to `name:<country_code>` if this tag does
76 -- not yet exist and vice versa.
77 CREATE OR REPLACE FUNCTION add_default_place_name(country_code VARCHAR(2),
81 default_language VARCHAR(10);
83 IF name is not null AND array_upper(akeys(name),1) > 1 THEN
84 default_language := get_country_language_code(country_code);
85 IF default_language IS NOT NULL THEN
86 IF name ? 'name' AND NOT name ? ('name:'||default_language) THEN
87 name := name || hstore(('name:'||default_language), (name -> 'name'));
88 ELSEIF name ? ('name:'||default_language) AND NOT name ? 'name' THEN
89 name := name || hstore('name', (name -> ('name:'||default_language)));
95 LANGUAGE plpgsql IMMUTABLE;
98 -- Find the nearest artificial postcode for the given geometry.
99 -- TODO For areas there should not be more than two inside the geometry.
100 CREATE OR REPLACE FUNCTION get_nearest_postcode(country VARCHAR(2), geom GEOMETRY)
107 -- If the geometry is an area then only one postcode must be within
108 -- that area, otherwise consider the area as not having a postcode.
109 IF ST_GeometryType(geom) in ('ST_Polygon','ST_MultiPolygon') THEN
110 SELECT min(postcode), count(*) FROM
111 (SELECT postcode FROM location_postcode
112 WHERE ST_Contains(geom, location_postcode.geometry) LIMIT 2) sub
122 SELECT postcode FROM location_postcode
123 WHERE ST_DWithin(geom, location_postcode.geometry, 0.05)
124 AND location_postcode.country_code = country
125 ORDER BY ST_Distance(geom, location_postcode.geometry) LIMIT 1
131 LANGUAGE plpgsql STABLE;
134 CREATE OR REPLACE FUNCTION get_country_code(place geometry)
138 place_centre GEOMETRY;
141 place_centre := ST_PointOnSurface(place);
143 -- RAISE WARNING 'get_country_code, start: %', ST_AsText(place_centre);
145 -- Try for a OSM polygon
147 SELECT country_code from location_area_country
148 WHERE country_code is not null and st_covers(geometry, place_centre) limit 1
150 RETURN nearcountry.country_code;
153 -- RAISE WARNING 'osm fallback: %', ST_AsText(place_centre);
155 -- Try for OSM fallback data
156 -- The order is to deal with places like HongKong that are 'states' within another polygon
158 SELECT country_code from country_osm_grid
159 WHERE st_covers(geometry, place_centre) order by area asc limit 1
161 RETURN nearcountry.country_code;
164 -- RAISE WARNING 'near osm fallback: %', ST_AsText(place_centre);
168 SELECT country_code from country_osm_grid
169 WHERE st_dwithin(geometry, place_centre, 0.5)
170 ORDER BY st_distance(geometry, place_centre) asc, area asc limit 1
172 RETURN nearcountry.country_code;
178 LANGUAGE plpgsql STABLE;
181 CREATE OR REPLACE FUNCTION get_country_language_code(search_country_code VARCHAR(2))
188 SELECT distinct country_default_language_code from country_name
189 WHERE country_code = search_country_code limit 1
191 RETURN lower(nearcountry.country_default_language_code);
196 LANGUAGE plpgsql STABLE;
199 CREATE OR REPLACE FUNCTION get_partition(in_country_code VARCHAR(10))
206 SELECT partition from country_name where country_code = in_country_code
208 RETURN nearcountry.partition;
213 LANGUAGE plpgsql STABLE;
216 -- Find the parent of an address with addr:street/addr:place tag.
218 -- \param street Value of addr:street or NULL if tag is missing.
219 -- \param place Value of addr:place or NULL if tag is missing.
220 -- \param partition Partition where to search the parent.
221 -- \param centroid Location of the address.
223 -- \return Place ID of the parent if one was found, NULL otherwise.
224 CREATE OR REPLACE FUNCTION find_parent_for_address(street TEXT, place TEXT,
230 parent_place_id BIGINT;
233 IF street is not null THEN
234 -- Check for addr:street attributes
235 -- Note that addr:street links can only be indexed, once the street itself is indexed
236 word_ids := word_ids_from_name(street);
237 IF word_ids is not null THEN
238 parent_place_id := getNearestNamedRoadPlaceId(partition, centroid, word_ids);
239 IF parent_place_id is not null THEN
240 --DEBUG: RAISE WARNING 'Get parent form addr:street: %', parent_place_id;
241 RETURN parent_place_id;
246 -- Check for addr:place attributes.
247 IF place is not null THEN
248 word_ids := word_ids_from_name(place);
249 IF word_ids is not null THEN
250 parent_place_id := getNearestNamedPlacePlaceId(partition, centroid, word_ids);
251 IF parent_place_id is not null THEN
252 --DEBUG: RAISE WARNING 'Get parent form addr:place: %', parent_place_id;
253 RETURN parent_place_id;
261 LANGUAGE plpgsql STABLE;
263 CREATE OR REPLACE FUNCTION delete_location(OLD_place_id BIGINT)
268 DELETE FROM location_area where place_id = OLD_place_id;
269 -- TODO:location_area
275 CREATE OR REPLACE FUNCTION near_feature_rank_distance(rank_search INTEGER)
279 IF rank_search <= 16 THEN -- city
281 ELSIF rank_search <= 18 THEN -- town
283 ELSIF rank_search <= 19 THEN -- village
285 ELSIF rank_search <= 20 THEN -- hamlet
292 LANGUAGE plpgsql IMMUTABLE;
295 CREATE OR REPLACE FUNCTION add_location(place_id BIGINT, country_code varchar(2),
296 partition INTEGER, keywords INTEGER[],
297 rank_search INTEGER, rank_address INTEGER,
298 in_postcode TEXT, geometry GEOMETRY)
308 PERFORM deleteLocationArea(partition, place_id, rank_search);
310 -- add postcode only if it contains a single entry, i.e. ignore postcode lists
312 IF in_postcode is not null AND in_postcode not similar to '%(,|;)%' THEN
313 postcode := upper(trim (in_postcode));
316 IF ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') THEN
317 centroid := ST_Centroid(geometry);
319 FOR secgeo IN select split_geometry(geometry) AS geom LOOP
320 PERFORM insertLocationAreaLarge(partition, place_id, country_code, keywords, rank_search, rank_address, false, postcode, centroid, secgeo);
323 ELSEIF ST_GeometryType(geometry) = 'ST_Point' THEN
324 radius := near_feature_rank_distance(rank_search);
325 --DEBUG: RAISE WARNING 'adding % radius %', place_id, radius;
327 -- Create a bounding box with an extent computed from the radius (in meters).
328 secgeo := ST_Envelope(ST_Collect(
329 ST_Project(geometry, radius, 0.785398)::geometry,
330 ST_Project(geometry, radius, 3.9269908)::geometry));
331 PERFORM insertLocationAreaLarge(partition, place_id, country_code, keywords, rank_search, rank_address, true, postcode, geometry, secgeo);
341 CREATE OR REPLACE FUNCTION quad_split_geometry(geometry GEOMETRY, maxarea FLOAT,
343 RETURNS SETOF GEOMETRY
357 remainingdepth INTEGER;
361 -- RAISE WARNING 'quad_split_geometry: maxarea=%, depth=%',maxarea,maxdepth;
363 IF (ST_GeometryType(geometry) not in ('ST_Polygon','ST_MultiPolygon') OR NOT ST_IsValid(geometry)) THEN
364 RETURN NEXT geometry;
368 remainingdepth := maxdepth - 1;
369 area := ST_AREA(geometry);
370 IF remainingdepth < 1 OR area < maxarea THEN
371 RETURN NEXT geometry;
375 xmin := st_xmin(geometry);
376 xmax := st_xmax(geometry);
377 ymin := st_ymin(geometry);
378 ymax := st_ymax(geometry);
379 secbox := ST_SetSRID(ST_MakeBox2D(ST_Point(ymin,xmin),ST_Point(ymax,xmax)),4326);
381 -- if the geometry completely covers the box don't bother to slice any more
382 IF ST_AREA(secbox) = area THEN
383 RETURN NEXT geometry;
387 xmid := (xmin+xmax)/2;
388 ymid := (ymin+ymax)/2;
394 secbox := ST_SetSRID(ST_MakeBox2D(ST_Point(xmin,ymin),ST_Point(xmid,ymid)),4326);
397 secbox := ST_SetSRID(ST_MakeBox2D(ST_Point(xmin,ymid),ST_Point(xmid,ymax)),4326);
400 secbox := ST_SetSRID(ST_MakeBox2D(ST_Point(xmid,ymin),ST_Point(xmax,ymid)),4326);
403 secbox := ST_SetSRID(ST_MakeBox2D(ST_Point(xmid,ymid),ST_Point(xmax,ymax)),4326);
406 IF st_intersects(geometry, secbox) THEN
407 secgeo := st_intersection(geometry, secbox);
408 IF NOT ST_IsEmpty(secgeo) AND ST_GeometryType(secgeo) in ('ST_Polygon','ST_MultiPolygon') THEN
409 FOR geo IN select quad_split_geometry(secgeo, maxarea, remainingdepth) as geom LOOP
410 IF NOT ST_IsEmpty(geo.geom) AND ST_GeometryType(geo.geom) in ('ST_Polygon','ST_MultiPolygon') THEN
412 RETURN NEXT geo.geom;
422 LANGUAGE plpgsql IMMUTABLE;
425 CREATE OR REPLACE FUNCTION split_geometry(geometry GEOMETRY)
426 RETURNS SETOF GEOMETRY
431 -- 10000000000 is ~~ 1x1 degree
432 FOR geo IN select quad_split_geometry(geometry, 0.01, 20) as geom LOOP
433 RETURN NEXT geo.geom;
438 LANGUAGE plpgsql IMMUTABLE;
441 CREATE OR REPLACE FUNCTION place_force_delete(placeid BIGINT)
446 osmtype character(1);
450 SELECT osm_type, osm_id, class, type FROM placex WHERE place_id = placeid INTO osmtype, osmid, pclass, ptype;
451 DELETE FROM import_polygon_delete where osm_type = osmtype and osm_id = osmid and class = pclass and type = ptype;
452 DELETE FROM import_polygon_error where osm_type = osmtype and osm_id = osmid and class = pclass and type = ptype;
453 -- force delete from place/placex by making it a very small geometry
454 UPDATE place set geometry = ST_SetSRID(ST_Point(0,0), 4326) where osm_type = osmtype and osm_id = osmid and class = pclass and type = ptype;
455 DELETE FROM place where osm_type = osmtype and osm_id = osmid and class = pclass and type = ptype;
463 CREATE OR REPLACE FUNCTION place_force_update(placeid BIGINT)
472 UPDATE placex SET indexed_status = 2 WHERE place_id = placeid;
473 SELECT geometry, rank_search FROM placex WHERE place_id = placeid INTO placegeom, rank;
474 IF placegeom IS NOT NULL AND ST_IsValid(placegeom) THEN
475 IF ST_GeometryType(placegeom) in ('ST_Polygon','ST_MultiPolygon') THEN
476 FOR geom IN select split_geometry(placegeom) FROM placex WHERE place_id = placeid LOOP
477 update placex set indexed_status = 2 where (st_covers(geom, placex.geometry) OR ST_Intersects(geom, placex.geometry))
478 AND rank_search > rank and indexed_status = 0 and ST_geometrytype(placex.geometry) = 'ST_Point' and (rank_search < 28 or name is not null or (rank >= 16 and address ? 'place'));
479 update placex set indexed_status = 2 where (st_covers(geom, placex.geometry) OR ST_Intersects(geom, placex.geometry))
480 AND rank_search > rank and indexed_status = 0 and ST_geometrytype(placex.geometry) != 'ST_Point' and (rank_search < 28 or name is not null or (rank >= 16 and address ? 'place'));
483 diameter := update_place_diameter(rank);
486 -- roads may cause reparenting for >27 rank places
487 update placex set indexed_status = 2 where indexed_status = 0 and rank_search > rank and ST_DWithin(placex.geometry, placegeom, diameter);
488 ELSEIF rank >= 16 THEN
489 -- up to rank 16, street-less addresses may need reparenting
490 update placex set indexed_status = 2 where indexed_status = 0 and rank_search > rank and ST_DWithin(placex.geometry, placegeom, diameter) and (rank_search < 28 or name is not null or address ? 'place');
492 -- for all other places the search terms may change as well
493 update placex set indexed_status = 2 where indexed_status = 0 and rank_search > rank and ST_DWithin(placex.geometry, placegeom, diameter) and (rank_search < 28 or name is not null);