1 DROP TYPE IF EXISTS nearfeaturecentr CASCADE;
2 CREATE TYPE nearfeaturecentr AS (
13 -- feature intersects geoemtry
14 -- for areas and linestrings they must touch at least along a line
15 CREATE OR REPLACE FUNCTION is_relevant_geometry(de9im TEXT, geom_type TEXT)
19 IF substring(de9im from 1 for 2) != 'FF' THEN
23 IF geom_type = 'ST_Point' THEN
24 RETURN substring(de9im from 4 for 1) = '0';
27 IF geom_type in ('ST_LineString', 'ST_MultiLineString') THEN
28 RETURN substring(de9im from 4 for 1) = '1';
31 RETURN substring(de9im from 4 for 1) = '2';
33 $$ LANGUAGE plpgsql IMMUTABLE;
35 create or replace function getNearFeatures(in_partition INTEGER, feature GEOMETRY, maxrank INTEGER) RETURNS setof nearfeaturecentr AS $$
37 r nearfeaturecentr%rowtype;
41 IF in_partition = -partition- THEN
43 SELECT place_id, keywords, rank_address, rank_search, min(ST_Distance(feature, centroid)) as distance, isguess, postcode, centroid
44 FROM location_area_large_-partition-
45 WHERE geometry && feature
46 AND is_relevant_geometry(ST_Relate(geometry, feature), ST_GeometryType(feature))
47 AND rank_address < maxrank
48 GROUP BY place_id, keywords, rank_address, rank_search, isguess, postcode, centroid
56 RAISE EXCEPTION 'Unknown partition %', in_partition;
59 LANGUAGE plpgsql STABLE;
61 create or replace function deleteLocationArea(in_partition INTEGER, in_place_id BIGINT, in_rank_search INTEGER) RETURNS BOOLEAN AS $$
65 IF in_rank_search <= 4 THEN
66 DELETE from location_area_country WHERE place_id = in_place_id;
71 IF in_partition = -partition- THEN
72 DELETE from location_area_large_-partition- WHERE place_id = in_place_id;
77 RAISE EXCEPTION 'Unknown partition %', in_partition;
84 create or replace function insertLocationAreaLarge(
85 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_keywords INTEGER[],
86 in_rank_search INTEGER, in_rank_address INTEGER, in_estimate BOOLEAN, postcode TEXT,
87 in_centroid GEOMETRY, in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
90 IF in_rank_address = 0 THEN
94 IF in_rank_search <= 4 and not in_estimate THEN
95 INSERT INTO location_area_country (place_id, country_code, geometry)
96 values (in_place_id, in_country_code, in_geometry);
101 IF in_partition = -partition- THEN
102 INSERT INTO location_area_large_-partition- (partition, place_id, country_code, keywords, rank_search, rank_address, isguess, postcode, centroid, geometry)
103 values (in_partition, in_place_id, in_country_code, in_keywords, in_rank_search, in_rank_address, in_estimate, postcode, in_centroid, in_geometry);
108 RAISE EXCEPTION 'Unknown partition %', in_partition;
114 CREATE OR REPLACE FUNCTION getNearestNamedRoadPlaceId(in_partition INTEGER,
116 isin_token INTEGER[])
124 IF in_partition = -partition- THEN
125 SELECT place_id FROM search_name_-partition-
127 WHERE name_vector && isin_token
128 AND centroid && ST_Expand(point, 0.015)
129 AND search_rank between 26 and 27
130 ORDER BY ST_Distance(centroid, point) ASC limit 1;
135 RAISE EXCEPTION 'Unknown partition %', in_partition;
138 LANGUAGE plpgsql STABLE;
140 CREATE OR REPLACE FUNCTION getNearestNamedPlacePlaceId(in_partition INTEGER,
142 isin_token INTEGER[])
150 IF in_partition = -partition- THEN
153 FROM search_name_-partition-
154 WHERE name_vector && isin_token
155 AND centroid && ST_Expand(point, 0.04)
156 AND search_rank between 16 and 25
157 ORDER BY ST_Distance(centroid, point) ASC limit 1;
162 RAISE EXCEPTION 'Unknown partition %', in_partition;
165 LANGUAGE plpgsql STABLE;
168 create or replace function insertSearchName(
169 in_partition INTEGER, in_place_id BIGINT, in_name_vector INTEGER[],
170 in_rank_search INTEGER, in_rank_address INTEGER, in_geometry GEOMETRY)
171 RETURNS BOOLEAN AS $$
175 IF in_partition = -partition- THEN
176 DELETE FROM search_name_-partition- values WHERE place_id = in_place_id;
177 IF in_rank_address > 0 THEN
178 INSERT INTO search_name_-partition- (place_id, search_rank, address_rank, name_vector, centroid)
179 values (in_place_id, in_rank_search, in_rank_address, in_name_vector, in_geometry);
185 RAISE EXCEPTION 'Unknown partition %', in_partition;
191 create or replace function deleteSearchName(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
195 IF in_partition = -partition- THEN
196 DELETE from search_name_-partition- WHERE place_id = in_place_id;
201 RAISE EXCEPTION 'Unknown partition %', in_partition;
208 create or replace function insertLocationRoad(
209 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
214 IF in_partition = -partition- THEN
215 DELETE FROM location_road_-partition- where place_id = in_place_id;
216 INSERT INTO location_road_-partition- (partition, place_id, country_code, geometry)
217 values (in_partition, in_place_id, in_country_code, in_geometry);
222 RAISE EXCEPTION 'Unknown partition %', in_partition;
228 create or replace function deleteRoad(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
233 IF in_partition = -partition- THEN
234 DELETE FROM location_road_-partition- where place_id = in_place_id;
239 RAISE EXCEPTION 'Unknown partition %', in_partition;
246 CREATE OR REPLACE FUNCTION getNearestRoadPlaceId(in_partition INTEGER, point GEOMETRY)
251 search_diameter FLOAT;
255 IF in_partition = -partition- THEN
256 search_diameter := 0.00005;
257 WHILE search_diameter < 0.1 LOOP
259 SELECT place_id FROM location_road_-partition-
260 WHERE ST_DWithin(geometry, point, search_diameter)
261 ORDER BY ST_Distance(geometry, point) ASC limit 1
265 search_diameter := search_diameter * 2;
271 RAISE EXCEPTION 'Unknown partition %', in_partition;
274 LANGUAGE plpgsql STABLE;
276 CREATE OR REPLACE FUNCTION getNearestParallelRoadFeature(in_partition INTEGER,
282 search_diameter FLOAT;
288 IF ST_GeometryType(line) not in ('ST_LineString') THEN
292 p1 := ST_LineInterpolatePoint(line,0);
293 p2 := ST_LineInterpolatePoint(line,0.5);
294 p3 := ST_LineInterpolatePoint(line,1);
297 IF in_partition = -partition- THEN
298 search_diameter := 0.0005;
299 WHILE search_diameter < 0.01 LOOP
301 SELECT place_id FROM location_road_-partition-
302 WHERE ST_DWithin(line, geometry, search_diameter)
303 ORDER BY (ST_distance(geometry, p1)+
304 ST_distance(geometry, p2)+
305 ST_distance(geometry, p3)) ASC limit 1
309 search_diameter := search_diameter * 2;
315 RAISE EXCEPTION 'Unknown partition %', in_partition;
318 LANGUAGE plpgsql STABLE;