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, isin_tokens INT[]) 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
49 ORDER BY rank_address, isin_tokens && keywords desc, isguess asc,
50 ST_Distance(feature, centroid) *
52 WHEN rank_address = 16 AND rank_search = 15 THEN 0.2 -- capital city
53 WHEN rank_address = 16 AND rank_search = 16 THEN 0.25 -- city
54 WHEN rank_address = 16 AND rank_search = 17 THEN 0.5 -- town
55 ELSE 1 END ASC -- everything else
63 RAISE EXCEPTION 'Unknown partition %', in_partition;
66 LANGUAGE plpgsql STABLE;
68 create or replace function deleteLocationArea(in_partition INTEGER, in_place_id BIGINT, in_rank_search INTEGER) RETURNS BOOLEAN AS $$
72 IF in_rank_search <= 4 THEN
73 DELETE from location_area_country WHERE place_id = in_place_id;
78 IF in_partition = -partition- THEN
79 DELETE from location_area_large_-partition- WHERE place_id = in_place_id;
84 RAISE EXCEPTION 'Unknown partition %', in_partition;
91 create or replace function insertLocationAreaLarge(
92 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_keywords INTEGER[],
93 in_rank_search INTEGER, in_rank_address INTEGER, in_estimate BOOLEAN, postcode TEXT,
94 in_centroid GEOMETRY, in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
97 IF in_rank_address = 0 THEN
101 IF in_rank_search <= 4 and not in_estimate THEN
102 INSERT INTO location_area_country (place_id, country_code, geometry)
103 values (in_place_id, in_country_code, in_geometry);
108 IF in_partition = -partition- THEN
109 INSERT INTO location_area_large_-partition- (partition, place_id, country_code, keywords, rank_search, rank_address, isguess, postcode, centroid, geometry)
110 values (in_partition, in_place_id, in_country_code, in_keywords, in_rank_search, in_rank_address, in_estimate, postcode, in_centroid, in_geometry);
115 RAISE EXCEPTION 'Unknown partition %', in_partition;
121 CREATE OR REPLACE FUNCTION getNearestNamedRoadPlaceId(in_partition INTEGER,
123 isin_token INTEGER[])
131 IF in_partition = -partition- THEN
132 SELECT place_id FROM search_name_-partition-
134 WHERE name_vector && isin_token
135 AND centroid && ST_Expand(point, 0.015)
136 AND search_rank between 26 and 27
137 ORDER BY ST_Distance(centroid, point) ASC limit 1;
142 RAISE EXCEPTION 'Unknown partition %', in_partition;
145 LANGUAGE plpgsql STABLE;
147 CREATE OR REPLACE FUNCTION getNearestNamedPlacePlaceId(in_partition INTEGER,
149 isin_token INTEGER[])
157 IF in_partition = -partition- THEN
160 FROM search_name_-partition-
161 WHERE name_vector && isin_token
162 AND centroid && ST_Expand(point, 0.04)
163 AND search_rank between 16 and 25
164 ORDER BY ST_Distance(centroid, point) ASC limit 1;
169 RAISE EXCEPTION 'Unknown partition %', in_partition;
172 LANGUAGE plpgsql STABLE;
175 create or replace function insertSearchName(
176 in_partition INTEGER, in_place_id BIGINT, in_name_vector INTEGER[],
177 in_rank_search INTEGER, in_rank_address INTEGER, in_geometry GEOMETRY)
178 RETURNS BOOLEAN AS $$
182 IF in_partition = -partition- THEN
183 DELETE FROM search_name_-partition- values WHERE place_id = in_place_id;
184 IF in_rank_address > 0 THEN
185 INSERT INTO search_name_-partition- (place_id, search_rank, address_rank, name_vector, centroid)
186 values (in_place_id, in_rank_search, in_rank_address, in_name_vector, in_geometry);
192 RAISE EXCEPTION 'Unknown partition %', in_partition;
198 create or replace function deleteSearchName(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
202 IF in_partition = -partition- THEN
203 DELETE from search_name_-partition- WHERE place_id = in_place_id;
208 RAISE EXCEPTION 'Unknown partition %', in_partition;
215 create or replace function insertLocationRoad(
216 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
221 IF in_partition = -partition- THEN
222 DELETE FROM location_road_-partition- where place_id = in_place_id;
223 INSERT INTO location_road_-partition- (partition, place_id, country_code, geometry)
224 values (in_partition, in_place_id, in_country_code, in_geometry);
229 RAISE EXCEPTION 'Unknown partition %', in_partition;
235 create or replace function deleteRoad(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
240 IF in_partition = -partition- THEN
241 DELETE FROM location_road_-partition- where place_id = in_place_id;
246 RAISE EXCEPTION 'Unknown partition %', in_partition;
253 CREATE OR REPLACE FUNCTION getNearestRoadPlaceId(in_partition INTEGER, point GEOMETRY)
258 search_diameter FLOAT;
262 IF in_partition = -partition- THEN
263 search_diameter := 0.00005;
264 WHILE search_diameter < 0.1 LOOP
266 SELECT place_id FROM location_road_-partition-
267 WHERE ST_DWithin(geometry, point, search_diameter)
268 ORDER BY ST_Distance(geometry, point) ASC limit 1
272 search_diameter := search_diameter * 2;
278 RAISE EXCEPTION 'Unknown partition %', in_partition;
281 LANGUAGE plpgsql STABLE;
283 CREATE OR REPLACE FUNCTION getNearestParallelRoadFeature(in_partition INTEGER,
289 search_diameter FLOAT;
295 IF ST_GeometryType(line) not in ('ST_LineString') THEN
299 p1 := ST_LineInterpolatePoint(line,0);
300 p2 := ST_LineInterpolatePoint(line,0.5);
301 p3 := ST_LineInterpolatePoint(line,1);
304 IF in_partition = -partition- THEN
305 search_diameter := 0.0005;
306 WHILE search_diameter < 0.01 LOOP
308 SELECT place_id FROM location_road_-partition-
309 WHERE ST_DWithin(line, geometry, search_diameter)
310 ORDER BY (ST_distance(geometry, p1)+
311 ST_distance(geometry, p2)+
312 ST_distance(geometry, p3)) ASC limit 1
316 search_diameter := search_diameter * 2;
322 RAISE EXCEPTION 'Unknown partition %', in_partition;
325 LANGUAGE plpgsql STABLE;