X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/46ae96b608437f428fc310912268b0ab7190fc6f..03873df21182ee9a87f168f6108d93187db0c83e:/sql/partitions.src.sql diff --git a/sql/partitions.src.sql b/sql/partitions.src.sql index e31eadff..3fb7962c 100644 --- a/sql/partitions.src.sql +++ b/sql/partitions.src.sql @@ -1,68 +1,77 @@ +drop type nearplace cascade; create type nearplace as ( - place_id integer + place_id BIGINT ); +drop type nearfeature cascade; create type nearfeature as ( - place_id integer, + place_id BIGINT, keywords int[], rank_address integer, rank_search integer, - distance float + distance float, + isguess boolean +); + +drop type nearfeaturecentr cascade; +create type nearfeaturecentr as ( + place_id BIGINT, + keywords int[], + rank_address integer, + rank_search integer, + distance float, + isguess boolean, + centroid GEOMETRY ); CREATE TABLE location_area_country () INHERITS (location_area_large); -CREATE INDEX idx_location_area_country_place_id ON location_area_country USING BTREE (place_id); -CREATE INDEX idx_location_area_country_geometry ON location_area_country USING GIST (geometry); +CREATE INDEX idx_location_area_country_geometry ON location_area_country USING GIST (geometry) TABLESPACE ssd; CREATE TABLE search_name_country () INHERITS (search_name_blank); -CREATE INDEX idx_search_name_country_place_id ON search_name_country USING BTREE (place_id); -CREATE INDEX idx_search_name_country_centroid ON search_name_country USING GIST (centroid); -CREATE INDEX idx_search_name_country_name_vector ON search_name_country USING GIN (name_vector gin__int_ops) WITH (fastupdate = off); -CREATE INDEX idx_search_name_country_nameaddress_vector ON search_name_country USING GIN (nameaddress_vector gin__int_ops) WITH (fastupdate = off); +CREATE INDEX idx_search_name_country_place_id ON search_name_country USING BTREE (place_id) TABLESPACE ssd; +CREATE INDEX idx_search_name_country_name_vector ON search_name_country USING GIN (name_vector) WITH (fastupdate = off) TABLESPACE ssd; -- start CREATE TABLE location_area_large_-partition- () INHERITS (location_area_large); -CREATE INDEX idx_location_area_large_-partition-_place_id ON location_area_large_-partition- USING BTREE (place_id); -CREATE INDEX idx_location_area_large_-partition-_geometry ON location_area_large_-partition- USING GIST (geometry); +CREATE INDEX idx_location_area_large_-partition-_place_id ON location_area_large_-partition- USING BTREE (place_id) TABLESPACE ssd; +CREATE INDEX idx_location_area_large_-partition-_geometry ON location_area_large_-partition- USING GIST (geometry) TABLESPACE ssd; CREATE TABLE search_name_-partition- () INHERITS (search_name_blank); -CREATE INDEX idx_search_name_-partition-_place_id ON search_name_-partition- USING BTREE (place_id); -CREATE INDEX idx_search_name_-partition-_centroid ON search_name_-partition- USING GIST (centroid); -CREATE INDEX idx_search_name_-partition-_name_vector ON search_name_-partition- USING GIN (name_vector gin__int_ops) WITH (fastupdate = off); -CREATE INDEX idx_search_name_-partition-_nameaddress_vector ON search_name_-partition- USING GIN (nameaddress_vector gin__int_ops) WITH (fastupdate = off); +CREATE INDEX idx_search_name_-partition-_place_id ON search_name_-partition- USING BTREE (place_id) TABLESPACE ssd; +CREATE INDEX idx_search_name_-partition-_centroid ON search_name_-partition- USING GIST (centroid) TABLESPACE ssd; +CREATE INDEX idx_search_name_-partition-_name_vector ON search_name_-partition- USING GIN (name_vector) WITH (fastupdate = off) TABLESPACE ssd; CREATE TABLE location_property_-partition- () INHERITS (location_property); CREATE INDEX idx_location_property_-partition-_place_id ON location_property_-partition- USING BTREE (place_id); CREATE INDEX idx_location_property_-partition-_parent_place_id ON location_property_-partition- USING BTREE (parent_place_id); CREATE INDEX idx_location_property_-partition-_housenumber_parent_place_id ON location_property_-partition- USING BTREE (parent_place_id, housenumber); ---CREATE INDEX idx_location_property_-partition-_centroid ON location_property_-partition- USING GIST (centroid); CREATE TABLE location_road_-partition- ( partition integer, - place_id INTEGER, + place_id BIGINT, country_code VARCHAR(2) ); SELECT AddGeometryColumn('location_road_-partition-', 'geometry', 4326, 'GEOMETRY', 2); -CREATE INDEX idx_location_road_-partition-_geometry ON location_road_-partition- USING GIST (geometry); -CREATE INDEX idx_location_road_-partition-_place_id ON location_road_-partition- USING BTREE (place_id); +CREATE INDEX idx_location_road_-partition-_geometry ON location_road_-partition- USING GIST (geometry) TABLESPACE ssd; +CREATE INDEX idx_location_road_-partition-_place_id ON location_road_-partition- USING BTREE (place_id) TABLESPACE ssd; -- end -create or replace function getNearFeatures(in_partition INTEGER, point GEOMETRY, maxrank INTEGER, isin_tokens INT[]) RETURNS setof nearfeature AS $$ +create or replace function getNearFeatures(in_partition INTEGER, point GEOMETRY, maxrank INTEGER, isin_tokens INT[]) RETURNS setof nearfeaturecentr AS $$ DECLARE - r nearfeature%rowtype; + r nearfeaturecentr%rowtype; BEGIN -- start IF in_partition = -partition- THEN FOR r IN - SELECT place_id, keywords, rank_address, rank_search, min(ST_Distance(point, centroid)) as distance FROM ( + SELECT place_id, keywords, rank_address, rank_search, min(ST_Distance(point, centroid)) as distance, isguess, centroid FROM ( SELECT * FROM location_area_large_-partition- WHERE ST_Contains(geometry, point) and rank_search < maxrank UNION ALL SELECT * FROM location_area_country WHERE ST_Contains(geometry, point) and rank_search < maxrank ) as location_area GROUP BY place_id, keywords, rank_address, rank_search, isguess, centroid - ORDER BY rank_address desc, isin_tokens && keywords desc, isguess asc, + ORDER BY rank_address, isin_tokens && keywords desc, isguess asc, ST_Distance(point, centroid) * CASE WHEN rank_address = 16 AND rank_search = 15 THEN 0.2 -- capital city @@ -81,7 +90,7 @@ END $$ LANGUAGE plpgsql; -create or replace function deleteLocationArea(in_partition INTEGER, in_place_id integer) RETURNS BOOLEAN AS $$ +create or replace function deleteLocationArea(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$ DECLARE BEGIN @@ -100,7 +109,7 @@ $$ LANGUAGE plpgsql; create or replace function insertLocationAreaLarge( - in_partition INTEGER, in_place_id integer, in_country_code VARCHAR(2), in_keywords INTEGER[], + in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_keywords INTEGER[], in_rank_search INTEGER, in_rank_address INTEGER, in_estimate BOOLEAN, in_centroid GEOMETRY, in_geometry GEOMETRY) RETURNS BOOLEAN AS $$ DECLARE @@ -133,13 +142,13 @@ BEGIN IF in_partition = -partition- THEN FOR r IN SELECT place_id, name_vector, address_rank, search_rank, - ST_Distance(centroid, point) as distance + ST_Distance(centroid, point) as distance, null as isguess FROM search_name_-partition- WHERE name_vector @> ARRAY[isin_token] AND search_rank < maxrank UNION ALL SELECT place_id, name_vector, address_rank, search_rank, - ST_Distance(centroid, point) as distance + ST_Distance(centroid, point) as distance, null as isguess FROM search_name_country WHERE name_vector @> ARRAY[isin_token] AND search_rank < maxrank @@ -166,7 +175,7 @@ BEGIN IF in_partition = -partition- THEN FOR r IN SELECT place_id, name_vector, address_rank, search_rank, - ST_Distance(centroid, point) as distance + ST_Distance(centroid, point) as distance, null as isguess FROM search_name_-partition- WHERE name_vector @> ARRAY[isin_token] AND ST_DWithin(centroid, point, 0.01) @@ -184,8 +193,31 @@ END $$ LANGUAGE plpgsql; +create or replace function getNearestPostcode(in_partition INTEGER, point GEOMETRY) + RETURNS TEXT AS $$ +DECLARE + out_postcode TEXT; +BEGIN + +-- start + IF in_partition = -partition- THEN + SELECT postcode + FROM location_area_large_-partition- join placex using (place_id) + WHERE st_contains(location_area_large_-partition-.geometry, point) + AND class = 'place' and type = 'postcode' + ORDER BY st_distance(location_area_large_-partition-.centroid, point) ASC limit 1 + INTO out_postcode; + RETURN out_postcode; + END IF; +-- end + + RAISE EXCEPTION 'Unknown partition %', in_partition; +END +$$ +LANGUAGE plpgsql; + create or replace function insertSearchName( - in_partition INTEGER, in_place_id integer, in_country_code VARCHAR(2), + in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_name_vector INTEGER[], in_nameaddress_vector INTEGER[], in_rank_search INTEGER, in_rank_address INTEGER, in_importance FLOAT, in_centroid GEOMETRY) RETURNS BOOLEAN AS $$ @@ -218,7 +250,7 @@ END $$ LANGUAGE plpgsql; -create or replace function deleteSearchName(in_partition INTEGER, in_place_id integer) RETURNS BOOLEAN AS $$ +create or replace function deleteSearchName(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$ DECLARE BEGIN @@ -240,7 +272,7 @@ $$ LANGUAGE plpgsql; create or replace function insertLocationRoad( - in_partition INTEGER, in_place_id integer, in_country_code VARCHAR(2), in_geometry GEOMETRY) RETURNS BOOLEAN AS $$ + in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_geometry GEOMETRY) RETURNS BOOLEAN AS $$ DECLARE BEGIN @@ -258,7 +290,7 @@ END $$ LANGUAGE plpgsql; -create or replace function deleteRoad(in_partition INTEGER, in_place_id integer) RETURNS BOOLEAN AS $$ +create or replace function deleteRoad(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$ DECLARE BEGIN @@ -288,7 +320,7 @@ BEGIN WHILE search_diameter < 0.1 LOOP FOR r IN SELECT place_id, null, null, null, - ST_Distance(geometry, point) as distance + ST_Distance(geometry, point) as distance, null as isguess FROM location_road_-partition- WHERE ST_DWithin(geometry, point, search_diameter) ORDER BY distance ASC limit 1 @@ -330,7 +362,7 @@ BEGIN WHILE search_diameter < 0.01 LOOP FOR r IN SELECT place_id, null, null, null, - ST_Distance(geometry, line) as distance + ST_Distance(geometry, line) as distance, null as isguess FROM location_road_-partition- WHERE ST_DWithin(line, geometry, search_diameter) ORDER BY (ST_distance(geometry, p1)+