1 drop type nearplace cascade;
2 create type nearplace as (
6 drop type nearfeature cascade;
7 create type nearfeature as (
16 CREATE TABLE location_area_country () INHERITS (location_area_large);
17 CREATE INDEX idx_location_area_country_geometry ON location_area_country USING GIST (geometry) TABLESPACE ssd;
19 CREATE TABLE search_name_country () INHERITS (search_name_blank);
20 CREATE INDEX idx_search_name_country_place_id ON search_name_country USING BTREE (place_id) TABLESPACE ssd;
21 CREATE INDEX idx_search_name_country_name_vector ON search_name_country USING GIN (name_vector) WITH (fastupdate = off) TABLESPACE ssd;
24 CREATE TABLE location_area_large_-partition- () INHERITS (location_area_large);
25 CREATE INDEX idx_location_area_large_-partition-_place_id ON location_area_large_-partition- USING BTREE (place_id) TABLESPACE ssd;
26 CREATE INDEX idx_location_area_large_-partition-_geometry ON location_area_large_-partition- USING GIST (geometry) TABLESPACE ssd;
28 CREATE TABLE search_name_-partition- () INHERITS (search_name_blank);
29 CREATE INDEX idx_search_name_-partition-_place_id ON search_name_-partition- USING BTREE (place_id) TABLESPACE ssd;
30 CREATE INDEX idx_search_name_-partition-_centroid ON search_name_-partition- USING GIST (centroid) TABLESPACE ssd;
31 CREATE INDEX idx_search_name_-partition-_name_vector ON search_name_-partition- USING GIN (name_vector) WITH (fastupdate = off) TABLESPACE ssd;
33 CREATE TABLE location_property_-partition- () INHERITS (location_property);
34 CREATE INDEX idx_location_property_-partition-_place_id ON location_property_-partition- USING BTREE (place_id);
35 CREATE INDEX idx_location_property_-partition-_parent_place_id ON location_property_-partition- USING BTREE (parent_place_id);
36 CREATE INDEX idx_location_property_-partition-_housenumber_parent_place_id ON location_property_-partition- USING BTREE (parent_place_id, housenumber);
38 CREATE TABLE location_road_-partition- (
41 country_code VARCHAR(2)
43 SELECT AddGeometryColumn('location_road_-partition-', 'geometry', 4326, 'GEOMETRY', 2);
44 CREATE INDEX idx_location_road_-partition-_geometry ON location_road_-partition- USING GIST (geometry) TABLESPACE ssd;
45 CREATE INDEX idx_location_road_-partition-_place_id ON location_road_-partition- USING BTREE (place_id) TABLESPACE ssd;
49 create or replace function getNearFeatures(in_partition INTEGER, point GEOMETRY, maxrank INTEGER, isin_tokens INT[]) RETURNS setof nearfeature AS $$
51 r nearfeature%rowtype;
55 IF in_partition = -partition- THEN
57 SELECT place_id, keywords, rank_address, rank_search, min(ST_Distance(point, centroid)) as distance, isguess FROM (
58 SELECT * FROM location_area_large_-partition- WHERE ST_Contains(geometry, point) and rank_search < maxrank
60 SELECT * FROM location_area_country WHERE ST_Contains(geometry, point) and rank_search < maxrank
62 GROUP BY place_id, keywords, rank_address, rank_search, isguess, centroid
63 ORDER BY rank_address desc, isin_tokens && keywords desc, isguess asc,
64 ST_Distance(point, centroid) *
66 WHEN rank_address = 16 AND rank_search = 15 THEN 0.2 -- capital city
67 WHEN rank_address = 16 AND rank_search = 16 THEN 0.25 -- city
68 WHEN rank_address = 16 AND rank_search = 17 THEN 0.5 -- town
69 ELSE 1 END ASC -- everything else
77 RAISE EXCEPTION 'Unknown partition %', in_partition;
82 create or replace function deleteLocationArea(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
87 IF in_partition = -partition- THEN
88 DELETE from location_area_large_-partition- WHERE place_id = in_place_id;
93 RAISE EXCEPTION 'Unknown partition %', in_partition;
100 create or replace function insertLocationAreaLarge(
101 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_keywords INTEGER[],
102 in_rank_search INTEGER, in_rank_address INTEGER, in_estimate BOOLEAN,
103 in_centroid GEOMETRY, in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
107 IF in_rank_search <= 4 THEN
108 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);
113 IF in_partition = -partition- THEN
114 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);
119 RAISE EXCEPTION 'Unknown partition %', in_partition;
125 create or replace function getNearestNamedFeature(in_partition INTEGER, point GEOMETRY, maxrank INTEGER, isin_token INTEGER) RETURNS setof nearfeature AS $$
127 r nearfeature%rowtype;
131 IF in_partition = -partition- THEN
133 SELECT place_id, name_vector, address_rank, search_rank,
134 ST_Distance(centroid, point) as distance, null as isguess
135 FROM search_name_-partition-
136 WHERE name_vector @> ARRAY[isin_token]
137 AND search_rank < maxrank
139 SELECT place_id, name_vector, address_rank, search_rank,
140 ST_Distance(centroid, point) as distance, null as isguess
141 FROM search_name_country
142 WHERE name_vector @> ARRAY[isin_token]
143 AND search_rank < maxrank
144 ORDER BY distance ASC limit 1
152 RAISE EXCEPTION 'Unknown partition %', in_partition;
157 create or replace function getNearestNamedRoadFeature(in_partition INTEGER, point GEOMETRY, isin_token INTEGER)
158 RETURNS setof nearfeature AS $$
160 r nearfeature%rowtype;
164 IF in_partition = -partition- THEN
166 SELECT place_id, name_vector, address_rank, search_rank,
167 ST_Distance(centroid, point) as distance, null as isguess
168 FROM search_name_-partition-
169 WHERE name_vector @> ARRAY[isin_token]
170 AND ST_DWithin(centroid, point, 0.01)
171 AND search_rank between 22 and 27
172 ORDER BY distance ASC limit 1
180 RAISE EXCEPTION 'Unknown partition %', in_partition;
185 create or replace function getNearestPostcode(in_partition INTEGER, point GEOMETRY)
192 IF in_partition = -partition- THEN
194 FROM location_area_large_-partition- join placex using (place_id)
195 WHERE st_contains(location_area_large_-partition-.geometry, point)
196 AND class = 'place' and type = 'postcode'
197 ORDER BY st_distance(location_area_large_-partition-.centroid, point) ASC limit 1
203 RAISE EXCEPTION 'Unknown partition %', in_partition;
208 create or replace function insertSearchName(
209 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2),
210 in_name_vector INTEGER[], in_nameaddress_vector INTEGER[],
211 in_rank_search INTEGER, in_rank_address INTEGER, in_importance FLOAT,
212 in_centroid GEOMETRY) RETURNS BOOLEAN AS $$
216 DELETE FROM search_name WHERE place_id = in_place_id;
217 INSERT INTO search_name values (in_place_id, in_rank_search, in_rank_address, in_importance, in_country_code,
218 in_name_vector, in_nameaddress_vector, in_centroid);
220 IF in_rank_search <= 4 THEN
221 DELETE FROM search_name_country WHERE place_id = in_place_id;
222 INSERT INTO search_name_country values (in_place_id, in_rank_search, in_rank_address, in_importance, in_country_code,
223 in_name_vector, in_nameaddress_vector, in_centroid);
228 IF in_partition = -partition- THEN
229 DELETE FROM search_name_-partition- values WHERE place_id = in_place_id;
230 INSERT INTO search_name_-partition- values (in_place_id, in_rank_search, in_rank_address, 0, in_country_code,
231 in_name_vector, in_nameaddress_vector, in_centroid);
236 RAISE EXCEPTION 'Unknown partition %', in_partition;
242 create or replace function deleteSearchName(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
246 DELETE from search_name WHERE place_id = in_place_id;
247 DELETE from search_name_country WHERE place_id = in_place_id;
250 IF in_partition = -partition- THEN
251 DELETE from search_name_-partition- WHERE place_id = in_place_id;
256 RAISE EXCEPTION 'Unknown partition %', in_partition;
263 create or replace function insertLocationRoad(
264 in_partition INTEGER, in_place_id BIGINT, in_country_code VARCHAR(2), in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
269 IF in_partition = -partition- THEN
270 DELETE FROM location_road_-partition- where place_id = in_place_id;
271 INSERT INTO location_road_-partition- values (in_partition, in_place_id, in_country_code, in_geometry);
276 RAISE EXCEPTION 'Unknown partition %', in_partition;
282 create or replace function deleteRoad(in_partition INTEGER, in_place_id BIGINT) RETURNS BOOLEAN AS $$
287 IF in_partition = -partition- THEN
288 DELETE FROM location_road_-partition- where place_id = in_place_id;
293 RAISE EXCEPTION 'Unknown partition %', in_partition;
300 create or replace function getNearestRoadFeature(in_partition INTEGER, point GEOMETRY) RETURNS setof nearfeature AS $$
302 r nearfeature%rowtype;
303 search_diameter FLOAT;
307 IF in_partition = -partition- THEN
308 search_diameter := 0.00005;
309 WHILE search_diameter < 0.1 LOOP
311 SELECT place_id, null, null, null,
312 ST_Distance(geometry, point) as distance, null as isguess
313 FROM location_road_-partition-
314 WHERE ST_DWithin(geometry, point, search_diameter)
315 ORDER BY distance ASC limit 1
320 search_diameter := search_diameter * 2;
326 RAISE EXCEPTION 'Unknown partition %', in_partition;
331 create or replace function getNearestParellelRoadFeature(in_partition INTEGER, line GEOMETRY) RETURNS setof nearfeature AS $$
333 r nearfeature%rowtype;
334 search_diameter FLOAT;
340 IF st_geometrytype(line) not in ('ST_LineString') THEN
344 p1 := ST_Line_Interpolate_Point(line,0);
345 p2 := ST_Line_Interpolate_Point(line,0.5);
346 p3 := ST_Line_Interpolate_Point(line,1);
349 IF in_partition = -partition- THEN
350 search_diameter := 0.0005;
351 WHILE search_diameter < 0.01 LOOP
353 SELECT place_id, null, null, null,
354 ST_Distance(geometry, line) as distance, null as isguess
355 FROM location_road_-partition-
356 WHERE ST_DWithin(line, geometry, search_diameter)
357 ORDER BY (ST_distance(geometry, p1)+
358 ST_distance(geometry, p2)+
359 ST_distance(geometry, p3)) ASC limit 1
364 search_diameter := search_diameter * 2;
370 RAISE EXCEPTION 'Unknown partition %', in_partition;