2010-10-24 03:12:37 +04:00
|
|
|
create type nearplace as (
|
2010-11-01 18:09:10 +03:00
|
|
|
place_id integer
|
2010-10-24 03:12:37 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
create type nearfeature as (
|
2010-11-01 18:09:10 +03:00
|
|
|
place_id integer,
|
2010-10-24 03:12:37 +04:00
|
|
|
keywords int[],
|
|
|
|
rank_address integer,
|
|
|
|
rank_search integer,
|
|
|
|
distance float
|
|
|
|
);
|
|
|
|
|
2010-10-26 19:22:41 +04:00
|
|
|
CREATE TABLE location_area_country () INHERITS (location_area_large);
|
2010-11-01 18:09:10 +03:00
|
|
|
CREATE INDEX idx_location_area_country_place_id ON location_area_country USING BTREE (place_id);
|
2010-10-26 19:22:41 +04:00
|
|
|
CREATE INDEX idx_location_area_country_geometry ON location_area_country USING GIST (geometry);
|
|
|
|
|
2010-10-24 03:12:37 +04:00
|
|
|
-- start
|
|
|
|
CREATE TABLE location_area_large_-partition- () INHERITS (location_area_large);
|
2010-11-01 18:09:10 +03:00
|
|
|
CREATE INDEX idx_location_area_large_-partition-_place_id ON location_area_large_-partition- USING BTREE (place_id);
|
2010-10-24 03:12:37 +04:00
|
|
|
CREATE INDEX idx_location_area_large_-partition-_geometry ON location_area_large_-partition- USING GIST (geometry);
|
|
|
|
|
|
|
|
CREATE TABLE location_area_roadnear_-partition- () INHERITS (location_area_roadnear);
|
2010-11-01 18:09:10 +03:00
|
|
|
CREATE INDEX idx_location_area_roadnear_-partition-_place_id ON location_area_roadfar_-partition- USING BTREE (place_id);
|
2010-10-24 03:12:37 +04:00
|
|
|
CREATE INDEX idx_location_area_roadnear_-partition-_geometry ON location_area_roadnear_-partition- USING GIST (geometry);
|
|
|
|
|
|
|
|
CREATE TABLE location_area_roadfar_-partition- () INHERITS (location_area_roadfar);
|
2010-11-01 18:09:10 +03:00
|
|
|
CREATE INDEX idx_location_area_roadfar_-partition-_place_id ON location_area_roadfar_-partition- USING BTREE (place_id);
|
2010-10-24 03:12:37 +04:00
|
|
|
CREATE INDEX idx_location_area_roadfar_-partition-_geometry ON location_area_roadfar_-partition- USING GIST (geometry);
|
|
|
|
-- end
|
|
|
|
|
|
|
|
create or replace function getNearRoads(in_partition TEXT, point GEOMETRY) RETURNS setof nearplace AS $$
|
|
|
|
DECLARE
|
|
|
|
r nearplace%rowtype;
|
2010-10-26 19:22:41 +04:00
|
|
|
a BOOLEAN;
|
2010-10-24 03:12:37 +04:00
|
|
|
BEGIN
|
|
|
|
|
|
|
|
-- start
|
|
|
|
IF in_partition = '-partition-' THEN
|
2010-10-26 19:22:41 +04:00
|
|
|
a := FALSE;
|
|
|
|
FOR r IN SELECT place_id FROM location_area_roadnear_-partition- WHERE ST_Contains(geometry, point) ORDER BY ST_Distance(point, centroid) ASC LIMIT 1 LOOP
|
|
|
|
a := TRUE;
|
|
|
|
RETURN NEXT r;
|
|
|
|
RETURN;
|
|
|
|
END LOOP;
|
|
|
|
FOR r IN SELECT place_id FROM location_area_roadfar_-partition- WHERE ST_Contains(geometry, point) ORDER BY ST_Distance(point, centroid) ASC LOOP
|
2010-10-24 03:12:37 +04:00
|
|
|
RETURN NEXT r;
|
2010-10-26 19:22:41 +04:00
|
|
|
RETURN;
|
2010-10-24 03:12:37 +04:00
|
|
|
END LOOP;
|
|
|
|
RETURN;
|
|
|
|
END IF;
|
|
|
|
-- end
|
|
|
|
|
|
|
|
RAISE EXCEPTION 'Unknown partition %', in_partition;
|
|
|
|
END
|
|
|
|
$$
|
|
|
|
LANGUAGE plpgsql;
|
|
|
|
|
2010-10-26 19:22:41 +04:00
|
|
|
create or replace function getNearFeatures(in_partition TEXT, point GEOMETRY, maxrank INTEGER, isin_tokens INT[]) RETURNS setof nearfeature AS $$
|
2010-10-24 03:12:37 +04:00
|
|
|
DECLARE
|
|
|
|
r nearfeature%rowtype;
|
|
|
|
BEGIN
|
|
|
|
|
|
|
|
-- start
|
|
|
|
IF in_partition = '-partition-' THEN
|
2010-10-26 19:22:41 +04:00
|
|
|
FOR r IN
|
|
|
|
SELECT place_id, keywords, rank_address, rank_search, ST_Distance(point, centroid) as distance 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
|
|
|
|
ORDER BY rank_search desc, isin_tokens && keywords desc, isguess asc, rank_address asc, ST_Distance(point, centroid) ASC
|
2010-10-24 03:12:37 +04:00
|
|
|
LOOP
|
|
|
|
RETURN NEXT r;
|
|
|
|
END LOOP;
|
|
|
|
RETURN;
|
|
|
|
END IF;
|
|
|
|
-- end
|
|
|
|
|
|
|
|
RAISE EXCEPTION 'Unknown partition %', in_partition;
|
|
|
|
END
|
|
|
|
$$
|
|
|
|
LANGUAGE plpgsql;
|
|
|
|
|
2010-11-01 18:09:10 +03:00
|
|
|
create or replace function deleteLocationArea(in_partition TEXT, in_place_id integer) RETURNS BOOLEAN AS $$
|
2010-10-24 03:12:37 +04:00
|
|
|
DECLARE
|
|
|
|
BEGIN
|
|
|
|
|
|
|
|
-- start
|
|
|
|
IF in_partition = '-partition-' THEN
|
2010-11-01 18:09:10 +03:00
|
|
|
-- DELETE from location_area_large_-partition- WHERE place_id = in_place_id;
|
|
|
|
-- DELETE from location_area_roadnear_-partition- WHERE place_id = in_place_id;
|
|
|
|
-- DELETE from location_area_roadfar_-partition- WHERE place_id = in_place_id;
|
2010-10-24 03:12:37 +04:00
|
|
|
RETURN TRUE;
|
|
|
|
END IF;
|
|
|
|
-- end
|
|
|
|
|
|
|
|
RAISE EXCEPTION 'Unknown partition %', in_partition;
|
|
|
|
|
|
|
|
RETURN FALSE;
|
|
|
|
END
|
|
|
|
$$
|
|
|
|
LANGUAGE plpgsql;
|
|
|
|
|
|
|
|
create or replace function insertLocationAreaLarge(
|
2010-11-01 18:09:10 +03:00
|
|
|
in_partition TEXT, in_place_id integer, in_country_code VARCHAR(2), in_keywords INTEGER[],
|
2010-10-24 03:12:37 +04:00
|
|
|
in_rank_search INTEGER, in_rank_address INTEGER, in_estimate BOOLEAN,
|
|
|
|
in_centroid GEOMETRY, in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
|
|
|
|
DECLARE
|
|
|
|
BEGIN
|
|
|
|
|
2010-10-26 19:22:41 +04:00
|
|
|
IF in_rank_search <= 4 THEN
|
|
|
|
INSERT INTO location_area_country values (in_partition, in_place_id, in_country_code, in_keywords, in_rank_search, in_rank_address, in_estimate, in_centroid, in_geometry);
|
|
|
|
RETURN TRUE;
|
|
|
|
END IF;
|
|
|
|
|
2010-10-24 03:12:37 +04:00
|
|
|
-- start
|
|
|
|
IF in_partition = '-partition-' THEN
|
2010-10-26 19:22:41 +04:00
|
|
|
INSERT INTO location_area_large_-partition- values (in_partition, in_place_id, in_country_code, in_keywords, in_rank_search, in_rank_address, in_estimate, in_centroid, in_geometry);
|
2010-10-24 03:12:37 +04:00
|
|
|
RETURN TRUE;
|
|
|
|
END IF;
|
|
|
|
-- end
|
|
|
|
|
|
|
|
RAISE EXCEPTION 'Unknown partition %', in_partition;
|
|
|
|
RETURN FALSE;
|
|
|
|
END
|
|
|
|
$$
|
|
|
|
LANGUAGE plpgsql;
|
|
|
|
|
|
|
|
create or replace function insertLocationAreaRoadNear(
|
2010-11-01 18:09:10 +03:00
|
|
|
in_partition TEXT, in_place_id integer, in_country_code VARCHAR(2), in_keywords INTEGER[],
|
2010-10-24 03:12:37 +04:00
|
|
|
in_rank_search INTEGER, in_rank_address INTEGER, in_estimate BOOLEAN,
|
|
|
|
in_centroid GEOMETRY, in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
|
|
|
|
DECLARE
|
|
|
|
BEGIN
|
|
|
|
|
|
|
|
-- start
|
|
|
|
IF in_partition = '-partition-' THEN
|
2010-10-26 19:22:41 +04:00
|
|
|
INSERT INTO location_area_roadnear_-partition- values (in_partition, in_place_id, in_country_code, in_keywords, in_rank_search, in_rank_address, in_estimate, in_centroid, in_geometry);
|
2010-10-24 03:12:37 +04:00
|
|
|
RETURN TRUE;
|
|
|
|
END IF;
|
|
|
|
-- end
|
|
|
|
|
|
|
|
RAISE EXCEPTION 'Unknown partition %', in_partition;
|
|
|
|
RETURN FALSE;
|
|
|
|
END
|
|
|
|
$$
|
|
|
|
LANGUAGE plpgsql;
|
|
|
|
|
|
|
|
create or replace function insertLocationAreaRoadFar(
|
2010-11-01 18:09:10 +03:00
|
|
|
in_partition TEXT, in_place_id integer, in_country_code VARCHAR(2), in_keywords INTEGER[],
|
2010-10-24 03:12:37 +04:00
|
|
|
in_rank_search INTEGER, in_rank_address INTEGER, in_estimate BOOLEAN,
|
|
|
|
in_centroid GEOMETRY, in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
|
|
|
|
DECLARE
|
|
|
|
BEGIN
|
|
|
|
|
|
|
|
-- start
|
|
|
|
IF in_partition = '-partition-' THEN
|
2010-10-26 19:22:41 +04:00
|
|
|
INSERT INTO location_area_roadfar_-partition- values (in_partition, in_place_id, in_country_code, in_keywords, in_rank_search, in_rank_address, in_estimate, in_centroid, in_geometry);
|
2010-10-24 03:12:37 +04:00
|
|
|
RETURN TRUE;
|
|
|
|
END IF;
|
|
|
|
-- end
|
|
|
|
|
|
|
|
RAISE EXCEPTION 'Unknown partition %', in_partition;
|
|
|
|
RETURN FALSE;
|
|
|
|
END
|
|
|
|
$$
|
|
|
|
LANGUAGE plpgsql;
|