1 create type nearplace as (
5 create type nearfeature as (
13 CREATE TABLE location_area_country () INHERITS (location_area_large);
14 CREATE INDEX idx_location_area_country_place_id ON location_area_country USING BTREE (place_id);
15 CREATE INDEX idx_location_area_country_geometry ON location_area_country USING GIST (geometry);
17 CREATE TABLE search_name_country () INHERITS (search_name_blank);
18 CREATE INDEX idx_search_name_country_place_id ON search_name_country USING BTREE (place_id);
19 CREATE INDEX idx_search_name_country_centroid ON search_name_country USING GIST (centroid);
20 CREATE INDEX idx_search_name_country_name_vector ON search_name_country USING GIN (name_vector gin__int_ops) WITH (fastupdate = off);
21 CREATE INDEX idx_search_name_country_nameaddress_vector ON search_name_country USING GIN (nameaddress_vector gin__int_ops) WITH (fastupdate = off);
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);
26 CREATE INDEX idx_location_area_large_-partition-_geometry ON location_area_large_-partition- USING GIST (geometry);
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);
30 CREATE INDEX idx_search_name_-partition-_centroid ON search_name_-partition- USING GIST (centroid);
31 CREATE INDEX idx_search_name_-partition-_name_vector ON search_name_-partition- USING GIN (name_vector gin__int_ops) WITH (fastupdate = off);
32 CREATE INDEX idx_search_name_-partition-_nameaddress_vector ON search_name_-partition- USING GIN (nameaddress_vector gin__int_ops) WITH (fastupdate = off);
34 CREATE TABLE location_property_-partition- () INHERITS (location_property);
35 CREATE INDEX idx_location_property_-partition-_place_id ON location_property_-partition- USING BTREE (place_id);
36 CREATE INDEX idx_location_property_-partition-_parent_place_id ON location_property_-partition- USING BTREE (parent_place_id);
37 CREATE INDEX idx_location_property_-partition-_housenumber_parent_place_id ON location_property_-partition- USING BTREE (parent_place_id, housenumber);
38 --CREATE INDEX idx_location_property_-partition-_centroid ON location_property_-partition- USING GIST (centroid);
40 CREATE TABLE location_road_-partition- (
43 country_code VARCHAR(2)
45 SELECT AddGeometryColumn('location_road_-partition-', 'geometry', 4326, 'GEOMETRY', 2);
46 CREATE INDEX idx_location_road_-partition-_geometry ON location_road_-partition- USING GIST (geometry);
47 CREATE INDEX idx_location_road_-partition-_place_id ON location_road_-partition- USING BTREE (place_id);
51 create or replace function getNearFeatures(in_partition INTEGER, point GEOMETRY, maxrank INTEGER, isin_tokens INT[]) RETURNS setof nearfeature AS $$
53 r nearfeature%rowtype;
57 IF in_partition = -partition- THEN
59 SELECT place_id, keywords, rank_address, rank_search, min(ST_Distance(point, centroid)) as distance FROM (
60 SELECT * FROM location_area_large_-partition- WHERE ST_Contains(geometry, point) and rank_search < maxrank
62 SELECT * FROM location_area_country WHERE ST_Contains(geometry, point) and rank_search < maxrank
64 GROUP BY place_id, keywords, rank_address, rank_search, isguess, centroid
65 ORDER BY rank_address desc, isin_tokens && keywords desc, isguess asc,
66 ST_Distance(point, centroid) *
68 WHEN rank_address = 16 AND rank_search = 15 THEN 0.2 -- capital city
69 WHEN rank_address = 16 AND rank_search = 16 THEN 0.25 -- city
70 WHEN rank_address = 16 AND rank_search = 17 THEN 0.5 -- town
71 ELSE 1 END ASC -- everything else
79 RAISE EXCEPTION 'Unknown partition %', in_partition;
84 create or replace function deleteLocationArea(in_partition INTEGER, in_place_id integer) RETURNS BOOLEAN AS $$
89 IF in_partition = -partition- THEN
90 DELETE from location_area_large_-partition- WHERE place_id = in_place_id;
95 RAISE EXCEPTION 'Unknown partition %', in_partition;
102 create or replace function insertLocationAreaLarge(
103 in_partition INTEGER, in_place_id integer, in_country_code VARCHAR(2), in_keywords INTEGER[],
104 in_rank_search INTEGER, in_rank_address INTEGER, in_estimate BOOLEAN,
105 in_centroid GEOMETRY, in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
109 IF in_rank_search <= 4 THEN
110 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);
115 IF in_partition = -partition- THEN
116 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);
121 RAISE EXCEPTION 'Unknown partition %', in_partition;
127 create or replace function getNearestNamedFeature(in_partition INTEGER, point GEOMETRY, maxrank INTEGER, isin_token INTEGER) RETURNS setof nearfeature AS $$
129 r nearfeature%rowtype;
133 IF in_partition = -partition- THEN
135 SELECT place_id, name_vector, address_rank, search_rank,
136 ST_Distance(centroid, point) as distance
137 FROM search_name_-partition-
138 WHERE name_vector @> ARRAY[isin_token]
139 AND search_rank < maxrank
141 SELECT place_id, name_vector, address_rank, search_rank,
142 ST_Distance(centroid, point) as distance
143 FROM search_name_country
144 WHERE name_vector @> ARRAY[isin_token]
145 AND search_rank < maxrank
146 ORDER BY distance ASC limit 1
154 RAISE EXCEPTION 'Unknown partition %', in_partition;
159 create or replace function getNearestNamedRoadFeature(in_partition INTEGER, point GEOMETRY, isin_token INTEGER)
160 RETURNS setof nearfeature AS $$
162 r nearfeature%rowtype;
166 IF in_partition = -partition- THEN
168 SELECT place_id, name_vector, address_rank, search_rank,
169 ST_Distance(centroid, point) as distance
170 FROM search_name_-partition-
171 WHERE name_vector @> ARRAY[isin_token]
172 AND ST_DWithin(centroid, point, 0.01)
173 AND search_rank between 22 and 27
174 ORDER BY distance ASC limit 1
182 RAISE EXCEPTION 'Unknown partition %', in_partition;
187 create or replace function insertSearchName(
188 in_partition INTEGER, in_place_id integer, in_country_code VARCHAR(2),
189 in_name_vector INTEGER[], in_nameaddress_vector INTEGER[],
190 in_rank_search INTEGER, in_rank_address INTEGER, in_importance FLOAT,
191 in_centroid GEOMETRY) RETURNS BOOLEAN AS $$
195 DELETE FROM search_name WHERE place_id = in_place_id;
196 INSERT INTO search_name values (in_place_id, in_rank_search, in_rank_address, in_importance, in_country_code,
197 in_name_vector, in_nameaddress_vector, in_centroid);
199 IF in_rank_search <= 4 THEN
200 DELETE FROM search_name_country WHERE place_id = in_place_id;
201 INSERT INTO search_name_country values (in_place_id, in_rank_search, in_rank_address, in_importance, in_country_code,
202 in_name_vector, in_nameaddress_vector, in_centroid);
207 IF in_partition = -partition- THEN
208 DELETE FROM search_name_-partition- values WHERE place_id = in_place_id;
209 INSERT INTO search_name_-partition- values (in_place_id, in_rank_search, in_rank_address, 0, in_country_code,
210 in_name_vector, in_nameaddress_vector, in_centroid);
215 RAISE EXCEPTION 'Unknown partition %', in_partition;
221 create or replace function deleteSearchName(in_partition INTEGER, in_place_id integer) RETURNS BOOLEAN AS $$
225 DELETE from search_name WHERE place_id = in_place_id;
226 DELETE from search_name_country WHERE place_id = in_place_id;
229 IF in_partition = -partition- THEN
230 DELETE from search_name_-partition- WHERE place_id = in_place_id;
235 RAISE EXCEPTION 'Unknown partition %', in_partition;
242 create or replace function insertLocationRoad(
243 in_partition INTEGER, in_place_id integer, in_country_code VARCHAR(2), in_geometry GEOMETRY) RETURNS BOOLEAN AS $$
248 IF in_partition = -partition- THEN
249 DELETE FROM location_road_-partition- where place_id = in_place_id;
250 INSERT INTO location_road_-partition- values (in_partition, in_place_id, in_country_code, in_geometry);
255 RAISE EXCEPTION 'Unknown partition %', in_partition;
261 create or replace function deleteRoad(in_partition INTEGER, in_place_id integer) RETURNS BOOLEAN AS $$
266 IF in_partition = -partition- THEN
267 DELETE FROM location_road_-partition- where place_id = in_place_id;
272 RAISE EXCEPTION 'Unknown partition %', in_partition;
279 create or replace function getNearestRoadFeature(in_partition INTEGER, point GEOMETRY) RETURNS setof nearfeature AS $$
281 r nearfeature%rowtype;
282 search_diameter FLOAT;
286 IF in_partition = -partition- THEN
287 search_diameter := 0.00005;
288 WHILE search_diameter < 0.1 LOOP
290 SELECT place_id, null, null, null,
291 ST_Distance(geometry, point) as distance
292 FROM location_road_-partition-
293 WHERE ST_DWithin(geometry, point, search_diameter)
294 ORDER BY distance ASC limit 1
299 search_diameter := search_diameter * 2;
305 RAISE EXCEPTION 'Unknown partition %', in_partition;
310 create or replace function getNearestParellelRoadFeature(in_partition INTEGER, line GEOMETRY) RETURNS setof nearfeature AS $$
312 r nearfeature%rowtype;
313 search_diameter FLOAT;
319 IF st_geometrytype(line) not in ('ST_LineString') THEN
323 p1 := ST_Line_Interpolate_Point(line,0);
324 p2 := ST_Line_Interpolate_Point(line,0.5);
325 p3 := ST_Line_Interpolate_Point(line,1);
328 IF in_partition = -partition- THEN
329 search_diameter := 0.0005;
330 WHILE search_diameter < 0.01 LOOP
332 SELECT place_id, null, null, null,
333 ST_Distance(geometry, line) as distance
334 FROM location_road_-partition-
335 WHERE ST_DWithin(line, geometry, search_diameter)
336 ORDER BY (ST_distance(geometry, p1)+
337 ST_distance(geometry, p2)+
338 ST_distance(geometry, p3)) ASC limit 1
343 search_diameter := search_diameter * 2;
349 RAISE EXCEPTION 'Unknown partition %', in_partition;